]> git.saurik.com Git - apple/libdispatch.git/blob - src/apply.c
libdispatch-703.30.5.tar.gz
[apple/libdispatch.git] / src / apply.c
1 /*
2 * Copyright (c) 2008-2013 Apple Inc. All rights reserved.
3 *
4 * @APPLE_APACHE_LICENSE_HEADER_START@
5 *
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
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
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.
17 *
18 * @APPLE_APACHE_LICENSE_HEADER_END@
19 */
20
21 #include "internal.h"
22
23 typedef void (*dispatch_apply_function_t)(void *, size_t);
24 static char const * const _dispatch_apply_key = "apply";
25
26 #define DISPATCH_APPLY_INVOKE_REDIRECT 0x1
27 #define DISPATCH_APPLY_INVOKE_WAIT 0x2
28
29 DISPATCH_ALWAYS_INLINE
30 static inline void
31 _dispatch_apply_invoke2(void *ctxt, long invoke_flags)
32 {
33 dispatch_apply_t da = (dispatch_apply_t)ctxt;
34 size_t const iter = da->da_iterations;
35 size_t idx, done = 0;
36
37 idx = os_atomic_inc_orig2o(da, da_index, acquire);
38 if (!fastpath(idx < iter)) goto out;
39
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;
44
45 _dispatch_perfmon_workitem_dec(); // this unit executes many items
46
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,
51 };
52 _dispatch_thread_context_push(&apply_ctxt);
53
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);
59 }
60 dispatch_invoke_flags_t flags = da->da_flags;
61
62 // Striding is the responsibility of the caller.
63 do {
64 dispatch_invoke_with_autoreleasepool(flags, {
65 _dispatch_client_callout2(da_ctxt, idx, func);
66 _dispatch_perfmon_workitem_inc();
67 done++;
68 idx = os_atomic_inc_orig2o(da, da_index, relaxed);
69 });
70 } while (fastpath(idx < iter));
71
72 if (invoke_flags & DISPATCH_APPLY_INVOKE_REDIRECT) {
73 _dispatch_reset_defaultpriority(old_dp);
74 _dispatch_thread_frame_pop(&dtf);
75 }
76
77 _dispatch_thread_context_pop(&apply_ctxt);
78
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);
83 }
84 out:
85 if (invoke_flags & DISPATCH_APPLY_INVOKE_WAIT) {
86 _dispatch_thread_event_wait(&da->da_event);
87 _dispatch_thread_event_destroy(&da->da_event);
88 }
89 if (os_atomic_dec2o(da, da_thr_cnt, release) == 0) {
90 #if DISPATCH_INTROSPECTION
91 _dispatch_continuation_free(da->da_dc);
92 #endif
93 _dispatch_continuation_free((dispatch_continuation_t)da);
94 }
95 }
96
97 DISPATCH_NOINLINE
98 void
99 _dispatch_apply_invoke(void *ctxt)
100 {
101 _dispatch_apply_invoke2(ctxt, 0);
102 }
103
104 DISPATCH_NOINLINE
105 static void
106 _dispatch_apply_invoke_and_wait(void *ctxt)
107 {
108 _dispatch_apply_invoke2(ctxt, DISPATCH_APPLY_INVOKE_WAIT);
109 _dispatch_perfmon_workitem_inc();
110 }
111
112 DISPATCH_NOINLINE
113 void
114 _dispatch_apply_redirect_invoke(void *ctxt)
115 {
116 _dispatch_apply_invoke2(ctxt, DISPATCH_APPLY_INVOKE_REDIRECT);
117 }
118
119 DISPATCH_ALWAYS_INLINE
120 static inline dispatch_invoke_flags_t
121 _dispatch_apply_autorelease_frequency(dispatch_queue_t dq)
122 {
123 dispatch_invoke_flags_t qaf = 0;
124
125 while (dq && !qaf) {
126 qaf = _dispatch_queue_autorelease_frequency(dq);
127 dq = slowpath(dq->do_targetq);
128 }
129 return qaf;
130 }
131
132 DISPATCH_NOINLINE
133 static void
134 _dispatch_apply_serial(void *ctxt)
135 {
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;
140 size_t idx = 0;
141
142 _dispatch_perfmon_workitem_dec(); // this unit executes many items
143 flags = _dispatch_apply_autorelease_frequency(dc->dc_data);
144 do {
145 dispatch_invoke_with_autoreleasepool(flags, {
146 _dispatch_client_callout2(dc->dc_ctxt, idx, (void*)dc->dc_func);
147 _dispatch_perfmon_workitem_inc();
148 });
149 } while (++idx < iter);
150
151 #if DISPATCH_INTROSPECTION
152 _dispatch_continuation_free(da->da_dc);
153 #endif
154 _dispatch_continuation_free((dispatch_continuation_t)da);
155 }
156
157 DISPATCH_ALWAYS_INLINE
158 static inline void
159 _dispatch_apply_f2(dispatch_queue_t dq, dispatch_apply_t da,
160 dispatch_function_t func)
161 {
162 uint32_t i = 0;
163 dispatch_continuation_t head = NULL, tail = NULL;
164
165 // The current thread does not need a continuation
166 uint32_t continuation_cnt = da->da_thr_cnt - 1;
167
168 dispatch_assert(continuation_cnt);
169
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;
173
174 _dispatch_continuation_init_f(next, dq, da, func, 0, 0, dc_flags);
175 next->do_next = head;
176 head = next;
177
178 if (!tail) {
179 tail = next;
180 }
181 }
182
183 _dispatch_thread_event_init(&da->da_event);
184
185 _dispatch_queue_push_list(dq, head, tail, head->dc_priority,
186 continuation_cnt);
187 // Call the first element directly
188 _dispatch_apply_invoke_and_wait(da);
189 }
190
191 DISPATCH_NOINLINE
192 static void
193 _dispatch_apply_redirect(void *ctxt)
194 {
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;
198
199 do {
200 uint32_t width = _dispatch_queue_try_reserve_apply_width(rq, da_width);
201
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);
206 }
207 da_width -= excess;
208 if (slowpath(!da_width)) {
209 return _dispatch_apply_serial(da);
210 }
211 da->da_thr_cnt -= excess;
212 }
213 if (!da->da_flags) {
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);
218 }
219 rq = rq->do_targetq;
220 } while (slowpath(rq->do_targetq));
221 _dispatch_apply_f2(rq, da, _dispatch_apply_redirect_invoke);
222 do {
223 _dispatch_queue_relinquish_width(dq, da_width);
224 dq = dq->do_targetq;
225 } while (slowpath(dq->do_targetq));
226 }
227
228 #define DISPATCH_APPLY_MAX UINT16_MAX // must be < sqrt(SIZE_MAX)
229
230 DISPATCH_NOINLINE
231 void
232 dispatch_apply_f(size_t iterations, dispatch_queue_t dq, void *ctxt,
233 void (*func)(void *, size_t))
234 {
235 if (slowpath(iterations == 0)) {
236 return;
237 }
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();
242
243 if (!slowpath(nested)) {
244 nested = iterations;
245 } else {
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;
249 }
250 if (iterations < thr_cnt) {
251 thr_cnt = (uint32_t)iterations;
252 }
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)) {
257 dq = dq->do_targetq;
258 }
259 }
260 struct dispatch_continuation_s dc = {
261 .dc_func = (void*)func,
262 .dc_ctxt = ctxt,
263 .dc_data = dq,
264 };
265 dispatch_apply_t da = (typeof(da))_dispatch_continuation_alloc();
266 da->da_index = 0;
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();
273 *da->da_dc = dc;
274 #else
275 da->da_dc = &dc;
276 #endif
277 da->da_flags = 0;
278
279 if (slowpath(dq->dq_width == 1) || slowpath(thr_cnt <= 1)) {
280 return dispatch_sync_f(dq, da, _dispatch_apply_serial);
281 }
282 if (slowpath(dq->do_targetq)) {
283 if (slowpath(dq == old_dq)) {
284 return dispatch_sync_f(dq, da, _dispatch_apply_serial);
285 } else {
286 return dispatch_sync_f(dq, da, _dispatch_apply_redirect);
287 }
288 }
289
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);
294 }
295
296 #ifdef __BLOCKS__
297 void
298 dispatch_apply(size_t iterations, dispatch_queue_t dq, void (^work)(size_t))
299 {
300 dispatch_apply_f(iterations, dq, work,
301 (dispatch_apply_function_t)_dispatch_Block_invoke(work));
302 }
303 #endif
304
305 #if 0
306 #ifdef __BLOCKS__
307 void
308 dispatch_stride(size_t offset, size_t stride, size_t iterations,
309 dispatch_queue_t dq, void (^work)(size_t))
310 {
311 dispatch_stride_f(offset, stride, iterations, dq, work,
312 (dispatch_apply_function_t)_dispatch_Block_invoke(work));
313 }
314 #endif
315
316 DISPATCH_NOINLINE
317 void
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))
320 {
321 if (stride == 0) {
322 stride = 1;
323 }
324 dispatch_apply(iterations / stride, queue, ^(size_t idx) {
325 size_t i = idx * stride + offset;
326 size_t stop = i + stride;
327 do {
328 func(ctxt, i++);
329 } while (i < stop);
330 });
331
332 dispatch_sync(queue, ^{
333 size_t i;
334 for (i = iterations - (iterations % stride); i < iterations; i++) {
335 func(ctxt, i + offset);
336 }
337 });
338 }
339 #endif