8 #include <sys/sysctl.h>
11 #include <dispatch/dispatch.h>
13 #include "../private/workqueue_private.h"
14 #include "../private/qos_private.h"
16 #include "wq_kevent.h"
18 static dispatch_semaphore_t sema
;
19 static dispatch_time_t timeout
;
21 static int do_wait(int threads
){
22 for (int i
= 0; i
< threads
; i
++){
23 int ret
= dispatch_semaphore_wait(sema
, timeout
);
25 fprintf(stderr
, "timout waiting for thread %d.\n", i
);
29 fprintf(stderr
, "\tsuccessfully signaled by %d threads.\n", threads
);
33 static void workqueue_func(pthread_priority_t priority
){
34 fprintf(stderr
, "WARNING: workqueue_func called.\n");
35 dispatch_semaphore_signal(sema
);
38 void (^cb
)(void) = NULL
;
39 static void workqueue_func_kevent(void **buf
, int *count
){
40 pthread_priority_t p
= (pthread_priority_t
)pthread_getspecific(4);
41 fprintf(stderr
, "\tthread with qos %s spawned.\n", describe_pri(p
));
47 dispatch_semaphore_signal(sema
);
50 int main(int argc
, char *argv
[]){
54 ret
= _pthread_workqueue_init_with_kevent(workqueue_func
, workqueue_func_kevent
, 0, 0);
57 sema
= dispatch_semaphore_create(0);
59 timeout
= dispatch_time(DISPATCH_TIME_NOW
, 5LL * NSEC_PER_SEC
);
61 _pthread_workqueue_set_event_manager_priority(_pthread_qos_class_encode(QOS_CLASS_UTILITY
,0,0));
63 // one constrained thread
64 requests
[0].priority
= _pthread_qos_class_encode(QOS_CLASS_USER_INTERACTIVE
, 0, 0);
65 requests
[0].count
= 1;
67 if ((ret
= do_req()) < 0) return ret
;
68 if ((ret
= do_wait(1)) < 0) return ret
;
70 // one overcommit thread
71 requests
[0].priority
= _pthread_qos_class_encode(QOS_CLASS_USER_INTERACTIVE
, 0, _PTHREAD_PRIORITY_OVERCOMMIT_FLAG
);
72 requests
[0].count
= 1;
74 if ((ret
= do_req()) < 0) return ret
;
75 if ((ret
= do_wait(1)) < 0) return ret
;
78 requests
[0].priority
= _pthread_qos_class_encode(QOS_CLASS_USER_INTERACTIVE
, 0, _PTHREAD_PRIORITY_EVENT_MANAGER_FLAG
);
79 requests
[0].count
= 1;
81 if ((ret
= do_req()) < 0) return ret
;
82 if ((ret
= do_wait(1)) < 0) return ret
;
84 // one constrained thread
85 requests
[0].priority
= _pthread_qos_class_encode(QOS_CLASS_USER_INTERACTIVE
, 0, 0);
86 requests
[0].count
= 1;
88 if ((ret
= do_req()) < 0) return ret
;
89 if ((ret
= do_wait(1)) < 0) return ret
;
91 // whole bunch of constrained threads
93 requests
[0].priority
= _pthread_qos_class_encode(QOS_CLASS_USER_INTERACTIVE
, 0, 0);
94 requests
[0].count
= 1;
98 for (int i
= 0; i
< 1000000; i
++){
103 for (int i
= 0; i
< 8; i
++)
104 if ((ret
= do_req()) < 0) return ret
;
105 if ((ret
= do_wait(8)) < 0) return ret
;