blob: 0d842cc2614d7cb481b3b84b2bfdb0c6751f742d [file] [log] [blame]
Elliott Hughes76b61672012-12-12 17:47:30 -08001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Mathieu Chartier858f1c52012-10-17 17:45:55 -070017#include "barrier.h"
Elliott Hughes76b61672012-12-12 17:47:30 -080018
Andreas Gampe1bb907e2015-06-22 10:04:39 -070019#include "base/logging.h"
Elliott Hughes76b61672012-12-12 17:47:30 -080020#include "base/mutex.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010021#include "base/time_utils.h"
Mathieu Chartier858f1c52012-10-17 17:45:55 -070022#include "thread.h"
23
24namespace art {
25
Mathieu Chartier35883cc2012-11-13 14:08:12 -080026Barrier::Barrier(int count)
27 : count_(count),
Ian Rogers8409ec42014-11-04 17:57:02 -080028 lock_("GC barrier lock", kThreadSuspendCountLock),
Mathieu Chartier858f1c52012-10-17 17:45:55 -070029 condition_("GC barrier condition", lock_) {
30}
31
32void Barrier::Pass(Thread* self) {
33 MutexLock mu(self, lock_);
34 SetCountLocked(self, count_ - 1);
35}
36
37void Barrier::Wait(Thread* self) {
38 Increment(self, -1);
39}
40
41void Barrier::Init(Thread* self, int count) {
42 MutexLock mu(self, lock_);
43 SetCountLocked(self, count);
44}
45
46void Barrier::Increment(Thread* self, int delta) {
47 MutexLock mu(self, lock_);
48 SetCountLocked(self, count_ + delta);
Dave Allison0aded082013-11-07 13:15:11 -080049
50 // Increment the count. If it becomes zero after the increment
51 // then all the threads have already passed the barrier. If
52 // it is non-zero then there is still one or more threads
53 // that have not yet called the Pass function. When the
54 // Pass function is called by the last thread, the count will
55 // be decremented to zero and a Broadcast will be made on the
56 // condition variable, thus waking this up.
Hans Boehm5567c112014-12-02 18:31:31 -080057 while (count_ != 0) {
Mathieu Chartier858f1c52012-10-17 17:45:55 -070058 condition_.Wait(self);
59 }
60}
61
Ian Rogers7b078e82014-09-10 14:44:24 -070062bool Barrier::Increment(Thread* self, int delta, uint32_t timeout_ms) {
Dave Allison0aded082013-11-07 13:15:11 -080063 MutexLock mu(self, lock_);
64 SetCountLocked(self, count_ + delta);
Ian Rogers7b078e82014-09-10 14:44:24 -070065 bool timed_out = false;
Dave Allison0aded082013-11-07 13:15:11 -080066 if (count_ != 0) {
Hans Boehm5567c112014-12-02 18:31:31 -080067 uint32_t timeout_ns = 0;
68 uint64_t abs_timeout = NanoTime() + MsToNs(timeout_ms);
69 for (;;) {
70 timed_out = condition_.TimedWait(self, timeout_ms, timeout_ns);
71 if (timed_out || count_ == 0) return timed_out;
72 // Compute time remaining on timeout.
73 uint64_t now = NanoTime();
74 int64_t time_left = abs_timeout - now;
75 if (time_left <= 0) return true;
76 timeout_ns = time_left % (1000*1000);
77 timeout_ms = time_left / (1000*1000);
78 }
Dave Allison0aded082013-11-07 13:15:11 -080079 }
Ian Rogers7b078e82014-09-10 14:44:24 -070080 return timed_out;
Dave Allison0aded082013-11-07 13:15:11 -080081}
82
Mathieu Chartier858f1c52012-10-17 17:45:55 -070083void Barrier::SetCountLocked(Thread* self, int count) {
84 count_ = count;
Ian Rogers7b078e82014-09-10 14:44:24 -070085 if (count == 0) {
Mathieu Chartier858f1c52012-10-17 17:45:55 -070086 condition_.Broadcast(self);
87 }
88}
89
90Barrier::~Barrier() {
Andreas Gampe1bb907e2015-06-22 10:04:39 -070091 if (gAborting == 0) {
92 // Only check when not aborting.
93 CHECK_EQ(count_, 0) << "Attempted to destroy barrier with non zero count";
94 } else {
95 if (count_ != 0) {
96 LOG(WARNING) << "Attempted to destroy barrier with non zero count " << count_;
97 }
98 }
Mathieu Chartier858f1c52012-10-17 17:45:55 -070099}
100
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700101} // namespace art