| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef MOJO_EDK_SYSTEM_WAITER_H_ | 5 #ifndef MOJO_EDK_SYSTEM_WAITER_H_ |
| 6 #define MOJO_EDK_SYSTEM_WAITER_H_ | 6 #define MOJO_EDK_SYSTEM_WAITER_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include "mojo/edk/system/awakable.h" | 10 #include "mojo/edk/system/awakable.h" |
| 11 #include "mojo/edk/util/cond_var.h" | 11 #include "mojo/edk/util/cond_var.h" |
| 12 #include "mojo/edk/util/mutex.h" | 12 #include "mojo/edk/util/mutex.h" |
| 13 #include "mojo/edk/util/thread_annotations.h" | 13 #include "mojo/edk/util/thread_annotations.h" |
| 14 #include "mojo/public/c/system/result.h" | 14 #include "mojo/public/c/system/result.h" |
| 15 #include "mojo/public/c/system/time.h" | 15 #include "mojo/public/c/system/time.h" |
| 16 #include "mojo/public/cpp/system/macros.h" | 16 #include "mojo/public/cpp/system/macros.h" |
| 17 | 17 |
| 18 namespace mojo { | 18 namespace mojo { |
| 19 namespace system { | 19 namespace system { |
| 20 | 20 |
| 21 // IMPORTANT (all-caps gets your attention, right?): |Waiter| methods are called | 21 // An implementation of |Awakable| that is used for blocking waits. This should |
| 22 // under other locks, in particular, |Dispatcher::lock_|s, so |Waiter| methods | 22 // be used in a non-persistent way (i.e., |Awake()| should be called at most |
| 23 // must never call out to other objects (in particular, |Dispatcher|s). This | 23 // once by each source, and only for "leading edges"). |
| 24 // class is thread-safe. | 24 // |
| 25 // IMPORTANT: |Waiter| methods are called under other locks, in particular, |
| 26 // |Dispatcher::lock_|s, so |Waiter| methods must never call out to other |
| 27 // objects (in particular, |Dispatcher|s). |
| 28 // |
| 29 // This class is thread-safe. |
| 25 class Waiter final : public Awakable { | 30 class Waiter final : public Awakable { |
| 26 public: | 31 public: |
| 27 Waiter(); | 32 Waiter(); |
| 28 ~Waiter() override; | 33 ~Waiter() override; |
| 29 | 34 |
| 30 // A |Waiter| can be used multiple times; |Init()| should be called before | 35 // A |Waiter| can be used multiple times; |Init()| should be called before |
| 31 // each time it's used. | 36 // each time it's used. |
| 32 void Init() MOJO_NOT_THREAD_SAFE; | 37 void Init() MOJO_NOT_THREAD_SAFE; |
| 33 | 38 |
| 34 // Waits until a suitable |Awake()| is called. (|context| may be null, in | 39 // Waits until a suitable |Awake()| is called. (|context| may be null, in |
| 35 // which case, obviously no context is ever returned.) | 40 // which case, obviously no context is ever returned.) |
| 36 // Returns: | 41 // Returns: |
| 37 // - |MOJO_RESULT_OK| if |Awake()| was called with |AwakeReason::SATISFIED|; | 42 // - |MOJO_RESULT_OK| if |Awake()| was called with |AwakeReason::SATISFIED|; |
| 38 // - |MOJO_RESULT_CANCELLED| if |Awake()| was called with | 43 // - |MOJO_RESULT_CANCELLED| if |Awake()| was called with |
| 39 // |AwakeReason::CANCELLED|; | 44 // |AwakeReason::CANCELLED|; |
| 40 // - |MOJO_RESULT_FAILED_PRECONDITION| if |Awake()| was called with | 45 // - |MOJO_RESULT_FAILED_PRECONDITION| if |Awake()| was called with |
| 41 // |AwakeReason::UNSATISFIABLE|; or | 46 // |AwakeReason::UNSATISFIABLE|; or |
| 42 // - |MOJO_RESULT_DEADLINE_EXCEEDED| if the deadline was exceeded. | 47 // - |MOJO_RESULT_DEADLINE_EXCEEDED| if the deadline was exceeded. |
| 43 // | 48 // |
| 44 // In all the cases except |MOJO_RESULT_DEADLINE_EXCEEDED|, the context and | 49 // In all the cases except |MOJO_RESULT_DEADLINE_EXCEEDED|, the context and |
| 45 // signals state passed to |Awake()| be made available via |*context| and | 50 // signals state passed to |Awake()| be made available via |*context| and |
| 46 // |*signals_state|, respectively (unless the respective pointer is null). | 51 // |*signals_state|, respectively (unless the respective pointer is null). |
| 47 MojoResult Wait(MojoDeadline deadline, | 52 MojoResult Wait(MojoDeadline deadline, |
| 48 uint64_t* context, | 53 uint64_t* context, |
| 49 HandleSignalsState* signals_state); | 54 HandleSignalsState* signals_state); |
| 50 | 55 |
| 51 // |Awakable| implementation: | 56 // |Awakable| implementation: |
| 52 bool Awake(uint64_t context, | 57 void Awake(uint64_t context, |
| 53 AwakeReason reason, | 58 AwakeReason reason, |
| 54 const HandleSignalsState& signals_state) override; | 59 const HandleSignalsState& signals_state) override; |
| 55 | 60 |
| 56 private: | 61 private: |
| 57 util::CondVar cv_; // Associated to |mutex_|. | 62 util::CondVar cv_; // Associated to |mutex_|. |
| 58 util::Mutex mutex_; | 63 util::Mutex mutex_; |
| 59 #ifndef NDEBUG | 64 #ifndef NDEBUG |
| 60 bool initialized_ MOJO_GUARDED_BY(mutex_); | 65 bool initialized_ MOJO_GUARDED_BY(mutex_); |
| 61 #endif | 66 #endif |
| 62 bool awoken_ MOJO_GUARDED_BY(mutex_); | 67 bool awoken_ MOJO_GUARDED_BY(mutex_); |
| 63 AwakeReason awake_reason_ MOJO_GUARDED_BY(mutex_); | 68 AwakeReason awake_reason_ MOJO_GUARDED_BY(mutex_); |
| 64 HandleSignalsState signals_state_ MOJO_GUARDED_BY(mutex_); | 69 HandleSignalsState signals_state_ MOJO_GUARDED_BY(mutex_); |
| 65 uint64_t awake_context_ MOJO_GUARDED_BY(mutex_); | 70 uint64_t awake_context_ MOJO_GUARDED_BY(mutex_); |
| 66 | 71 |
| 67 MOJO_DISALLOW_COPY_AND_ASSIGN(Waiter); | 72 MOJO_DISALLOW_COPY_AND_ASSIGN(Waiter); |
| 68 }; | 73 }; |
| 69 | 74 |
| 70 } // namespace system | 75 } // namespace system |
| 71 } // namespace mojo | 76 } // namespace mojo |
| 72 | 77 |
| 73 #endif // MOJO_EDK_SYSTEM_WAITER_H_ | 78 #endif // MOJO_EDK_SYSTEM_WAITER_H_ |
| OLD | NEW |