Skip to content

Commit 6e809ee

Browse files
committed
Add unit test for process shared conditions
1 parent 61c36c1 commit 6e809ee

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-1
lines changed

libc/thread/pthread_cond_destroy.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ errno_t pthread_cond_destroy(pthread_cond_t *cond) {
3232

3333
// check if there's active waiters
3434
#if PTHREAD_USE_NSYNC
35-
if (cond->_pshared) {
35+
if (!cond->_pshared) {
3636
if (((nsync_cv *)cond)->waiters)
3737
return EINVAL;
3838
} else {
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#include <pthread.h>
2+
#include <stdatomic.h>
3+
#include <stdio.h>
4+
#include <sys/mman.h>
5+
#include <sys/wait.h>
6+
#include <unistd.h>
7+
8+
int main() {
9+
int ws;
10+
FILE *f;
11+
pid_t pid;
12+
pthread_condattr_t ca;
13+
pthread_mutexattr_t ma;
14+
struct Shared {
15+
atomic_int state;
16+
pthread_cond_t cond;
17+
pthread_mutex_t lock;
18+
} *s;
19+
if (!(f = tmpfile()))
20+
return 0;
21+
if (ftruncate(fileno(f), 512))
22+
return 1;
23+
if ((s = (struct Shared *)mmap(0, 512, PROT_READ | PROT_WRITE,
24+
MAP_SHARED | MAP_ANONYMOUS, -1, 0)) ==
25+
MAP_FAILED)
26+
return 2;
27+
if (pthread_condattr_init(&ca))
28+
return 3;
29+
if (pthread_mutexattr_init(&ma))
30+
return 4;
31+
if (pthread_condattr_setpshared(&ca, PTHREAD_PROCESS_SHARED))
32+
return 5;
33+
if (pthread_mutexattr_setpshared(&ma, PTHREAD_PROCESS_SHARED))
34+
return 6;
35+
if (pthread_cond_init(&s->cond, &ca))
36+
return 7;
37+
if (pthread_mutex_init(&s->lock, &ma))
38+
return 8;
39+
if (pthread_mutexattr_destroy(&ma))
40+
return 9;
41+
if (pthread_condattr_destroy(&ca))
42+
return 10;
43+
if ((pid = fork()) == -1)
44+
return 11;
45+
if (!pid) {
46+
alarm(2);
47+
if (pthread_mutex_lock(&s->lock))
48+
return 12;
49+
s->state = 1;
50+
if (pthread_cond_wait(&s->cond, &s->lock))
51+
return 13;
52+
if (pthread_mutex_unlock(&s->lock))
53+
return 14;
54+
for (;;)
55+
if (s->state == 2)
56+
break;
57+
if (pthread_mutex_lock(&s->lock))
58+
return 15;
59+
if (pthread_cond_signal(&s->cond))
60+
return 16;
61+
if (pthread_mutex_unlock(&s->lock))
62+
return 17;
63+
_exit(0);
64+
}
65+
alarm(2);
66+
for (;;)
67+
if (s->state == 1)
68+
break;
69+
if (pthread_mutex_lock(&s->lock))
70+
return 18;
71+
if (pthread_cond_signal(&s->cond))
72+
return 19;
73+
if (pthread_mutex_unlock(&s->lock))
74+
return 20;
75+
if (pthread_mutex_lock(&s->lock))
76+
return 21;
77+
s->state = 2;
78+
if (pthread_cond_wait(&s->cond, &s->lock))
79+
return 22;
80+
if (pthread_mutex_unlock(&s->lock))
81+
return 23;
82+
if (wait(&ws) != pid)
83+
return 24;
84+
if (!WIFEXITED(ws))
85+
return 25;
86+
if (WEXITSTATUS(ws))
87+
return 26;
88+
if (pthread_mutex_destroy(&s->lock))
89+
return 27;
90+
if (pthread_cond_destroy(&s->cond))
91+
return 28;
92+
return 0;
93+
}

0 commit comments

Comments
 (0)