| 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 { | 
|  | 41  public: | 
|  | 42   // Returns the current state of the emitter as POLL events bitfield. | 
|  | 43   virtual uint32_t GetEventStatus() = 0; | 
|  | 44   sdk_util::SimpleLock& GetLock() { return emitter_lock_; } | 
|  | 45 | 
| 60  protected: | 46  protected: | 
| 61   // Called automatically prior to delete to inform the EventListeners that |  | 
| 62   // this EventEmitter is abandoning an associated EventInfo. |  | 
| 63   virtual void Destroy(); |  | 
| 64 |  | 
| 65  private: |  | 
| 66   // Register or unregister an EventInfo.  The lock of the EventListener | 47   // Register or unregister an EventInfo.  The lock of the EventListener | 
| 67   // associated with this EventInfo must be held prior to calling these | 48   // associated with this EventInfo must be held prior to calling these | 
| 68   // functions.  These functions are private to ensure they are called by the | 49   // functions.  These functions are private to ensure they are called by the | 
| 69   // EventListener. | 50   // EventListener. | 
| 70   void RegisterEventInfo(const ScopedEventInfo& info); | 51   void RegisterListener(EventListener* listener, uint32_t events); | 
| 71   void UnregisterEventInfo(const ScopedEventInfo& info); | 52   void UnregisterListener(EventListener* listener); | 
| 72 | 53 | 
| 73  public: | 54   // Event lock must be held when this function is called. | 
| 74   // Returns the current state of the emitter as POLL events bitfield. | 55   void RaiseEvents_Locked(uint32_t events); | 
| 75   virtual uint32_t GetEventStatus() = 0; | 56   void RegisterListener_Locked(EventListener* listener, uint32_t events); | 
| 76 | 57   void UnregisterListener_Locked(EventListener* listener); | 
| 77   // Returns the type of the emitter (compatible with st_mode in stat) |  | 
| 78   virtual int GetType() = 0; |  | 
| 79 | 58 | 
| 80  protected: | 59  protected: | 
| 81   // Called by the thread causing the Event. | 60   sdk_util::SimpleLock emitter_lock_; | 
| 82   void RaiseEvent(uint32_t events); | 61   EventListenerMap_t listeners_; | 
| 83 | 62 | 
| 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_; |  | 
| 94   EventInfoSet_t events_; |  | 
| 95   friend class EventListener; | 63   friend class EventListener; | 
|  | 64   friend class EventListenerGroup; | 
|  | 65   friend class EventListenerSingle; | 
| 96 }; | 66 }; | 
| 97 | 67 | 
| 98 typedef sdk_util::ScopedRef<EventEmitter> ScopedEventEmitter; |  | 
| 99 |  | 
| 100 }  // namespace nacl_io | 68 }  // namespace nacl_io | 
| 101 | 69 | 
| 102 | 70 | 
| 103 #endif  // LIBRARIES_NACL_IO_EVENT_EMITTER_H_ | 71 #endif  // LIBRARIES_NACL_IO_EVENT_EMITTER_H_ | 
| OLD | NEW | 
|---|