]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_utilities/lib/dispatch.h
Security-58286.1.32.tar.gz
[apple/security.git] / OSX / libsecurity_utilities / lib / dispatch.h
1 /*
2 * Copyright (c) 2014 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 //
25 // dispatch - libdispatch wrapper
26 //
27 #ifndef _H_DISPATCH
28 #define _H_DISPATCH
29
30 #include <dispatch/dispatch.h>
31 #include <security_utilities/utilities.h>
32 #include <security_utilities/threading.h>
33
34 #include <exception>
35
36 namespace Security {
37 namespace Dispatch {
38
39
40 // Wraps dispatch objects which can be used to queue blocks, i.e. dispatch groups and queues.
41 // If a block throws an exception, no further blocks are enqueued and the exception is rethrown
42 // after waiting for completion of all blocks.
43 class ExceptionAwareEnqueuing {
44 NOCOPY(ExceptionAwareEnqueuing)
45 public:
46 ExceptionAwareEnqueuing();
47
48 void enqueueWithDispatcher(void (^dispatcher)(dispatch_block_t), dispatch_block_t block);
49 void throwPendingException();
50 private:
51 Mutex mLock;
52 bool mExceptionPending;
53 std::exception_ptr mException;
54 };
55
56
57 class Queue {
58 NOCOPY(Queue)
59 public:
60 Queue(const char *label, bool concurrent, dispatch_qos_class_t qos_class = QOS_CLASS_UNSPECIFIED);
61 virtual ~Queue();
62
63 operator dispatch_queue_t () const { return mQueue; }
64
65 void enqueue(dispatch_block_t block);
66 void wait();
67
68 private:
69 ExceptionAwareEnqueuing enqueuing;
70 dispatch_queue_t mQueue;
71 };
72
73
74 class Group {
75 NOCOPY(Group)
76 public:
77 Group();
78 virtual ~Group();
79
80 operator dispatch_group_t () const { return mGroup; }
81
82 void enqueue(dispatch_queue_t queue, dispatch_block_t block);
83 void wait();
84
85 private:
86 ExceptionAwareEnqueuing enqueuing;
87 dispatch_group_t mGroup;
88 };
89
90
91 class Semaphore {
92 NOCOPY(Semaphore)
93 public:
94 Semaphore(long count);
95 Semaphore(Semaphore& semaphore);
96 virtual ~Semaphore();
97
98 operator dispatch_semaphore_t () const { return mSemaphore; };
99
100 bool signal();
101 bool wait(dispatch_time_t timeout = DISPATCH_TIME_FOREVER);
102
103 private:
104 dispatch_semaphore_t mSemaphore;
105 };
106
107
108 class SemaphoreWait {
109 NOCOPY(SemaphoreWait)
110 public:
111 SemaphoreWait(SemaphoreWait& originalWait);
112 SemaphoreWait(Semaphore& semaphore, dispatch_time_t timeout = DISPATCH_TIME_FOREVER);
113 virtual ~SemaphoreWait();
114
115 bool acquired() const { return mAcquired; };
116
117 private:
118 Semaphore &mSemaphore;
119 bool mAcquired;
120 };
121
122
123 } // end namespace Dispatch
124 } // end namespace Security
125
126 #endif // !_H_DISPATCH