Elliott Hughes | 76b6167 | 2012-12-12 17:47:30 -0800 | [diff] [blame] | 1 | /* |
| 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 Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 17 | #include "barrier.h" |
Elliott Hughes | 76b6167 | 2012-12-12 17:47:30 -0800 | [diff] [blame] | 18 | |
Andreas Gampe | 39b378c | 2017-12-07 15:44:13 -0800 | [diff] [blame] | 19 | #include <android-base/logging.h> |
| 20 | |
| 21 | #include "base/aborting.h" |
Elliott Hughes | 76b6167 | 2012-12-12 17:47:30 -0800 | [diff] [blame] | 22 | #include "base/mutex.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 23 | #include "base/time_utils.h" |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 24 | #include "thread.h" |
| 25 | |
| 26 | namespace art { |
| 27 | |
Mathieu Chartier | 4f1e328 | 2019-03-27 14:41:32 -0700 | [diff] [blame] | 28 | Barrier::Barrier(int count, bool verify_count_on_shutdown) |
Mathieu Chartier | 35883cc | 2012-11-13 14:08:12 -0800 | [diff] [blame] | 29 | : count_(count), |
Andreas Gampe | 365a64b | 2018-11-29 11:24:00 -0800 | [diff] [blame] | 30 | lock_(new Mutex("GC barrier lock", kThreadSuspendCountLock)), |
Mathieu Chartier | 4f1e328 | 2019-03-27 14:41:32 -0700 | [diff] [blame] | 31 | condition_(new ConditionVariable("GC barrier condition", *lock_)), |
| 32 | verify_count_on_shutdown_(verify_count_on_shutdown) { |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 33 | } |
| 34 | |
Alex Light | 318afe6 | 2018-03-22 16:50:10 -0700 | [diff] [blame] | 35 | template void Barrier::Increment<Barrier::kAllowHoldingLocks>(Thread* self, int delta); |
| 36 | template void Barrier::Increment<Barrier::kDisallowHoldingLocks>(Thread* self, int delta); |
| 37 | |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 38 | void Barrier::Pass(Thread* self) { |
Andreas Gampe | 365a64b | 2018-11-29 11:24:00 -0800 | [diff] [blame] | 39 | MutexLock mu(self, *GetLock()); |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 40 | SetCountLocked(self, count_ - 1); |
| 41 | } |
| 42 | |
| 43 | void Barrier::Wait(Thread* self) { |
| 44 | Increment(self, -1); |
| 45 | } |
| 46 | |
| 47 | void Barrier::Init(Thread* self, int count) { |
Andreas Gampe | 365a64b | 2018-11-29 11:24:00 -0800 | [diff] [blame] | 48 | MutexLock mu(self, *GetLock()); |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 49 | SetCountLocked(self, count); |
| 50 | } |
| 51 | |
Alex Light | 318afe6 | 2018-03-22 16:50:10 -0700 | [diff] [blame] | 52 | template <Barrier::LockHandling locks> |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 53 | void Barrier::Increment(Thread* self, int delta) { |
Andreas Gampe | 365a64b | 2018-11-29 11:24:00 -0800 | [diff] [blame] | 54 | MutexLock mu(self, *GetLock()); |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 55 | SetCountLocked(self, count_ + delta); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 56 | |
| 57 | // Increment the count. If it becomes zero after the increment |
| 58 | // then all the threads have already passed the barrier. If |
| 59 | // it is non-zero then there is still one or more threads |
| 60 | // that have not yet called the Pass function. When the |
| 61 | // Pass function is called by the last thread, the count will |
| 62 | // be decremented to zero and a Broadcast will be made on the |
| 63 | // condition variable, thus waking this up. |
Hans Boehm | 5567c11 | 2014-12-02 18:31:31 -0800 | [diff] [blame] | 64 | while (count_ != 0) { |
Alex Light | 318afe6 | 2018-03-22 16:50:10 -0700 | [diff] [blame] | 65 | if (locks == kAllowHoldingLocks) { |
Andreas Gampe | 365a64b | 2018-11-29 11:24:00 -0800 | [diff] [blame] | 66 | condition_->WaitHoldingLocks(self); |
Alex Light | 318afe6 | 2018-03-22 16:50:10 -0700 | [diff] [blame] | 67 | } else { |
Andreas Gampe | 365a64b | 2018-11-29 11:24:00 -0800 | [diff] [blame] | 68 | condition_->Wait(self); |
Alex Light | 318afe6 | 2018-03-22 16:50:10 -0700 | [diff] [blame] | 69 | } |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 73 | bool Barrier::Increment(Thread* self, int delta, uint32_t timeout_ms) { |
Andreas Gampe | 365a64b | 2018-11-29 11:24:00 -0800 | [diff] [blame] | 74 | MutexLock mu(self, *GetLock()); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 75 | SetCountLocked(self, count_ + delta); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 76 | bool timed_out = false; |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 77 | if (count_ != 0) { |
Hans Boehm | 5567c11 | 2014-12-02 18:31:31 -0800 | [diff] [blame] | 78 | uint32_t timeout_ns = 0; |
| 79 | uint64_t abs_timeout = NanoTime() + MsToNs(timeout_ms); |
| 80 | for (;;) { |
Andreas Gampe | 365a64b | 2018-11-29 11:24:00 -0800 | [diff] [blame] | 81 | timed_out = condition_->TimedWait(self, timeout_ms, timeout_ns); |
Hans Boehm | 5567c11 | 2014-12-02 18:31:31 -0800 | [diff] [blame] | 82 | if (timed_out || count_ == 0) return timed_out; |
| 83 | // Compute time remaining on timeout. |
| 84 | uint64_t now = NanoTime(); |
| 85 | int64_t time_left = abs_timeout - now; |
| 86 | if (time_left <= 0) return true; |
| 87 | timeout_ns = time_left % (1000*1000); |
| 88 | timeout_ms = time_left / (1000*1000); |
| 89 | } |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 90 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 91 | return timed_out; |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Hiroshi Yamauchi | a82769c | 2016-12-02 17:01:51 -0800 | [diff] [blame] | 94 | int Barrier::GetCount(Thread* self) { |
Andreas Gampe | 365a64b | 2018-11-29 11:24:00 -0800 | [diff] [blame] | 95 | MutexLock mu(self, *GetLock()); |
Hiroshi Yamauchi | a82769c | 2016-12-02 17:01:51 -0800 | [diff] [blame] | 96 | return count_; |
| 97 | } |
| 98 | |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 99 | void Barrier::SetCountLocked(Thread* self, int count) { |
| 100 | count_ = count; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 101 | if (count == 0) { |
Andreas Gampe | 365a64b | 2018-11-29 11:24:00 -0800 | [diff] [blame] | 102 | condition_->Broadcast(self); |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
| 106 | Barrier::~Barrier() { |
Mathieu Chartier | 4f1e328 | 2019-03-27 14:41:32 -0700 | [diff] [blame] | 107 | if (count_ != 0) { |
| 108 | // Only check when not aborting and if we verify the count on shutdown. |
| 109 | LOG((gAborting == 0 && verify_count_on_shutdown_) ? FATAL : WARNING) |
Mathieu Chartier | 5a83225 | 2019-03-28 07:31:16 -0700 | [diff] [blame] | 110 | << "Attempted to destroy barrier with non zero count " << count_; |
Andreas Gampe | 1bb907e | 2015-06-22 10:04:39 -0700 | [diff] [blame] | 111 | } |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Brian Carlstrom | 0cd7ec2 | 2013-07-17 23:40:20 -0700 | [diff] [blame] | 114 | } // namespace art |