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 #ifndef LIBRARIES_NACL_IO_EVENT_EMITTER_H_
5 #define LIBRARIES_NACL_IO_EVENT_EMITTER_H_
12 #include "nacl_io/error.h"
14 #include "sdk_util/auto_lock.h"
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"
25 typedef sdk_util::ScopedRef
<EventEmitter
> ScopedEventEmitter
;
26 typedef std::map
<EventListener
*, uint32_t> EventListenerMap_t
;
28 bool operator<(const ScopedEventEmitter
& src_a
,
29 const ScopedEventEmitter
& src_b
);
33 // The EventEmitter class provides notification of events to EventListeners
34 // by registering EventInfo objects and signaling the EventListener
35 // whenever thier state is changed.
37 // See "Kernel Events" in event_listener.h for additional information.
39 class EventEmitter
: public sdk_util::RefObject
{
43 // This returns a snapshot, to ensure the status doesn't change from
44 // fetch to use, hold the lock and call GetEventStatus_Locked.
45 uint32_t GetEventStatus() {
46 AUTO_LOCK(emitter_lock_
);
47 return GetEventStatus_Locked();
50 uint32_t GetEventStatus_Locked() { return event_status_
; }
52 sdk_util::SimpleLock
& GetLock() { return emitter_lock_
; }
54 // Updates the specified bits in the event status, and signals any
55 // listeners waiting on those bits.
56 void RaiseEvents_Locked(uint32_t events
);
58 // Clears the specified bits in the event status.
59 void ClearEvents_Locked(uint32_t events
);
61 // Register or unregister an EventInfo. The lock of the EventListener
62 // associated with this EventInfo must be held prior to calling these
63 // functions. These functions are private to ensure they are called by the
65 void RegisterListener(EventListener
* listener
, uint32_t events
);
66 void UnregisterListener(EventListener
* listener
);
67 void RegisterListener_Locked(EventListener
* listener
, uint32_t events
);
68 void UnregisterListener_Locked(EventListener
* listener
);
71 uint32_t event_status_
;
72 sdk_util::SimpleLock emitter_lock_
;
73 EventListenerMap_t listeners_
;
74 DISALLOW_COPY_AND_ASSIGN(EventEmitter
);
77 } // namespace nacl_io
79 #endif // LIBRARIES_NACL_IO_EVENT_EMITTER_H_