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> |
Elliott Hughes | 76b6167 | 2012-12-12 17:47:30 -0800 | [diff] [blame] | 31 | #include "base/mutex.h" |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 32 | |
| 33 | namespace art { |
| 34 | |
Hans Boehm | 5567c11 | 2014-12-02 18:31:31 -0800 | [diff] [blame] | 35 | // TODO: Maybe give this a better name. |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 36 | class Barrier { |
| 37 | public: |
Brian Carlstrom | 93ba893 | 2013-07-17 21:31:49 -0700 | [diff] [blame] | 38 | explicit Barrier(int count); |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 39 | virtual ~Barrier(); |
| 40 | |
Hans Boehm | 5567c11 | 2014-12-02 18:31:31 -0800 | [diff] [blame] | 41 | // Pass through the barrier, decrement the count but do not block. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 42 | void Pass(Thread* self) REQUIRES(!lock_); |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 43 | |
| 44 | // Wait on the barrier, decrement the count. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 45 | void Wait(Thread* self) REQUIRES(!lock_); |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 46 | |
Hans Boehm | 5567c11 | 2014-12-02 18:31:31 -0800 | [diff] [blame] | 47 | // The following three calls are only safe if we somehow know that no other thread both |
| 48 | // - has been woken up, and |
| 49 | // - has not left the Wait() or Increment() call. |
| 50 | // If these calls are made in that situation, the offending thread is likely to go back |
| 51 | // to sleep, resulting in a deadlock. |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 52 | |
| 53 | // Increment the count by delta, wait on condition if count is non zero. |
Mathieu Chartier | 90ef3db | 2015-08-04 15:19:41 -0700 | [diff] [blame] | 54 | void Increment(Thread* self, int delta) REQUIRES(!lock_); |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 55 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 56 | // Increment the count by delta, wait on condition if count is non zero, with a timeout. Returns |
| 57 | // true if time out occurred. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 58 | bool Increment(Thread* self, int delta, uint32_t timeout_ms) REQUIRES(!lock_); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 59 | |
Hans Boehm | 5567c11 | 2014-12-02 18:31:31 -0800 | [diff] [blame] | 60 | // Set the count to a new value. This should only be used if there is no possibility that |
| 61 | // another thread is still in Wait(). See above. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 62 | void Init(Thread* self, int count) REQUIRES(!lock_); |
Hans Boehm | 5567c11 | 2014-12-02 18:31:31 -0800 | [diff] [blame] | 63 | |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 64 | private: |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 65 | void SetCountLocked(Thread* self, int count) REQUIRES(lock_); |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 66 | |
| 67 | // Counter, when this reaches 0 all people blocked on the barrier are signalled. |
| 68 | int count_ GUARDED_BY(lock_); |
| 69 | |
Ian Rogers | 8409ec4 | 2014-11-04 17:57:02 -0800 | [diff] [blame] | 70 | Mutex lock_ ACQUIRED_AFTER(Locks::abort_lock_); |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 71 | ConditionVariable condition_ GUARDED_BY(lock_); |
| 72 | }; |
| 73 | |
| 74 | } // namespace art |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 75 | #endif // ART_RUNTIME_BARRIER_H_ |