]> git.saurik.com Git - apple/libpthread.git/blob - tests/bsdthread_set_self.c
libpthread-301.1.6.tar.gz
[apple/libpthread.git] / tests / bsdthread_set_self.c
1 #include <assert.h>
2 #include <errno.h>
3 #include <stdatomic.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <unistd.h>
8 #include <time.h>
9 #include <sys/types.h>
10 #include <sys/sysctl.h>
11 #include <sys/qos.h>
12
13 #include <pthread.h>
14 #include <pthread/tsd_private.h>
15 #include <pthread/qos_private.h>
16 #include <pthread/workqueue_private.h>
17
18 #include <dispatch/dispatch.h>
19
20 #include "darwintest_defaults.h"
21
22 T_DECL(bsdthread_set_self_constrained_transition, "bsdthread_ctl(SET_SELF) with overcommit change",
23 T_META_ALL_VALID_ARCHS(YES))
24 {
25 dispatch_async(dispatch_get_global_queue(0, 0), ^{
26 pthread_priority_t overcommit = (pthread_priority_t)_pthread_getspecific_direct(_PTHREAD_TSD_SLOT_PTHREAD_QOS_CLASS) |
27 _PTHREAD_PRIORITY_OVERCOMMIT_FLAG;
28 pthread_priority_t constrained = overcommit & (~_PTHREAD_PRIORITY_OVERCOMMIT_FLAG);
29
30 T_ASSERT_POSIX_ZERO(_pthread_set_properties_self(_PTHREAD_SET_SELF_QOS_FLAG, overcommit, 0), NULL);
31 T_ASSERT_POSIX_ZERO(_pthread_set_properties_self(_PTHREAD_SET_SELF_QOS_FLAG, constrained, 0), NULL);
32 T_ASSERT_POSIX_ZERO(_pthread_set_properties_self(_PTHREAD_SET_SELF_QOS_FLAG, overcommit, 0), NULL);
33 T_ASSERT_POSIX_ZERO(_pthread_set_properties_self(_PTHREAD_SET_SELF_QOS_FLAG, constrained, 0), NULL);
34 T_ASSERT_POSIX_ZERO(_pthread_set_properties_self(_PTHREAD_SET_SELF_QOS_FLAG, overcommit, 0), NULL);
35 T_ASSERT_POSIX_ZERO(_pthread_set_properties_self(_PTHREAD_SET_SELF_QOS_FLAG, constrained, 0), NULL);
36
37 T_END;
38 });
39
40 dispatch_main();
41 }
42
43 T_DECL(bsdthread_set_self_constrained_threads, "bsdthread_ctl(SET_SELF) with overcommit change",
44 T_META_CHECK_LEAKS(NO), T_META_ALL_VALID_ARCHS(YES))
45 {
46 static const int THREADS = 128;
47 static atomic_int threads_started;
48 dispatch_queue_t q = dispatch_queue_create("my queue", DISPATCH_QUEUE_CONCURRENT);
49 dispatch_set_target_queue(q, dispatch_get_global_queue(0, 0));
50 for (int i = 0; i < THREADS; i++) {
51 dispatch_async(q, ^{
52 int thread_id = ++threads_started;
53 T_PASS("Thread %d started successfully", thread_id);
54 if (thread_id == THREADS){
55 T_PASS("All threads started successfully");
56 T_END;
57 }
58
59 pthread_priority_t overcommit = (pthread_priority_t)_pthread_getspecific_direct(_PTHREAD_TSD_SLOT_PTHREAD_QOS_CLASS) |
60 _PTHREAD_PRIORITY_OVERCOMMIT_FLAG;
61 T_ASSERT_POSIX_ZERO(_pthread_set_properties_self(_PTHREAD_SET_SELF_QOS_FLAG, overcommit, 0), NULL);
62
63 uint64_t t = clock_gettime_nsec_np(CLOCK_MONOTONIC_RAW);
64 while (t > clock_gettime_nsec_np(CLOCK_MONOTONIC_RAW) - (thread_id == 1 ? 30 : 60) * NSEC_PER_SEC) {
65 sleep(1);
66 }
67 if (thread_id == 1) {
68 T_FAIL("Where are my threads?");
69 T_END;
70 }
71 });
72 }
73
74 dispatch_main();
75 }