8 #include <sys/sysctl.h>
11 #include <dispatch/dispatch.h>
13 #include "../private/pthread/workqueue_private.h"
14 #include "../private/pthread/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
);
62 requests
[0].priority
= _pthread_qos_class_encode(QOS_CLASS_UNSPECIFIED
, 0, _PTHREAD_PRIORITY_EVENT_MANAGER_FLAG
);
63 requests
[0].count
= 1;
65 if ((ret
= do_req()) < 0) return ret
;
66 if ((ret
= do_wait(1)) < 0) return ret
;
68 fprintf(stderr
, "event_manager_priority -> USER_INITIATED\n");
69 _pthread_workqueue_set_event_manager_priority(_pthread_qos_class_encode(QOS_CLASS_USER_INITIATED
, 0, 0));
71 if ((ret
= do_req()) < 0) return ret
;
72 if ((ret
= do_wait(1)) < 0) return ret
;
74 fprintf(stderr
, "event_manager_priority -> UTILITY\n");
75 _pthread_workqueue_set_event_manager_priority(_pthread_qos_class_encode(QOS_CLASS_UTILITY
, 0, 0));
77 if ((ret
= do_req()) < 0) return ret
;
78 if ((ret
= do_wait(1)) < 0) return ret
;
80 fprintf(stderr
, "event_manager_priority -> 60\n");
81 _pthread_workqueue_set_event_manager_priority(_PTHREAD_PRIORITY_SCHED_PRI_FLAG
| 60);;
83 cb
= ^(void){sleep(2);};
85 if ((ret
= do_req()) < 0) return ret
;
86 if ((ret
= do_wait(1)) < 0) return ret
;
88 requests
[0].priority
= _pthread_qos_class_encode(QOS_CLASS_USER_INITIATED
, 0, 0);
89 requests
[0].count
= 1;
91 if ((ret
= do_req()) < 0) return ret
;
92 if ((ret
= do_wait(1)) < 0) return ret
;