lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | Barriers pseudocode |
| 2 | =================== |
| 3 | |
| 4 | int pthread_barrier_wait(barrier_t *barrier); |
| 5 | |
| 6 | struct barrier_t { |
| 7 | |
| 8 | unsigned int lock: |
| 9 | - internal mutex |
| 10 | |
| 11 | unsigned int left; |
| 12 | - current barrier count, # of threads still needed. |
| 13 | |
| 14 | unsigned int init_count; |
| 15 | - number of threads needed for the barrier to continue. |
| 16 | |
| 17 | unsigned int curr_event; |
| 18 | - generation count |
| 19 | } |
| 20 | |
| 21 | pthread_barrier_wait(barrier_t *barrier) |
| 22 | { |
| 23 | unsigned int event; |
| 24 | result = 0; |
| 25 | |
| 26 | lll_lock(barrier->lock); |
| 27 | if (!--barrier->left) { |
| 28 | barrier->curr_event++; |
| 29 | futex_wake(&barrier->curr_event, INT_MAX) |
| 30 | |
| 31 | result = BARRIER_SERIAL_THREAD; |
| 32 | } else { |
| 33 | event = barrier->curr_event; |
| 34 | lll_unlock(barrier->lock); |
| 35 | do { |
| 36 | futex_wait(&barrier->curr_event, event) |
| 37 | } while (event == barrier->curr_event); |
| 38 | } |
| 39 | |
| 40 | if (atomic_increment_val (barrier->left) == barrier->init_count) |
| 41 | lll_unlock(barrier->lock); |
| 42 | |
| 43 | return result; |
| 44 | } |