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.
8 #include "nacl_io/event_emitter.h"
9 #include "nacl_io/event_listener.h"
10 #include "nacl_io/fifo_interface.h"
12 #include "sdk_util/auto_lock.h"
16 bool operator<(const ScopedEventEmitter
& src_a
,
17 const ScopedEventEmitter
& src_b
) {
18 return src_a
.get() < src_b
.get();
21 EventEmitter::EventEmitter() : event_status_(0) {
24 void EventEmitter::RegisterListener(EventListener
* listener
, uint32_t events
) {
25 AUTO_LOCK(emitter_lock_
);
26 RegisterListener_Locked(listener
, events
);
29 void EventEmitter::UnregisterListener(EventListener
* listener
) {
30 AUTO_LOCK(emitter_lock_
);
31 UnregisterListener_Locked(listener
);
34 void EventEmitter::RegisterListener_Locked(EventListener
* listener
,
36 assert(listeners_
.count(listener
) == 0);
37 listeners_
[listener
] = events
;
40 void EventEmitter::UnregisterListener_Locked(EventListener
* listener
) {
41 assert(listeners_
.count(listener
) == 1);
42 listeners_
.erase(listener
);
45 void EventEmitter::ClearEvents_Locked(uint32_t event_bits
) {
46 event_status_
&= ~event_bits
;
49 void EventEmitter::RaiseEvents_Locked(uint32_t event_bits
) {
50 event_status_
|= event_bits
;
52 EventListenerMap_t::iterator it
;
53 for (it
= listeners_
.begin(); it
!= listeners_
.end(); it
++) {
54 uint32_t bits
= it
->second
& event_bits
;
56 it
->first
->ReceiveEvents(this, bits
);
60 } // namespace nacl_io