Home | History | Annotate | Download | only in nacl_io
      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 
      7 #include "nacl_io/event_emitter.h"
      8 #include "nacl_io/event_listener.h"
      9 
     10 #include "sdk_util/auto_lock.h"
     11 
     12 namespace nacl_io {
     13 
     14 bool operator<(const ScopedEventInfo& src_a, const ScopedEventInfo& src_b) {
     15   return src_a.get() < src_b.get();
     16 }
     17 
     18 void EventEmitter::Destroy() {
     19   // We can not grab the EmitterLock prior to grabbing the EventListener lock,
     20   // however the ref count proves this is the only thread which has a
     21   // reference to the emitter at this point so accessing events_ is safe.
     22   EventInfoSet_t::iterator it;
     23   for (it = events_.begin(); it != events_.end(); it++) {
     24     ScopedEventInfo info = *it;
     25     info->listener->AbandonedEventInfo(info);
     26   }
     27 }
     28 
     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
     60