1 .\" Copyright (c) 2017, Yubin Ruan <ablacktshirt@gmail.com>
2 .\" and Copyright (c) 2017, Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
26 .TH PTHREAD_MUTEXATTR_SETROBUST 3 2019-03-06 "Linux" "Linux Programmer's Manual"
28 pthread_mutexattr_getrobust, pthread_mutexattr_setrobust
29 \- get and set the robustness attribute of a mutex attributes object
32 .B #include <pthread.h>
34 .BI "int pthread_mutexattr_getrobust(const pthread_mutexattr_t *" attr ,
35 .BI " int *" robustness ");"
36 .BI "int pthread_mutexattr_setrobust(const pthread_mutexattr_t *" attr ,
37 .BI " int " robustness ");"
40 Compile and link with \fI\-pthread\fP.
43 Feature Test Macro Requirements for glibc (see
44 .BR feature_test_macros (7)):
47 .BR pthread_mutexattr_getrobust (),
48 .BR pthread_mutexattr_setrobust ():
52 _POSIX_C_SOURCE >= 200809L
54 .\" But see https://sourceware.org/bugzilla/show_bug.cgi?id=22125
59 .BR pthread_mutexattr_getrobust ()
60 function places the value of the robustness attribute of
61 the mutex attributes object referred to by
66 .BR pthread_mutexattr_setrobust ()
67 function sets the value of the robustness attribute of
68 the mutex attributes object referred to by
70 to the value specified in
73 The robustness attribute specifies the behavior of the mutex when
74 the owning thread dies without unlocking the mutex.
75 The following values are valid for
78 .BR PTHREAD_MUTEX_STALLED
79 This is the default value for a mutex attributes object.
80 If a mutex is initialized with the
81 .BR PTHREAD_MUTEX_STALLED
82 attribute and its owner dies without unlocking it,
83 the mutex remains locked afterwards and any future attempts to call
84 .BR pthread_mutex_lock (3)
85 on the mutex will block indefinitely.
87 .B PTHREAD_MUTEX_ROBUST
88 If a mutex is initialized with the
89 .BR PTHREAD_MUTEX_ROBUST
90 attribute and its owner dies without unlocking it,
91 any future attempts to call
92 .BR pthread_mutex_lock (3)
93 on this mutex will succeed and return
95 to indicate that the original owner no longer exists and the mutex is in
96 an inconsistent state.
99 is returned, the next owner should call
100 .BR pthread_mutex_consistent (3)
101 on the acquired mutex to make it consistent again before using it any further.
103 If the next owner unlocks the mutex using
104 .BR pthread_mutex_unlock (3)
105 before making it consistent, the mutex will be permanently unusable and any
106 subsequent attempts to lock it using
107 .BR pthread_mutex_lock (3)
108 will fail with the error
109 .BR ENOTRECOVERABLE .
110 The only permitted operation on such a mutex is
111 .BR pthread_mutex_destroy (3).
113 If the next owner terminates before calling
114 .BR pthread_mutex_consistent (3),
116 .BR pthread_mutex_lock (3)
117 operations on this mutex will still return
123 .BR pthread_mutexattr_getrobust ()
125 .BR pthread_mutexattr_setrobust ()
126 should refer to a mutex attributes object that was initialized by
127 .BR pthread_mutexattr_init (3),
128 otherwise the behavior is undefined.
130 On success, these functions return 0.
131 On error, they return a positive error number.
133 In the glibc implementation,
134 .BR pthread_mutexattr_getrobust ()
140 .B PTHREAD_MUTEX_STALLED
142 .B PTHREAD_MUTEX_ROBUST
144 .BR pthread_mutexattr_setrobust ().
146 .BR pthread_mutexattr_getrobust ()
148 .BR pthread_mutexattr_setrobust ()
149 were added to glibc in version 2.12.
153 In the Linux implementation,
154 when using process-shared robust mutexes, a waiting thread also receives the
156 notification if the owner of a robust mutex performs an
158 without first unlocking the mutex.
159 POSIX.1 does not specify this detail,
160 but the same behavior also occurs in at least some
161 .\" E.g., Solaris, according to its manual page
162 other implementations.
164 Before the addition of
165 .BR pthread_mutexattr_getrobust ()
167 .BR pthread_mutexattr_setrobust ()
169 glibc defined the following equivalent nonstandard functions if
174 .BI "int pthread_mutexattr_getrobust_np(const pthread_mutexattr_t *" attr ,
175 .BI " int *" robustness ");"
176 .BI "int pthread_mutexattr_setrobust_np(const pthread_mutexattr_t *" attr ,
177 .BI " int " robustness ");"
180 Correspondingly, the constants
181 .B PTHREAD_MUTEX_STALLED_NP
183 .B PTHREAD_MUTEX_ROBUST_NP
186 These GNU-specific APIs, which first appeared in glibc 2.4,
187 are nowadays obsolete and should not be used in new programs.
190 The program below demonstrates the use of the robustness attribute of a
191 mutex attributes object.
192 In this program, a thread holding the mutex
193 dies prematurely without unlocking the mutex.
194 The main thread subsequently acquires the mutex
195 successfully and gets the error
197 after which it makes the mutex consistent.
199 The following shell session shows what we see when running this program:
204 [original owner] Setting lock...
205 [original owner] Locked. Now exiting without unlocking.
206 [main thread] Attempting to lock the robust mutex.
207 [main thread] pthread_mutex_lock() returned EOWNERDEAD
208 [main thread] Now make the mutex consistent
209 [main thread] Mutex is now consistent; unlocking
220 #define handle_error_en(en, msg) \e
221 do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
223 static pthread_mutex_t mtx;
226 original_owner_thread(void *ptr)
228 printf("[original owner] Setting lock...\en");
229 pthread_mutex_lock(&mtx);
230 printf("[original owner] Locked. Now exiting without unlocking.\en");
235 main(int argc, char *argv[])
238 pthread_mutexattr_t attr;
241 pthread_mutexattr_init(&attr);
242 /* initialize the attributes object */
243 pthread_mutexattr_setrobust(&attr, PTHREAD_MUTEX_ROBUST);
246 pthread_mutex_init(&mtx, &attr); /* initialize the mutex */
248 pthread_create(&thr, NULL, original_owner_thread, NULL);
252 /* "original_owner_thread" should have exited by now */
254 printf("[main thread] Attempting to lock the robust mutex.\en");
255 s = pthread_mutex_lock(&mtx);
256 if (s == EOWNERDEAD) {
257 printf("[main thread] pthread_mutex_lock() returned EOWNERDEAD\en");
258 printf("[main thread] Now make the mutex consistent\en");
259 s = pthread_mutex_consistent(&mtx);
261 handle_error_en(s, "pthread_mutex_consistent");
262 printf("[main thread] Mutex is now consistent; unlocking\en");
263 s = pthread_mutex_unlock(&mtx);
265 handle_error_en(s, "pthread_mutex_unlock");
269 printf("[main thread] pthread_mutex_lock() unexpectedly succeeded\en");
272 printf("[main thread] pthread_mutex_lock() unexpectedly failed\en");
273 handle_error_en(s, "pthread_mutex_lock");
280 .BR get_robust_list (2),
281 .BR set_robust_list (2),
282 .BR pthread_mutex_init (3),
283 .BR pthread_mutex_consistent (3),
284 .BR pthread_mutex_lock (3),