2 * Copyright (c) 2008-2013 Apple Inc. All rights reserved.
4 * @APPLE_APACHE_LICENSE_HEADER_START@
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * @APPLE_APACHE_LICENSE_HEADER_END@
21 #ifndef __DISPATCH_GROUP__
22 #define __DISPATCH_GROUP__
24 #ifndef __DISPATCH_INDIRECT__
25 #error "Please #include <dispatch/dispatch.h> instead of this file directly."
26 #include <dispatch/base.h> // for HeaderDoc
29 DISPATCH_ASSUME_NONNULL_BEGIN
32 * @typedef dispatch_group_t
34 * A group of blocks submitted to queues for asynchronous invocation.
36 DISPATCH_DECL(dispatch_group
);
41 * @function dispatch_group_create
44 * Creates new group with which blocks may be associated.
47 * This function creates a new group with which blocks may be associated.
48 * The dispatch group may be used to wait for the completion of the blocks it
49 * references. The group object memory is freed with dispatch_release().
52 * The newly created group, or NULL on failure.
54 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
55 DISPATCH_EXPORT DISPATCH_MALLOC DISPATCH_RETURNS_RETAINED DISPATCH_WARN_RESULT
58 dispatch_group_create(void);
61 * @function dispatch_group_async
64 * Submits a block to a dispatch queue and associates the block with the given
68 * Submits a block to a dispatch queue and associates the block with the given
69 * dispatch group. The dispatch group may be used to wait for the completion
70 * of the blocks it references.
73 * A dispatch group to associate with the submitted block.
74 * The result of passing NULL in this parameter is undefined.
77 * The dispatch queue to which the block will be submitted for asynchronous
81 * The block to perform asynchronously.
84 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
85 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
87 dispatch_group_async(dispatch_group_t group
,
88 dispatch_queue_t queue
,
89 dispatch_block_t block
);
90 #endif /* __BLOCKS__ */
93 * @function dispatch_group_async_f
96 * Submits a function to a dispatch queue and associates the block with the
97 * given dispatch group.
100 * See dispatch_group_async() for details.
103 * A dispatch group to associate with the submitted function.
104 * The result of passing NULL in this parameter is undefined.
107 * The dispatch queue to which the function will be submitted for asynchronous
111 * The application-defined context parameter to pass to the function.
114 * The application-defined function to invoke on the target queue. The first
115 * parameter passed to this function is the context provided to
116 * dispatch_group_async_f().
118 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
119 DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NONNULL2 DISPATCH_NONNULL4
122 dispatch_group_async_f(dispatch_group_t group
,
123 dispatch_queue_t queue
,
124 void *_Nullable context
,
125 dispatch_function_t work
);
128 * @function dispatch_group_wait
131 * Wait synchronously until all the blocks associated with a group have
132 * completed or until the specified timeout has elapsed.
135 * This function waits for the completion of the blocks associated with the
136 * given dispatch group, and returns after all blocks have completed or when
137 * the specified timeout has elapsed.
139 * This function will return immediately if there are no blocks associated
140 * with the dispatch group (i.e. the group is empty).
142 * The result of calling this function from multiple threads simultaneously
143 * with the same dispatch group is undefined.
145 * After the successful return of this function, the dispatch group is empty.
146 * It may either be released with dispatch_release() or re-used for additional
147 * blocks. See dispatch_group_async() for more information.
150 * The dispatch group to wait on.
151 * The result of passing NULL in this parameter is undefined.
154 * When to timeout (see dispatch_time). As a convenience, there are the
155 * DISPATCH_TIME_NOW and DISPATCH_TIME_FOREVER constants.
158 * Returns zero on success (all blocks associated with the group completed
159 * within the specified timeout) or non-zero on error (i.e. timed out).
161 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
162 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
164 dispatch_group_wait(dispatch_group_t group
, dispatch_time_t timeout
);
167 * @function dispatch_group_notify
170 * Schedule a block to be submitted to a queue when all the blocks associated
171 * with a group have completed.
174 * This function schedules a notification block to be submitted to the specified
175 * queue once all blocks associated with the dispatch group have completed.
177 * If no blocks are associated with the dispatch group (i.e. the group is empty)
178 * then the notification block will be submitted immediately.
180 * The group will be empty at the time the notification block is submitted to
181 * the target queue. The group may either be released with dispatch_release()
182 * or reused for additional operations.
183 * See dispatch_group_async() for more information.
186 * The dispatch group to observe.
187 * The result of passing NULL in this parameter is undefined.
190 * The queue to which the supplied block will be submitted when the group
194 * The block to submit when the group completes.
197 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
198 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
200 dispatch_group_notify(dispatch_group_t group
,
201 dispatch_queue_t queue
,
202 dispatch_block_t block
);
203 #endif /* __BLOCKS__ */
206 * @function dispatch_group_notify_f
209 * Schedule a function to be submitted to a queue when all the blocks
210 * associated with a group have completed.
213 * See dispatch_group_notify() for details.
216 * The dispatch group to observe.
217 * The result of passing NULL in this parameter is undefined.
220 * The application-defined context parameter to pass to the function.
223 * The application-defined function to invoke on the target queue. The first
224 * parameter passed to this function is the context provided to
225 * dispatch_group_notify_f().
227 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
228 DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NONNULL2 DISPATCH_NONNULL4
231 dispatch_group_notify_f(dispatch_group_t group
,
232 dispatch_queue_t queue
,
233 void *_Nullable context
,
234 dispatch_function_t work
);
237 * @function dispatch_group_enter
240 * Manually indicate a block has entered the group
243 * Calling this function indicates another block has joined the group through
244 * a means other than dispatch_group_async(). Calls to this function must be
245 * balanced with dispatch_group_leave().
248 * The dispatch group to update.
249 * The result of passing NULL in this parameter is undefined.
251 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
252 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
254 dispatch_group_enter(dispatch_group_t group
);
257 * @function dispatch_group_leave
260 * Manually indicate a block in the group has completed
263 * Calling this function indicates block has completed and left the dispatch
264 * group by a means other than dispatch_group_async().
267 * The dispatch group to update.
268 * The result of passing NULL in this parameter is undefined.
270 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
271 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
273 dispatch_group_leave(dispatch_group_t group
);
277 DISPATCH_ASSUME_NONNULL_END