]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - packages/Python/lldbsuite/test/macosx/queues/main.c
Vendor import of lldb trunk r351319 (just before the release_80 branch
[FreeBSD/FreeBSD.git] / packages / Python / lldbsuite / test / macosx / queues / main.c
1 #include <stdatomic.h>
2 #include <string.h>
3 #include <unistd.h>
4 #include <dispatch/dispatch.h>
5 #include <pthread.h>
6
7 atomic_int finished_enqueueing_work = 0;
8 atomic_int thread_count = 0;
9
10 void
11 doing_the_work_1(void *in)
12 {
13     // This is only counted once because the first job in the queue
14     // starves all the others.
15     atomic_fetch_add(&thread_count, 1);
16     while (1)
17         sleep (1);
18 }
19
20 void
21 submit_work_1a(void *in)
22 {
23     dispatch_queue_t *work_performer_1 = (dispatch_queue_t*) in;
24     dispatch_async_f (*work_performer_1, NULL, doing_the_work_1);
25     dispatch_async_f (*work_performer_1, NULL, doing_the_work_1);
26 }
27
28 void
29 submit_work_1b(void *in)
30 {
31     dispatch_queue_t *work_performer_1 = (dispatch_queue_t*) in;
32     dispatch_async_f (*work_performer_1, NULL, doing_the_work_1);
33     dispatch_async_f (*work_performer_1, NULL, doing_the_work_1);
34     atomic_fetch_add(&thread_count, 1);
35     while (1)
36         sleep (1);
37 }
38
39 void
40 doing_the_work_2(void *in)
41 {
42     atomic_fetch_add(&thread_count, 1);
43     while (1)
44         sleep (1);
45 }
46
47 void
48 submit_work_2(void *in)
49 {
50     dispatch_queue_t *work_performer_2 = (dispatch_queue_t*) in;
51     int i = 0;
52     while (i++ < 5000)
53     {
54         dispatch_async_f (*work_performer_2, NULL, doing_the_work_2);
55         dispatch_async_f (*work_performer_2, NULL, doing_the_work_2);
56     }
57     atomic_fetch_add(&finished_enqueueing_work, 1);
58 }
59
60
61 void
62 doing_the_work_3(void *in)
63 {
64     // This counts four times, since the queue is marked as CONCURRENT.
65     atomic_fetch_add(&thread_count, 1);
66     while (1)
67         sleep (1);
68 }
69
70 void
71 submit_work_3(void *in)
72 {
73     dispatch_queue_t *work_performer_3 = (dispatch_queue_t*) in;
74     dispatch_async_f (*work_performer_3, NULL, doing_the_work_3);
75     dispatch_async_f (*work_performer_3, NULL, doing_the_work_3);
76     dispatch_async_f (*work_performer_3, NULL, doing_the_work_3);
77     dispatch_async_f (*work_performer_3, NULL, doing_the_work_3);
78 }
79
80
81 void
82 stopper ()
83 {
84     while (1)
85         sleep (1);
86 }
87
88
89 int main (int argc, const char **argv)
90 {
91     dispatch_queue_t work_submittor_1 = dispatch_queue_create ("com.apple.work_submittor_1", DISPATCH_QUEUE_SERIAL);
92     dispatch_queue_t work_submittor_2 = dispatch_queue_create ("com.apple.work_submittor_and_quit_2", DISPATCH_QUEUE_SERIAL);
93     dispatch_queue_t work_submittor_3 = dispatch_queue_create ("com.apple.work_submittor_3", DISPATCH_QUEUE_SERIAL);
94
95     dispatch_queue_t work_performer_1 = dispatch_queue_create ("com.apple.work_performer_1", DISPATCH_QUEUE_SERIAL);
96     dispatch_queue_t work_performer_2 = dispatch_queue_create ("com.apple.work_performer_2", DISPATCH_QUEUE_SERIAL);
97
98     dispatch_queue_t work_performer_3 = dispatch_queue_create ("com.apple.work_performer_3", DISPATCH_QUEUE_CONCURRENT);
99
100     dispatch_async_f (work_submittor_1, (void*) &work_performer_1, submit_work_1a);
101     dispatch_async_f (work_submittor_1, (void*) &work_performer_1, submit_work_1b);
102
103     dispatch_async_f (work_submittor_2, (void*) &work_performer_2, submit_work_2);
104
105     dispatch_async_f (work_submittor_3, (void*) &work_performer_3, submit_work_3);
106
107
108     // Spin up threads with each of the different libdispatch QoS values.
109     dispatch_async (dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
110             pthread_setname_np ("user initiated QoS");
111             atomic_fetch_add(&thread_count, 1);
112             while (1)
113                 sleep (10);
114       });
115     dispatch_async (dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0), ^{
116             pthread_setname_np ("user interactive QoS");
117             atomic_fetch_add(&thread_count, 1);
118             while (1)
119                 sleep (10);
120       });
121     dispatch_async (dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0), ^{
122             pthread_setname_np ("default QoS");
123             atomic_fetch_add(&thread_count, 1);
124             while (1)
125                 sleep (10);
126       });
127     dispatch_async (dispatch_get_global_queue(QOS_CLASS_UTILITY, 0), ^{
128             pthread_setname_np ("utility QoS");
129             atomic_fetch_add(&thread_count, 1);
130             while (1)
131                 sleep (10);
132       });
133     dispatch_async (dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
134             pthread_setname_np ("background QoS");
135             atomic_fetch_add(&thread_count, 1);
136             while (1)
137                 sleep (10);
138       });
139     dispatch_async (dispatch_get_global_queue(QOS_CLASS_UNSPECIFIED, 0), ^{
140             pthread_setname_np ("unspecified QoS");
141             atomic_fetch_add(&thread_count, 1);
142             while (1)
143                 sleep (10);
144       });
145
146     // Unfortunately there is no pthread_barrier on darwin.
147     while ((atomic_load(&thread_count) < 13) || (finished_enqueueing_work == 0))
148         sleep (1);
149
150     stopper ();
151 }