1116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Copyright 2014 The Chromium Authors. All rights reserved.
2116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Use of this source code is governed by a BSD-style license that can be
3116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// found in the LICENSE file.
4116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
5116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "sandbox/mac/dispatch_source_mach.h"
6116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
7116680a4aac90f2aa7413d9095a592090648e557Ben Murdochnamespace sandbox {
8116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
9116680a4aac90f2aa7413d9095a592090648e557Ben MurdochDispatchSourceMach::DispatchSourceMach(const char* name,
10116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                       mach_port_t port,
11116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                       void (^event_handler)())
12116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    // TODO(rsesek): Specify DISPATCH_QUEUE_SERIAL, in the 10.7 SDK. NULL
13116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    // means the same thing but is not symbolically clear.
14116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    : DispatchSourceMach(dispatch_queue_create(name, NULL),
15116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                         port,
16116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                         event_handler) {
17116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Since the queue was created above in the delegated constructor, and it was
18116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // subsequently retained, release it here.
19116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  dispatch_release(queue_);
20116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
21116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
22116680a4aac90f2aa7413d9095a592090648e557Ben MurdochDispatchSourceMach::DispatchSourceMach(dispatch_queue_t queue,
23116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                       mach_port_t port,
24116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                       void (^event_handler)())
25116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    : queue_(queue),
26116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      source_(dispatch_source_create(DISPATCH_SOURCE_TYPE_MACH_RECV,
27116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch          port, 0, queue_)),
28116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      source_canceled_(dispatch_semaphore_create(0)) {
29116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  dispatch_retain(queue);
30116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
31116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  dispatch_source_set_event_handler(source_, event_handler);
32116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  dispatch_source_set_cancel_handler(source_, ^{
33116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      dispatch_semaphore_signal(source_canceled_);
34116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  });
35116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
36116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
37116680a4aac90f2aa7413d9095a592090648e557Ben MurdochDispatchSourceMach::~DispatchSourceMach() {
38116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  Cancel();
39116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
40116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
41116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid DispatchSourceMach::Resume() {
42116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  dispatch_resume(source_);
43116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
44116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
45116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid DispatchSourceMach::Cancel() {
46116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (source_) {
47116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    dispatch_source_cancel(source_);
48116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    dispatch_release(source_);
49116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    source_ = NULL;
50116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
51116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    dispatch_semaphore_wait(source_canceled_, DISPATCH_TIME_FOREVER);
52116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    dispatch_release(source_canceled_);
53116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    source_canceled_ = NULL;
54116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
55116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
56116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (queue_) {
57116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    dispatch_release(queue_);
58116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    queue_ = NULL;
59116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
60116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
61116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
62116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}  // namespace sandbox
63