1 // 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.
5 #include "base/mac/dispatch_source_mach.h"
9 DispatchSourceMach::DispatchSourceMach(const char* name
,
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
),
17 // Since the queue was created above in the delegated constructor, and it was
18 // subsequently retained, release it here.
19 dispatch_release(queue_
);
22 DispatchSourceMach::DispatchSourceMach(dispatch_queue_t queue
,
24 void (^event_handler
)())
26 source_(dispatch_source_create(DISPATCH_SOURCE_TYPE_MACH_RECV
,
28 source_canceled_(dispatch_semaphore_create(0)) {
29 dispatch_retain(queue
);
31 dispatch_source_set_event_handler(source_
, event_handler
);
32 dispatch_source_set_cancel_handler(source_
, ^{
33 dispatch_semaphore_signal(source_canceled_
);
37 DispatchSourceMach::~DispatchSourceMach() {
41 void DispatchSourceMach::Resume() {
42 dispatch_resume(source_
);
45 void DispatchSourceMach::Cancel() {
47 dispatch_source_cancel(source_
);
48 dispatch_release(source_
);
51 dispatch_semaphore_wait(source_canceled_
, DISPATCH_TIME_FOREVER
);
52 dispatch_release(source_canceled_
);
53 source_canceled_
= NULL
;
57 dispatch_release(queue_
);