Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1309)

Side by Side Diff: mojo/edk/system/waiter.h

Issue 779503003: Extract Awakable from Waiter (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Review Update Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "base/macros.h" 10 #include "base/macros.h"
11 #include "base/synchronization/condition_variable.h" 11 #include "base/synchronization/condition_variable.h"
12 #include "base/synchronization/lock.h" 12 #include "base/synchronization/lock.h"
13 #include "mojo/edk/system/awakable.h"
13 #include "mojo/edk/system/system_impl_export.h" 14 #include "mojo/edk/system/system_impl_export.h"
14 #include "mojo/public/c/system/types.h" 15 #include "mojo/public/c/system/types.h"
15 16
16 namespace mojo { 17 namespace mojo {
17 namespace system { 18 namespace system {
18 19
19 // IMPORTANT (all-caps gets your attention, right?): |Waiter| methods are called 20 // IMPORTANT (all-caps gets your attention, right?): |Waiter| methods are called
20 // under other locks, in particular, |Dispatcher::lock_|s, so |Waiter| methods 21 // under other locks, in particular, |Dispatcher::lock_|s, so |Waiter| methods
21 // must never call out to other objects (in particular, |Dispatcher|s). This 22 // must never call out to other objects (in particular, |Dispatcher|s). This
22 // class is thread-safe. 23 // class is thread-safe.
23 class MOJO_SYSTEM_IMPL_EXPORT Waiter { 24 class MOJO_SYSTEM_IMPL_EXPORT Waiter : public Awakable {
24 public: 25 public:
25 Waiter(); 26 Waiter();
26 ~Waiter(); 27 ~Waiter();
viettrungluu 2014/12/03 22:31:27 Maybe this should be declared virtual. (It's not
Hajime Morrita 2014/12/03 22:47:11 Added protected ~Awakable() to make it clear that
27 28
28 // A |Waiter| can be used multiple times; |Init()| should be called before 29 // A |Waiter| can be used multiple times; |Init()| should be called before
29 // each time it's used. 30 // each time it's used.
30 void Init(); 31 void Init();
31 32
32 // Waits until a suitable |Awake()| is called. (|context| may be null, in 33 // Waits until a suitable |Awake()| is called. (|context| may be null, in
33 // which case, obviously no context is ever returned.) 34 // which case, obviously no context is ever returned.)
34 // Returns: 35 // Returns:
35 // - The result given to the first call to |Awake()| (possibly before this 36 // - The result given to the first call to |Awake()| (possibly before this
36 // call to |Wait()|); in this case, |*context| is set to the value passed 37 // call to |Wait()|); in this case, |*context| is set to the value passed
(...skipping 13 matching lines...) Expand all
50 // |MojoWait()|/|MojoWaitMany()| was called) was closed (hence the 51 // |MojoWait()|/|MojoWaitMany()| was called) was closed (hence the
51 // dispatcher closed); and 52 // dispatcher closed); and
52 // - |MOJO_RESULT_FAILED_PRECONDITION| if one of the set of flags passed to 53 // - |MOJO_RESULT_FAILED_PRECONDITION| if one of the set of flags passed to
53 // |MojoWait()|/|MojoWaitMany()| cannot or can no longer be satisfied by 54 // |MojoWait()|/|MojoWaitMany()| cannot or can no longer be satisfied by
54 // the corresponding handle (e.g., if the other end of a message or data 55 // the corresponding handle (e.g., if the other end of a message or data
55 // pipe is closed). 56 // pipe is closed).
56 MojoResult Wait(MojoDeadline deadline, uint32_t* context); 57 MojoResult Wait(MojoDeadline deadline, uint32_t* context);
57 58
58 // Wake the waiter up with the given result and context (or no-op if it's been 59 // Wake the waiter up with the given result and context (or no-op if it's been
59 // woken up already). 60 // woken up already).
60 void Awake(MojoResult result, uint32_t context); 61 void Awake(MojoResult result, uintptr_t context) override;
61 62
62 private: 63 private:
63 base::ConditionVariable cv_; // Associated to |lock_|. 64 base::ConditionVariable cv_; // Associated to |lock_|.
64 base::Lock lock_; // Protects the following members. 65 base::Lock lock_; // Protects the following members.
65 #ifndef NDEBUG 66 #ifndef NDEBUG
66 bool initialized_; 67 bool initialized_;
67 #endif 68 #endif
68 bool awoken_; 69 bool awoken_;
69 MojoResult awake_result_; 70 MojoResult awake_result_;
70 // This is a |uint32_t| because we really only need to store an index (for 71 // This is a |uint32_t| because we really only need to store an index (for
viettrungluu 2014/12/03 22:31:27 Remove this comment.
71 // |MojoWaitMany()|). But in tests, it's convenient to use this for other 72 // |MojoWaitMany()|). But in tests, it's convenient to use this for other
72 // purposes (e.g., to distinguish between different wake-up reasons). 73 // purposes (e.g., to distinguish between different wake-up reasons).
73 uint32_t awake_context_; 74 uintptr_t awake_context_;
74 75
75 DISALLOW_COPY_AND_ASSIGN(Waiter); 76 DISALLOW_COPY_AND_ASSIGN(Waiter);
76 }; 77 };
77 78
78 } // namespace system 79 } // namespace system
79 } // namespace mojo 80 } // namespace mojo
80 81
81 #endif // MOJO_EDK_SYSTEM_WAITER_H_ 82 #endif // MOJO_EDK_SYSTEM_WAITER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698