]> git.saurik.com Git - apple/libpthread.git/blame - tests/wq_event_manager.c
libpthread-218.51.1.tar.gz
[apple/libpthread.git] / tests / wq_event_manager.c
CommitLineData
964d3577
A
1#include <assert.h>
2#include <errno.h>
3#include <stdio.h>
4#include <stdlib.h>
5#include <string.h>
6#include <unistd.h>
7#include <sys/types.h>
8#include <sys/sysctl.h>
9#include <sys/qos.h>
10
11#include <dispatch/dispatch.h>
12
13#include "../private/workqueue_private.h"
14#include "../private/qos_private.h"
15
16#include "wq_kevent.h"
17
18static dispatch_semaphore_t sema;
19static dispatch_time_t timeout;
20
21static int do_wait(int threads){
22 for (int i = 0; i < threads; i++){
23 int ret = dispatch_semaphore_wait(sema, timeout);
24 if (ret){
25 fprintf(stderr, "timout waiting for thread %d.\n", i);
26 return 1;
27 }
28 }
29 fprintf(stderr, "\tsuccessfully signaled by %d threads.\n", threads);
30 return 0;
31}
32
33static void workqueue_func(pthread_priority_t priority){
34 fprintf(stderr, "WARNING: workqueue_func called.\n");
35 dispatch_semaphore_signal(sema);
36}
37
38void (^cb)(void) = NULL;
39static 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));
42
43 if (cb){
44 cb();
45 }
46
47 dispatch_semaphore_signal(sema);
48}
49
50int main(int argc, char *argv[]){
51 int ret = 0;
52 int exit_status = 0;
53
54 ret = _pthread_workqueue_init_with_kevent(workqueue_func, workqueue_func_kevent, 0, 0);
55 assert(ret == 0);
56
57 sema = dispatch_semaphore_create(0);
58 assert(sema != NULL);
59 timeout = dispatch_time(DISPATCH_TIME_NOW, 5LL * NSEC_PER_SEC);
60
61 // one event manager
62 requests[0].priority = _pthread_qos_class_encode(QOS_CLASS_UNSPECIFIED, 0, _PTHREAD_PRIORITY_EVENT_MANAGER_FLAG);
63 requests[0].count = 1;
64
65 if ((ret = do_req()) < 0) return ret;
66 if ((ret = do_wait(1)) < 0) return ret;
67
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));
70
71 if ((ret = do_req()) < 0) return ret;
72 if ((ret = do_wait(1)) < 0) return ret;
73
74 fprintf(stderr, "event_manager_priority -> UTILITY\n");
75 _pthread_workqueue_set_event_manager_priority(_pthread_qos_class_encode(QOS_CLASS_UTILITY, 0, 0));
76
77 if ((ret = do_req()) < 0) return ret;
78 if ((ret = do_wait(1)) < 0) return ret;
79
80 fprintf(stderr, "event_manager_priority -> 60\n");
81 _pthread_workqueue_set_event_manager_priority(_PTHREAD_PRIORITY_SCHED_PRI_FLAG | 60);;
82
83 cb = ^(void){sleep(2);};
84
85 if ((ret = do_req()) < 0) return ret;
86 if ((ret = do_wait(1)) < 0) return ret;
87
88 requests[0].priority = _pthread_qos_class_encode(QOS_CLASS_USER_INITIATED, 0, 0);
89 requests[0].count = 1;
90
91 if ((ret = do_req()) < 0) return ret;
92 if ((ret = do_wait(1)) < 0) return ret;
93
94 return 0;
95}