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"
23 #include <dispatch/dispatch.h>
24 #include "src/internal.h"
28 #if HAVE_TARGETCONDITIONALS_H
29 #include <TargetConditionals.h>
32 #include "dispatch_test.h"
34 #if TARGET_OS_EMBEDDED
42 static dispatch_queue_t queues[COUNT];
43 static size_t lap_count_down = LAPS;
44 static size_t count_down;
45 static uint64_t start;
47 static void do_test(void);
50 collect(void *context __attribute__((unused)))
60 delta = _dispatch_absolute_time() - start;
61 math = delta = _dispatch_time_mach2nano(delta);
62 math /= COUNT * COUNT * 2ul + COUNT * 2ul;
64 printf("lap: %ld\n", lap_count_down);
65 printf("count: %lu\n", COUNT);
66 printf("delta: %ju ns\n", (uintmax_t)delta);
67 printf("math: %Lf ns / lap\n", math);
69 for (i = 0; i < COUNT; i++) {
70 dispatch_release(queues[i]);
73 // our malloc could be a lot better,
74 // this result is really a malloc torture test
75 test_long_less_than("Latency" , (unsigned long)math, 1000);
77 if (--lap_count_down) {
81 // give the threads some time to settle before test_stop() runs "leaks"
82 // ...also note, this is a total cheat. dispatch_after lets this
83 // thread go idle, so dispatch cleans up the continuations cache.
84 // Doign the "old style" sleep left that stuff around and leaks
85 // took a LONG TIME to complete. Long enough that the test harness
86 // decided to kill us.
87 dispatch_after_f(dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC), dispatch_get_main_queue(), NULL, test_stop_after_delay);
93 dispatch_queue_t this_q = context;
94 size_t replies = (size_t)dispatch_get_context(this_q);
96 dispatch_set_context(this_q, (void *)--replies);
98 //printf("collect from: %s\n", dispatch_queue_get_label(dispatch_get_current_queue()));
99 dispatch_async_f(dispatch_get_main_queue(), NULL, collect);
106 dispatch_queue_t reply_q = context;
108 dispatch_async_f(reply_q, reply_q, pong);
112 start_node(void *context)
114 dispatch_queue_t this_q = context;
117 dispatch_set_context(this_q, (void *)COUNT);
119 for (i = 0; i < COUNT; i++) {
120 dispatch_async_f(queues[i], this_q, ping);
131 start = _dispatch_absolute_time();
133 for (i = 0; i < COUNT; i++) {
135 snprintf(buf, sizeof(buf), "com.example.starfish-node#%ld", i);
136 queues[i] = dispatch_queue_create(buf, NULL);
137 dispatch_suspend(queues[i]);
140 for (i = 0; i < COUNT; i++) {
141 dispatch_async_f(queues[i], queues[i], start_node);
144 for (i = 0; i < COUNT; i++) {
145 dispatch_resume(queues[i]);
152 test_start("Dispatch Starfish");