Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 1 | #include "barrier.h" |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 2 | #include "../src/mutex.h" |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 3 | #include "thread.h" |
| 4 | |
| 5 | namespace art { |
| 6 | |
Mathieu Chartier | 35883cc | 2012-11-13 14:08:12 -0800 | [diff] [blame] | 7 | Barrier::Barrier(int count) |
| 8 | : count_(count), |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 9 | lock_("GC barrier lock"), |
| 10 | condition_("GC barrier condition", lock_) { |
| 11 | } |
| 12 | |
| 13 | void Barrier::Pass(Thread* self) { |
| 14 | MutexLock mu(self, lock_); |
| 15 | SetCountLocked(self, count_ - 1); |
| 16 | } |
| 17 | |
| 18 | void Barrier::Wait(Thread* self) { |
| 19 | Increment(self, -1); |
| 20 | } |
| 21 | |
| 22 | void Barrier::Init(Thread* self, int count) { |
| 23 | MutexLock mu(self, lock_); |
| 24 | SetCountLocked(self, count); |
| 25 | } |
| 26 | |
| 27 | void Barrier::Increment(Thread* self, int delta) { |
| 28 | MutexLock mu(self, lock_); |
| 29 | SetCountLocked(self, count_ + delta); |
| 30 | if (count_ != 0) { |
| 31 | condition_.Wait(self); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | void Barrier::SetCountLocked(Thread* self, int count) { |
| 36 | count_ = count; |
| 37 | if (count_ == 0) { |
| 38 | condition_.Broadcast(self); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | Barrier::~Barrier() { |
Ian Rogers | 5bd97c4 | 2012-11-27 02:38:26 -0800 | [diff] [blame] | 43 | CHECK(!count_) << "Attempted to destroy barrier with non zero count"; |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | } |