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@
23 typedef void (*dispatch_apply_function_t
)(void *, size_t);
24 static char const * const _dispatch_apply_key
= "apply";
26 #define DISPATCH_APPLY_INVOKE_REDIRECT 0x1
27 #define DISPATCH_APPLY_INVOKE_WAIT 0x2
29 DISPATCH_ALWAYS_INLINE
31 _dispatch_apply_invoke2(void *ctxt
, long invoke_flags
)
33 dispatch_apply_t da
= (dispatch_apply_t
)ctxt
;
34 size_t const iter
= da
->da_iterations
;
37 idx
= os_atomic_inc_orig2o(da
, da_index
, acquire
);
38 if (!fastpath(idx
< iter
)) goto out
;
40 // da_dc is only safe to access once the 'index lock' has been acquired
41 dispatch_apply_function_t
const func
= (void *)da
->da_dc
->dc_func
;
42 void *const da_ctxt
= da
->da_dc
->dc_ctxt
;
43 dispatch_queue_t dq
= da
->da_dc
->dc_data
;
45 _dispatch_perfmon_workitem_dec(); // this unit executes many items
47 // Handle nested dispatch_apply rdar://problem/9294578
48 dispatch_thread_context_s apply_ctxt
= {
49 .dtc_key
= _dispatch_apply_key
,
50 .dtc_apply_nesting
= da
->da_nested
,
52 _dispatch_thread_context_push(&apply_ctxt
);
54 dispatch_thread_frame_s dtf
;
55 pthread_priority_t old_dp
;
56 if (invoke_flags
& DISPATCH_APPLY_INVOKE_REDIRECT
) {
57 _dispatch_thread_frame_push(&dtf
, dq
);
58 old_dp
= _dispatch_set_defaultpriority(dq
->dq_priority
, NULL
);
60 dispatch_invoke_flags_t flags
= da
->da_flags
;
62 // Striding is the responsibility of the caller.
64 dispatch_invoke_with_autoreleasepool(flags
, {
65 _dispatch_client_callout2(da_ctxt
, idx
, func
);
66 _dispatch_perfmon_workitem_inc();
68 idx
= os_atomic_inc_orig2o(da
, da_index
, relaxed
);
70 } while (fastpath(idx
< iter
));
72 if (invoke_flags
& DISPATCH_APPLY_INVOKE_REDIRECT
) {
73 _dispatch_reset_defaultpriority(old_dp
);
74 _dispatch_thread_frame_pop(&dtf
);
77 _dispatch_thread_context_pop(&apply_ctxt
);
79 // The thread that finished the last workitem wakes up the possibly waiting
80 // thread that called dispatch_apply. They could be one and the same.
81 if (!os_atomic_sub2o(da
, da_todo
, done
, release
)) {
82 _dispatch_thread_event_signal(&da
->da_event
);
85 if (invoke_flags
& DISPATCH_APPLY_INVOKE_WAIT
) {
86 _dispatch_thread_event_wait(&da
->da_event
);
87 _dispatch_thread_event_destroy(&da
->da_event
);
89 if (os_atomic_dec2o(da
, da_thr_cnt
, release
) == 0) {
90 #if DISPATCH_INTROSPECTION
91 _dispatch_continuation_free(da
->da_dc
);
93 _dispatch_continuation_free((dispatch_continuation_t
)da
);
99 _dispatch_apply_invoke(void *ctxt
)
101 _dispatch_apply_invoke2(ctxt
, 0);
106 _dispatch_apply_invoke_and_wait(void *ctxt
)
108 _dispatch_apply_invoke2(ctxt
, DISPATCH_APPLY_INVOKE_WAIT
);
109 _dispatch_perfmon_workitem_inc();
114 _dispatch_apply_redirect_invoke(void *ctxt
)
116 _dispatch_apply_invoke2(ctxt
, DISPATCH_APPLY_INVOKE_REDIRECT
);
119 DISPATCH_ALWAYS_INLINE
120 static inline dispatch_invoke_flags_t
121 _dispatch_apply_autorelease_frequency(dispatch_queue_t dq
)
123 dispatch_invoke_flags_t qaf
= 0;
126 qaf
= _dispatch_queue_autorelease_frequency(dq
);
127 dq
= slowpath(dq
->do_targetq
);
134 _dispatch_apply_serial(void *ctxt
)
136 dispatch_apply_t da
= (dispatch_apply_t
)ctxt
;
137 dispatch_continuation_t dc
= da
->da_dc
;
138 size_t const iter
= da
->da_iterations
;
139 dispatch_invoke_flags_t flags
;
142 _dispatch_perfmon_workitem_dec(); // this unit executes many items
143 flags
= _dispatch_apply_autorelease_frequency(dc
->dc_data
);
145 dispatch_invoke_with_autoreleasepool(flags
, {
146 _dispatch_client_callout2(dc
->dc_ctxt
, idx
, (void*)dc
->dc_func
);
147 _dispatch_perfmon_workitem_inc();
149 } while (++idx
< iter
);
151 #if DISPATCH_INTROSPECTION
152 _dispatch_continuation_free(da
->da_dc
);
154 _dispatch_continuation_free((dispatch_continuation_t
)da
);
157 DISPATCH_ALWAYS_INLINE
159 _dispatch_apply_f2(dispatch_queue_t dq
, dispatch_apply_t da
,
160 dispatch_function_t func
)
163 dispatch_continuation_t head
= NULL
, tail
= NULL
;
165 // The current thread does not need a continuation
166 uint32_t continuation_cnt
= da
->da_thr_cnt
- 1;
168 dispatch_assert(continuation_cnt
);
170 for (i
= 0; i
< continuation_cnt
; i
++) {
171 dispatch_continuation_t next
= _dispatch_continuation_alloc();
172 uintptr_t dc_flags
= DISPATCH_OBJ_CONSUME_BIT
;
174 _dispatch_continuation_init_f(next
, dq
, da
, func
, 0, 0, dc_flags
);
175 next
->do_next
= head
;
183 _dispatch_thread_event_init(&da
->da_event
);
185 _dispatch_queue_push_list(dq
, head
, tail
, head
->dc_priority
,
187 // Call the first element directly
188 _dispatch_apply_invoke_and_wait(da
);
193 _dispatch_apply_redirect(void *ctxt
)
195 dispatch_apply_t da
= (dispatch_apply_t
)ctxt
;
196 uint32_t da_width
= da
->da_thr_cnt
- 1;
197 dispatch_queue_t dq
= da
->da_dc
->dc_data
, rq
= dq
, tq
;
200 uint32_t width
= _dispatch_queue_try_reserve_apply_width(rq
, da_width
);
202 if (slowpath(da_width
> width
)) {
203 uint32_t excess
= da_width
- width
;
204 for (tq
= dq
; tq
!= rq
; tq
= tq
->do_targetq
) {
205 _dispatch_queue_relinquish_width(tq
, excess
);
208 if (slowpath(!da_width
)) {
209 return _dispatch_apply_serial(da
);
211 da
->da_thr_cnt
-= excess
;
214 // find first queue in descending target queue order that has
215 // an autorelease frequency set, and use that as the frequency for
216 // this continuation.
217 da
->da_flags
= _dispatch_queue_autorelease_frequency(dq
);
220 } while (slowpath(rq
->do_targetq
));
221 _dispatch_apply_f2(rq
, da
, _dispatch_apply_redirect_invoke
);
223 _dispatch_queue_relinquish_width(dq
, da_width
);
225 } while (slowpath(dq
->do_targetq
));
228 #define DISPATCH_APPLY_MAX UINT16_MAX // must be < sqrt(SIZE_MAX)
232 dispatch_apply_f(size_t iterations
, dispatch_queue_t dq
, void *ctxt
,
233 void (*func
)(void *, size_t))
235 if (slowpath(iterations
== 0)) {
238 uint32_t thr_cnt
= dispatch_hw_config(active_cpus
);
239 dispatch_thread_context_t dtctxt
= _dispatch_thread_context_find(_dispatch_apply_key
);
240 size_t nested
= dtctxt
? dtctxt
->dtc_apply_nesting
: 0;
241 dispatch_queue_t old_dq
= _dispatch_queue_get_current();
243 if (!slowpath(nested
)) {
246 thr_cnt
= nested
< thr_cnt
? thr_cnt
/ nested
: 1;
247 nested
= nested
< DISPATCH_APPLY_MAX
&& iterations
< DISPATCH_APPLY_MAX
248 ? nested
* iterations
: DISPATCH_APPLY_MAX
;
250 if (iterations
< thr_cnt
) {
251 thr_cnt
= (uint32_t)iterations
;
253 if (slowpath(dq
== DISPATCH_APPLY_CURRENT_ROOT_QUEUE
)) {
254 dq
= old_dq
? old_dq
: _dispatch_get_root_queue(
255 _DISPATCH_QOS_CLASS_DEFAULT
, false);
256 while (slowpath(dq
->do_targetq
)) {
260 struct dispatch_continuation_s dc
= {
261 .dc_func
= (void*)func
,
265 dispatch_apply_t da
= (typeof(da
))_dispatch_continuation_alloc();
267 da
->da_todo
= iterations
;
268 da
->da_iterations
= iterations
;
269 da
->da_nested
= nested
;
270 da
->da_thr_cnt
= thr_cnt
;
271 #if DISPATCH_INTROSPECTION
272 da
->da_dc
= _dispatch_continuation_alloc();
279 if (slowpath(dq
->dq_width
== 1) || slowpath(thr_cnt
<= 1)) {
280 return dispatch_sync_f(dq
, da
, _dispatch_apply_serial
);
282 if (slowpath(dq
->do_targetq
)) {
283 if (slowpath(dq
== old_dq
)) {
284 return dispatch_sync_f(dq
, da
, _dispatch_apply_serial
);
286 return dispatch_sync_f(dq
, da
, _dispatch_apply_redirect
);
290 dispatch_thread_frame_s dtf
;
291 _dispatch_thread_frame_push(&dtf
, dq
);
292 _dispatch_apply_f2(dq
, da
, _dispatch_apply_invoke
);
293 _dispatch_thread_frame_pop(&dtf
);
298 dispatch_apply(size_t iterations
, dispatch_queue_t dq
, void (^work
)(size_t))
300 dispatch_apply_f(iterations
, dq
, work
,
301 (dispatch_apply_function_t
)_dispatch_Block_invoke(work
));
308 dispatch_stride(size_t offset
, size_t stride
, size_t iterations
,
309 dispatch_queue_t dq
, void (^work
)(size_t))
311 dispatch_stride_f(offset
, stride
, iterations
, dq
, work
,
312 (dispatch_apply_function_t
)_dispatch_Block_invoke(work
));
318 dispatch_stride_f(size_t offset
, size_t stride
, size_t iterations
,
319 dispatch_queue_t dq
, void *ctxt
, void (*func
)(void *, size_t))
324 dispatch_apply(iterations
/ stride
, queue
, ^(size_t idx
) {
325 size_t i
= idx
* stride
+ offset
;
326 size_t stop
= i
+ stride
;
332 dispatch_sync(queue
, ^{
334 for (i
= iterations
- (iterations
% stride
); i
< iterations
; i
++) {
335 func(ctxt
, i
+ offset
);