1858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier/* 2858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier * Copyright (C) 2012 The Android Open Source Project 3858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier * 4858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier * Licensed under the Apache License, Version 2.0 (the "License"); 5858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier * you may not use this file except in compliance with the License. 6858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier * You may obtain a copy of the License at 7858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier * 8858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier * http://www.apache.org/licenses/LICENSE-2.0 9858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier * 10858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier * Unless required by applicable law or agreed to in writing, software 11858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier * distributed under the License is distributed on an "AS IS" BASIS, 12858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier * See the License for the specific language governing permissions and 14858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier * limitations under the License. 15858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier */ 16858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier 175567c11b9157eec110c0631aa2bff5836631e868Hans Boehm// CAUTION: THIS IS NOT A FULLY GENERAL BARRIER API. 185567c11b9157eec110c0631aa2bff5836631e868Hans Boehm 195567c11b9157eec110c0631aa2bff5836631e868Hans Boehm// It may either be used as a "latch" or single-use barrier, or it may be reused under 205567c11b9157eec110c0631aa2bff5836631e868Hans Boehm// very limited conditions, e.g. if only Pass(), but not Wait() is called. Unlike a standard 215567c11b9157eec110c0631aa2bff5836631e868Hans Boehm// latch API, it is possible to initialize the latch to a count of zero, repeatedly call 225567c11b9157eec110c0631aa2bff5836631e868Hans Boehm// Pass() or Wait(), and only then set the count using the Increment() method. Threads at 235567c11b9157eec110c0631aa2bff5836631e868Hans Boehm// a Wait() are only awoken if the count reaches zero AFTER the decrement is applied. 245567c11b9157eec110c0631aa2bff5836631e868Hans Boehm// This works because, also unlike most latch APIs, there is no way to Wait() without 255567c11b9157eec110c0631aa2bff5836631e868Hans Boehm// decrementing the count, and thus nobody can spuriosly wake up on the initial zero. 265567c11b9157eec110c0631aa2bff5836631e868Hans Boehm 27fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#ifndef ART_RUNTIME_BARRIER_H_ 28fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#define ART_RUNTIME_BARRIER_H_ 29858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier 30700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers#include <memory> 3176b6167407c2b6f5d40ad895b2793a6b037f54b2Elliott Hughes#include "base/mutex.h" 32858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier 33858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartiernamespace art { 34858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier 355567c11b9157eec110c0631aa2bff5836631e868Hans Boehm// TODO: Maybe give this a better name. 36858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartierclass Barrier { 37858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier public: 3893ba893c20532990a430741e0a97212900094e8cBrian Carlstrom explicit Barrier(int count); 39858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier virtual ~Barrier(); 40858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier 415567c11b9157eec110c0631aa2bff5836631e868Hans Boehm // Pass through the barrier, decrement the count but do not block. 4290443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier void Pass(Thread* self) REQUIRES(!lock_); 43858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier 44858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier // Wait on the barrier, decrement the count. 4590443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier void Wait(Thread* self) REQUIRES(!lock_); 46858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier 475567c11b9157eec110c0631aa2bff5836631e868Hans Boehm // The following three calls are only safe if we somehow know that no other thread both 485567c11b9157eec110c0631aa2bff5836631e868Hans Boehm // - has been woken up, and 495567c11b9157eec110c0631aa2bff5836631e868Hans Boehm // - has not left the Wait() or Increment() call. 505567c11b9157eec110c0631aa2bff5836631e868Hans Boehm // If these calls are made in that situation, the offending thread is likely to go back 515567c11b9157eec110c0631aa2bff5836631e868Hans Boehm // to sleep, resulting in a deadlock. 52858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier 53858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier // Increment the count by delta, wait on condition if count is non zero. 5490ef3db4bd1d4865f5f9cb95c8e7d9afb46994f9Mathieu Chartier void Increment(Thread* self, int delta) REQUIRES(!lock_); 55858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier 567b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers // Increment the count by delta, wait on condition if count is non zero, with a timeout. Returns 577b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers // true if time out occurred. 5890443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier bool Increment(Thread* self, int delta, uint32_t timeout_ms) REQUIRES(!lock_); 590aded089f565008ba5908e395e5914ca4f91f2deDave Allison 605567c11b9157eec110c0631aa2bff5836631e868Hans Boehm // Set the count to a new value. This should only be used if there is no possibility that 615567c11b9157eec110c0631aa2bff5836631e868Hans Boehm // another thread is still in Wait(). See above. 6290443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier void Init(Thread* self, int count) REQUIRES(!lock_); 635567c11b9157eec110c0631aa2bff5836631e868Hans Boehm 64858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier private: 6590443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier void SetCountLocked(Thread* self, int count) REQUIRES(lock_); 66858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier 67858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier // Counter, when this reaches 0 all people blocked on the barrier are signalled. 68858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier int count_ GUARDED_BY(lock_); 69858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier 708409ec440079020bbe4ad066cf18a5fadfba67d2Ian Rogers Mutex lock_ ACQUIRED_AFTER(Locks::abort_lock_); 71858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier ConditionVariable condition_ GUARDED_BY(lock_); 72858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier}; 73858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier 74858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier} // namespace art 75fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#endif // ART_RUNTIME_BARRIER_H_ 76