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#ifndef LIBRARIES_NACL_IO_EVENT_EMITTER_H_
6#define LIBRARIES_NACL_IO_EVENT_EMITTER_H_
7
8#include <stdint.h>
9
10#include <map>
11#include <set>
12
13#include "nacl_io/error.h"
14
15#include "sdk_util/auto_lock.h"
16#include "sdk_util/macros.h"
17#include "sdk_util/ref_object.h"
18#include "sdk_util/scoped_ref.h"
19#include "sdk_util/simple_lock.h"
20
21namespace nacl_io {
22
23class EventEmitter;
24class EventListener;
25
26typedef sdk_util::ScopedRef<EventEmitter> ScopedEventEmitter;
27typedef std::map<EventListener*, uint32_t> EventListenerMap_t;
28
29bool operator<(const ScopedEventEmitter& src_a,
30               const ScopedEventEmitter& src_b);
31
32// EventEmitter
33//
34// The EventEmitter class provides notification of events to EventListeners
35// by registering EventInfo objects and signaling the EventListener
36// whenever thier state is changed.
37//
38// See "Kernel Events" in event_listener.h for additional information.
39
40class EventEmitter : public sdk_util::RefObject {
41 public:
42  EventEmitter();
43
44  // This returns a snapshot, to ensure the status doesn't change from
45  // fetch to use, hold the lock and call GetEventStatus_Locked.
46  uint32_t GetEventStatus() {
47    AUTO_LOCK(emitter_lock_);
48    return GetEventStatus_Locked();
49  }
50
51  uint32_t GetEventStatus_Locked() { return event_status_; }
52
53  sdk_util::SimpleLock& GetLock() { return emitter_lock_; }
54
55  // Updates the specified bits in the event status, and signals any
56  // listeners waiting on those bits.
57  void RaiseEvents_Locked(uint32_t events);
58
59  // Clears the specified bits in the event status.
60  void ClearEvents_Locked(uint32_t events);
61
62  // Register or unregister an EventInfo.  The lock of the EventListener
63  // associated with this EventInfo must be held prior to calling these
64  // functions.  These functions are private to ensure they are called by the
65  // EventListener.
66  void RegisterListener(EventListener* listener, uint32_t events);
67  void UnregisterListener(EventListener* listener);
68  void RegisterListener_Locked(EventListener* listener, uint32_t events);
69  void UnregisterListener_Locked(EventListener* listener);
70
71 private:
72  uint32_t event_status_;
73  sdk_util::SimpleLock emitter_lock_;
74  EventListenerMap_t listeners_;
75  DISALLOW_COPY_AND_ASSIGN(EventEmitter);
76};
77
78}  // namespace nacl_io
79
80#endif  // LIBRARIES_NACL_IO_EVENT_EMITTER_H_
81