2 * Copyright (c) 2008-2009 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 #include "config/config.h"
24 #include <dispatch/dispatch.h>
25 #define __DISPATCH_INDIRECT__
26 #include "src/queue_private.h"
30 #if HAVE_TARGETCONDITIONALS_H
31 #include <TargetConditionals.h>
34 #include "dispatch_test.h"
41 #if TARGET_OS_EMBEDDED
42 #define LOOP_COUNT 2000000
44 #define LOOP_COUNT 100000000
47 char *labels[PRIORITIES] = { "LOW", "DEFAULT", "HIGH" };
48 int priorities[PRIORITIES] = { DISPATCH_QUEUE_PRIORITY_LOW, DISPATCH_QUEUE_PRIORITY_DEFAULT, DISPATCH_QUEUE_PRIORITY_HIGH };
55 #define ITERATIONS (size_t)(PRIORITIES * BLOCKS * 0.50)
56 size_t iterations = ITERATIONS;
60 size_t maxcount = BLOCKS;
61 size_t sc[PRIORITIES];
66 for (y = 0; y < PRIORITIES; ++y) {
67 sc[y] = counts[y].count;
70 for (y = 0; y < PRIORITIES; ++y) {
71 printf("%s: %ld\n", labels[y], sc[y]);
74 double fraction = (double)sc[y] / (double)maxcount;
75 double value = fraction * (double)80;
76 for (x = 0; x < 80; ++x) {
77 printf("%s", (value > x) ? "*" : " ");
82 test_long("blocks completed", total, ITERATIONS);
83 test_long_less_than("high priority precedence", (long)sc[0], (long)sc[2]);
87 cpubusy(void* context)
89 size_t *count = context;
93 for (idx = 0; idx < LOOP_COUNT; ++idx) {
97 if ((iterdone = __sync_sub_and_fetch(&iterations, 1)) == 0) {
98 __sync_add_and_fetch(&done, 1);
99 __sync_add_and_fetch(count, 1);
103 } else if (iterdone > 0) {
104 __sync_add_and_fetch(count, 1);
109 submit_work(dispatch_queue_t queue, void* context)
113 for (i = 0; i < BLOCKS; ++i) {
114 dispatch_async_f(queue, context, cpubusy);
117 #if USE_SET_TARGET_QUEUE
118 dispatch_release(queue);
123 main(int argc __attribute__((unused)), char* argv[] __attribute__((unused)))
125 dispatch_queue_t q[PRIORITIES];
128 #if USE_SET_TARGET_QUEUE
129 test_start("Dispatch Priority (Set Target Queue)");
130 for(i = 0; i < PRIORITIES; i++) {
131 q[i] = dispatch_queue_create(labels[i], NULL);
132 test_ptr_notnull("q[i]", q[i]);
134 dispatch_set_target_queue(q[i], dispatch_get_global_queue(priorities[i], 0));
135 dispatch_queue_set_width(q[i], DISPATCH_QUEUE_WIDTH_MAX_LOGICAL_CPUS);
138 test_start("Dispatch Priority");
139 for(i = 0; i < PRIORITIES; i++) {
140 q[i] = dispatch_get_global_queue(priorities[i], 0);
144 for(i = 0; i < PRIORITIES; i++) {
145 submit_work(q[i], &counts[i].count);