OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_REQUEST_CONTEXT_H_ | 5 #ifndef MOJO_EDK_SYSTEM_REQUEST_CONTEXT_H_ |
6 #define MOJO_EDK_SYSTEM_REQUEST_CONTEXT_H_ | 6 #define MOJO_EDK_SYSTEM_REQUEST_CONTEXT_H_ |
7 | 7 |
8 #include "base/containers/stack_container.h" | 8 #include "base/containers/stack_container.h" |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "mojo/edk/system/handle_signals_state.h" | 10 #include "mojo/edk/system/handle_signals_state.h" |
11 #include "mojo/edk/system/system_impl_export.h" | |
11 #include "mojo/edk/system/watcher.h" | 12 #include "mojo/edk/system/watcher.h" |
12 | 13 |
13 namespace base { | 14 namespace base { |
14 template<typename T> class ThreadLocalPointer; | 15 template<typename T> class ThreadLocalPointer; |
15 } | 16 } |
16 | 17 |
17 namespace mojo { | 18 namespace mojo { |
18 namespace edk { | 19 namespace edk { |
19 | 20 |
20 // A RequestContext is a thread-local object which exists for the duration of | 21 // A RequestContext is a thread-local object which exists for the duration of |
21 // a single system API call. It is constructed immediately upon EDK entry and | 22 // a single system API call. It is constructed immediately upon EDK entry and |
22 // destructed immediately before returning to the caller, after any internal | 23 // destructed immediately before returning to the caller, after any internal |
23 // locks have been released. | 24 // locks have been released. |
24 // | 25 // |
25 // NOTE: It is legal to construct a RequestContext while another one already | 26 // NOTE: It is legal to construct a RequestContext while another one already |
26 // exists on the current thread, but it is not safe to use the nested context | 27 // exists on the current thread, but it is not safe to use the nested context |
27 // for any reason. Therefore it is important to always use | 28 // for any reason. Therefore it is important to always use |
28 // |RequestContext::current()| rather than referring to any local instance | 29 // |RequestContext::current()| rather than referring to any local instance |
29 // directly. | 30 // directly. |
30 class RequestContext { | 31 class MOJO_SYSTEM_IMPL_EXPORT RequestContext { |
31 public: | 32 public: |
32 RequestContext(); | 33 // Identifies the source of the current stack frame's RequestContext. |
34 enum class Source { | |
35 LOCAL_API_CALL, | |
36 EXTERNAL_PROCESS, | |
37 }; | |
38 | |
39 explicit RequestContext(Source source); | |
Anand Mistry (off Chromium)
2016/03/16 23:04:14
If local is the default and "external process" is
Ken Rockot(use gerrit already)
2016/03/17 00:15:34
Done
| |
33 ~RequestContext(); | 40 ~RequestContext(); |
34 | 41 |
35 // Returns the current thread-local RequestContext. | 42 // Returns the current thread-local RequestContext. |
36 static RequestContext* current(); | 43 static RequestContext* current(); |
37 | 44 |
45 Source source() const { return source_; } | |
46 | |
38 // Adds a finalizer to this RequestContext corresponding to a watch callback | 47 // Adds a finalizer to this RequestContext corresponding to a watch callback |
39 // which should be triggered in response to some handle state change. If | 48 // which should be triggered in response to some handle state change. If |
40 // the Watcher hasn't been cancelled by the time this RequestContext is | 49 // the Watcher hasn't been cancelled by the time this RequestContext is |
41 // destroyed, its WatchCallback will be invoked with |result| and |state| | 50 // destroyed, its WatchCallback will be invoked with |result| and |state| |
42 // arguments. | 51 // arguments. |
43 void AddWatchNotifyFinalizer(scoped_refptr<Watcher> watcher, | 52 void AddWatchNotifyFinalizer(scoped_refptr<Watcher> watcher, |
44 MojoResult result, | 53 MojoResult result, |
45 const HandleSignalsState& state); | 54 const HandleSignalsState& state); |
46 | 55 |
47 // Adds a finalizer to this RequestContext which cancels a watch. | 56 // Adds a finalizer to this RequestContext which cancels a watch. |
48 void AddWatchCancelFinalizer(scoped_refptr<Watcher> watcher); | 57 void AddWatchCancelFinalizer(scoped_refptr<Watcher> watcher); |
49 | 58 |
50 private: | 59 private: |
60 // | |
Anand Mistry (off Chromium)
2016/03/16 23:04:14
nit: blank comment line.
Ken Rockot(use gerrit already)
2016/03/17 00:15:34
done
| |
51 // Is this request context the current one? | 61 // Is this request context the current one? |
52 bool IsCurrent() const; | 62 bool IsCurrent() const; |
53 | 63 |
54 struct WatchNotifyFinalizer { | 64 struct WatchNotifyFinalizer { |
55 WatchNotifyFinalizer(scoped_refptr<Watcher> watcher, | 65 WatchNotifyFinalizer(scoped_refptr<Watcher> watcher, |
56 MojoResult result, | 66 MojoResult result, |
57 const HandleSignalsState& state); | 67 const HandleSignalsState& state); |
58 ~WatchNotifyFinalizer(); | 68 ~WatchNotifyFinalizer(); |
59 | 69 |
60 scoped_refptr<Watcher> watcher; | 70 scoped_refptr<Watcher> watcher; |
61 MojoResult result; | 71 MojoResult result; |
62 HandleSignalsState state; | 72 HandleSignalsState state; |
63 }; | 73 }; |
64 | 74 |
65 // Chosen by fair dice roll. | 75 // Chosen by fair dice roll. |
66 // | 76 // |
67 // TODO: We should measure the distribution of # of finalizers typical to | 77 // TODO: We should measure the distribution of # of finalizers typical to |
68 // any RequestContext and adjust this number accordingly. It's probably | 78 // any RequestContext and adjust this number accordingly. It's probably |
69 // almost always 1, but 4 seems like a harmless upper bound for now. | 79 // almost always 1, but 4 seems like a harmless upper bound for now. |
70 static const size_t kStaticWatchFinalizersCapacity = 4; | 80 static const size_t kStaticWatchFinalizersCapacity = 4; |
71 | 81 |
72 using WatchNotifyFinalizerList = | 82 using WatchNotifyFinalizerList = |
73 base::StackVector<WatchNotifyFinalizer, kStaticWatchFinalizersCapacity>; | 83 base::StackVector<WatchNotifyFinalizer, kStaticWatchFinalizersCapacity>; |
74 using WatchCancelFinalizerList = | 84 using WatchCancelFinalizerList = |
75 base::StackVector<scoped_refptr<Watcher>, kStaticWatchFinalizersCapacity>; | 85 base::StackVector<scoped_refptr<Watcher>, kStaticWatchFinalizersCapacity>; |
76 | 86 |
87 Source source_; | |
88 | |
89 // A nested RequestContext may override the Source of the thread-local | |
90 // RequestContext for the duration of the nested context's lifetime. | |
91 Source outer_source_; | |
92 | |
77 WatchNotifyFinalizerList watch_notify_finalizers_; | 93 WatchNotifyFinalizerList watch_notify_finalizers_; |
78 WatchCancelFinalizerList watch_cancel_finalizers_; | 94 WatchCancelFinalizerList watch_cancel_finalizers_; |
79 | 95 |
80 // Pointer to the TLS context. Although this can easily be accessed via the | 96 // Pointer to the TLS context. Although this can easily be accessed via the |
81 // global LazyInstance, accessing a LazyInstance has a large cost relative to | 97 // global LazyInstance, accessing a LazyInstance has a large cost relative to |
82 // the rest of this class and its usages. | 98 // the rest of this class and its usages. |
83 base::ThreadLocalPointer<RequestContext>* tls_context_; | 99 base::ThreadLocalPointer<RequestContext>* tls_context_; |
84 | 100 |
85 DISALLOW_COPY_AND_ASSIGN(RequestContext); | 101 DISALLOW_COPY_AND_ASSIGN(RequestContext); |
86 }; | 102 }; |
87 | 103 |
88 } // namespace edk | 104 } // namespace edk |
89 } // namespace mojo | 105 } // namespace mojo |
90 | 106 |
91 #endif // MOJO_EDK_SYSTEM_REQUEST_CONTEXT_H_ | 107 #endif // MOJO_EDK_SYSTEM_REQUEST_CONTEXT_H_ |
OLD | NEW |