]> git.saurik.com Git - apple/libpthread.git/blob - tests/cond.c
0fc91c2b5aa9082edc67c80f81e326caa44ec588
[apple/libpthread.git] / tests / cond.c
1 #include <assert.h>
2 #include <pthread.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <unistd.h>
7 #include <stdbool.h>
8 #include <errno.h>
9 #include <libkern/OSAtomic.h>
10
11 #include "darwintest_defaults.h"
12 #include <darwintest_utils.h>
13
14 struct context {
15 pthread_cond_t cond;
16 pthread_mutex_t mutex;
17 pthread_cond_t ready_cond;
18 long waiters;
19 long count;
20 bool ready;
21 char _padding[7];
22 };
23
24
25 static void *wait_thread(void *ptr) {
26 struct context *context = ptr;
27
28 // tell producer thread that we are ready
29 T_QUIET;
30 T_ASSERT_POSIX_ZERO(pthread_mutex_lock(&context->mutex), "pthread_mutex_lock");
31 context->ready = true;
32 T_QUIET;
33 T_ASSERT_POSIX_ZERO(pthread_cond_signal(&context->ready_cond), "pthread_cond_signal");
34
35 bool loop = true;
36 while (loop) {
37
38 if (context->count > 0) {
39 ++context->waiters;
40 T_QUIET;
41 T_ASSERT_POSIX_ZERO(pthread_cond_wait(&context->cond, &context->mutex), "[%ld] pthread_rwlock_unlock", context->count);
42 --context->waiters;
43 --context->count;
44 } else {
45 loop = false;
46 }
47
48 }
49
50 T_QUIET;
51 T_ASSERT_POSIX_ZERO(pthread_mutex_unlock(&context->mutex), "[%ld] pthread_mutex_unlock", context->count);
52
53 return NULL;
54 }
55
56 T_DECL(cond, "pthread_cond",
57 T_META_ALL_VALID_ARCHS(YES), T_META_TIMEOUT(120), T_META_CHECK_LEAKS(NO))
58 {
59 struct context context = {
60 .cond = PTHREAD_COND_INITIALIZER,
61 .mutex = PTHREAD_MUTEX_INITIALIZER,
62 .ready_cond = PTHREAD_COND_INITIALIZER,
63 .waiters = 0,
64 .count = 50000 * dt_ncpu(),
65 .ready = false,
66 };
67 int i;
68 int res;
69 int threads = 2;
70 pthread_t p[threads];
71 for (i = 0; i < threads; ++i) {
72 T_QUIET;
73 T_ASSERT_POSIX_ZERO(pthread_mutex_lock(&context.mutex), "pthread_mutex_lock");
74
75 context.ready = false;
76
77 T_QUIET;
78 T_ASSERT_POSIX_ZERO(pthread_create(&p[i], NULL, wait_thread, &context), "pthread_create");
79
80 do {
81 // mutex will be dropped and allow consumer thread to acquire
82 T_QUIET;
83 T_ASSERT_POSIX_ZERO(pthread_cond_wait(&context.ready_cond, &context.mutex), "pthread_cond_wait");
84 } while (context.ready == false);
85
86 T_QUIET;
87 T_ASSERT_POSIX_ZERO(pthread_mutex_unlock(&context.mutex), "pthread_mutex_lock");
88
89 T_LOG("Thread %d ready.", i);
90 }
91
92 T_LOG("All threads ready.");
93
94 long half = context.count / 2;
95
96 bool loop = true;
97 while (loop) {
98 T_QUIET;
99 T_ASSERT_POSIX_ZERO(pthread_mutex_lock(&context.mutex), "[%ld] pthread_mutex_lock", context.count);
100 if (context.waiters) {
101 char *str;
102 if (context.count > half) {
103 str = "pthread_cond_broadcast";
104 res = pthread_cond_broadcast(&context.cond);
105 } else {
106 str = "pthread_cond_signal";
107 res = pthread_cond_signal(&context.cond);
108 }
109 T_QUIET;
110 T_ASSERT_POSIX_ZERO(res, "[%ld] %s", context.count, str);
111 }
112 if (context.count <= 0) {
113 loop = false;
114 T_PASS("Completed stres test successfully.");
115 }
116
117 T_QUIET;
118 T_ASSERT_POSIX_ZERO(pthread_mutex_unlock(&context.mutex),
119 "[%ld] pthread_mutex_unlock", context.count);
120 }
121
122 for (i = 0; i < threads; ++i) {
123 T_ASSERT_POSIX_ZERO(pthread_join(p[i], NULL), NULL);
124 }
125 }