fix pthread-robust-detach test
[libc-test] / src / regression / pthread-robust-detach.c
1 // commit 12e1e324683a1d381b7f15dd36c99b37dd44d940 2015-04-10
2 // robust mutex should work with detached threads too
3 #include <pthread.h>
4 #include <string.h>
5 #include <errno.h>
6 #include <time.h>
7 #include "test.h"
8
9 #define TX(r,f,x) ( ((r)=(f))==x || (t_error(#f" failed: got %d \"%s\" want %d \"%s\"\n", r, strerror(r), x, strerror(x)), 0) )
10 #define T(r,f) TX(r,f,0)
11
12 static pthread_barrier_t barrier2;
13
14 static void *start_lock(void *arg)
15 {
16         pthread_mutex_lock(arg);
17         pthread_barrier_wait(&barrier2);
18         return 0;
19 }
20
21 int main(void)
22 {
23         pthread_t td;
24         int r;
25         pthread_mutexattr_t mtx_a;
26         pthread_mutex_t mtx;
27         struct timespec ts;
28
29         T(r, pthread_barrier_init(&barrier2, 0, 2));
30         T(r, pthread_mutexattr_init(&mtx_a));
31         T(r, pthread_mutexattr_setrobust(&mtx_a, PTHREAD_MUTEX_ROBUST));
32         T(r, pthread_mutexattr_setpshared(&mtx_a, PTHREAD_PROCESS_SHARED));
33         T(r, pthread_mutex_init(&mtx, &mtx_a));
34         T(r, pthread_create(&td, 0, start_lock, &mtx));
35         T(r, pthread_detach(td));
36         pthread_barrier_wait(&barrier2);
37
38         // enough time to ensure that the other thread is dead
39         clock_gettime(CLOCK_REALTIME, &ts);
40         ts.tv_nsec += 100*1000*1000;
41         if (ts.tv_nsec >= 1000*1000*1000) {
42                 ts.tv_sec++;
43                 ts.tv_nsec -= 1000*1000*1000;
44         }
45
46         TX(r, pthread_mutex_timedlock(&mtx, &ts), EOWNERDEAD);
47         return t_status;
48 }