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

Side by Side Diff: base/message_pump_libevent.h

Issue 14068: Reverting 6911. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 12 years 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
« no previous file with comments | « base/message_loop.cc ('k') | base/message_pump_libevent.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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 BASE_MESSAGE_PUMP_LIBEVENT_H_ 5 #ifndef BASE_MESSAGE_PUMP_LIBEVENT_H_
6 #define BASE_MESSAGE_PUMP_LIBEVENT_H_ 6 #define BASE_MESSAGE_PUMP_LIBEVENT_H_
7 7
8 #include "base/message_pump.h" 8 #include "base/message_pump.h"
9 #include "base/scoped_ptr.h"
10 #include "base/time.h" 9 #include "base/time.h"
11 10
12 // Declare structs we need from libevent.h rather than including it 11 // Declare structs we need from libevent.h rather than including it
13 struct event_base; 12 struct event_base;
14 struct event; 13 struct event;
15 14
16 namespace base { 15 namespace base {
17 16
18 // Class to monitor sockets and issue callbacks when sockets are ready for I/O 17 // Class to monitor sockets and issue callbacks when sockets are ready for I/O
19 // TODO(dkegel): add support for background file IO somehow 18 // TODO(dkegel): add support for background file IO somehow
20 class MessagePumpLibevent : public MessagePump { 19 class MessagePumpLibevent : public MessagePump {
21 public: 20 public:
22 21 // Used with WatchSocket to asynchronously monitor the I/O readiness of a
23 // Object returned by WatchFileDescriptor to manage further watching. 22 // socket.
24 class FileDescriptorWatcher {
25 public:
26 FileDescriptorWatcher();
27 ~FileDescriptorWatcher(); // Implicitly calls StopWatching.
28
29 // NOTE: These methods aren't called StartWatching()/StopWatching() to
30 // avoid confusion with the win32 ObjectWatcher class.
31
32 // Stop watching the FD, always safe to call. No-op if there's nothing
33 // to do.
34 bool StopWatchingFileDescriptor();
35
36 private:
37 // Called by MessagePumpLibevent, ownership of |e| is transferred to this
38 // object.
39 // If this FileWatcher is already watching an event, the previous event is
40 // terminated and cleaned up here.
41 void Init(event* e, bool is_persistent);
42 friend class MessagePumpLibevent;
43
44 private:
45 bool is_persistent_; // false if this event is one-shot.
46 scoped_ptr<event> event_;
47 DISALLOW_COPY_AND_ASSIGN(FileDescriptorWatcher);
48 };
49
50 // Used with WatchFileDescptor to asynchronously monitor the I/O readiness of
51 // a File Descriptor.
52 class Watcher { 23 class Watcher {
53 public: 24 public:
54 virtual ~Watcher() {} 25 virtual ~Watcher() {}
55 // Called from MessageLoop::Run when an FD can be read from/written to 26 // Called from MessageLoop::Run when a ready socket is detected.
56 // without blocking 27 virtual void OnSocketReady(short eventmask) = 0;
57 virtual void OnFileCanReadWithoutBlocking(int fd) = 0; 28 };
58 virtual void OnFileCanWriteWithoutBlocking(int fd) = 0; 29
30 // Used with WatchFileHandle to monitor I/O readiness for a File Handle.
31 class FileWatcher {
32 public:
33 virtual ~FileWatcher() {}
34 // Called from MessageLoop::Run when a non-blocking read/write can be made.
35 virtual void OnFileReadReady(int fd) = 0;
36 virtual void OnFileWriteReady(int fd) = 0;
59 }; 37 };
60 38
61 MessagePumpLibevent(); 39 MessagePumpLibevent();
62 virtual ~MessagePumpLibevent(); 40 virtual ~MessagePumpLibevent();
63 41
64 enum Mode { 42 // Have the current thread's message loop watch for a ready socket.
65 WATCH_READ, 43 // Caller must provide a struct event for this socket for libevent's use.
66 WATCH_WRITE, 44 // The event and interest_mask fields are defined in libevent.
67 WATCH_READ_WRITE 45 // Returns true on success.
68 }; 46 // TODO(dkegel): hide libevent better; abstraction still too leaky
47 // TODO(dkegel): better error handing
48 // TODO(dkegel): switch to edge-triggered readiness notification
49 void WatchSocket(int socket, short interest_mask, event* e, Watcher*);
69 50
70 // Have the current thread's message loop watch for a a situation in which 51 // TODO(playmobil): Merge this with WatchSocket().
71 // reading/writing to the FD can be performed without Blocking. 52 void WatchFileHandle(int fd, short interest_mask, event* e, FileWatcher*);
72 // Callers must provide a preallocated FileDescriptorWatcher object which 53
73 // can later be used to manage the Lifetime of this event. 54 // Stop watching a socket.
74 // Returns true on success. 55 // Event was previously initialized by WatchSocket.
75 // TODO(dkegel): switch to edge-triggered readiness notification 56 void UnwatchSocket(event* e);
76 bool WatchFileDescriptor(int fd, 57
77 bool persistent, 58 // Stop watching a File Handle.
78 Mode mode, 59 // Event was previously initialized by WatchFileHandle.
79 FileDescriptorWatcher *controller, 60 void UnwatchFileHandle(event* e);
80 Watcher *delegate);
81 61
82 // MessagePump methods: 62 // MessagePump methods:
83 virtual void Run(Delegate* delegate); 63 virtual void Run(Delegate* delegate);
84 virtual void Quit(); 64 virtual void Quit();
85 virtual void ScheduleWork(); 65 virtual void ScheduleWork();
86 virtual void ScheduleDelayedWork(const Time& delayed_work_time); 66 virtual void ScheduleDelayedWork(const Time& delayed_work_time);
87 67
88 private: 68 private:
89 69
90 // Risky part of constructor. Returns true on success. 70 // Risky part of constructor. Returns true on success.
91 bool Init(); 71 bool Init();
92 72
93 // This flag is set to false when Run should return. 73 // This flag is set to false when Run should return.
94 bool keep_running_; 74 bool keep_running_;
95 75
96 // This flag is set when inside Run. 76 // This flag is set when inside Run.
97 bool in_run_; 77 bool in_run_;
98 78
99 // The time at which we should call DoDelayedWork. 79 // The time at which we should call DoDelayedWork.
100 Time delayed_work_time_; 80 Time delayed_work_time_;
101 81
102 // Libevent dispatcher. Watches all sockets registered with it, and sends 82 // Libevent dispatcher. Watches all sockets registered with it, and sends
103 // readiness callbacks when a socket is ready for I/O. 83 // readiness callbacks when a socket is ready for I/O.
104 event_base* event_base_; 84 event_base* event_base_;
105 85
106 // Called by libevent to tell us a registered FD can be read/written to. 86 // Called by libevent to tell us a registered socket is ready
107 static void OnLibeventNotification(int fd, short flags, 87 static void OnReadinessNotification(int socket, short flags, void* context);
108 void* context); 88
89 // Called by libevent to tell us a registered fd is ready.
90 static void OnFileReadReadinessNotification(int fd, short flags,
91 void* context);
92 static void OnFileWriteReadinessNotification(int fd, short flags,
93 void* context);
109 94
110 // Unix pipe used to implement ScheduleWork() 95 // Unix pipe used to implement ScheduleWork()
111 // ... callback; called by libevent inside Run() when pipe is ready to read 96 // ... callback; called by libevent inside Run() when pipe is ready to read
112 static void OnWakeup(int socket, short flags, void* context); 97 static void OnWakeup(int socket, short flags, void* context);
113 // ... write end; ScheduleWork() writes a single byte to it 98 // ... write end; ScheduleWork() writes a single byte to it
114 int wakeup_pipe_in_; 99 int wakeup_pipe_in_;
115 // ... read end; OnWakeup reads it and then breaks Run() out of its sleep 100 // ... read end; OnWakeup reads it and then breaks Run() out of its sleep
116 int wakeup_pipe_out_; 101 int wakeup_pipe_out_;
117 // ... libevent wrapper for read end 102 // ... libevent wrapper for read end
118 event* wakeup_event_; 103 event* wakeup_event_;
119 104
120 DISALLOW_COPY_AND_ASSIGN(MessagePumpLibevent); 105 DISALLOW_COPY_AND_ASSIGN(MessagePumpLibevent);
121 }; 106 };
122 107
123 } // namespace base 108 } // namespace base
124 109
125 #endif // BASE_MESSAGE_PUMP_LIBEVENT_H_ 110 #endif // BASE_MESSAGE_PUMP_LIBEVENT_H_
OLDNEW
« no previous file with comments | « base/message_loop.cc ('k') | base/message_pump_libevent.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698