1 // Copyright 2014 The Chromium Authors 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 BASE_APPLE_DISPATCH_SOURCE_MACH_H_ 6 #define BASE_APPLE_DISPATCH_SOURCE_MACH_H_ 7 8 #include <dispatch/dispatch.h> 9 10 #include <memory> 11 12 #include "base/base_export.h" 13 14 namespace base::apple { 15 16 // This class encapsulates a MACH_RECV dispatch source. When this object is 17 // destroyed, the source will be cancelled and it will wait for the source 18 // to stop executing work. The source can run on either a user-supplied queue, 19 // or it can create its own for the source. 20 class BASE_EXPORT DispatchSourceMach { 21 public: 22 // Creates a new dispatch source for the |port| and schedules it on a new 23 // queue that will be created with |name|. When a Mach message is received, 24 // the |event_handler| will be called. 25 DispatchSourceMach(const char* name, 26 mach_port_t port, 27 void (^event_handler)()); 28 29 // Creates a new dispatch source with the same semantics as above, but rather 30 // than creating a new queue, it schedules the source on |queue|. 31 DispatchSourceMach(dispatch_queue_t queue, 32 mach_port_t port, 33 void (^event_handler)()); 34 35 DispatchSourceMach(const DispatchSourceMach&) = delete; 36 DispatchSourceMach& operator=(const DispatchSourceMach&) = delete; 37 38 // Cancels the source and waits for it to become fully cancelled before 39 // releasing the source. 40 ~DispatchSourceMach(); 41 42 // Resumes the source. This must be called before any Mach messages will 43 // be received. 44 void Resume(); 45 46 dispatch_queue_t Queue() const; 47 48 private: 49 struct Storage; 50 std::unique_ptr<Storage> storage_; 51 }; 52 53 } // namespace base::apple 54 55 #endif // BASE_APPLE_DISPATCH_SOURCE_MACH_H_ 56