1 #include <pthread.h>
2 #include <stdbool.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <unistd.h>
6 #include <sys/time.h>
7 #include <mach/mach_time.h>
8 #include <pthread.h>
9
10 #include <pthread/tsd_private.h>
11 #include <pthread/qos_private.h>
12
13 #include <dispatch/dispatch.h>
14 #include <dispatch/private.h>
15 #include <darwintest.h>
16 #include <pthread/workqueue_private.h>
17
18 static mach_timebase_info_data_t timebase_info;
19
20 static uint64_t
nanos_to_abs(uint64_t nanos)21 nanos_to_abs(uint64_t nanos)
22 {
23 return nanos * timebase_info.denom / timebase_info.numer;
24 }
25
26 static void
spin_for_duration(uint32_t seconds)27 spin_for_duration(uint32_t seconds)
28 {
29 kern_return_t kr = mach_timebase_info(&timebase_info);
30 assert(kr == KERN_SUCCESS);
31
32 uint64_t duration = nanos_to_abs((uint64_t)seconds * NSEC_PER_SEC);
33 uint64_t current_time = mach_absolute_time();
34 uint64_t timeout = duration + current_time;
35
36 uint64_t spin_count = 0;
37
38 while (mach_absolute_time() < timeout) {
39 spin_count++;
40 }
41 return;
42 }
43
44 T_DECL(cooperative_workqueue_and_vfork, "rdar://74489806") {
45 dispatch_queue_t dq = dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0);
46 T_ASSERT_NE(dq, NULL, "global_queue");
47
48 dispatch_async(dq, ^{
49 /* We are a workqueue non-overcommit thread, we should be getting a
50 * quantum */
51 spin_for_duration(1);
52
53 pid_t child;
54 if ((child = vfork()) == 0) {
55 usleep(100);
56 spin_for_duration(1);
57 _exit(0);
58 }
59
60 int status;
61 waitpid(child, &status, 0);
62 T_ASSERT_EQ(status, 0, "child status");
63
64 T_END;
65 });
66
67 dispatch_main();
68 }
69
70 T_DECL(adjust_quantum_nonovercommit_to_overcommit_switch, "rdar://75084197")
71 {
72 dispatch_queue_t dq = dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0);
73 T_ASSERT_NE(dq, NULL, "global_queue");
74
75 dispatch_async(dq, ^{
76 /* We are a workqueue non-overcommit thread, we should be getting a
77 * quantum that we expire here */
78 spin_for_duration(1);
79
80 /* Should not panic when we switch to overcommit */
81 pthread_priority_t overcommit = (pthread_priority_t)_pthread_getspecific_direct(_PTHREAD_TSD_SLOT_PTHREAD_QOS_CLASS) |
82 _PTHREAD_PRIORITY_OVERCOMMIT_FLAG;
83 T_ASSERT_POSIX_ZERO(_pthread_set_properties_self(_PTHREAD_SET_SELF_QOS_FLAG, overcommit, 0), NULL);
84
85 T_END;
86 });
87
88 dispatch_main();
89 }
90
91 T_DECL(cooperative_to_overcommit_switch, "Switching from cooperative queue to another type should not panic")
92 {
93 dispatch_queue_t cooperative_dq = dispatch_get_global_queue(QOS_CLASS_UTILITY, DISPATCH_QUEUE_COOPERATIVE);
94 T_ASSERT_NE(cooperative_dq, NULL, "global_queue");
95
96 dispatch_queue_attr_t attr = dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL_WITH_AUTORELEASE_POOL, QOS_CLASS_USER_INITIATED, 0);
97 dispatch_queue_t dq = dispatch_queue_create("serial IN overcommit queue", attr);
98
99 dispatch_async(cooperative_dq, ^{
100 spin_for_duration(1);
101
102 dispatch_async_and_wait(dq, ^{
103 spin_for_duration(1);
104 });
105
106 dispatch_release(dq);
107 T_END;
108 });
109
110 dispatch_main();
111 }
112