src/libmetha/worker.c
static int
lm_worker_wait(worker_t *w)
{
    int msg;
    pthread_mutex_lock(&w->lock);

    if (w->message == LM_WORKER_MSG_NONE) {
        pthread_rwlock_wrlock(&w->m->w_lk_num_waiting);
        w->m->waiting_queue[w->m->w_num_waiting] = w;
        w->m->w_num_waiting ++;
#ifdef DEBUG
        fprintf(stderr, "* worker:(%p) state: WAITING (num_waiting: %d)\n", w, w->m->w_num_waiting);
#endif

        if (w->m->w_num_waiting >= w->m->num_threads)
            lm_notify(w->m, LM_EV_IDLE);

        w->state = LM_WORKER_STATE_WAITING;
        pthread_rwlock_unlock(&w->m->w_lk_num_waiting);

        pthread_cond_wait(&w->wakeup_cond, &w->lock);
    }

    msg = w->message;

    if (msg == LM_WORKER_MSG_STOP)
        w->state = LM_WORKER_STATE_STOPPED;
    else {
        lm_worker_sort(w);
        w->state = LM_WORKER_STATE_RUNNING;
    }

#ifdef DEBUG
    fprintf(stderr, "* worker:(%p) state: %s\n", w, worker_state_str[w->state]);
#endif

    w->message = LM_WORKER_MSG_NONE;
    pthread_mutex_unlock(&w->lock);

    return msg;
}
/** 
 * Wait for a message from another worker or the 
 * parent metha object. This might be a continue 
 * or a stop message. The message is returned by
 * the function.
 *
 * Note that this function will only add the 
 * worker to the waiting queue, and not remove it
 * once woken up. The external function that
 * wakes this worker up MUST remove the worker
 * from the queue before waking it up.
 **/
Wait for a message from another worker or the
parent metha object. This might be a continue
or a stop message. The message is returned by
the function.

Note that this function will only add the
worker to the waiting queue, and not remove it
once woken up. The external function that
wakes this worker up MUST remove the worker
from the queue before waking it up.
Wait for a message from another worker or the parent metha object . This might be a continue or a stop message . The message is returned by the function . Note that this function will only add the worker to the waiting queue and not remove it once woken up . The external function that wakes this worker up MUST remove the worker from the queue before waking it up .
static int
lm_worker_wait(worker_t *w)
{
    int msg;
    pthread_mutex_lock(&w->lock);
    if (w->message == LM_WORKER_MSG_NONE) {
        pthread_rwlock_wrlock(&w->m->w_lk_num_waiting);
        w->m->waiting_queue[w->m->w_num_waiting] = w;
        w->m->w_num_waiting ++;
#ifdef DEBUG
        fprintf(stderr, "* worker:(%p) state: WAITING (num_waiting: %d)\n", w, w->m->w_num_waiting);
#endif
        if (w->m->w_num_waiting >= w->m->num_threads)
            lm_notify(w->m, LM_EV_IDLE);
        w->state = LM_WORKER_STATE_WAITING;
        pthread_rwlock_unlock(&w->m->w_lk_num_waiting);
        pthread_cond_wait(&w->wakeup_cond, &w->lock);
    }
    msg = w->message;
    if (msg == LM_WORKER_MSG_STOP)
        w->state = LM_WORKER_STATE_STOPPED;
    else {
        lm_worker_sort(w);
        w->state = LM_WORKER_STATE_RUNNING;
    }
#ifdef DEBUG
    fprintf(stderr, "* worker:(%p) state: %s\n", w, worker_state_str[w->state]);
#endif
    w->message = LM_WORKER_MSG_NONE;
    pthread_mutex_unlock(&w->lock);
    return msg;
}
static int lm_worker_wait ( worker_t * w ) { int msg ; pthread_mutex_lock ( & w -> lock ) ; if ( w -> message == LM_WORKER_MSG_NONE ) { pthread_rwlock_wrlock ( & w -> m -> w_lk_num_waiting ) ; w -> m -> waiting_queue [ w -> m -> w_num_waiting ] = w ; w -> m -> w_num_waiting ++ ; #ifdef DEBUG fprintf ( stderr , " \n " , w , w -> m -> w_num_waiting ) ; #endif if ( w -> m -> w_num_waiting >= w -> m -> num_threads ) lm_notify ( w -> m , LM_EV_IDLE ) ; w -> state = LM_WORKER_STATE_WAITING ; pthread_rwlock_unlock ( & w -> m -> w_lk_num_waiting ) ; pthread_cond_wait ( & w -> wakeup_cond , & w -> lock ) ; } msg = w -> message ; if ( msg == LM_WORKER_MSG_STOP ) w -> state = LM_WORKER_STATE_STOPPED ; else { lm_worker_sort ( w ) ; w -> state = LM_WORKER_STATE_RUNNING ; } #ifdef DEBUG fprintf ( stderr , " \n " , w , worker_state_str [ w -> state ] ) ; #endif w -> message = LM_WORKER_MSG_NONE ; pthread_mutex_unlock ( & w -> lock ) ; return msg ; }
Wait for a message from another worker or the
 parent metha object.
Wait for a message from another worker or the parent metha object .