Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [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 | |
Hans Boehm | 5567c11 | 2014-12-02 18:31:31 -0800 | [diff] [blame] | 17 | // CAUTION: THIS IS NOT A FULLY GENERAL BARRIER API. |
| 18 | |
| 19 | // It may either be used as a "latch" or single-use barrier, or it may be reused under |
| 20 | // very limited conditions, e.g. if only Pass(), but not Wait() is called. Unlike a standard |
| 21 | // latch API, it is possible to initialize the latch to a count of zero, repeatedly call |
| 22 | // Pass() or Wait(), and only then set the count using the Increment() method. Threads at |
| 23 | // a Wait() are only awoken if the count reaches zero AFTER the decrement is applied. |
| 24 | // This works because, also unlike most latch APIs, there is no way to Wait() without |
| 25 | // decrementing the count, and thus nobody can spuriosly wake up on the initial zero. |
| 26 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 27 | #ifndef ART_RUNTIME_BARRIER_H_ |
| 28 | #define ART_RUNTIME_BARRIER_H_ |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 29 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 30 | #include <memory> |
Andreas Gampe | 365a64b | 2018-11-29 11:24:00 -0800 | [diff] [blame] | 31 | |
| 32 | #include "base/locks.h" |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 33 | |
| 34 | namespace art { |
| 35 | |
Andreas Gampe | 365a64b | 2018-11-29 11:24:00 -0800 | [diff] [blame] | 36 | class ConditionVariable; |
| 37 | class LOCKABLE Mutex; |
| 38 | |
Hans Boehm | 5567c11 | 2014-12-02 18:31:31 -0800 | [diff] [blame] | 39 | // TODO: Maybe give this a better name. |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 40 | class Barrier { |
| 41 | public: |
Alex Light | 318afe6 | 2018-03-22 16:50:10 -0700 | [diff] [blame] | 42 | enum LockHandling { |
| 43 | kAllowHoldingLocks, |
| 44 | kDisallowHoldingLocks, |
| 45 | }; |
| 46 | |
Mathieu Chartier | 4f1e328 | 2019-03-27 14:41:32 -0700 | [diff] [blame] | 47 | // If verify_count_on_shutdown is true, the destructor verifies that the count is zero in the |
Mathieu Chartier | 5a83225 | 2019-03-28 07:31:16 -0700 | [diff] [blame] | 48 | // destructor. This means that all expected threads went through the barrier. |
Mathieu Chartier | 4f1e328 | 2019-03-27 14:41:32 -0700 | [diff] [blame] | 49 | explicit Barrier(int count, bool verify_count_on_shutdown = true); |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 50 | virtual ~Barrier(); |
| 51 | |
Hans Boehm | 5567c11 | 2014-12-02 18:31:31 -0800 | [diff] [blame] | 52 | // Pass through the barrier, decrement the count but do not block. |
Andreas Gampe | 365a64b | 2018-11-29 11:24:00 -0800 | [diff] [blame] | 53 | void Pass(Thread* self) REQUIRES(!GetLock()); |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 54 | |
| 55 | // Wait on the barrier, decrement the count. |
Andreas Gampe | 365a64b | 2018-11-29 11:24:00 -0800 | [diff] [blame] | 56 | void Wait(Thread* self) REQUIRES(!GetLock()); |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 57 | |
Hans Boehm | 5567c11 | 2014-12-02 18:31:31 -0800 | [diff] [blame] | 58 | // The following three calls are only safe if we somehow know that no other thread both |
| 59 | // - has been woken up, and |
| 60 | // - has not left the Wait() or Increment() call. |
| 61 | // If these calls are made in that situation, the offending thread is likely to go back |
| 62 | // to sleep, resulting in a deadlock. |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 63 | |
Alex Light | 318afe6 | 2018-03-22 16:50:10 -0700 | [diff] [blame] | 64 | // Increment the count by delta, wait on condition if count is non zero. If LockHandling is |
| 65 | // kAllowHoldingLocks we will not check that all locks are released when waiting. |
| 66 | template <Barrier::LockHandling locks = kDisallowHoldingLocks> |
Andreas Gampe | 365a64b | 2018-11-29 11:24:00 -0800 | [diff] [blame] | 67 | void Increment(Thread* self, int delta) REQUIRES(!GetLock()); |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 68 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 69 | // Increment the count by delta, wait on condition if count is non zero, with a timeout. Returns |
| 70 | // true if time out occurred. |
Andreas Gampe | 365a64b | 2018-11-29 11:24:00 -0800 | [diff] [blame] | 71 | bool Increment(Thread* self, int delta, uint32_t timeout_ms) REQUIRES(!GetLock()); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 72 | |
Hans Boehm | 5567c11 | 2014-12-02 18:31:31 -0800 | [diff] [blame] | 73 | // Set the count to a new value. This should only be used if there is no possibility that |
| 74 | // another thread is still in Wait(). See above. |
Andreas Gampe | 365a64b | 2018-11-29 11:24:00 -0800 | [diff] [blame] | 75 | void Init(Thread* self, int count) REQUIRES(!GetLock()); |
Hans Boehm | 5567c11 | 2014-12-02 18:31:31 -0800 | [diff] [blame] | 76 | |
Andreas Gampe | 365a64b | 2018-11-29 11:24:00 -0800 | [diff] [blame] | 77 | int GetCount(Thread* self) REQUIRES(!GetLock()); |
Hiroshi Yamauchi | a82769c | 2016-12-02 17:01:51 -0800 | [diff] [blame] | 78 | |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 79 | private: |
Andreas Gampe | 365a64b | 2018-11-29 11:24:00 -0800 | [diff] [blame] | 80 | void SetCountLocked(Thread* self, int count) REQUIRES(GetLock()); |
| 81 | |
| 82 | Mutex* GetLock() { |
| 83 | return lock_.get(); |
| 84 | } |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 85 | |
| 86 | // Counter, when this reaches 0 all people blocked on the barrier are signalled. |
Andreas Gampe | 365a64b | 2018-11-29 11:24:00 -0800 | [diff] [blame] | 87 | int count_ GUARDED_BY(GetLock()); |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 88 | |
Andreas Gampe | 365a64b | 2018-11-29 11:24:00 -0800 | [diff] [blame] | 89 | std::unique_ptr<Mutex> lock_ ACQUIRED_AFTER(Locks::abort_lock_); |
| 90 | std::unique_ptr<ConditionVariable> condition_ GUARDED_BY(GetLock()); |
Mathieu Chartier | 4f1e328 | 2019-03-27 14:41:32 -0700 | [diff] [blame] | 91 | const bool verify_count_on_shutdown_; |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | } // namespace art |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 95 | #endif // ART_RUNTIME_BARRIER_H_ |