1 .\" Copyright (c) 2017, Yubin Ruan <ablacktshirt@gmail.com>
2 .\" and Copyright (c) 2017, Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .TH PTHREAD_MUTEXATTR_SETROBUST 3 2021-03-22 "Linux" "Linux Programmer's Manual"
8 pthread_mutexattr_getrobust, pthread_mutexattr_setrobust
9 \- get and set the robustness attribute of a mutex attributes object
12 .RI ( libpthread ", " \-lpthread )
15 .B #include <pthread.h>
17 .BI "int pthread_mutexattr_getrobust(const pthread_mutexattr_t *" attr ,
18 .BI " int *" robustness ");"
19 .BI "int pthread_mutexattr_setrobust(pthread_mutexattr_t *" attr ,
20 .BI " int " robustness ");"
24 Feature Test Macro Requirements for glibc (see
25 .BR feature_test_macros (7)):
28 .BR pthread_mutexattr_getrobust (),
29 .BR pthread_mutexattr_setrobust ():
31 _POSIX_C_SOURCE >= 200809L
33 .\" But see https://sourceware.org/bugzilla/show_bug.cgi?id=22125
37 .BR pthread_mutexattr_getrobust ()
38 function places the value of the robustness attribute of
39 the mutex attributes object referred to by
44 .BR pthread_mutexattr_setrobust ()
45 function sets the value of the robustness attribute of
46 the mutex attributes object referred to by
48 to the value specified in
51 The robustness attribute specifies the behavior of the mutex when
52 the owning thread dies without unlocking the mutex.
53 The following values are valid for
56 .B PTHREAD_MUTEX_STALLED
57 This is the default value for a mutex attributes object.
58 If a mutex is initialized with the
59 .B PTHREAD_MUTEX_STALLED
60 attribute and its owner dies without unlocking it,
61 the mutex remains locked afterwards and any future attempts to call
62 .BR pthread_mutex_lock (3)
63 on the mutex will block indefinitely.
65 .B PTHREAD_MUTEX_ROBUST
66 If a mutex is initialized with the
67 .B PTHREAD_MUTEX_ROBUST
68 attribute and its owner dies without unlocking it,
69 any future attempts to call
70 .BR pthread_mutex_lock (3)
71 on this mutex will succeed and return
73 to indicate that the original owner no longer exists and the mutex is in
74 an inconsistent state.
77 is returned, the next owner should call
78 .BR pthread_mutex_consistent (3)
79 on the acquired mutex to make it consistent again before using it any further.
81 If the next owner unlocks the mutex using
82 .BR pthread_mutex_unlock (3)
83 before making it consistent, the mutex will be permanently unusable and any
84 subsequent attempts to lock it using
85 .BR pthread_mutex_lock (3)
86 will fail with the error
88 The only permitted operation on such a mutex is
89 .BR pthread_mutex_destroy (3).
91 If the next owner terminates before calling
92 .BR pthread_mutex_consistent (3),
94 .BR pthread_mutex_lock (3)
95 operations on this mutex will still return
101 .BR pthread_mutexattr_getrobust ()
103 .BR pthread_mutexattr_setrobust ()
104 should refer to a mutex attributes object that was initialized by
105 .BR pthread_mutexattr_init (3),
106 otherwise the behavior is undefined.
108 On success, these functions return 0.
109 On error, they return a positive error number.
111 In the glibc implementation,
112 .BR pthread_mutexattr_getrobust ()
118 .B PTHREAD_MUTEX_STALLED
120 .B PTHREAD_MUTEX_ROBUST
122 .BR pthread_mutexattr_setrobust ().
124 .BR pthread_mutexattr_getrobust ()
126 .BR pthread_mutexattr_setrobust ()
127 were added to glibc in version 2.12.
131 In the Linux implementation,
132 when using process-shared robust mutexes, a waiting thread also receives the
134 notification if the owner of a robust mutex performs an
136 without first unlocking the mutex.
137 POSIX.1 does not specify this detail,
138 but the same behavior also occurs in at least some
139 .\" E.g., Solaris, according to its manual page
140 other implementations.
142 Before the addition of
143 .BR pthread_mutexattr_getrobust ()
145 .BR pthread_mutexattr_setrobust ()
147 glibc defined the following equivalent nonstandard functions if
152 .BI "int pthread_mutexattr_getrobust_np(const pthread_mutexattr_t *" attr ,
153 .BI " int *" robustness ");"
154 .BI "int pthread_mutexattr_setrobust_np(const pthread_mutexattr_t *" attr ,
155 .BI " int " robustness ");"
158 Correspondingly, the constants
159 .B PTHREAD_MUTEX_STALLED_NP
161 .B PTHREAD_MUTEX_ROBUST_NP
164 These GNU-specific APIs, which first appeared in glibc 2.4,
165 are nowadays obsolete and should not be used in new programs;
166 since glibc 2.34 these APIs are marked as deprecated.
168 The program below demonstrates the use of the robustness attribute of a
169 mutex attributes object.
170 In this program, a thread holding the mutex
171 dies prematurely without unlocking the mutex.
172 The main thread subsequently acquires the mutex
173 successfully and gets the error
175 after which it makes the mutex consistent.
177 The following shell session shows what we see when running this program:
182 [original owner] Setting lock...
183 [original owner] Locked. Now exiting without unlocking.
184 [main] Attempting to lock the robust mutex.
185 [main] pthread_mutex_lock() returned EOWNERDEAD
186 [main] Now make the mutex consistent
187 [main] Mutex is now consistent; unlocking
198 #define handle_error_en(en, msg) \e
199 do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
201 static pthread_mutex_t mtx;
204 original_owner_thread(void *ptr)
206 printf("[original owner] Setting lock...\en");
207 pthread_mutex_lock(&mtx);
208 printf("[original owner] Locked. Now exiting without unlocking.\en");
213 main(int argc, char *argv[])
216 pthread_mutexattr_t attr;
219 pthread_mutexattr_init(&attr);
221 pthread_mutexattr_setrobust(&attr, PTHREAD_MUTEX_ROBUST);
223 pthread_mutex_init(&mtx, &attr);
225 pthread_create(&thr, NULL, original_owner_thread, NULL);
229 /* "original_owner_thread" should have exited by now. */
231 printf("[main] Attempting to lock the robust mutex.\en");
232 s = pthread_mutex_lock(&mtx);
233 if (s == EOWNERDEAD) {
234 printf("[main] pthread_mutex_lock() returned EOWNERDEAD\en");
235 printf("[main] Now make the mutex consistent\en");
236 s = pthread_mutex_consistent(&mtx);
238 handle_error_en(s, "pthread_mutex_consistent");
239 printf("[main] Mutex is now consistent; unlocking\en");
240 s = pthread_mutex_unlock(&mtx);
242 handle_error_en(s, "pthread_mutex_unlock");
246 printf("[main] pthread_mutex_lock() unexpectedly succeeded\en");
249 printf("[main] pthread_mutex_lock() unexpectedly failed\en");
250 handle_error_en(s, "pthread_mutex_lock");
257 .BR get_robust_list (2),
258 .BR set_robust_list (2),
259 .BR pthread_mutex_consistent (3),
260 .BR pthread_mutex_init (3),
261 .BR pthread_mutex_lock (3),