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

Side by Side Diff: base/message_pump_libevent.cc

Issue 3202: Make tcp_client_socket_unittest pass on Linux.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 12 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
« no previous file with comments | « base/message_pump_libevent.h ('k') | build/SConscript.main » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2008 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 "base/message_pump_libevent.h"
6
7 #include "base/logging.h"
8 #include "base/time.h"
9 #include "third_party/libevent/event.h"
10
11 #include <fcntl.h>
12
13 namespace base {
14
15 // Return 0 on success
16 // Too small a function to bother putting in a library?
17 static int SetNonBlocking(int fd)
18 {
19 int flags = fcntl(fd, F_GETFL, 0);
20 if (-1 == flags)
21 flags = 0;
22 return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
23 }
24
25 // Called if a byte is received on the wakeup pipe.
26 void MessagePumpLibevent::OnWakeup(int socket, short flags, void* context) {
27
28 base::MessagePumpLibevent* that =
29 static_cast<base::MessagePumpLibevent*>(context);
30 DCHECK(that->wakeup_pipe_out_ == socket);
31
32 // Remove and discard the wakeup byte.
33 char buf;
34 int nread = read(socket, &buf, 1);
35 DCHECK(nread == 1);
36 // Tell libevent to break out of inner loop.
37 event_base_loopbreak(that->event_base_);
38 }
39
40 MessagePumpLibevent::MessagePumpLibevent()
41 : keep_running_(true),
42 in_run_(false),
43 wakeup_pipe_in_(-1),
44 wakeup_pipe_out_(-1),
45 event_base_(event_base_new()) {
46 if (!Init())
47 NOTREACHED();
48 }
49
50 bool MessagePumpLibevent::Init() {
51 int fds[2];
52 if (pipe(fds))
53 return false;
54 if (SetNonBlocking(fds[0]))
55 return false;
56 if (SetNonBlocking(fds[1]))
57 return false;
58 wakeup_pipe_out_ = fds[0];
59 wakeup_pipe_in_ = fds[1];
60
61 wakeup_event_ = new event;
62 event_set(wakeup_event_, wakeup_pipe_out_, EV_READ | EV_PERSIST,
63 OnWakeup, this);
64 event_base_set(event_base_, wakeup_event_);
65
66 if (event_add(wakeup_event_, 0))
67 return false;
68 return true;
69 }
70
71 MessagePumpLibevent::~MessagePumpLibevent() {
72 DCHECK(wakeup_event_);
73 DCHECK(event_base_);
74 event_del(wakeup_event_);
75 delete wakeup_event_;
76 event_base_free(event_base_);
77 }
78
79 void MessagePumpLibevent::WatchSocket(int socket, short interest_mask,
80 event* e, Watcher* watcher) {
81
82 // Set current interest mask and message pump for this event
83 event_set(e, socket, interest_mask, OnReadinessNotification, watcher);
84
85 // Tell libevent which message pump this socket will belong to when we add it.
86 event_base_set(event_base_, e);
87
88 // Add this socket to the list of monitored sockets.
89 if (event_add(e, NULL))
90 NOTREACHED();
91 }
92
93 void MessagePumpLibevent::UnwatchSocket(event* e) {
94 // Remove this socket from the list of monitored sockets.
95 if (event_del(e))
96 NOTREACHED();
97 }
98
99 void MessagePumpLibevent::OnReadinessNotification(int socket, short flags,
100 void* context) {
101 // The given socket is ready for I/O.
102 // Tell the owner what kind of I/O the socket is ready for.
103 Watcher* watcher = static_cast<Watcher*>(context);
104 watcher->OnSocketReady(flags);
105 }
106
107 // Reentrant!
108 void MessagePumpLibevent::Run(Delegate* delegate) {
109 DCHECK(keep_running_) << "Quit must have been called outside of Run!";
110
111 bool old_in_run = in_run_;
112 in_run_ = true;
113
114 for (;;) {
115 bool did_work = delegate->DoWork();
116 if (!keep_running_)
117 break;
118
119 did_work |= delegate->DoDelayedWork(&delayed_work_time_);
120 if (!keep_running_)
121 break;
122
123 if (did_work)
124 continue;
125
126 did_work = delegate->DoIdleWork();
127 if (!keep_running_)
128 break;
129
130 if (did_work)
131 continue;
132
133 // EVLOOP_ONCE tells libevent to only block once,
134 // but to service all pending events when it wakes up.
135 if (delayed_work_time_.is_null()) {
136 event_base_loop(event_base_, EVLOOP_ONCE);
137 } else {
138 TimeDelta delay = delayed_work_time_ - Time::Now();
139 if (delay > TimeDelta()) {
140 struct timeval poll_tv;
141 poll_tv.tv_sec = delay.InSeconds();
142 poll_tv.tv_usec = delay.InMicroseconds() % Time::kMicrosecondsPerSecond;
143 event_base_loopexit(event_base_, &poll_tv);
144 event_base_loop(event_base_, EVLOOP_ONCE);
145 } else {
146 // It looks like delayed_work_time_ indicates a time in the past, so we
147 // need to call DoDelayedWork now.
148 delayed_work_time_ = Time();
149 }
150 }
151 }
152
153 keep_running_ = true;
154 in_run_ = old_in_run;
155 }
156
157 void MessagePumpLibevent::Quit() {
158 DCHECK(in_run_);
159 // Tell both libevent and Run that they should break out of their loops.
160 keep_running_ = false;
161 ScheduleWork();
162 }
163
164 void MessagePumpLibevent::ScheduleWork() {
165 // Tell libevent (in a threadsafe way) that it should break out of its loop.
166 char buf = 0;
167 int nwrite = write(wakeup_pipe_in_, &buf, 1);
168 DCHECK(nwrite == 1);
169 }
170
171 void MessagePumpLibevent::ScheduleDelayedWork(const Time& delayed_work_time) {
172 // We know that we can't be blocked on Wait right now since this method can
173 // only be called on the same thread as Run, so we only need to update our
174 // record of how long to sleep when we do sleep.
175 delayed_work_time_ = delayed_work_time;
176 }
177
178 } // namespace base
179
OLDNEW
« no previous file with comments | « base/message_pump_libevent.h ('k') | build/SConscript.main » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698