2011-02-12 00:22:29 -05:00
|
|
|
#include "pthread_impl.h"
|
|
|
|
|
2011-09-25 02:38:03 -04:00
|
|
|
static void unlock(pthread_cond_t *c)
|
|
|
|
{
|
|
|
|
a_dec(&c->_c_bcast);
|
|
|
|
if (c->_c_leavers) __wake(&c->_c_bcast, -1, 0);
|
|
|
|
}
|
|
|
|
|
2011-02-12 00:22:29 -05:00
|
|
|
int pthread_cond_broadcast(pthread_cond_t *c)
|
|
|
|
{
|
2011-09-25 02:38:03 -04:00
|
|
|
pthread_mutex_t *m;
|
|
|
|
int w;
|
|
|
|
|
|
|
|
if (!c->_c_waiters) return 0;
|
|
|
|
a_inc(&c->_c_bcast);
|
|
|
|
if (!c->_c_waiters) {
|
|
|
|
unlock(c);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-09-23 22:58:45 -04:00
|
|
|
a_store(&c->_c_block, 0);
|
2011-09-25 02:38:03 -04:00
|
|
|
|
|
|
|
m = c->_c_mutex;
|
|
|
|
|
|
|
|
/* If mutex ptr is not available, simply wake all waiters. */
|
|
|
|
if (m == (void *)-1) {
|
|
|
|
unlock(c);
|
|
|
|
__wake(&c->_c_block, -1, 0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Move waiter count to the mutex */
|
2011-09-25 21:14:40 -04:00
|
|
|
for (;;) {
|
|
|
|
w = c->_c_waiters;
|
|
|
|
a_fetch_add(&m->_m_waiters, w);
|
|
|
|
if (a_cas(&c->_c_waiters, w, 0) == w) break;
|
|
|
|
a_fetch_add(&m->_m_waiters, -w);
|
|
|
|
}
|
2011-09-25 02:38:03 -04:00
|
|
|
|
2011-09-25 02:56:01 -04:00
|
|
|
/* Perform the futex requeue, waking one waiter unless we know
|
|
|
|
* that the calling thread holds the mutex. */
|
2011-09-25 02:38:03 -04:00
|
|
|
__syscall(SYS_futex, &c->_c_block, FUTEX_REQUEUE,
|
2011-09-25 02:56:01 -04:00
|
|
|
!m->_m_type || (m->_m_lock&INT_MAX)!=pthread_self()->tid,
|
|
|
|
INT_MAX, &m->_m_lock);
|
2011-09-25 02:38:03 -04:00
|
|
|
|
|
|
|
unlock(c);
|
2011-02-12 00:22:29 -05:00
|
|
|
return 0;
|
|
|
|
}
|