OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 23 matching lines...) Expand all Loading... |
34 ~TimerEvent() { event_del(&ev); } | 34 ~TimerEvent() { event_del(&ev); } |
35 event ev; | 35 event ev; |
36 std::unique_ptr<QueuedTask> task; | 36 std::unique_ptr<QueuedTask> task; |
37 }; | 37 }; |
38 | 38 |
39 bool SetNonBlocking(int fd) { | 39 bool SetNonBlocking(int fd) { |
40 const int flags = fcntl(fd, F_GETFL); | 40 const int flags = fcntl(fd, F_GETFL); |
41 RTC_CHECK(flags != -1); | 41 RTC_CHECK(flags != -1); |
42 return (flags & O_NONBLOCK) || fcntl(fd, F_SETFL, flags | O_NONBLOCK) != -1; | 42 return (flags & O_NONBLOCK) || fcntl(fd, F_SETFL, flags | O_NONBLOCK) != -1; |
43 } | 43 } |
| 44 |
| 45 // TODO(tommi): This is a hack to support two versions of libevent that we're |
| 46 // compatible with. The method we really want to call is event_assign(), |
| 47 // since event_set() has been marked as deprecated (and doesn't accept |
| 48 // passing event_base__ as a parameter). However, the version of libevent |
| 49 // that we have in Chromium, doesn't have event_assign(), so we need to call |
| 50 // event_set() there. |
| 51 void EventAssign(struct event* ev, |
| 52 struct event_base* base, |
| 53 int fd, |
| 54 short events, |
| 55 void (*callback)(int, short, void*), |
| 56 void* arg) { |
| 57 #if defined(_EVENT2_EVENT_H_) |
| 58 RTC_CHECK_EQ(0, event_assign(ev, base, fd, events, callback, arg)); |
| 59 #else |
| 60 event_set(ev, fd, events, callback, arg); |
| 61 RTC_CHECK_EQ(0, event_base_set(base, ev)); |
| 62 #endif |
| 63 } |
44 } // namespace | 64 } // namespace |
45 | 65 |
46 struct TaskQueue::QueueContext { | 66 struct TaskQueue::QueueContext { |
47 explicit QueueContext(TaskQueue* q) : queue(q), is_active(true) {} | 67 explicit QueueContext(TaskQueue* q) : queue(q), is_active(true) {} |
48 TaskQueue* queue; | 68 TaskQueue* queue; |
49 bool is_active; | 69 bool is_active; |
50 // Holds a list of events pending timers for cleanup when the loop exits. | 70 // Holds a list of events pending timers for cleanup when the loop exits. |
51 std::list<TimerEvent*> pending_timers_; | 71 std::list<TimerEvent*> pending_timers_; |
52 }; | 72 }; |
53 | 73 |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 : event_base_(event_base_new()), | 137 : event_base_(event_base_new()), |
118 wakeup_event_(new event()), | 138 wakeup_event_(new event()), |
119 thread_(&TaskQueue::ThreadMain, this, queue_name) { | 139 thread_(&TaskQueue::ThreadMain, this, queue_name) { |
120 RTC_DCHECK(queue_name); | 140 RTC_DCHECK(queue_name); |
121 int fds[2]; | 141 int fds[2]; |
122 RTC_CHECK(pipe(fds) == 0); | 142 RTC_CHECK(pipe(fds) == 0); |
123 SetNonBlocking(fds[0]); | 143 SetNonBlocking(fds[0]); |
124 SetNonBlocking(fds[1]); | 144 SetNonBlocking(fds[1]); |
125 wakeup_pipe_out_ = fds[0]; | 145 wakeup_pipe_out_ = fds[0]; |
126 wakeup_pipe_in_ = fds[1]; | 146 wakeup_pipe_in_ = fds[1]; |
127 // TODO(tommi): This is a hack to support two versions of libevent that we're | 147 EventAssign(wakeup_event_.get(), event_base_, wakeup_pipe_out_, |
128 // compatible with. The method we really want to call is event_assign(), | 148 EV_READ | EV_PERSIST, OnWakeup, this); |
129 // since event_set() has been marked as deprecated (and doesn't accept | |
130 // passing event_base__ as a parameter). However, the version of libevent | |
131 // that we have in Chromium, doesn't have event_assign(), so we need to call | |
132 // event_set() there. | |
133 #if defined(_EVENT2_EVENT_H_) | |
134 event_assign(wakeup_event_.get(), event_base_, wakeup_pipe_out_, | |
135 EV_READ | EV_PERSIST, OnWakeup, this); | |
136 #else | |
137 event_set(wakeup_event_.get(), wakeup_pipe_out_, EV_READ | EV_PERSIST, | |
138 OnWakeup, this); | |
139 #endif | |
140 event_base_set(event_base_, wakeup_event_.get()); | |
141 event_add(wakeup_event_.get(), 0); | 149 event_add(wakeup_event_.get(), 0); |
142 thread_.Start(); | 150 thread_.Start(); |
143 } | 151 } |
144 | 152 |
145 TaskQueue::~TaskQueue() { | 153 TaskQueue::~TaskQueue() { |
146 RTC_DCHECK(!IsCurrent()); | 154 RTC_DCHECK(!IsCurrent()); |
147 struct timespec ts; | 155 struct timespec ts; |
148 char message = kQuit; | 156 char message = kQuit; |
149 while (write(wakeup_pipe_in_, &message, sizeof(message)) != sizeof(message)) { | 157 while (write(wakeup_pipe_in_, &message, sizeof(message)) != sizeof(message)) { |
150 // The queue is full, so we have no choice but to wait and retry. | 158 // The queue is full, so we have no choice but to wait and retry. |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 return t.get() == task_id; | 223 return t.get() == task_id; |
216 }); | 224 }); |
217 } | 225 } |
218 } | 226 } |
219 } | 227 } |
220 | 228 |
221 void TaskQueue::PostDelayedTask(std::unique_ptr<QueuedTask> task, | 229 void TaskQueue::PostDelayedTask(std::unique_ptr<QueuedTask> task, |
222 uint32_t milliseconds) { | 230 uint32_t milliseconds) { |
223 if (IsCurrent()) { | 231 if (IsCurrent()) { |
224 TimerEvent* timer = new TimerEvent(std::move(task)); | 232 TimerEvent* timer = new TimerEvent(std::move(task)); |
225 evtimer_set(&timer->ev, &TaskQueue::RunTimer, timer); | 233 EventAssign(&timer->ev, event_base_, -1, 0, &TaskQueue::RunTimer, timer); |
226 event_base_set(event_base_, &timer->ev); | |
227 QueueContext* ctx = | 234 QueueContext* ctx = |
228 static_cast<QueueContext*>(pthread_getspecific(GetQueuePtrTls())); | 235 static_cast<QueueContext*>(pthread_getspecific(GetQueuePtrTls())); |
229 ctx->pending_timers_.push_back(timer); | 236 ctx->pending_timers_.push_back(timer); |
230 timeval tv = {milliseconds / 1000, (milliseconds % 1000) * 1000}; | 237 timeval tv = {milliseconds / 1000, (milliseconds % 1000) * 1000}; |
231 event_add(&timer->ev, &tv); | 238 event_add(&timer->ev, &tv); |
232 } else { | 239 } else { |
233 PostTask(std::unique_ptr<QueuedTask>( | 240 PostTask(std::unique_ptr<QueuedTask>( |
234 new SetTimerTask(std::move(task), milliseconds))); | 241 new SetTimerTask(std::move(task), milliseconds))); |
235 } | 242 } |
236 } | 243 } |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
320 CritScope lock(&pending_lock_); | 327 CritScope lock(&pending_lock_); |
321 pending_replies_.push_back(reply_task); | 328 pending_replies_.push_back(reply_task); |
322 } | 329 } |
323 | 330 |
324 void TaskQueue::ReplyTaskDone(PostAndReplyTask* reply_task) { | 331 void TaskQueue::ReplyTaskDone(PostAndReplyTask* reply_task) { |
325 CritScope lock(&pending_lock_); | 332 CritScope lock(&pending_lock_); |
326 pending_replies_.remove(reply_task); | 333 pending_replies_.remove(reply_task); |
327 } | 334 } |
328 | 335 |
329 } // namespace rtc | 336 } // namespace rtc |
OLD | NEW |