401 lines
13 KiB
HTML
401 lines
13 KiB
HTML
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<HTML><HEAD><TITLE>Man page of PTHREAD_MUTEXATTR_SETROBUST</TITLE>
|
|
</HEAD><BODY>
|
|
<H1>PTHREAD_MUTEXATTR_SETROBUST</H1>
|
|
Section: Linux Programmer's Manual (3)<BR>Updated: 2019-10-10<BR><A HREF="#index">Index</A>
|
|
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
|
|
|
|
<A NAME="lbAB"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
pthread_mutexattr_getrobust, pthread_mutexattr_setrobust
|
|
- get and set the robustness attribute of a mutex attributes object
|
|
<A NAME="lbAC"> </A>
|
|
<H2>SYNOPSIS</H2>
|
|
|
|
<PRE>
|
|
<B>#include <<A HREF="file:///usr/include/pthread.h">pthread.h</A>></B>
|
|
|
|
<B>int pthread_mutexattr_getrobust(const pthread_mutexattr_t *</B><I>attr</I><B>,</B>
|
|
<B> int *</B><I>robustness</I><B>);</B>
|
|
<B>int pthread_mutexattr_setrobust(const pthread_mutexattr_t *</B><I>attr</I><B>,</B>
|
|
<B> int </B><I>robustness</I><B>);</B>
|
|
</PRE>
|
|
|
|
<P>
|
|
|
|
Compile and link with <I>-pthread</I>.
|
|
<P>
|
|
|
|
|
|
Feature Test Macro Requirements for glibc (see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A></B>(7)):
|
|
|
|
|
|
<P>
|
|
|
|
<B>pthread_mutexattr_getrobust</B>(),
|
|
|
|
<B>pthread_mutexattr_setrobust</B>():
|
|
|
|
<BR>
|
|
|
|
<DL COMPACT><DT id="1"><DD>
|
|
|
|
_POSIX_C_SOURCE >= 200809L
|
|
|
|
|
|
</DL>
|
|
|
|
|
|
<A NAME="lbAD"> </A>
|
|
<H2>DESCRIPTION</H2>
|
|
|
|
The
|
|
<B>pthread_mutexattr_getrobust</B>()
|
|
|
|
function places the value of the robustness attribute of
|
|
the mutex attributes object referred to by
|
|
<I>attr</I>
|
|
|
|
in
|
|
<I>*robustness</I>.
|
|
|
|
The
|
|
<B>pthread_mutexattr_setrobust</B>()
|
|
|
|
function sets the value of the robustness attribute of
|
|
the mutex attributes object referred to by
|
|
<I>attr</I>
|
|
|
|
to the value specified in
|
|
<I>*robustness</I>.
|
|
|
|
<P>
|
|
|
|
The robustness attribute specifies the behavior of the mutex when
|
|
the owning thread dies without unlocking the mutex.
|
|
The following values are valid for
|
|
<I>robustness</I>:
|
|
|
|
<DL COMPACT>
|
|
<DT id="2"><B>PTHREAD_MUTEX_STALLED</B>
|
|
|
|
<DD>
|
|
This is the default value for a mutex attributes object.
|
|
If a mutex is initialized with the
|
|
<B>PTHREAD_MUTEX_STALLED</B>
|
|
|
|
attribute and its owner dies without unlocking it,
|
|
the mutex remains locked afterwards and any future attempts to call
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_mutex_lock">pthread_mutex_lock</A></B>(3)
|
|
|
|
on the mutex will block indefinitely.
|
|
<DT id="3"><B>PTHREAD_MUTEX_ROBUST</B>
|
|
|
|
<DD>
|
|
If a mutex is initialized with the
|
|
<B>PTHREAD_MUTEX_ROBUST</B>
|
|
|
|
attribute and its owner dies without unlocking it,
|
|
any future attempts to call
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_mutex_lock">pthread_mutex_lock</A></B>(3)
|
|
|
|
on this mutex will succeed and return
|
|
<B>EOWNERDEAD</B>
|
|
|
|
to indicate that the original owner no longer exists and the mutex is in
|
|
an inconsistent state.
|
|
Usually after
|
|
<B>EOWNERDEAD</B>
|
|
|
|
is returned, the next owner should call
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_mutex_consistent">pthread_mutex_consistent</A></B>(3)
|
|
|
|
on the acquired mutex to make it consistent again before using it any further.
|
|
<DT id="4"><DD>
|
|
If the next owner unlocks the mutex using
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_mutex_unlock">pthread_mutex_unlock</A></B>(3)
|
|
|
|
before making it consistent, the mutex will be permanently unusable and any
|
|
subsequent attempts to lock it using
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_mutex_lock">pthread_mutex_lock</A></B>(3)
|
|
|
|
will fail with the error
|
|
<B>ENOTRECOVERABLE</B>.
|
|
|
|
The only permitted operation on such a mutex is
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_mutex_destroy">pthread_mutex_destroy</A></B>(3).
|
|
|
|
<DT id="5"><DD>
|
|
If the next owner terminates before calling
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_mutex_consistent">pthread_mutex_consistent</A></B>(3),
|
|
|
|
further
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_mutex_lock">pthread_mutex_lock</A></B>(3)
|
|
|
|
operations on this mutex will still return
|
|
<B>EOWNERDEAD</B>.
|
|
|
|
</DL>
|
|
<P>
|
|
|
|
Note that the
|
|
<I>attr</I>
|
|
|
|
argument of
|
|
<B>pthread_mutexattr_getrobust</B>()
|
|
|
|
and
|
|
<B>pthread_mutexattr_setrobust</B>()
|
|
|
|
should refer to a mutex attributes object that was initialized by
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_mutexattr_init">pthread_mutexattr_init</A></B>(3),
|
|
|
|
otherwise the behavior is undefined.
|
|
<A NAME="lbAE"> </A>
|
|
<H2>RETURN VALUE</H2>
|
|
|
|
On success, these functions return 0.
|
|
On error, they return a positive error number.
|
|
<P>
|
|
|
|
In the glibc implementation,
|
|
<B>pthread_mutexattr_getrobust</B>()
|
|
|
|
always return zero.
|
|
<A NAME="lbAF"> </A>
|
|
<H2>ERRORS</H2>
|
|
|
|
<DL COMPACT>
|
|
<DT id="6"><B>EINVAL</B>
|
|
|
|
<DD>
|
|
A value other than
|
|
<B>PTHREAD_MUTEX_STALLED</B>
|
|
|
|
or
|
|
<B>PTHREAD_MUTEX_ROBUST</B>
|
|
|
|
was passed to
|
|
<B>pthread_mutexattr_setrobust</B>().
|
|
|
|
</DL>
|
|
<A NAME="lbAG"> </A>
|
|
<H2>VERSIONS</H2>
|
|
|
|
<B>pthread_mutexattr_getrobust</B>()
|
|
|
|
and
|
|
<B>pthread_mutexattr_setrobust</B>()
|
|
|
|
were added to glibc in version 2.12.
|
|
<A NAME="lbAH"> </A>
|
|
<H2>CONFORMING TO</H2>
|
|
|
|
POSIX.1-2008.
|
|
<A NAME="lbAI"> </A>
|
|
<H2>NOTES</H2>
|
|
|
|
In the Linux implementation,
|
|
when using process-shared robust mutexes, a waiting thread also receives the
|
|
<B>EOWNERDEAD</B>
|
|
|
|
notification if the owner of a robust mutex performs an
|
|
<B><A HREF="/cgi-bin/man/man2html?2+execve">execve</A></B>(2)
|
|
|
|
without first unlocking the mutex.
|
|
POSIX.1 does not specify this detail,
|
|
but the same behavior also occurs in at least some
|
|
|
|
other implementations.
|
|
<P>
|
|
|
|
Before the addition of
|
|
<B>pthread_mutexattr_getrobust</B>()
|
|
|
|
and
|
|
<B>pthread_mutexattr_setrobust</B>()
|
|
|
|
to POSIX,
|
|
glibc defined the following equivalent nonstandard functions if
|
|
<B>_GNU_SOURCE</B>
|
|
|
|
was defined:
|
|
<P>
|
|
|
|
<PRE>
|
|
<B>int pthread_mutexattr_getrobust_np(const pthread_mutexattr_t *</B><I>attr</I><B>,</B>
|
|
<B> int *</B><I>robustness</I><B>);</B>
|
|
<B>int pthread_mutexattr_setrobust_np(const pthread_mutexattr_t *</B><I>attr</I><B>,</B>
|
|
<B> int </B><I>robustness</I><B>);</B>
|
|
</PRE>
|
|
|
|
<P>
|
|
|
|
Correspondingly, the constants
|
|
<B>PTHREAD_MUTEX_STALLED_NP</B>
|
|
|
|
and
|
|
<B>PTHREAD_MUTEX_ROBUST_NP</B>
|
|
|
|
were also defined.
|
|
<P>
|
|
|
|
These GNU-specific APIs, which first appeared in glibc 2.4,
|
|
are nowadays obsolete and should not be used in new programs.
|
|
<A NAME="lbAJ"> </A>
|
|
<H2>EXAMPLE</H2>
|
|
|
|
<P>
|
|
|
|
The program below demonstrates the use of the robustness attribute of a
|
|
mutex attributes object.
|
|
In this program, a thread holding the mutex
|
|
dies prematurely without unlocking the mutex.
|
|
The main thread subsequently acquires the mutex
|
|
successfully and gets the error
|
|
<B>EOWNERDEAD</B>,
|
|
|
|
after which it makes the mutex consistent.
|
|
<P>
|
|
|
|
The following shell session shows what we see when running this program:
|
|
<P>
|
|
|
|
|
|
|
|
$ <B>./a.out</B>
|
|
[original owner] Setting lock...
|
|
[original owner] Locked. Now exiting without unlocking.
|
|
[main thread] Attempting to lock the robust mutex.
|
|
[main thread] pthread_mutex_lock() returned EOWNERDEAD
|
|
[main thread] Now make the mutex consistent
|
|
[main thread] Mutex is now consistent; unlocking
|
|
|
|
|
|
<A NAME="lbAK"> </A>
|
|
<H3>Program source</H3>
|
|
|
|
|
|
#include <<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdio.h">stdio.h</A>>
|
|
#include <<A HREF="file:///usr/include/unistd.h">unistd.h</A>>
|
|
#include <<A HREF="file:///usr/include/pthread.h">pthread.h</A>>
|
|
#include <<A HREF="file:///usr/include/errno.h">errno.h</A>>
|
|
<P>
|
|
#define handle_error_en(en, msg) \
|
|
<BR> do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
|
|
<P>
|
|
static pthread_mutex_t mtx;
|
|
<P>
|
|
static void *
|
|
original_owner_thread(void *ptr)
|
|
{
|
|
<BR> printf("[original owner] Setting lock...\n");
|
|
<BR> pthread_mutex_lock(&mtx);
|
|
<BR> printf("[original owner] Locked. Now exiting without unlocking.\n");
|
|
<BR> pthread_exit(NULL);
|
|
}
|
|
<P>
|
|
int
|
|
main(int argc, char *argv[])
|
|
{
|
|
<BR> pthread_t thr;
|
|
<BR> pthread_mutexattr_t attr;
|
|
<BR> int s;
|
|
<P>
|
|
<BR> pthread_mutexattr_init(&attr);
|
|
<BR> /* initialize the attributes object */
|
|
<BR> pthread_mutexattr_setrobust(&attr, PTHREAD_MUTEX_ROBUST);
|
|
<BR> /* set robustness */
|
|
<P>
|
|
<BR> pthread_mutex_init(&mtx, &attr); /* initialize the mutex */
|
|
<P>
|
|
<BR> pthread_create(&thr, NULL, original_owner_thread, NULL);
|
|
<P>
|
|
<BR> <A HREF="/cgi-bin/man/man2html?2+sleep">sleep</A>(2);
|
|
<P>
|
|
<BR> /* "original_owner_thread" should have exited by now */
|
|
<P>
|
|
<BR> printf("[main thread] Attempting to lock the robust mutex.\n");
|
|
<BR> s = pthread_mutex_lock(&mtx);
|
|
<BR> if (s == EOWNERDEAD) {
|
|
<BR> printf("[main thread] pthread_mutex_lock() returned EOWNERDEAD\n");
|
|
<BR> printf("[main thread] Now make the mutex consistent\n");
|
|
<BR> s = pthread_mutex_consistent(&mtx);
|
|
<BR> if (s != 0)
|
|
<BR> handle_error_en(s, "pthread_mutex_consistent");
|
|
<BR> printf("[main thread] Mutex is now consistent; unlocking\n");
|
|
<BR> s = pthread_mutex_unlock(&mtx);
|
|
<BR> if (s != 0)
|
|
<BR> handle_error_en(s, "pthread_mutex_unlock");
|
|
<P>
|
|
<BR> exit(EXIT_SUCCESS);
|
|
<BR> } else if (s == 0) {
|
|
<BR> printf("[main thread] pthread_mutex_lock() unexpectedly succeeded\n");
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> } else {
|
|
<BR> printf("[main thread] pthread_mutex_lock() unexpectedly failed\n");
|
|
<BR> handle_error_en(s, "pthread_mutex_lock");
|
|
<BR> }
|
|
}
|
|
|
|
<A NAME="lbAL"> </A>
|
|
<H2>SEE ALSO</H2>
|
|
|
|
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+get_robust_list">get_robust_list</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+set_robust_list">set_robust_list</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_mutex_consistent">pthread_mutex_consistent</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_mutex_init">pthread_mutex_init</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_mutex_lock">pthread_mutex_lock</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?7+pthreads">pthreads</A></B>(7)
|
|
|
|
<A NAME="lbAM"> </A>
|
|
<H2>COLOPHON</H2>
|
|
|
|
This page is part of release 5.05 of the Linux
|
|
<I>man-pages</I>
|
|
|
|
project.
|
|
A description of the project,
|
|
information about reporting bugs,
|
|
and the latest version of this page,
|
|
can be found at
|
|
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
|
|
<P>
|
|
|
|
<HR>
|
|
<A NAME="index"> </A><H2>Index</H2>
|
|
<DL>
|
|
<DT id="7"><A HREF="#lbAB">NAME</A><DD>
|
|
<DT id="8"><A HREF="#lbAC">SYNOPSIS</A><DD>
|
|
<DT id="9"><A HREF="#lbAD">DESCRIPTION</A><DD>
|
|
<DT id="10"><A HREF="#lbAE">RETURN VALUE</A><DD>
|
|
<DT id="11"><A HREF="#lbAF">ERRORS</A><DD>
|
|
<DT id="12"><A HREF="#lbAG">VERSIONS</A><DD>
|
|
<DT id="13"><A HREF="#lbAH">CONFORMING TO</A><DD>
|
|
<DT id="14"><A HREF="#lbAI">NOTES</A><DD>
|
|
<DT id="15"><A HREF="#lbAJ">EXAMPLE</A><DD>
|
|
<DL>
|
|
<DT id="16"><A HREF="#lbAK">Program source</A><DD>
|
|
</DL>
|
|
<DT id="17"><A HREF="#lbAL">SEE ALSO</A><DD>
|
|
<DT id="18"><A HREF="#lbAM">COLOPHON</A><DD>
|
|
</DL>
|
|
<HR>
|
|
This document was created by
|
|
<A HREF="/cgi-bin/man/man2html">man2html</A>,
|
|
using the manual pages.<BR>
|
|
Time: 00:05:53 GMT, March 31, 2021
|
|
</BODY>
|
|
</HTML>
|