man-pages/man3/pthread_mutexattr_setrobust.3.html
2021-03-31 01:06:50 +01:00

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">&nbsp;</A>
<H2>NAME</H2>
pthread_mutexattr_getrobust, pthread_mutexattr_setrobust
- get and set the robustness attribute of a mutex attributes object
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/pthread.h">pthread.h</A>&gt;</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 &gt;= 200809L
</DL>
<A NAME="lbAD">&nbsp;</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">&nbsp;</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">&nbsp;</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">&nbsp;</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">&nbsp;</A>
<H2>CONFORMING TO</H2>
POSIX.1-2008.
<A NAME="lbAI">&nbsp;</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">&nbsp;</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">&nbsp;</A>
<H3>Program source</H3>
#include &lt;<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdio.h">stdio.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/unistd.h">unistd.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/pthread.h">pthread.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/errno.h">errno.h</A>&gt;
<P>
#define handle_error_en(en, msg) \
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;do&nbsp;{&nbsp;errno&nbsp;=&nbsp;en;&nbsp;perror(msg);&nbsp;exit(EXIT_FAILURE);&nbsp;}&nbsp;while&nbsp;(0)
<P>
static pthread_mutex_t mtx;
<P>
static void *
original_owner_thread(void *ptr)
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;[original&nbsp;owner]&nbsp;Setting&nbsp;lock...\n&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;pthread_mutex_lock(&amp;mtx);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;[original&nbsp;owner]&nbsp;Locked.&nbsp;Now&nbsp;exiting&nbsp;without&nbsp;unlocking.\n&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;pthread_exit(NULL);
}
<P>
int
main(int argc, char *argv[])
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;pthread_t&nbsp;thr;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;pthread_mutexattr_t&nbsp;attr;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;s;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;pthread_mutexattr_init(&amp;attr);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;initialize&nbsp;the&nbsp;attributes&nbsp;object&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;pthread_mutexattr_setrobust(&amp;attr,&nbsp;PTHREAD_MUTEX_ROBUST);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;set&nbsp;robustness&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;pthread_mutex_init(&amp;mtx,&nbsp;&amp;attr);&nbsp;&nbsp;&nbsp;/*&nbsp;initialize&nbsp;the&nbsp;mutex&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;pthread_create(&amp;thr,&nbsp;NULL,&nbsp;original_owner_thread,&nbsp;NULL);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;<A HREF="/cgi-bin/man/man2html?2+sleep">sleep</A>(2);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;&quot;original_owner_thread&quot;&nbsp;should&nbsp;have&nbsp;exited&nbsp;by&nbsp;now&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;[main&nbsp;thread]&nbsp;Attempting&nbsp;to&nbsp;lock&nbsp;the&nbsp;robust&nbsp;mutex.\n&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;s&nbsp;=&nbsp;pthread_mutex_lock(&amp;mtx);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(s&nbsp;==&nbsp;EOWNERDEAD)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;[main&nbsp;thread]&nbsp;pthread_mutex_lock()&nbsp;returned&nbsp;EOWNERDEAD\n&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;[main&nbsp;thread]&nbsp;Now&nbsp;make&nbsp;the&nbsp;mutex&nbsp;consistent\n&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s&nbsp;=&nbsp;pthread_mutex_consistent(&amp;mtx);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(s&nbsp;!=&nbsp;0)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;handle_error_en(s,&nbsp;&quot;pthread_mutex_consistent&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;[main&nbsp;thread]&nbsp;Mutex&nbsp;is&nbsp;now&nbsp;consistent;&nbsp;unlocking\n&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s&nbsp;=&nbsp;pthread_mutex_unlock(&amp;mtx);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(s&nbsp;!=&nbsp;0)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;handle_error_en(s,&nbsp;&quot;pthread_mutex_unlock&quot;);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_SUCCESS);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;if&nbsp;(s&nbsp;==&nbsp;0)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;[main&nbsp;thread]&nbsp;pthread_mutex_lock()&nbsp;unexpectedly&nbsp;succeeded\n&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;[main&nbsp;thread]&nbsp;pthread_mutex_lock()&nbsp;unexpectedly&nbsp;failed\n&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;handle_error_en(s,&nbsp;&quot;pthread_mutex_lock&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
}
<A NAME="lbAL">&nbsp;</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">&nbsp;</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">&nbsp;</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>