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/system_impl_export.h" |
12 #include "mojo/edk/system/watch.h" | 12 #include "mojo/edk/system/watcher.h" |
13 | 13 |
14 namespace base { | 14 namespace base { |
15 template<typename T> class ThreadLocalPointer; | 15 template<typename T> class ThreadLocalPointer; |
16 } | 16 } |
17 | 17 |
18 namespace mojo { | 18 namespace mojo { |
19 namespace edk { | 19 namespace edk { |
20 | 20 |
21 // 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 |
22 // 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 |
(...skipping 19 matching lines...) Expand all Loading... |
42 explicit RequestContext(Source source); | 42 explicit RequestContext(Source source); |
43 ~RequestContext(); | 43 ~RequestContext(); |
44 | 44 |
45 // Returns the current thread-local RequestContext. | 45 // Returns the current thread-local RequestContext. |
46 static RequestContext* current(); | 46 static RequestContext* current(); |
47 | 47 |
48 Source source() const { return source_; } | 48 Source source() const { return source_; } |
49 | 49 |
50 // Adds a finalizer to this RequestContext corresponding to a watch callback | 50 // Adds a finalizer to this RequestContext corresponding to a watch callback |
51 // which should be triggered in response to some handle state change. If | 51 // which should be triggered in response to some handle state change. If |
52 // the WatcherDispatcher hasn't been closed by the time this RequestContext is | 52 // the Watcher hasn't been cancelled by the time this RequestContext is |
53 // destroyed, its WatchCallback will be invoked with |result| and |state| | 53 // destroyed, its WatchCallback will be invoked with |result| and |state| |
54 // arguments. | 54 // arguments. |
55 void AddWatchNotifyFinalizer(scoped_refptr<Watch> watch, | 55 void AddWatchNotifyFinalizer(scoped_refptr<Watcher> watcher, |
56 MojoResult result, | 56 MojoResult result, |
57 const HandleSignalsState& state); | 57 const HandleSignalsState& state); |
58 | 58 |
59 // Adds a finalizer to this RequestContext corresponding to a watch callback | 59 // Adds a finalizer to this RequestContext which cancels a watch. |
60 // which should be triggered to notify of watch cancellation. This appends to | 60 void AddWatchCancelFinalizer(scoped_refptr<Watcher> watcher); |
61 // a separate finalizer list from AddWatchNotifyFinalizer, as pending | |
62 // cancellations must always preempt other pending notifications. | |
63 void AddWatchCancelFinalizer(scoped_refptr<Watch> watch); | |
64 | 61 |
65 private: | 62 private: |
66 // Is this request context the current one? | 63 // Is this request context the current one? |
67 bool IsCurrent() const; | 64 bool IsCurrent() const; |
68 | 65 |
69 struct WatchNotifyFinalizer { | 66 struct WatchNotifyFinalizer { |
70 WatchNotifyFinalizer(scoped_refptr<Watch> watch, | 67 WatchNotifyFinalizer(scoped_refptr<Watcher> watcher, |
71 MojoResult result, | 68 MojoResult result, |
72 const HandleSignalsState& state); | 69 const HandleSignalsState& state); |
73 WatchNotifyFinalizer(const WatchNotifyFinalizer& other); | 70 WatchNotifyFinalizer(const WatchNotifyFinalizer& other); |
74 ~WatchNotifyFinalizer(); | 71 ~WatchNotifyFinalizer(); |
75 | 72 |
76 scoped_refptr<Watch> watch; | 73 scoped_refptr<Watcher> watcher; |
77 MojoResult result; | 74 MojoResult result; |
78 HandleSignalsState state; | 75 HandleSignalsState state; |
79 }; | 76 }; |
80 | 77 |
81 // NOTE: This upper bound was chosen somewhat arbitrarily after observing some | 78 // Chosen by fair dice roll. |
82 // rare worst-case behavior in Chrome. A vast majority of RequestContexts only | 79 // |
83 // ever accumulate 0 or 1 finalizers. | 80 // TODO: We should measure the distribution of # of finalizers typical to |
84 static const size_t kStaticWatchFinalizersCapacity = 8; | 81 // any RequestContext and adjust this number accordingly. It's probably |
| 82 // almost always 1, but 4 seems like a harmless upper bound for now. |
| 83 static const size_t kStaticWatchFinalizersCapacity = 4; |
85 | 84 |
86 using WatchNotifyFinalizerList = | 85 using WatchNotifyFinalizerList = |
87 base::StackVector<WatchNotifyFinalizer, kStaticWatchFinalizersCapacity>; | 86 base::StackVector<WatchNotifyFinalizer, kStaticWatchFinalizersCapacity>; |
88 using WatchCancelFinalizerList = | 87 using WatchCancelFinalizerList = |
89 base::StackVector<scoped_refptr<Watch>, kStaticWatchFinalizersCapacity>; | 88 base::StackVector<scoped_refptr<Watcher>, kStaticWatchFinalizersCapacity>; |
90 | 89 |
91 const Source source_; | 90 const Source source_; |
92 | 91 |
93 WatchNotifyFinalizerList watch_notify_finalizers_; | 92 WatchNotifyFinalizerList watch_notify_finalizers_; |
94 WatchCancelFinalizerList watch_cancel_finalizers_; | 93 WatchCancelFinalizerList watch_cancel_finalizers_; |
95 | 94 |
96 // Pointer to the TLS context. Although this can easily be accessed via the | 95 // Pointer to the TLS context. Although this can easily be accessed via the |
97 // global LazyInstance, accessing a LazyInstance has a large cost relative to | 96 // global LazyInstance, accessing a LazyInstance has a large cost relative to |
98 // the rest of this class and its usages. | 97 // the rest of this class and its usages. |
99 base::ThreadLocalPointer<RequestContext>* tls_context_; | 98 base::ThreadLocalPointer<RequestContext>* tls_context_; |
100 | 99 |
101 DISALLOW_COPY_AND_ASSIGN(RequestContext); | 100 DISALLOW_COPY_AND_ASSIGN(RequestContext); |
102 }; | 101 }; |
103 | 102 |
104 } // namespace edk | 103 } // namespace edk |
105 } // namespace mojo | 104 } // namespace mojo |
106 | 105 |
107 #endif // MOJO_EDK_SYSTEM_REQUEST_CONTEXT_H_ | 106 #endif // MOJO_EDK_SYSTEM_REQUEST_CONTEXT_H_ |
OLD | NEW |