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/ref_object.h"
16#include "sdk_util/scoped_ref.h"
17#include "sdk_util/simple_lock.h"
18
19
20namespace nacl_io {
21
22class EventEmitter;
23class EventListener;
24
25// A ref counted object (non POD derived from RefObject) for storing the
26// state of a single signal request.  Requests are unique to any
27// FD/EventListener pair.
28struct EventInfo : public sdk_util::RefObject {
29  // User provied data to be returned on EventListener::Wait
30  uint64_t user_data;
31
32  // Bitfield of POLL events currently signaled.
33  uint32_t events;
34
35  // Bitfield of POLL events of interest.
36  uint32_t filter;
37
38  // We do not use a ScopedRef to prevent circular references.
39  EventEmitter* emitter;
40  EventListener* listener;
41  uint32_t id;
42};
43
44typedef sdk_util::ScopedRef<EventInfo> ScopedEventInfo;
45
46// Provide comparison for std::map and std::set
47bool operator<(const ScopedEventInfo& src_a, const ScopedEventInfo& src_b);
48
49typedef std::map<int, ScopedEventInfo> EventInfoMap_t;
50typedef std::set<ScopedEventInfo> EventInfoSet_t;
51
52// EventEmitter
53//
54// The EventEmitter class provides notification of events to EventListeners
55// by registering EventInfo objects and signaling the EventListener
56// whenever thier state is changed.
57//
58// See "Kernel Events" in event_listener.h for additional information.
59class EventEmitter : public sdk_util::RefObject {
60 protected:
61  // Called automatically prior to delete to inform the EventListeners that
62  // this EventEmitter is abandoning an associated EventInfo.
63  virtual void Destroy();
64
65 private:
66  // Register or unregister an EventInfo.  The lock of the EventListener
67  // associated with this EventInfo must be held prior to calling these
68  // functions.  These functions are private to ensure they are called by the
69  // EventListener.
70  void RegisterEventInfo(const ScopedEventInfo& info);
71  void UnregisterEventInfo(const ScopedEventInfo& info);
72
73 public:
74  // Returns the current state of the emitter as POLL events bitfield.
75  virtual uint32_t GetEventStatus() = 0;
76
77  // Returns the type of the emitter (compatible with st_mode in stat)
78  virtual int GetType() = 0;
79
80 protected:
81  // Called by the thread causing the Event.
82  void RaiseEvent(uint32_t events);
83
84  // Provided to allow one EventEmitter to register the same EventInfo with
85  // a child EventEmitter so that they can both signal the EventListener.
86  // Called after registering locally, but while lock is still held.
87  virtual void ChainRegisterEventInfo(const ScopedEventInfo& event);
88
89  // Called before unregistering locally, but while lock is still held.
90  virtual void ChainUnregisterEventInfo(const ScopedEventInfo& event);
91
92private:
93  sdk_util::SimpleLock emitter_lock_;
94  EventInfoSet_t events_;
95  friend class EventListener;
96};
97
98typedef sdk_util::ScopedRef<EventEmitter> ScopedEventEmitter;
99
100}  // namespace nacl_io
101
102
103#endif  // LIBRARIES_NACL_IO_EVENT_EMITTER_H_