2 * Copyright (c) 2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
25 // dispatch - libdispatch wrapper
30 #include <dispatch/dispatch.h>
31 #include <security_utilities/utilities.h>
32 #include <security_utilities/threading.h>
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
)
46 ExceptionAwareEnqueuing();
48 void enqueueWithDispatcher(void (^dispatcher
)(dispatch_block_t
), dispatch_block_t block
);
49 void throwPendingException();
52 bool mExceptionPending
;
53 std::exception_ptr mException
;
60 Queue(const char *label
, bool concurrent
, dispatch_qos_class_t qos_class
= QOS_CLASS_UNSPECIFIED
);
63 operator dispatch_queue_t () const { return mQueue
; }
65 void enqueue(dispatch_block_t block
);
69 ExceptionAwareEnqueuing enqueuing
;
70 dispatch_queue_t mQueue
;
80 operator dispatch_group_t () const { return mGroup
; }
82 void enqueue(dispatch_queue_t queue
, dispatch_block_t block
);
86 ExceptionAwareEnqueuing enqueuing
;
87 dispatch_group_t mGroup
;
94 Semaphore(long count
);
95 Semaphore(Semaphore
& semaphore
);
98 operator dispatch_semaphore_t () const { return mSemaphore
; };
101 bool wait(dispatch_time_t timeout
= DISPATCH_TIME_FOREVER
);
104 dispatch_semaphore_t mSemaphore
;
108 class SemaphoreWait
{
109 NOCOPY(SemaphoreWait
)
111 SemaphoreWait(SemaphoreWait
& originalWait
);
112 SemaphoreWait(Semaphore
& semaphore
, dispatch_time_t timeout
= DISPATCH_TIME_FOREVER
);
113 virtual ~SemaphoreWait();
115 bool acquired() const { return mAcquired
; };
118 Semaphore
&mSemaphore
;
123 } // end namespace Dispatch
124 } // end namespace Security
126 #endif // !_H_DISPATCH