]> git.saurik.com Git - apple/libdispatch.git/blob - testing/dispatch_readsync.c
libdispatch-84.5.3.tar.gz
[apple/libdispatch.git] / testing / dispatch_readsync.c
1 #include <dispatch/dispatch.h>
2 #include <pthread.h>
3 #include <stdio.h>
4 #include <stdint.h>
5 #include <limits.h>
6 #include <assert.h>
7
8 #include "dispatch_test.h"
9
10 #define LAPS 10000
11 #define INTERVAL 100
12
13 static size_t r_count = LAPS;
14 static size_t w_count = LAPS / INTERVAL;
15
16 static void
17 writer(void *ctxt __attribute__((unused)))
18 {
19 if (--w_count == 0) {
20 if (r_count == 0) {
21 test_stop();
22 }
23 }
24 }
25
26 static void
27 reader(void *ctxt __attribute__((unused)))
28 {
29 if (__sync_sub_and_fetch(&r_count, 1) == 0) {
30 if (r_count == 0) {
31 test_stop();
32 }
33 }
34 }
35
36 int
37 main(void)
38 {
39 dispatch_queue_t dq;
40
41 test_start("Dispatch Reader/Writer Queues");
42
43 dq = dispatch_queue_create("com.apple.libdispatch.test_readsync", NULL);
44 assert(dq);
45
46 dispatch_queue_set_width(dq, LONG_MAX);
47
48 dispatch_apply(LAPS, dispatch_get_concurrent_queue(0), ^(size_t idx) {
49 dispatch_sync_f(dq, NULL, reader);
50
51 if (idx % INTERVAL) {
52 dispatch_barrier_async_f(dq, NULL, writer);
53 }
54 });
55
56 dispatch_release(dq);
57
58 dispatch_main();
59 }