9 #include <sys/sysctl.h>
12 #include <dispatch/dispatch.h>
14 #include "../private/workqueue_private.h"
15 #include "../private/qos_private.h"
17 #include "wq_kevent.h"
19 static int rand_qos(){
21 case 0: return QOS_CLASS_USER_INTERACTIVE
;
22 case 1: return QOS_CLASS_USER_INITIATED
;
23 case 2: return QOS_CLASS_DEFAULT
;
24 case 3: return QOS_CLASS_UTILITY
;
25 case 4: return QOS_CLASS_BACKGROUND
;
27 return QOS_CLASS_UNSPECIFIED
;
30 static void workqueue_func(pthread_priority_t priority
){
31 fprintf(stderr
, "WARNING: workqueue_func called.\n");
34 static void workqueue_func_kevent(void **buf
, int *count
){
35 pthread_priority_t p
= (pthread_priority_t
)pthread_getspecific(4);
36 fprintf(stderr
, "\tthread with qos %s spawned (count: %d).\n", describe_pri(p
), *count
);
38 //struct timeval start, stop;
39 //gettimeofday(&start, NULL);
41 for (int i
= 0; i
< (rand() % 10000) * 1000 + 50000; i
++){
45 //gettimeofday(&stop, NULL);
46 //fprintf(stderr, "\tthread exited %ld usec later\n", stop.tv_usec - start.tv_usec + (stop.tv_sec - start.tv_sec) * 1000000);
49 int main(int argc
, char *argv
[]){
52 ret
= _pthread_workqueue_init_with_kevent(workqueue_func
, workqueue_func_kevent
, 0, 0);
56 while (iteration
++ < 1000){
57 switch (iteration
% 5){
60 bzero(requests
, sizeof(requests
));
61 requests
[0].priority
= _pthread_qos_class_encode(rand_qos(), 0, _PTHREAD_PRIORITY_EVENT_MANAGER_FLAG
);
62 requests
[0].count
= 1;
64 if ((ret
= do_req()) < 0) return ret
;
68 // one constrained thread
69 bzero(requests
, sizeof(requests
));
70 requests
[0].priority
= _pthread_qos_class_encode(rand_qos(), 0, 0);
71 requests
[0].count
= rand() % 2;
72 if (requests
[0].count
> 0 && (ret
= do_req()) < 0) return ret
;
77 bzero(requests
, sizeof(requests
));
78 requests
[0].priority
= _pthread_qos_class_encode(rand_qos(), 0, _PTHREAD_PRIORITY_EVENT_MANAGER_FLAG
);
79 requests
[0].count
= 1;
81 if ((ret
= do_req()) < 0) return ret
;
85 // one overcommit thread
86 bzero(requests
, sizeof(requests
));
87 requests
[0].priority
= _pthread_qos_class_encode(rand_qos(), 0, _PTHREAD_PRIORITY_OVERCOMMIT_FLAG
);
88 requests
[0].count
= rand() % 2;
90 if (requests
[0].count
> 0 && (ret
= do_req()) < 0) return ret
;
94 // varied constrained threads
95 bzero(requests
, sizeof(requests
));
96 requests
[0].priority
= _pthread_qos_class_encode(QOS_CLASS_USER_INTERACTIVE
, 0, 0);
97 requests
[0].count
= rand() % 4;
98 requests
[1].priority
= _pthread_qos_class_encode(QOS_CLASS_USER_INITIATED
, 0, 0);
99 requests
[1].count
= rand() % 4;
100 requests
[2].priority
= _pthread_qos_class_encode(QOS_CLASS_UTILITY
, 0, 0);
101 requests
[2].count
= rand() % 4;
102 requests
[3].priority
= _pthread_qos_class_encode(QOS_CLASS_BACKGROUND
, 0, 0);
103 requests
[3].count
= rand() % 4;
104 if ((requests
[0].count
+ requests
[1].count
+ requests
[2].count
+ requests
[3].count
) > 0 && (ret
= do_req()) < 0) return ret
;
107 usleep(rand() % 100000);