1 #include "darwintest_defaults.h"
2 #include <darwintest_utils.h>
3 #include <pthread/dependency_private.h>
5 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
6 static pthread_cond_t cond
= PTHREAD_COND_INITIALIZER
;
9 pthread_dependency_t
*req
;
15 do_test(void *__unused arg
)
17 pthread_mutex_lock(&mutex
);
20 while (job
.req
== 0) {
21 pthread_cond_wait(&cond
, &mutex
);
23 if (job
.usleep
) usleep(job
.usleep
);
24 pthread_dependency_fulfill_np(job
.req
, job
.req
);
28 pthread_mutex_unlock(&mutex
);
33 post_req(pthread_dependency_t
*req
, useconds_t delay
, bool done
)
35 pthread_mutex_lock(&mutex
);
39 pthread_cond_signal(&cond
);
40 pthread_mutex_unlock(&mutex
);
43 T_DECL(dependency
, "dependency", T_META_ALL_VALID_ARCHS(YES
))
45 pthread_dependency_t req
;
50 T_ASSERT_POSIX_ZERO(pthread_create(&pth
, NULL
, do_test
, NULL
), NULL
);
52 T_LOG("Waiting on a pdependency that takes some time");
54 pthread_dependency_init_np(&req
, pth
, NULL
);
55 post_req(&req
, 100000, false);
56 v
= pthread_dependency_wait_np(&req
);
57 T_EXPECT_EQ(v
, &req
, "pthread_dependency_wait worked");
59 T_LOG("Waiting on a pdependency that is already fulfilled");
61 pthread_dependency_init_np(&req
, pth
, NULL
);
62 post_req(&req
, 0, false);
64 v
= pthread_dependency_wait_np(&req
);
65 T_EXPECT_EQ(v
, &req
, "pthread_dependency_wait worked");
67 T_LOG("Waiting on a fulfilled pdependency with the other thread exiting");
69 pthread_dependency_init_np(&req
, pth
, NULL
);
70 post_req(&req
, 0, true);
71 ret
= pthread_join(pth
, NULL
);
72 T_EXPECT_POSIX_ZERO(ret
, "pthread_join");
74 v
= pthread_dependency_wait_np(&req
);
75 T_EXPECT_EQ(v
, &req
, "pthread_dependency_wait worked");