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
30 * @typedef dispatch_group_t
32 * A group of blocks submitted to queues for asynchronous invocation.
34 DISPATCH_DECL(dispatch_group
);
39 * @function dispatch_group_create
42 * Creates new group with which blocks may be associated.
45 * This function creates a new group with which blocks may be associated.
46 * The dispatch group may be used to wait for the completion of the blocks it
47 * references. The group object memory is freed with dispatch_release().
50 * The newly created group, or NULL on failure.
52 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
53 DISPATCH_EXPORT DISPATCH_MALLOC DISPATCH_RETURNS_RETAINED DISPATCH_WARN_RESULT
56 dispatch_group_create(void);
59 * @function dispatch_group_async
62 * Submits a block to a dispatch queue and associates the block with the given
66 * Submits a block to a dispatch queue and associates the block with the given
67 * dispatch group. The dispatch group may be used to wait for the completion
68 * of the blocks it references.
71 * A dispatch group to associate with the submitted block.
72 * The result of passing NULL in this parameter is undefined.
75 * The dispatch queue to which the block will be submitted for asynchronous
79 * The block to perform asynchronously.
82 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
83 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
85 dispatch_group_async(dispatch_group_t group
,
86 dispatch_queue_t queue
,
87 dispatch_block_t block
);
88 #endif /* __BLOCKS__ */
91 * @function dispatch_group_async_f
94 * Submits a function to a dispatch queue and associates the block with the
95 * given dispatch group.
98 * See dispatch_group_async() for details.
101 * A dispatch group to associate with the submitted function.
102 * The result of passing NULL in this parameter is undefined.
105 * The dispatch queue to which the function will be submitted for asynchronous
109 * The application-defined context parameter to pass to the function.
112 * The application-defined function to invoke on the target queue. The first
113 * parameter passed to this function is the context provided to
114 * dispatch_group_async_f().
116 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
117 DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NONNULL2 DISPATCH_NONNULL4
120 dispatch_group_async_f(dispatch_group_t group
,
121 dispatch_queue_t queue
,
123 dispatch_function_t work
);
126 * @function dispatch_group_wait
129 * Wait synchronously until all the blocks associated with a group have
130 * completed or until the specified timeout has elapsed.
133 * This function waits for the completion of the blocks associated with the
134 * given dispatch group, and returns after all blocks have completed or when
135 * the specified timeout has elapsed. When a timeout occurs, the group is
136 * restored to its original state.
138 * This function will return immediately if there are no blocks associated
139 * with the dispatch group (i.e. the group is empty).
141 * The result of calling this function from multiple threads simultaneously
142 * with the same dispatch group is undefined.
144 * After the successful return of this function, the dispatch group is empty.
145 * It may either be released with dispatch_release() or re-used for additional
146 * blocks. See dispatch_group_async() for more information.
149 * The dispatch group to wait on.
150 * The result of passing NULL in this parameter is undefined.
153 * When to timeout (see dispatch_time). As a convenience, there are the
154 * DISPATCH_TIME_NOW and DISPATCH_TIME_FOREVER constants.
157 * Returns zero on success (all blocks associated with the group completed
158 * within the specified timeout) or non-zero on error (i.e. timed out).
160 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
161 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
163 dispatch_group_wait(dispatch_group_t group
, dispatch_time_t timeout
);
166 * @function dispatch_group_notify
169 * Schedule a block to be submitted to a queue when all the blocks associated
170 * with a group have completed.
173 * This function schedules a notification block to be submitted to the specified
174 * queue once all blocks associated with the dispatch group have completed.
176 * If no blocks are associated with the dispatch group (i.e. the group is empty)
177 * then the notification block will be submitted immediately.
179 * The group will be empty at the time the notification block is submitted to
180 * the target queue. The group may either be released with dispatch_release()
181 * or reused for additional operations.
182 * See dispatch_group_async() for more information.
185 * The dispatch group to observe.
186 * The result of passing NULL in this parameter is undefined.
189 * The queue to which the supplied block will be submitted when the group
193 * The block to submit when the group completes.
196 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
197 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
199 dispatch_group_notify(dispatch_group_t group
,
200 dispatch_queue_t queue
,
201 dispatch_block_t block
);
202 #endif /* __BLOCKS__ */
205 * @function dispatch_group_notify_f
208 * Schedule a function to be submitted to a queue when all the blocks
209 * associated with a group have completed.
212 * See dispatch_group_notify() for details.
215 * The dispatch group to observe.
216 * The result of passing NULL in this parameter is undefined.
219 * The application-defined context parameter to pass to the function.
222 * The application-defined function to invoke on the target queue. The first
223 * parameter passed to this function is the context provided to
224 * dispatch_group_notify_f().
226 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
227 DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NONNULL2 DISPATCH_NONNULL4
230 dispatch_group_notify_f(dispatch_group_t group
,
231 dispatch_queue_t queue
,
233 dispatch_function_t work
);
236 * @function dispatch_group_enter
239 * Manually indicate a block has entered the group
242 * Calling this function indicates another block has joined the group through
243 * a means other than dispatch_group_async(). Calls to this function must be
244 * balanced with dispatch_group_leave().
247 * The dispatch group to update.
248 * The result of passing NULL in this parameter is undefined.
250 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
251 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
253 dispatch_group_enter(dispatch_group_t group
);
256 * @function dispatch_group_leave
259 * Manually indicate a block in the group has completed
262 * Calling this function indicates block has completed and left the dispatch
263 * groupJ by a means other than dispatch_group_async().
266 * The dispatch group to update.
267 * The result of passing NULL in this parameter is undefined.
269 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
270 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
272 dispatch_group_leave(dispatch_group_t group
);