9 #include <libkern/OSAtomic.h>
11 #include <darwintest.h>
12 #include <darwintest_utils.h>
16 pthread_mutex_t mutex
;
21 static void *wait_thread(void *ptr
) {
23 struct context
*context
= ptr
;
27 res
= pthread_mutex_lock(&context
->mutex
);
29 T_ASSERT_POSIX_ZERO(res
, "[%ld] pthread_mutex_lock", context
->count
);
32 if (context
->count
> 0) {
34 res
= pthread_cond_wait(&context
->cond
, &context
->mutex
);
36 T_ASSERT_POSIX_ZERO(res
, "[%ld] pthread_rwlock_unlock", context
->count
);
44 res
= pthread_mutex_unlock(&context
->mutex
);
46 T_ASSERT_POSIX_ZERO(res
, "[%ld] pthread_mutex_unlock", context
->count
);
53 T_DECL(cond
, "pthread_cond",
54 T_META_ALL_VALID_ARCHS(YES
))
56 struct context context
= {
57 .cond
= PTHREAD_COND_INITIALIZER
,
58 .mutex
= PTHREAD_MUTEX_INITIALIZER
,
60 .count
= 100000 * dt_ncpu(),
66 for (i
= 0; i
< threads
; ++i
) {
67 T_ASSERT_POSIX_ZERO(pthread_create(&p
[i
], NULL
, wait_thread
, &context
), NULL
);
70 long half
= context
.count
/ 2;
74 res
= pthread_mutex_lock(&context
.mutex
);
76 T_ASSERT_POSIX_ZERO(res
, "[%ld] pthread_mutex_lock", context
.count
);
78 if (context
.waiters
) {
80 if (context
.count
> half
) {
81 str
= "pthread_cond_broadcast";
82 res
= pthread_cond_broadcast(&context
.cond
);
84 str
= "pthread_cond_signal";
85 res
= pthread_cond_signal(&context
.cond
);
88 T_ASSERT_POSIX_ZERO(res
, "[%ld] %s", context
.count
, str
);
91 if (context
.count
<= 0) {
93 T_PASS("Completed stres test successfully.");
96 res
= pthread_mutex_unlock(&context
.mutex
);
98 T_ASSERT_POSIX_ZERO(res
, "[%ld] pthread_mutex_unlock", context
.count
);
102 for (i
= 0; i
< threads
; ++i
) {
103 T_ASSERT_POSIX_ZERO(pthread_join(p
[i
], NULL
), NULL
);