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