DESIGN-barrier.txt 938 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. Barriers pseudocode
  2. ===================
  3. int pthread_barrier_wait(barrier_t *barrier);
  4. struct barrier_t {
  5. unsigned int lock:
  6. - internal mutex
  7. unsigned int left;
  8. - current barrier count, # of threads still needed.
  9. unsigned int init_count;
  10. - number of threads needed for the barrier to continue.
  11. unsigned int curr_event;
  12. - generation count
  13. }
  14. pthread_barrier_wait(barrier_t *barrier)
  15. {
  16. unsigned int event;
  17. result = 0;
  18. lll_lock(barrier->lock);
  19. if (!--barrier->left) {
  20. barrier->curr_event++;
  21. futex_wake(&barrier->curr_event, INT_MAX)
  22. result = BARRIER_SERIAL_THREAD;
  23. } else {
  24. event = barrier->curr_event;
  25. lll_unlock(barrier->lock);
  26. do {
  27. futex_wait(&barrier->curr_event, event)
  28. } while (event == barrier->curr_event);
  29. }
  30. if (atomic_increment_val (barrier->left) == barrier->init_count)
  31. lll_unlock(barrier->lock);
  32. return result;
  33. }