Page 1 of 1

Thread SlotS

Posted: Fri Aug 07, 2015 11:59 am
by JoVM
Is there a thread_join function in chibiOS 3.0 to do thread slots.

Code: Select all

static int thread_create( int client_fd )
{
    int ret, i;

    /*
     * Find in-active or finished thread slot
     */
    for( i = 0; i < MAX_NUM_THREADS; i++ )
    {
        if( threads[i].active == 0 )
            break;

        if( threads[i].data.thread_complete == 1 )
        {
            printf( "  [ threading ]  Cleaning up thread %d\n", i );
            thread_join(threads[i].thread, NULL );
            memset( &threads[i], 0, sizeof(pthread_info_t) );
            break;
        }
    }

    if( i == MAX_NUM_THREADS )
        return( -1 );

    /*
     * Fill thread-info for thread
     */
    memcpy( &threads[i].data, &base_info, sizeof(base_info) );
    threads[i].active = 1;
    threads[i].data.client_fd = client_fd;

    if( ( ret = thread_create( &threads[i].thread, NULL, handle_ssl_connection, &threads[i].data ) ) != 0 )
    {
        return( ret );
    }

    return( 0 );
}

Re: Thread SlotS

Posted: Fri Aug 07, 2015 1:03 pm
by Giovanni
it is roughly equivalent to chThdWait().

Giovanni

Re: Thread SlotS

Posted: Fri Aug 07, 2015 3:10 pm
by JoVM
Hi Giovanni,

chThdWait() blocks the invoking thread. But this thread can't be blocked cause it's accepting connections. It manages a thread for each connection. If the handler thread is finished it frees a place in the list.


Kr,Jo

Re: Thread SlotS

Posted: Fri Aug 07, 2015 3:17 pm
by Giovanni
pthread_join() is blocking as well, both do not block if the thread is already terminated.

Giovanni

Re: Thread SlotS

Posted: Fri Aug 07, 2015 3:42 pm
by JoVM
tnx Giovanni,


Code: Select all

if (tp->p_state != CH_STATE_FINAL) {
    list_insert(currp, &tp->p_waiting);
    chSchGoSleepS(CH_STATE_WTEXIT);
  }


CH_STATE_FINAL means the handler thread is cleaned?

Re: Thread SlotS

Posted: Fri Aug 07, 2015 3:46 pm
by Giovanni
It means that the thread is terminated.

Giovanni