| 16 | } |
| 17 | |
| 18 | void wait_group::add(int n) |
| 19 | { |
| 20 | long long state = state_.add_fetch((long long)n << 32); |
| 21 | |
| 22 | // The high 32 bits store the number of tasks. |
| 23 | int c = (int)(state >> 32); |
| 24 | |
| 25 | // The low 32 bits store the number of waiters. |
| 26 | unsigned w = (unsigned)state; |
| 27 | |
| 28 | // count must be >= 0. |
| 29 | if (c < 0){ |
| 30 | logger_fatal("Negative wait_group counter, c=%d", c); |
| 31 | } |
| 32 | |
| 33 | if (w != 0 && n > 0 && c == n){ |
| 34 | logger_fatal("Add called concurrently with wait"); |
| 35 | } |
| 36 | |
| 37 | if (c > 0 || w == 0) { |
| 38 | return; |
| 39 | } |
| 40 | |
| 41 | // Check if the state has been changed. |
| 42 | if (state_ != state) { |
| 43 | logger_fatal("Add called concurrently with wait"); |
| 44 | } |
| 45 | |
| 46 | // The count should be zero, so clear state and wakeup all the waiters. |
| 47 | state_ = 0; |
| 48 | |
| 49 | for (size_t i = 0; i < w; i++) { |
| 50 | #ifdef _DEBUG |
| 51 | unsigned long* tid = new unsigned long; |
| 52 | *tid = acl::thread::self(); |
| 53 | box_->push(tid); |
| 54 | #else |
| 55 | box_->push(NULL); |
| 56 | #endif |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | void wait_group::done() |
| 61 | { |
no test coverage detected