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
172f62cfe690257d516233b399f68d322b1cf8e4f4Hans Boehm// CAUTION: THIS IS NOT A FULLY GENERAL BARRIER API.
182f62cfe690257d516233b399f68d322b1cf8e4f4Hans Boehm
192f62cfe690257d516233b399f68d322b1cf8e4f4Hans Boehm// It may either be used as a "latch" or single-use barrier, or it may be reused under
202f62cfe690257d516233b399f68d322b1cf8e4f4Hans Boehm// very limited conditions, e.g. if only Pass(), but not Wait() is called.  Unlike a standard
212f62cfe690257d516233b399f68d322b1cf8e4f4Hans Boehm// latch API, it is possible to initialize the latch to a count of zero, repeatedly call
222f62cfe690257d516233b399f68d322b1cf8e4f4Hans Boehm// Pass() or Wait(), and only then set the count using the Increment() method.  Threads at
232f62cfe690257d516233b399f68d322b1cf8e4f4Hans Boehm// a Wait() are only awoken if the count reaches zero AFTER the decrement is applied.
242f62cfe690257d516233b399f68d322b1cf8e4f4Hans Boehm// This works because, also unlike most latch APIs, there is no way to Wait() without
252f62cfe690257d516233b399f68d322b1cf8e4f4Hans Boehm// decrementing the count, and thus nobody can spuriosly wake up on the initial zero.
262f62cfe690257d516233b399f68d322b1cf8e4f4Hans 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
352f62cfe690257d516233b399f68d322b1cf8e4f4Hans 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
412f62cfe690257d516233b399f68d322b1cf8e4f4Hans Boehm  // Pass through the barrier, decrement the count but do not block.
42858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier  void Pass(Thread* self);
43858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier
44858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier  // Wait on the barrier, decrement the count.
45858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier  void Wait(Thread* self);
46858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier
472f62cfe690257d516233b399f68d322b1cf8e4f4Hans Boehm  // The following three calls are only safe if we somehow know that no other thread both
482f62cfe690257d516233b399f68d322b1cf8e4f4Hans Boehm  // - has been woken up, and
492f62cfe690257d516233b399f68d322b1cf8e4f4Hans Boehm  // - has not left the Wait() or Increment() call.
502f62cfe690257d516233b399f68d322b1cf8e4f4Hans Boehm  // If these calls are made in that situation, the offending thread is likely to go back
512f62cfe690257d516233b399f68d322b1cf8e4f4Hans Boehm  // to sleep, resulting in a deadlock.
52858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier
53858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier  // Increment the count by delta, wait on condition if count is non zero.
54858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier  void Increment(Thread* self, int delta);
55858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier
560aded089f565008ba5908e395e5914ca4f91f2deDave Allison  // Increment the count by delta, wait on condition if count is non zero, with a timeout
570aded089f565008ba5908e395e5914ca4f91f2deDave Allison  void Increment(Thread* self, int delta, uint32_t timeout_ms) LOCKS_EXCLUDED(lock_);
580aded089f565008ba5908e395e5914ca4f91f2deDave Allison
592f62cfe690257d516233b399f68d322b1cf8e4f4Hans Boehm  // Set the count to a new value.  This should only be used if there is no possibility that
602f62cfe690257d516233b399f68d322b1cf8e4f4Hans Boehm  // another thread is still in Wait().  See above.
612f62cfe690257d516233b399f68d322b1cf8e4f4Hans Boehm  void Init(Thread* self, int count);
622f62cfe690257d516233b399f68d322b1cf8e4f4Hans Boehm
63858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier private:
64858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier  void SetCountLocked(Thread* self, int count) EXCLUSIVE_LOCKS_REQUIRED(lock_);
65858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier
66858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier  // Counter, when this reaches 0 all people blocked on the barrier are signalled.
67858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier  int count_ GUARDED_BY(lock_);
68858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier
69858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier  Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
70858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier  ConditionVariable condition_ GUARDED_BY(lock_);
71858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier};
72858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier
73858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier}  // namespace art
74fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#endif  // ART_RUNTIME_BARRIER_H_
75