Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame^] | 1 | #include "barrier.h" |
| 2 | #include "../mutex.h" |
| 3 | #include "thread.h" |
| 4 | |
| 5 | namespace art { |
| 6 | |
| 7 | Barrier::Barrier() |
| 8 | : count_(0), |
| 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() { |
| 43 | CHECK(!count_) << "Attempted to destory barrier with non zero count"; |
| 44 | } |
| 45 | |
| 46 | } |