blob: 4329a5a2450cd4ae557e4e392c887d3e75d4afdb [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 Gampe39b378c2017-12-07 15:44:13 -080019#include <android-base/logging.h>
20
21#include "base/aborting.h"
Elliott Hughes76b61672012-12-12 17:47:30 -080022#include "base/mutex.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010023#include "base/time_utils.h"
Mathieu Chartier858f1c52012-10-17 17:45:55 -070024#include "thread.h"
25
26namespace art {
27
Mathieu Chartier35883cc2012-11-13 14:08:12 -080028Barrier::Barrier(int count)
29 : count_(count),
Ian Rogers8409ec42014-11-04 17:57:02 -080030 lock_("GC barrier lock", kThreadSuspendCountLock),
Mathieu Chartier858f1c52012-10-17 17:45:55 -070031 condition_("GC barrier condition", lock_) {
32}
33
34void Barrier::Pass(Thread* self) {
35 MutexLock mu(self, lock_);
36 SetCountLocked(self, count_ - 1);
37}
38
39void Barrier::Wait(Thread* self) {
40 Increment(self, -1);
41}
42
43void Barrier::Init(Thread* self, int count) {
44 MutexLock mu(self, lock_);
45 SetCountLocked(self, count);
46}
47
48void Barrier::Increment(Thread* self, int delta) {
49 MutexLock mu(self, lock_);
50 SetCountLocked(self, count_ + delta);
Dave Allison0aded082013-11-07 13:15:11 -080051
52 // Increment the count. If it becomes zero after the increment
53 // then all the threads have already passed the barrier. If
54 // it is non-zero then there is still one or more threads
55 // that have not yet called the Pass function. When the
56 // Pass function is called by the last thread, the count will
57 // be decremented to zero and a Broadcast will be made on the
58 // condition variable, thus waking this up.
Hans Boehm5567c112014-12-02 18:31:31 -080059 while (count_ != 0) {
Mathieu Chartier858f1c52012-10-17 17:45:55 -070060 condition_.Wait(self);
61 }
62}
63
Ian Rogers7b078e82014-09-10 14:44:24 -070064bool Barrier::Increment(Thread* self, int delta, uint32_t timeout_ms) {
Dave Allison0aded082013-11-07 13:15:11 -080065 MutexLock mu(self, lock_);
66 SetCountLocked(self, count_ + delta);
Ian Rogers7b078e82014-09-10 14:44:24 -070067 bool timed_out = false;
Dave Allison0aded082013-11-07 13:15:11 -080068 if (count_ != 0) {
Hans Boehm5567c112014-12-02 18:31:31 -080069 uint32_t timeout_ns = 0;
70 uint64_t abs_timeout = NanoTime() + MsToNs(timeout_ms);
71 for (;;) {
72 timed_out = condition_.TimedWait(self, timeout_ms, timeout_ns);
73 if (timed_out || count_ == 0) return timed_out;
74 // Compute time remaining on timeout.
75 uint64_t now = NanoTime();
76 int64_t time_left = abs_timeout - now;
77 if (time_left <= 0) return true;
78 timeout_ns = time_left % (1000*1000);
79 timeout_ms = time_left / (1000*1000);
80 }
Dave Allison0aded082013-11-07 13:15:11 -080081 }
Ian Rogers7b078e82014-09-10 14:44:24 -070082 return timed_out;
Dave Allison0aded082013-11-07 13:15:11 -080083}
84
Hiroshi Yamauchia82769c2016-12-02 17:01:51 -080085int Barrier::GetCount(Thread* self) {
86 MutexLock mu(self, lock_);
87 return count_;
88}
89
Mathieu Chartier858f1c52012-10-17 17:45:55 -070090void Barrier::SetCountLocked(Thread* self, int count) {
91 count_ = count;
Ian Rogers7b078e82014-09-10 14:44:24 -070092 if (count == 0) {
Mathieu Chartier858f1c52012-10-17 17:45:55 -070093 condition_.Broadcast(self);
94 }
95}
96
97Barrier::~Barrier() {
Andreas Gampe1bb907e2015-06-22 10:04:39 -070098 if (gAborting == 0) {
99 // Only check when not aborting.
100 CHECK_EQ(count_, 0) << "Attempted to destroy barrier with non zero count";
101 } else {
102 if (count_ != 0) {
103 LOG(WARNING) << "Attempted to destroy barrier with non zero count " << count_;
104 }
105 }
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700106}
107
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700108} // namespace art