blob: 052536ad34c381dd5bae4cd88931ac24fdb73df5 [file] [log] [blame]
Mathieu Chartier858f1c52012-10-17 17:45:55 -07001#include "barrier.h"
Mathieu Chartier0e4627e2012-10-23 16:13:36 -07002#include "../src/mutex.h"
Mathieu Chartier858f1c52012-10-17 17:45:55 -07003#include "thread.h"
4
5namespace art {
6
Mathieu Chartier35883cc2012-11-13 14:08:12 -08007Barrier::Barrier(int count)
8 : count_(count),
Mathieu Chartier858f1c52012-10-17 17:45:55 -07009 lock_("GC barrier lock"),
10 condition_("GC barrier condition", lock_) {
11}
12
13void Barrier::Pass(Thread* self) {
14 MutexLock mu(self, lock_);
15 SetCountLocked(self, count_ - 1);
16}
17
18void Barrier::Wait(Thread* self) {
19 Increment(self, -1);
20}
21
22void Barrier::Init(Thread* self, int count) {
23 MutexLock mu(self, lock_);
24 SetCountLocked(self, count);
25}
26
27void 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
35void Barrier::SetCountLocked(Thread* self, int count) {
36 count_ = count;
37 if (count_ == 0) {
38 condition_.Broadcast(self);
39 }
40}
41
42Barrier::~Barrier() {
43 CHECK(!count_) << "Attempted to destory barrier with non zero count";
44}
45
46}