Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(499)

Side by Side Diff: native_client_sdk/src/libraries/nacl_io/event_emitter.h

Issue 23498015: [NaCl SDK] Support non blocking TCP/UDP (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Added Fifo Tests Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 #ifndef LIBRARIES_NACL_IO_EVENT_EMITTER_H_ 5 #ifndef LIBRARIES_NACL_IO_EVENT_EMITTER_H_
6 #define LIBRARIES_NACL_IO_EVENT_EMITTER_H_ 6 #define LIBRARIES_NACL_IO_EVENT_EMITTER_H_
7 7
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <map> 10 #include <map>
11 #include <set> 11 #include <set>
12 12
13 #include "nacl_io/error.h" 13 #include "nacl_io/error.h"
14 14
15 #include "sdk_util/macros.h"
15 #include "sdk_util/ref_object.h" 16 #include "sdk_util/ref_object.h"
16 #include "sdk_util/scoped_ref.h" 17 #include "sdk_util/scoped_ref.h"
17 #include "sdk_util/simple_lock.h" 18 #include "sdk_util/simple_lock.h"
18 19
19
20 namespace nacl_io { 20 namespace nacl_io {
21 21
22 class EventEmitter; 22 class EventEmitter;
23 class EventListener; 23 class EventListener;
24 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.
28 struct EventInfo : public sdk_util::RefObject {
29 // User provied data to be returned on EventListener::Wait
30 uint64_t user_data;
31 25
32 // Bitfield of POLL events currently signaled. 26 typedef sdk_util::ScopedRef<EventEmitter> ScopedEventEmitter;
33 uint32_t events; 27 typedef std::map<EventListener*, uint32_t> EventListenerMap_t;
34 28
35 // Bitfield of POLL events of interest. 29 bool operator<(const ScopedEventEmitter& src_a,
36 uint32_t filter; 30 const ScopedEventEmitter& src_b);
37
38 // We do not use a ScopedRef to prevent circular references.
39 EventEmitter* emitter;
40 EventListener* listener;
41 uint32_t id;
42 };
43
44 typedef sdk_util::ScopedRef<EventInfo> ScopedEventInfo;
45
46 // Provide comparison for std::map and std::set
47 bool operator<(const ScopedEventInfo& src_a, const ScopedEventInfo& src_b);
48
49 typedef std::map<int, ScopedEventInfo> EventInfoMap_t;
50 typedef std::set<ScopedEventInfo> EventInfoSet_t;
51 31
52 // EventEmitter 32 // EventEmitter
53 // 33 //
54 // The EventEmitter class provides notification of events to EventListeners 34 // The EventEmitter class provides notification of events to EventListeners
55 // by registering EventInfo objects and signaling the EventListener 35 // by registering EventInfo objects and signaling the EventListener
56 // whenever thier state is changed. 36 // whenever thier state is changed.
57 // 37 //
58 // See "Kernel Events" in event_listener.h for additional information. 38 // See "Kernel Events" in event_listener.h for additional information.
39
59 class EventEmitter : public sdk_util::RefObject { 40 class EventEmitter : public sdk_util::RefObject {
60 protected: 41 public:
61 // Called automatically prior to delete to inform the EventListeners that 42 EventEmitter();
62 // this EventEmitter is abandoning an associated EventInfo.
63 virtual void Destroy();
64 43
65 private: 44 // This returns a snapshot, to ensure the status doens't change from
binji 2013/09/15 22:18:58 sp: doesn't
noelallen1 2013/09/17 21:21:54 Done.
45 // fetch to use, hold the lock.
46 uint32_t GetEventStatus() { return event_status_; }
47
48 sdk_util::SimpleLock& GetLock() { return emitter_lock_; }
49
50 // Updates the specified bits in the event status, and signals any
51 // listeners waiting on those bits.
52 void RaiseEvents_Locked(uint32_t events);
53
54 // Clears the specified bits in the event status.
55 void ClearEvents_Locked(uint32_t events);
56
66 // Register or unregister an EventInfo. The lock of the EventListener 57 // Register or unregister an EventInfo. The lock of the EventListener
67 // associated with this EventInfo must be held prior to calling these 58 // associated with this EventInfo must be held prior to calling these
68 // functions. These functions are private to ensure they are called by the 59 // functions. These functions are private to ensure they are called by the
69 // EventListener. 60 // EventListener.
70 void RegisterEventInfo(const ScopedEventInfo& info); 61 void RegisterListener(EventListener* listener, uint32_t events);
71 void UnregisterEventInfo(const ScopedEventInfo& info); 62 void UnregisterListener(EventListener* listener);
63 void RegisterListener_Locked(EventListener* listener, uint32_t events);
64 void UnregisterListener_Locked(EventListener* listener);
72 65
73 public: 66 private:
74 // Returns the current state of the emitter as POLL events bitfield. 67 uint32_t event_status_;
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
92 private:
93 sdk_util::SimpleLock emitter_lock_; 68 sdk_util::SimpleLock emitter_lock_;
94 EventInfoSet_t events_; 69 EventListenerMap_t listeners_;
95 friend class EventListener; 70 DISALLOW_COPY_AND_ASSIGN(EventEmitter);
96 }; 71 };
97 72
98 typedef sdk_util::ScopedRef<EventEmitter> ScopedEventEmitter;
99
100 } // namespace nacl_io 73 } // namespace nacl_io
101 74
102 75
103 #endif // LIBRARIES_NACL_IO_EVENT_EMITTER_H_ 76 #endif // LIBRARIES_NACL_IO_EVENT_EMITTER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698