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 "base/macros.h" |
| 10 #include "mojo/edk/system/handle_signals_state.h" |
| 11 #include "mojo/edk/system/watcher.h" |
| 12 |
| 13 namespace mojo { |
| 14 namespace edk { |
| 15 |
| 16 // A RequestContext is a thread-local object which exists for the duration of |
| 17 // a single system API call. It is constructed immediately upon EDK entry and |
| 18 // destructed immediately before returning to the caller, after any internal |
| 19 // locks have been released. |
| 20 // |
| 21 // NOTE: It is legal to construct a RequestContext while another one already |
| 22 // exists on the current thread, but it is not safe to use the nested context |
| 23 // for any reason. Therefore it is important to always use |
| 24 // |RequestContext::current()| rather than referring to any local instance |
| 25 // directly. |
| 26 class RequestContext { |
| 27 public: |
| 28 RequestContext(); |
| 29 ~RequestContext(); |
| 30 |
| 31 // Returns the current thread-local RequestContext. |
| 32 static RequestContext* current(); |
| 33 |
| 34 // Adds a finalizer to this RequestContext corresponding to a watch callback |
| 35 // which should be triggered in response to some handle state change. If |
| 36 // the Watcher hasn't been cancelled by the time this RequestContext is |
| 37 // destroyed, its WatchCallback will be invoked with |result| and |state| |
| 38 // arguments. |
| 39 void AddWatchNotifyFinalizer(scoped_refptr<Watcher> watcher, |
| 40 MojoResult result, |
| 41 const HandleSignalsState& state); |
| 42 |
| 43 // Adds a finalizer to this RequestContext which cancels a watch. |
| 44 void AddWatchCancelFinalizer(scoped_refptr<Watcher> watcher); |
| 45 |
| 46 private: |
| 47 // Is this request context the current one? |
| 48 bool IsCurrent() const; |
| 49 |
| 50 struct WatchNotifyFinalizer { |
| 51 WatchNotifyFinalizer(scoped_refptr<Watcher> watcher, |
| 52 MojoResult result, |
| 53 const HandleSignalsState& state); |
| 54 ~WatchNotifyFinalizer(); |
| 55 |
| 56 scoped_refptr<Watcher> watcher; |
| 57 MojoResult result; |
| 58 HandleSignalsState state; |
| 59 }; |
| 60 |
| 61 // Chosen by fair dice roll. |
| 62 // |
| 63 // TODO: We should measure the distribution of # of finalizers typical to |
| 64 // any RequestContext and adjust this number accordingly. It's probably |
| 65 // almost always 1, but 4 seems like a harmless upper bound for now. |
| 66 static const size_t kStaticWatchFinalizersCapacity = 4; |
| 67 |
| 68 using WatchNotifyFinalizerList = |
| 69 base::StackVector<WatchNotifyFinalizer, kStaticWatchFinalizersCapacity>; |
| 70 using WatchCancelFinalizerList = |
| 71 base::StackVector<scoped_refptr<Watcher>, kStaticWatchFinalizersCapacity>; |
| 72 |
| 73 WatchNotifyFinalizerList watch_notify_finalizers_; |
| 74 WatchCancelFinalizerList watch_cancel_finalizers_; |
| 75 |
| 76 DISALLOW_COPY_AND_ASSIGN(RequestContext); |
| 77 }; |
| 78 |
| 79 } // namespace edk |
| 80 } // namespace mojo |
| 81 |
| 82 #endif // MOJO_EDK_SYSTEM_REQUEST_CONTEXT_H_ |
OLD | NEW |