Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #ifndef SANDBOX_MAC_DISPATCH_SOURCE_MACH_H_ | |
| 6 #define SANDBOX_MAC_DISPATCH_SOURCE_MACH_H_ | |
| 7 | |
| 8 #include <dispatch/dispatch.h> | |
| 9 | |
| 10 #include "base/basictypes.h" | |
| 11 | |
| 12 namespace sandbox { | |
| 13 | |
| 14 // This class encapsulates a MACH_RECV dispatch source. When this object is | |
| 15 // destructed, the source will be cancelled and it will wait for the source | |
|
Mark Mentovai
2014/07/16 15:45:38
I think “destruct” is an awkward verb and can usua
Robert Sesek
2014/07/16 19:18:17
Done.
| |
| 16 // to stop executing work. The source can run on either a user-supplied queue, | |
| 17 // or it can create its own for the source. | |
| 18 class DispatchSourceMach { | |
| 19 public: | |
| 20 // Creates a new dispatch source for the |port| and schedules it on a new | |
| 21 // queue that will be created with |name|. When a Mach message is received, | |
| 22 // the |event_handler| will be called. | |
| 23 DispatchSourceMach(const char* name, | |
| 24 mach_port_t port, | |
| 25 void (^event_handler)()); | |
| 26 | |
| 27 // Creates a new dispatch source with the same semantics as above, but rather | |
| 28 // than creating a new queue, it schedules the source on |queue|. | |
| 29 DispatchSourceMach(dispatch_queue_t queue, | |
| 30 mach_port_t port, | |
| 31 void (^event_handler)()); | |
| 32 | |
| 33 // Cancels the source and waits for it to become fully cancelled before | |
| 34 // releasing the source. | |
| 35 ~DispatchSourceMach(); | |
| 36 | |
| 37 // Resumes the source. This must be called before any Mach messages will | |
| 38 // be received. | |
| 39 void Resume(); | |
| 40 | |
| 41 // Cancels the source, after which this class will no longer receive Mach | |
| 42 // messages. Calling this more than once is a no-op. | |
| 43 void Cancel(); | |
|
Mark Mentovai
2014/07/16 15:45:38
This can be private.
Robert Sesek
2014/07/16 19:18:17
Done.
| |
| 44 | |
| 45 private: | |
| 46 // The dispatch queue used to service the source_. | |
| 47 dispatch_queue_t queue_; | |
| 48 | |
| 49 // A MACH_RECV dispatch source. | |
| 50 dispatch_source_t source_; | |
| 51 | |
| 52 // Semaphore used to wait on the |source_|'s cancellation in the destructor. | |
| 53 dispatch_semaphore_t source_canceled_; | |
| 54 | |
| 55 DISALLOW_COPY_AND_ASSIGN(DispatchSourceMach); | |
| 56 }; | |
| 57 | |
| 58 } // namespace sandbox | |
| 59 | |
| 60 #endif // SANDBOX_MAC_DISPATCH_SOURCE_MACH_H_ | |
| OLD | NEW |