OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef MOJO_EDK_SYSTEM_REQUEST_CONTEXT_H_ | |
6 #define MOJO_EDK_SYSTEM_REQUEST_CONTEXT_H_ | |
7 | |
8 #include "base/containers/stack_container.h" | |
9 #include "mojo/edk/system/watcher.h" | |
10 | |
11 namespace mojo { | |
12 namespace edk { | |
13 | |
14 // A RequestContext is a thread-local object which exists for the duration of | |
15 // a single system API call. It is constructed immediately upon EDK entry and | |
16 // destructed immediately before returning to the caller, after any internal | |
17 // locks have been released. | |
18 // | |
19 // NOTE: It is legal to construct a RequestContext while another one already | |
Anand Mistry (off Chromium)
2016/03/01 07:30:40
Are there any specific cases where this happens ri
Ken Rockot(use gerrit already)
2016/03/01 08:17:44
So if you WriteMessage to a pipe endpoint whose pe
Anand Mistry (off Chromium)
2016/03/02 01:50:35
This approach is good. The thing that confused me
| |
20 // exists on the current thread, but it is not safe to use the nested context | |
21 // for any reason. Therefore it is important to always use | |
22 // |RequestContext::current()| rather than referring to any local instance | |
23 // directly. | |
24 class RequestContext { | |
25 public: | |
26 RequestContext(); | |
27 ~RequestContext(); | |
28 | |
29 // Returns the current thread-local RequestContext. | |
30 static RequestContext* current(); | |
31 | |
32 // Adds a finalizer to this RequestContext corresponding to a watch callback | |
33 // which should be triggered in response to some handle state change. If | |
34 // the Watcher hasn't been cancelled by the time this RequestContext is | |
35 // destroyed, its WatchCallback will be invoked with |result| and |state| | |
36 // arguments. | |
37 void AddWatchFinalizer(scoped_refptr<Watcher> watcher, | |
38 MojoResult result, | |
39 MojoHandleSignalsState state); | |
40 | |
41 private: | |
42 // Is this request context the current one? | |
43 bool IsCurrent() const; | |
44 | |
45 struct WatchFinalizer { | |
46 WatchFinalizer(scoped_refptr<Watcher> watcher, | |
47 MojoResult result, | |
48 MojoHandleSignalsState state); | |
49 ~WatchFinalizer(); | |
50 | |
51 scoped_refptr<Watcher> watcher; | |
52 MojoResult result; | |
53 MojoHandleSignalsState state; | |
54 }; | |
55 | |
56 // Chosen by fair dice roll. | |
57 // | |
58 // TODO: We should measure the distribution of # of finalizers typical to | |
59 // any RequestContext and adjust this number accordingly. It's probably | |
60 // almost always 1, but 4 seems like a harmless upper bound for now. | |
61 static const size_t kStaticWatchFinalizersCapacity = 4; | |
62 | |
63 using WatchFinalizerList = | |
64 base::StackVector<WatchFinalizer, kStaticWatchFinalizersCapacity>; | |
65 | |
66 WatchFinalizerList watch_finalizers_; | |
67 }; | |
Anand Mistry (off Chromium)
2016/03/01 07:30:40
DISALLOW_COPY_AND_ASSIGN
Ken Rockot(use gerrit already)
2016/03/01 08:17:44
Done
| |
68 | |
69 } // namespace edk | |
70 } // namespace mojo | |
71 | |
72 #endif // MOJO_EDK_SYSTEM_REQUEST_CONTEXT_H_ | |
OLD | NEW |