blob: 745c9dec7d1f19f48edb0f1e0172f45522e71005 [file] [log] [blame]
[email protected]6e9bfe12014-07-17 06:36:541// Copyright 2014 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
rsesek6cba5922015-05-13 19:49:105#include "base/mac/dispatch_source_mach.h"
[email protected]6e9bfe12014-07-17 06:36:546
rsesek6cba5922015-05-13 19:49:107namespace base {
[email protected]6e9bfe12014-07-17 06:36:548
9DispatchSourceMach::DispatchSourceMach(const char* name,
10 mach_port_t port,
11 void (^event_handler)())
12 // TODO(rsesek): Specify DISPATCH_QUEUE_SERIAL, in the 10.7 SDK. NULL
13 // means the same thing but is not symbolically clear.
14 : DispatchSourceMach(dispatch_queue_create(name, NULL),
15 port,
16 event_handler) {
17 // Since the queue was created above in the delegated constructor, and it was
18 // subsequently retained, release it here.
19 dispatch_release(queue_);
20}
21
22DispatchSourceMach::DispatchSourceMach(dispatch_queue_t queue,
23 mach_port_t port,
24 void (^event_handler)())
25 : queue_(queue),
26 source_(dispatch_source_create(DISPATCH_SOURCE_TYPE_MACH_RECV,
27 port, 0, queue_)),
28 source_canceled_(dispatch_semaphore_create(0)) {
29 dispatch_retain(queue);
30
31 dispatch_source_set_event_handler(source_, event_handler);
32 dispatch_source_set_cancel_handler(source_, ^{
33 dispatch_semaphore_signal(source_canceled_);
34 });
35}
36
37DispatchSourceMach::~DispatchSourceMach() {
38 Cancel();
39}
40
41void DispatchSourceMach::Resume() {
42 dispatch_resume(source_);
43}
44
45void DispatchSourceMach::Cancel() {
46 if (source_) {
47 dispatch_source_cancel(source_);
48 dispatch_release(source_);
49 source_ = NULL;
50
51 dispatch_semaphore_wait(source_canceled_, DISPATCH_TIME_FOREVER);
52 dispatch_release(source_canceled_);
53 source_canceled_ = NULL;
54 }
55
56 if (queue_) {
57 dispatch_release(queue_);
58 queue_ = NULL;
59 }
60}
61
rsesek6cba5922015-05-13 19:49:1062} // namespace base