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 3 * found in the LICENSE file. 4 */ 5 #ifndef LIBRARIES_NACL_IO_EVENT_EMITTER_H_ 6 #define LIBRARIES_NACL_IO_EVENT_EMITTER_H_ 7 8 #include <stdint.h> 9 10 #include <map> 11 #include <set> 12 13 #include "nacl_io/error.h" 14 15 #include "sdk_util/macros.h" 16 #include "sdk_util/ref_object.h" 17 #include "sdk_util/scoped_ref.h" 18 #include "sdk_util/simple_lock.h" 19 20 namespace nacl_io { 21 22 class EventEmitter; 23 class EventListener; 24 25 26 typedef sdk_util::ScopedRef<EventEmitter> ScopedEventEmitter; 27 typedef std::map<EventListener*, uint32_t> EventListenerMap_t; 28 29 bool operator<(const ScopedEventEmitter& src_a, 30 const ScopedEventEmitter& src_b); 31 32 // EventEmitter 33 // 34 // The EventEmitter class provides notification of events to EventListeners 35 // by registering EventInfo objects and signaling the EventListener 36 // whenever thier state is changed. 37 // 38 // See "Kernel Events" in event_listener.h for additional information. 39 40 class EventEmitter : public sdk_util::RefObject { 41 public: 42 EventEmitter(); 43 44 // This returns a snapshot, to ensure the status doesn't change from 45 // fetch to use, hold the lock. 46 uint32_t GetEventStatus() { return event_status_; } 47 48 sdk_util::SimpleLock& GetLock() { return emitter_lock_; } 49 50 // Updates the specified bits in the event status, and signals any 51 // listeners waiting on those bits. 52 void RaiseEvents_Locked(uint32_t events); 53 54 // Clears the specified bits in the event status. 55 void ClearEvents_Locked(uint32_t events); 56 57 // Register or unregister an EventInfo. The lock of the EventListener 58 // associated with this EventInfo must be held prior to calling these 59 // functions. These functions are private to ensure they are called by the 60 // EventListener. 61 void RegisterListener(EventListener* listener, uint32_t events); 62 void UnregisterListener(EventListener* listener); 63 void RegisterListener_Locked(EventListener* listener, uint32_t events); 64 void UnregisterListener_Locked(EventListener* listener); 65 66 private: 67 uint32_t event_status_; 68 sdk_util::SimpleLock emitter_lock_; 69 EventListenerMap_t listeners_; 70 DISALLOW_COPY_AND_ASSIGN(EventEmitter); 71 }; 72 73 } // namespace nacl_io 74 75 76 #endif // LIBRARIES_NACL_IO_EVENT_EMITTER_H_