/* * Broadcast a condition variable */ int lthread_cond_broadcast(struct lthread_cond *c) { struct lthread *lt; if (c == NULL) { DIAG_EVENT(c, LT_DIAG_COND_BROADCAST, c, POSIX_ERRNO(EINVAL)); return POSIX_ERRNO(EINVAL); } DIAG_EVENT(c, LT_DIAG_COND_BROADCAST, c, 0); do { /* drain the queue waking everybody */ lt = _lthread_queue_remove(c->blocked); if (lt != NULL) { DIAG_EVENT(c, LT_DIAG_COND_BROADCAST, c, lt); /* wake up */ _ready_queue_insert((struct lthread_sched *)lt->sched, lt); } } while (!_lthread_queue_empty(c->blocked)); _reschedule(); DIAG_EVENT(c, LT_DIAG_COND_BROADCAST, c, 0); return 0; }
/* * Unlock a mutex */ int lthread_mutex_unlock(struct lthread_mutex *m) { struct lthread *lt = THIS_LTHREAD; struct lthread *unblocked; if ((m == NULL) || (m->blocked == NULL)) { DIAG_EVENT(m, LT_DIAG_MUTEX_UNLOCKED, m, POSIX_ERRNO(EINVAL)); return POSIX_ERRNO(EINVAL); } /* fail if its owned */ if (m->owner != lt || m->owner == NULL) { DIAG_EVENT(m, LT_DIAG_MUTEX_UNLOCKED, m, POSIX_ERRNO(EPERM)); return POSIX_ERRNO(EPERM); } rte_atomic64_dec(&m->count); /* if there are blocked threads then make one ready */ while (rte_atomic64_read(&m->count) > 0) { unblocked = _lthread_queue_remove(m->blocked); if (unblocked != NULL) { rte_atomic64_dec(&m->count); DIAG_EVENT(m, LT_DIAG_MUTEX_UNLOCKED, m, unblocked); RTE_ASSERT(unblocked->sched != NULL); _ready_queue_insert((struct lthread_sched *) unblocked->sched, unblocked); break; } } /* release the lock */ m->owner = NULL; return 0; }
/* * Signal a condition variable * attempt to resume any blocked thread */ int lthread_cond_signal(struct lthread_cond *c) { struct lthread *lt; if (c == NULL) { DIAG_EVENT(c, LT_DIAG_COND_SIGNAL, c, POSIX_ERRNO(EINVAL)); return POSIX_ERRNO(EINVAL); } lt = _lthread_queue_remove(c->blocked); if (lt != NULL) { /* okay wake up this thread */ DIAG_EVENT(c, LT_DIAG_COND_SIGNAL, c, lt); _ready_queue_insert((struct lthread_sched *)lt->sched, lt); } return 0; }