]> git.saurik.com Git - apple/libpthread.git/blob - tests/pthread_dependency.c
libpthread-416.60.2.tar.gz
[apple/libpthread.git] / tests / pthread_dependency.c
1 #include "darwintest_defaults.h"
2 #include <darwintest_utils.h>
3 #include <pthread/dependency_private.h>
4
5 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
6 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
7
8 static struct job {
9 pthread_dependency_t *req;
10 useconds_t usleep;
11 int done;
12 } job;
13
14 static void *
15 do_test(void *__unused arg)
16 {
17 pthread_mutex_lock(&mutex);
18
19 while (!job.done) {
20 while (job.req == 0) {
21 pthread_cond_wait(&cond, &mutex);
22 }
23 if (job.usleep) usleep(job.usleep);
24 pthread_dependency_fulfill_np(job.req, job.req);
25 job.req = NULL;
26 }
27
28 pthread_mutex_unlock(&mutex);
29 return NULL;
30 }
31
32 static void
33 post_req(pthread_dependency_t *req, useconds_t delay, bool done)
34 {
35 pthread_mutex_lock(&mutex);
36 job.req = req;
37 job.usleep = delay;
38 job.done = done;
39 pthread_cond_signal(&cond);
40 pthread_mutex_unlock(&mutex);
41 }
42
43 T_DECL(dependency, "dependency", T_META_ALL_VALID_ARCHS(YES))
44 {
45 pthread_dependency_t req;
46 pthread_t pth;
47 void *v;
48 int ret;
49
50 T_ASSERT_POSIX_ZERO(pthread_create(&pth, NULL, do_test, NULL), NULL);
51
52 T_LOG("Waiting on a pdependency that takes some time");
53
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");
58
59 T_LOG("Waiting on a pdependency that is already fulfilled");
60
61 pthread_dependency_init_np(&req, pth, NULL);
62 post_req(&req, 0, false);
63 usleep(100000);
64 v = pthread_dependency_wait_np(&req);
65 T_EXPECT_EQ(v, &req, "pthread_dependency_wait worked");
66
67 T_LOG("Waiting on a fulfilled pdependency with the other thread exiting");
68
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");
73
74 v = pthread_dependency_wait_np(&req);
75 T_EXPECT_EQ(v, &req, "pthread_dependency_wait worked");
76
77 T_END;
78 }