1#include <stdio.h>
2#include <stdlib.h>
3#include <errno.h>
4#include <pthread.h>
5#include <unistd.h>
6#include <sys/time.h>
7
8
9static pthread_mutex_t m;
10
11static void *
12tf (void *data)
13{
14 int err = pthread_mutex_lock (mutex: &m);
15 if (err == EOWNERDEAD)
16 {
17 err = pthread_mutex_consistent (mutex: &m);
18 if (err)
19 {
20 puts (s: "pthread_mutex_consistent");
21 exit (1);
22 }
23 }
24 else if (err)
25 {
26 puts (s: "pthread_mutex_lock");
27 exit (1);
28 }
29 printf (format: "thread%ld got the lock.\n", (long int) data);
30 sleep (seconds: 1);
31 /* exit without unlock */
32 return NULL;
33}
34
35static int
36do_test (void)
37{
38 int err, i;
39 pthread_t t[3];
40 pthread_mutexattr_t ma;
41
42 pthread_mutexattr_init (attr: &ma);
43 err = pthread_mutexattr_setrobust (attr: &ma, robustness: PTHREAD_MUTEX_ROBUST_NP);
44 if (err)
45 {
46 puts (s: "pthread_mutexattr_setrobust");
47 return 1;
48 }
49#ifdef ENABLE_PI
50 if (pthread_mutexattr_setprotocol (attr: &ma, protocol: PTHREAD_PRIO_INHERIT) != 0)
51 {
52 puts (s: "pthread_mutexattr_setprotocol failed");
53 return 1;
54 }
55#endif
56 err = pthread_mutex_init (mutex: &m, mutexattr: &ma);
57#ifdef ENABLE_PI
58 if (err == ENOTSUP)
59 {
60 puts (s: "PI robust mutexes not supported");
61 return 0;
62 }
63#endif
64 if (err)
65 {
66 puts (s: "pthread_mutex_init");
67 return 1;
68 }
69
70 for (i = 0; i < sizeof (t) / sizeof (t[0]); i++)
71 {
72 err = pthread_create (newthread: &t[i], NULL, start_routine: tf, arg: (void *) (long int) i);
73 if (err)
74 {
75 puts (s: "pthread_create");
76 return 1;
77 }
78 }
79
80 for (i = 0; i < sizeof (t) / sizeof (t[0]); i++)
81 {
82 err = pthread_join (th: t[i], NULL);
83 if (err)
84 {
85 puts (s: "pthread_join");
86 return 1;
87 }
88 }
89 return 0;
90}
91
92#define TEST_FUNCTION do_test ()
93#include "../test-skeleton.c"
94

source code of glibc/sysdeps/pthread/tst-robust9.c