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 #include <assert.h> 6 #include <poll.h> 7 8 #include "nacl_io/event_emitter.h" 9 #include "nacl_io/event_listener.h" 10 #include "nacl_io/fifo_interface.h" 11 12 #include "sdk_util/auto_lock.h" 13 14 namespace nacl_io { 15 16 bool operator<(const ScopedEventEmitter& src_a, 17 const ScopedEventEmitter& src_b) { 18 return src_a.get() < src_b.get(); 19 } 20 21 EventEmitter::EventEmitter() : event_status_(0) {} 22 23 void EventEmitter::RegisterListener(EventListener* listener, uint32_t events) { 24 AUTO_LOCK(emitter_lock_); 25 RegisterListener_Locked(listener, events); 26 } 27 28 void EventEmitter::UnregisterListener(EventListener* listener) { 29 AUTO_LOCK(emitter_lock_); 30 UnregisterListener_Locked(listener); 31 } 32 33 void EventEmitter::RegisterListener_Locked(EventListener* listener, 34 uint32_t events) { 35 assert(listeners_.count(listener) == 0); 36 listeners_[listener] = events; 37 } 38 39 void EventEmitter::UnregisterListener_Locked(EventListener* listener) { 40 assert(listeners_.count(listener) == 1); 41 listeners_.erase(listener); 42 } 43 44 void EventEmitter::ClearEvents_Locked(uint32_t event_bits) { 45 event_status_ &= ~event_bits; 46 } 47 48 void EventEmitter::RaiseEvents_Locked(uint32_t event_bits) { 49 event_status_ |= event_bits; 50 51 EventListenerMap_t::iterator it; 52 for (it = listeners_.begin(); it != listeners_.end(); it++) { 53 uint32_t bits = it->second & event_bits; 54 if (0 != bits) 55 it->first->ReceiveEvents(this, bits); 56 } 57 } 58 59 } // namespace nacl_io 60