2 #include <dispatch/dispatch.h>
3 #include <dispatch/queue_private.h>
7 #include <TargetConditionals.h>
9 #include "dispatch_test.h"
16 #if TARGET_OS_EMBEDDED
17 #define LOOP_COUNT 2000000
19 #define LOOP_COUNT 100000000
22 char *labels
[PRIORITIES
] = { "LOW", "DEFAULT", "HIGH" };
23 int priorities
[PRIORITIES
] = { DISPATCH_QUEUE_PRIORITY_LOW
, DISPATCH_QUEUE_PRIORITY_DEFAULT
, DISPATCH_QUEUE_PRIORITY_HIGH
};
30 #define ITERATIONS (size_t)(PRIORITIES * BLOCKS * 0.50)
31 size_t iterations
= ITERATIONS
;
35 size_t maxcount
= BLOCKS
;
36 size_t sc
[PRIORITIES
];
41 for (y
= 0; y
< PRIORITIES
; ++y
) {
42 sc
[y
] = counts
[y
].count
;
45 for (y
= 0; y
< PRIORITIES
; ++y
) {
46 printf("%s: %ld\n", labels
[y
], sc
[y
]);
49 double fraction
= (double)sc
[y
] / (double)maxcount
;
50 double value
= fraction
* (double)80;
51 for (x
= 0; x
< 80; ++x
) {
52 printf("%s", (value
> x
) ? "*" : " ");
57 test_long("blocks completed", total
, ITERATIONS
);
58 test_long_less_than("high priority precedence", (long)sc
[0], (long)sc
[2]);
62 cpubusy(void* context
)
64 size_t *count
= context
;
68 for (idx
= 0; idx
< LOOP_COUNT
; ++idx
) {
72 if ((iterdone
= __sync_sub_and_fetch(&iterations
, 1)) == 0) {
73 __sync_add_and_fetch(&done
, 1);
74 __sync_add_and_fetch(count
, 1);
78 } else if (iterdone
> 0) {
79 __sync_add_and_fetch(count
, 1);
84 submit_work(dispatch_queue_t queue
, void* context
)
88 for (i
= 0; i
< BLOCKS
; ++i
) {
89 dispatch_async_f(queue
, context
, cpubusy
);
92 #if USE_SET_TARGET_QUEUE
93 dispatch_release(queue
);
98 main(int argc
__attribute__((unused
)), char* argv
[] __attribute__((unused
)))
100 dispatch_queue_t q
[PRIORITIES
];
103 #if USE_SET_TARGET_QUEUE
104 test_start("Dispatch Priority (Set Target Queue)");
105 for(i
= 0; i
< PRIORITIES
; i
++) {
106 q
[i
] = dispatch_queue_create(labels
[i
], NULL
);
107 test_ptr_notnull("q[i]", q
[i
]);
109 dispatch_set_target_queue(q
[i
], dispatch_get_global_queue(priorities
[i
], 0));
110 dispatch_queue_set_width(q
[i
], DISPATCH_QUEUE_WIDTH_MAX_LOGICAL_CPUS
);
113 test_start("Dispatch Priority");
114 for(i
= 0; i
< PRIORITIES
; i
++) {
115 q
[i
] = dispatch_get_global_queue(priorities
[i
], 0);
119 for(i
= 0; i
< PRIORITIES
; i
++) {
120 submit_work(q
[i
], &counts
[i
].count
);