Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 /* Copyright (c) 2013 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 LIBRARIES_NACL_IO_EVENT_EMITTER_H_ | 5 #ifndef LIBRARIES_NACL_IO_EVENT_EMITTER_H_ |
| 6 #define LIBRARIES_NACL_IO_EVENT_EMITTER_H_ | 6 #define LIBRARIES_NACL_IO_EVENT_EMITTER_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <map> | 10 #include <map> |
| 11 #include <set> | 11 #include <set> |
| 12 | 12 |
| 13 #include "nacl_io/error.h" | 13 #include "nacl_io/error.h" |
| 14 | 14 |
| 15 #include "sdk_util/ref_object.h" | 15 #include "sdk_util/ref_object.h" |
| 16 #include "sdk_util/scoped_ref.h" | 16 #include "sdk_util/scoped_ref.h" |
| 17 #include "sdk_util/simple_lock.h" | 17 #include "sdk_util/simple_lock.h" |
| 18 | 18 |
| 19 | 19 |
| 20 namespace nacl_io { | 20 namespace nacl_io { |
| 21 | 21 |
| 22 class EventEmitter; | 22 class EventEmitter; |
| 23 class EventListener; | 23 class EventListener; |
| 24 | 24 |
| 25 // A ref counted object (non POD derived from RefObject) for storing the | |
| 26 // state of a single signal request. Requests are unique to any | |
| 27 // FD/EventListener pair. | |
| 28 struct EventInfo : public sdk_util::RefObject { | |
| 29 // User provied data to be returned on EventListener::Wait | |
| 30 uint64_t user_data; | |
| 31 | 25 |
| 32 // Bitfield of POLL events currently signaled. | 26 typedef sdk_util::ScopedRef<EventEmitter> ScopedEventEmitter; |
| 33 uint32_t events; | 27 typedef std::map<EventListener*, uint32_t> EventListenerMap_t; |
| 34 | 28 |
| 35 // Bitfield of POLL events of interest. | 29 bool operator<(const ScopedEventEmitter& src_a, |
| 36 uint32_t filter; | 30 const ScopedEventEmitter& src_b); |
| 37 | |
| 38 // We do not use a ScopedRef to prevent circular references. | |
| 39 EventEmitter* emitter; | |
| 40 EventListener* listener; | |
| 41 uint32_t id; | |
| 42 }; | |
| 43 | |
| 44 typedef sdk_util::ScopedRef<EventInfo> ScopedEventInfo; | |
| 45 | |
| 46 // Provide comparison for std::map and std::set | |
| 47 bool operator<(const ScopedEventInfo& src_a, const ScopedEventInfo& src_b); | |
| 48 | |
| 49 typedef std::map<int, ScopedEventInfo> EventInfoMap_t; | |
| 50 typedef std::set<ScopedEventInfo> EventInfoSet_t; | |
| 51 | 31 |
| 52 // EventEmitter | 32 // EventEmitter |
| 53 // | 33 // |
| 54 // The EventEmitter class provides notification of events to EventListeners | 34 // The EventEmitter class provides notification of events to EventListeners |
| 55 // by registering EventInfo objects and signaling the EventListener | 35 // by registering EventInfo objects and signaling the EventListener |
| 56 // whenever thier state is changed. | 36 // whenever thier state is changed. |
| 57 // | 37 // |
| 58 // See "Kernel Events" in event_listener.h for additional information. | 38 // See "Kernel Events" in event_listener.h for additional information. |
| 39 | |
| 59 class EventEmitter : public sdk_util::RefObject { | 40 class EventEmitter : public sdk_util::RefObject { |
| 60 protected: | 41 public: |
| 61 // Called automatically prior to delete to inform the EventListeners that | 42 // Returns the current state of the emitter as POLL events bitfield. |
| 62 // this EventEmitter is abandoning an associated EventInfo. | 43 virtual uint32_t GetEventStatus() = 0; |
| 63 virtual void Destroy(); | 44 sdk_util::SimpleLock& GetLock() { return emitter_lock_; } |
| 64 | 45 |
| 65 private: | |
| 66 // Register or unregister an EventInfo. The lock of the EventListener | 46 // Register or unregister an EventInfo. The lock of the EventListener |
| 67 // associated with this EventInfo must be held prior to calling these | 47 // associated with this EventInfo must be held prior to calling these |
| 68 // functions. These functions are private to ensure they are called by the | 48 // functions. These functions are private to ensure they are called by the |
| 69 // EventListener. | 49 // EventListener. |
| 70 void RegisterEventInfo(const ScopedEventInfo& info); | 50 void RegisterListener(EventListener* listener, uint32_t events); |
| 71 void UnregisterEventInfo(const ScopedEventInfo& info); | 51 void UnregisterListener(EventListener* listener); |
| 72 | 52 |
| 73 public: | 53 // Event lock must be held when this function is called. |
| 74 // Returns the current state of the emitter as POLL events bitfield. | 54 void RaiseEvents_Locked(uint32_t events); |
| 75 virtual uint32_t GetEventStatus() = 0; | 55 void RegisterListener_Locked(EventListener* listener, uint32_t events); |
| 76 | 56 void UnregisterListener_Locked(EventListener* listener); |
| 77 // Returns the type of the emitter (compatible with st_mode in stat) | |
| 78 virtual int GetType() = 0; | |
| 79 | 57 |
| 80 protected: | 58 protected: |
|
binji
2013/09/12 01:47:57
private?
noelallen1
2013/09/12 23:19:03
Done.
| |
| 81 // Called by the thread causing the Event. | |
| 82 void RaiseEvent(uint32_t events); | |
| 83 | |
| 84 // Provided to allow one EventEmitter to register the same EventInfo with | |
| 85 // a child EventEmitter so that they can both signal the EventListener. | |
| 86 // Called after registering locally, but while lock is still held. | |
| 87 virtual void ChainRegisterEventInfo(const ScopedEventInfo& event); | |
| 88 | |
| 89 // Called before unregistering locally, but while lock is still held. | |
| 90 virtual void ChainUnregisterEventInfo(const ScopedEventInfo& event); | |
| 91 | |
| 92 private: | |
| 93 sdk_util::SimpleLock emitter_lock_; | 59 sdk_util::SimpleLock emitter_lock_; |
| 94 EventInfoSet_t events_; | 60 EventListenerMap_t listeners_; |
| 95 friend class EventListener; | |
| 96 }; | 61 }; |
| 97 | 62 |
| 98 typedef sdk_util::ScopedRef<EventEmitter> ScopedEventEmitter; | |
| 99 | |
| 100 } // namespace nacl_io | 63 } // namespace nacl_io |
| 101 | 64 |
| 102 | 65 |
| 103 #endif // LIBRARIES_NACL_IO_EVENT_EMITTER_H_ | 66 #endif // LIBRARIES_NACL_IO_EVENT_EMITTER_H_ |
| OLD | NEW |