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 illegal to construct a RequestContext while one already exists on |
| 22 // the current thread, unless constructed from within the previous |
| 23 // RequestContext's destructor. |
| 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 AddWatchNotifyFinalizer(scoped_refptr<Watcher> watcher, |
| 38 MojoResult result, |
| 39 const HandleSignalsState& state); |
| 40 |
| 41 // Adds a finalizer to this RequestContext which cancels a watch. |
| 42 void AddWatchCancelFinalizer(scoped_refptr<Watcher> watcher); |
| 43 |
| 44 private: |
| 45 struct WatchNotifyFinalizer { |
| 46 WatchNotifyFinalizer(scoped_refptr<Watcher> watcher, |
| 47 MojoResult result, |
| 48 const HandleSignalsState& state); |
| 49 ~WatchNotifyFinalizer(); |
| 50 |
| 51 scoped_refptr<Watcher> watcher; |
| 52 MojoResult result; |
| 53 HandleSignalsState 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 WatchNotifyFinalizerList = |
| 64 base::StackVector<WatchNotifyFinalizer, kStaticWatchFinalizersCapacity>; |
| 65 using WatchCancelFinalizerList = |
| 66 base::StackVector<scoped_refptr<Watcher>, kStaticWatchFinalizersCapacity>; |
| 67 |
| 68 WatchNotifyFinalizerList watch_notify_finalizers_; |
| 69 WatchCancelFinalizerList watch_cancel_finalizers_; |
| 70 |
| 71 DISALLOW_COPY_AND_ASSIGN(RequestContext); |
| 72 }; |
| 73 |
| 74 } // namespace edk |
| 75 } // namespace mojo |
| 76 |
| 77 #endif // MOJO_EDK_SYSTEM_REQUEST_CONTEXT_H_ |
OLD | NEW |