| 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 #include <assert.h> | 5 #include <assert.h> | 
|  | 6 #include <poll.h> | 
| 6 | 7 | 
| 7 #include "nacl_io/event_emitter.h" | 8 #include "nacl_io/event_emitter.h" | 
| 8 #include "nacl_io/event_listener.h" | 9 #include "nacl_io/event_listener.h" | 
|  | 10 #include "nacl_io/fifo_interface.h" | 
| 9 | 11 | 
| 10 #include "sdk_util/auto_lock.h" | 12 #include "sdk_util/auto_lock.h" | 
| 11 | 13 | 
| 12 namespace nacl_io { | 14 namespace nacl_io { | 
| 13 | 15 | 
| 14 bool operator<(const ScopedEventInfo& src_a, const ScopedEventInfo& src_b) { | 16 bool operator<(const ScopedEventEmitter& src_a, | 
|  | 17                const ScopedEventEmitter& src_b) { | 
| 15   return src_a.get() < src_b.get(); | 18   return src_a.get() < src_b.get(); | 
| 16 } | 19 } | 
| 17 | 20 | 
| 18 void EventEmitter::Destroy() { | 21 EventEmitter::EventEmitter() : event_status_(0) {} | 
| 19   // We can not grab the EmitterLock prior to grabbing the EventListener lock, | 22 | 
| 20   // however the ref count proves this is the only thread which has a | 23 void EventEmitter::RegisterListener(EventListener* listener, uint32_t events) { | 
| 21   // reference to the emitter at this point so accessing events_ is safe. | 24   AUTO_LOCK(emitter_lock_); | 
| 22   EventInfoSet_t::iterator it; | 25   RegisterListener_Locked(listener, events); | 
| 23   for (it = events_.begin(); it != events_.end(); it++) { | 26 } | 
| 24     ScopedEventInfo info = *it; | 27 | 
| 25     info->listener->AbandonedEventInfo(info); | 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); | 
| 26   } | 56   } | 
| 27 } | 57 } | 
| 28 | 58 | 
| 29 void EventEmitter::RegisterEventInfo(const ScopedEventInfo& info) { |  | 
| 30   AUTO_LOCK(emitter_lock_); |  | 
| 31 |  | 
| 32   events_.insert(info); |  | 
| 33   ChainRegisterEventInfo(info); |  | 
| 34 } |  | 
| 35 |  | 
| 36 void EventEmitter::UnregisterEventInfo(const ScopedEventInfo& info) { |  | 
| 37   AUTO_LOCK(emitter_lock_); |  | 
| 38 |  | 
| 39   ChainUnregisterEventInfo(info); |  | 
| 40   events_.erase(info); |  | 
| 41 } |  | 
| 42 void EventEmitter::RaiseEvent(uint32_t event_bits) { |  | 
| 43   AUTO_LOCK(emitter_lock_); |  | 
| 44 |  | 
| 45   EventInfoSet_t::iterator it; |  | 
| 46   for (it = events_.begin(); it != events_.end(); it++) { |  | 
| 47     // If this event is allowed by the filter, signal it |  | 
| 48     ScopedEventInfo info = *it; |  | 
| 49     if (info->filter & event_bits) { |  | 
| 50       info->events |= event_bits & info->filter; |  | 
| 51       info->listener->Signal(info); |  | 
| 52     } |  | 
| 53   } |  | 
| 54 } |  | 
| 55 |  | 
| 56 void EventEmitter::ChainRegisterEventInfo(const ScopedEventInfo& info) {} |  | 
| 57 void EventEmitter::ChainUnregisterEventInfo(const ScopedEventInfo& info) {} |  | 
| 58 |  | 
| 59 }  // namespace nacl_io | 59 }  // namespace nacl_io | 
| OLD | NEW | 
|---|