pthread_cond_broadcast.c 931 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. #include "pthread_impl.h"
  2. int pthread_cond_broadcast(pthread_cond_t *c)
  3. {
  4. pthread_mutex_t *m;
  5. if (!c->_c_waiters) return 0;
  6. a_inc(&c->_c_seq);
  7. /* If cond var is process-shared, simply wake all waiters. */
  8. if (c->_c_mutex == (void *)-1) {
  9. __wake(&c->_c_seq, -1, 0);
  10. return 0;
  11. }
  12. /* Block waiters from returning so we can use the mutex. */
  13. while (a_swap(&c->_c_lock, 1))
  14. __wait(&c->_c_lock, &c->_c_lockwait, 1, 1);
  15. if (!c->_c_waiters)
  16. goto out;
  17. m = c->_c_mutex;
  18. /* Move waiter count to the mutex */
  19. a_fetch_add(&m->_m_waiters, c->_c_waiters2);
  20. c->_c_waiters2 = 0;
  21. /* Perform the futex requeue, waking one waiter unless we know
  22. * that the calling thread holds the mutex. */
  23. __syscall(SYS_futex, &c->_c_seq, FUTEX_REQUEUE,
  24. !m->_m_type || (m->_m_lock&INT_MAX)!=__pthread_self()->tid,
  25. INT_MAX, &m->_m_lock);
  26. out:
  27. a_store(&c->_c_lock, 0);
  28. if (c->_c_lockwait) __wake(&c->_c_lock, 1, 0);
  29. return 0;
  30. }