File: dispatch_group.c

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (204 lines) | stat: -rw-r--r-- 5,121 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
 * Copyright (c) 2008-2011 Apple Inc. All rights reserved.
 *
 * @APPLE_APACHE_LICENSE_HEADER_START@
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @APPLE_APACHE_LICENSE_HEADER_END@
 */

#include <dispatch/dispatch.h>
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
#include <unistd.h>
#endif
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#ifdef __APPLE__
#include <libkern/OSAtomic.h>
#endif
#include <math.h>
#include <bsdtests.h>
#include "dispatch_test.h"

#ifndef NSEC_PER_SEC
#define NSEC_PER_SEC 1000000000
#endif

#if TARGET_OS_EMBEDDED
#define LOOP_COUNT 50000
#else
#define LOOP_COUNT 200000
#endif

static void test_group_notify(void*);

static dispatch_group_t
create_group(size_t count, unsigned int delay)
{
	size_t i;

	dispatch_group_t group = dispatch_group_create();

	for (i = 0; i < count; ++i) {
		dispatch_queue_t queue = dispatch_queue_create(NULL, NULL);
		assert(queue);

		dispatch_group_async(group, queue, ^{
			if (delay) {
				fprintf(stderr, "sleeping...\n");
				sleep(delay);
				fprintf(stderr, "done.\n");
			}
		});

		dispatch_release(queue);
		}
	return group;
}

static void
test_group(void *ctxt __attribute__((unused)))
{
	long res;
	dispatch_group_t group;

	group = create_group(100, 0);
	test_ptr_notnull("dispatch_group_async", group);

	dispatch_group_wait(group, DISPATCH_TIME_FOREVER);

	// should be OK to re-use a group
	dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{});
	dispatch_group_wait(group, DISPATCH_TIME_FOREVER);

	dispatch_release(group);
	group = NULL;

	group = create_group(3, 7);
	test_ptr_notnull("dispatch_group_async", group);

	res = dispatch_group_wait(group, dispatch_time(DISPATCH_TIME_NOW, 5ull * NSEC_PER_SEC));
	test_long("dispatch_group_wait", !res, 0);

	// retry after timeout (this time succeed)
	res = dispatch_group_wait(group, dispatch_time(DISPATCH_TIME_NOW, 5ull * NSEC_PER_SEC));
	test_long("dispatch_group_wait", res, 0);

	dispatch_release(group);
	group = NULL;

	group = create_group(100, 0);
	test_ptr_notnull("dispatch_group_async", group);

	dispatch_group_notify(group, dispatch_get_main_queue(), ^{
		dispatch_test_current("Notification Received", dispatch_get_main_queue());
		dispatch_async_f(dispatch_get_main_queue(), NULL, test_group_notify);
	});

	dispatch_release(group);
	group = NULL;
}

static long completed;

static void
test_group_notify2(long cycle, dispatch_group_t tested)
{
	static dispatch_queue_t rq, nq;
	static dispatch_once_t once;
	dispatch_once(&once, ^{
		rq = dispatch_queue_create("release", 0);
		dispatch_suspend(rq);
		nq = dispatch_queue_create("notify", 0);
	});
	dispatch_resume(rq);

	// n=4 works great for a 4CPU Mac Pro, this might work for a wider range of
	// systems.
#if HAVE_ARC4RANDOM
	const int n = 1 + arc4random() % 8;
#else
    const int n = 1 + random() % 8;
#endif
	dispatch_group_t group = dispatch_group_create();
	dispatch_queue_t qa[n];

	dispatch_group_enter(group);
	for (int i = 0; i < n; i++) {
		char buf[48];
		sprintf(buf, "T%ld-%d", cycle, i);
		qa[i] = dispatch_queue_create(buf, 0);
	}

	__block float eh = 0;
	for (int i = 0; i < n; i++) {
		dispatch_queue_t q = qa[i];
		dispatch_group_async(group, q, ^{
			// Seems to trigger a little more reliably with some work being
			// done in this block
			assert(cycle && "cycle must be non-zero");
			eh = (float)sin(M_1_PI / cycle);
		});
	}
	dispatch_group_leave(group);

	dispatch_group_notify(group, nq, ^{
		completed = cycle;
		dispatch_group_leave(tested);
	});

	// Releasing qa's queues here seems to avoid the race, so we are arranging
	// for the current iteration's queues to be released on the next iteration.
	dispatch_sync(rq, ^{});
	dispatch_suspend(rq);
	for (int i = 0; i < n; i++) {
		dispatch_queue_t q = qa[i];
		dispatch_async(rq, ^{ dispatch_release(q); });
	}
	dispatch_release(group);
}

static void
test_group_notify(void *ctxt __attribute__((unused)))
{
	// <rdar://problem/11445820>
	dispatch_group_t tested = dispatch_group_create();
	test_ptr_notnull("dispatch_group_create", tested);
	long i;
	for (i = 1; i <= LOOP_COUNT; i++) {
		if (!(i % (LOOP_COUNT/10))) {
			fprintf(stderr, "#%ld\n", i);
		}
		dispatch_group_enter(tested);
		test_group_notify2(i, tested);
		if (dispatch_group_wait(tested, dispatch_time(DISPATCH_TIME_NOW,
				NSEC_PER_SEC))) {
			break;
		}
	}
	test_long("dispatch_group_notify", i - 1, LOOP_COUNT);
	test_stop();
}

int
main(void)
{
	dispatch_test_start("Dispatch Group");
	dispatch_async_f(dispatch_get_main_queue(), NULL, test_group);
	dispatch_main();

	return 0;
}