Blame man3/pthread_mutexattr_setrobust.3

Packit 7cfc04
.\" Copyright (c) 2017, Yubin Ruan <ablacktshirt@gmail.com>
Packit 7cfc04
.\" and Copyright (c) 2017, Michael Kerrisk <mtk.manpages@gmail.com>
Packit 7cfc04
.\"
Packit 7cfc04
.\" %%%LICENSE_START(VERBATIM)
Packit 7cfc04
.\" Permission is granted to make and distribute verbatim copies of this
Packit 7cfc04
.\" manual provided the copyright notice and this permission notice are
Packit 7cfc04
.\" preserved on all copies.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Permission is granted to copy and distribute modified versions of this
Packit 7cfc04
.\" manual under the conditions for verbatim copying, provided that the
Packit 7cfc04
.\" entire resulting derived work is distributed under the terms of a
Packit 7cfc04
.\" permission notice identical to this one.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Since the Linux kernel and libraries are constantly changing, this
Packit 7cfc04
.\" manual page may be incorrect or out-of-date.  The author(s) assume no
Packit 7cfc04
.\" responsibility for errors or omissions, or for damages resulting from
Packit 7cfc04
.\" the use of the information contained herein.  The author(s) may not
Packit 7cfc04
.\" have taken the same level of care in the production of this manual,
Packit 7cfc04
.\" which is licensed free of charge, as they might when working
Packit 7cfc04
.\" professionally.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Formatted or processed versions of this manual, if unaccompanied by
Packit 7cfc04
.\" the source, must acknowledge the copyright and authors of this work.
Packit 7cfc04
.\" %%%LICENSE_END
Packit 7cfc04
.\"
Packit 7cfc04
.TH PTHREAD_MUTEXATTR_SETROBUST 3 2017-08-20 "Linux" "Linux Programmer's Manual"
Packit 7cfc04
.SH NAME
Packit 7cfc04
pthread_mutexattr_getrobust, pthread_mutexattr_setrobust
Packit 7cfc04
\- get and set the robustness attribute of a mutex attributes object
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
.nf
Packit 7cfc04
.B #include <pthread.h>
Packit 7cfc04
.PP
Packit 7cfc04
.BI "int pthread_mutexattr_getrobust(const pthread_mutexattr_t *" attr ,
Packit 7cfc04
.BI "                                int *" robustness ");"
Packit 7cfc04
.BI "int pthread_mutexattr_setrobust(const pthread_mutexattr_t *" attr ,
Packit 7cfc04
.BI "                                int " robustness ");"
Packit 7cfc04
.fi
Packit 7cfc04
.PP
Packit 7cfc04
Compile and link with \fI\-pthread\fP.
Packit 7cfc04
.PP
Packit 7cfc04
.in -4n
Packit 7cfc04
Feature Test Macro Requirements for glibc (see
Packit 7cfc04
.BR feature_test_macros (7)):
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
.BR pthread_mutexattr_getrobust (),
Packit 7cfc04
.BR pthread_mutexattr_setrobust ():
Packit 7cfc04
.br
Packit 7cfc04
.RS 4
Packit 7cfc04
.ad l
Packit 7cfc04
_POSIX_C_SOURCE >= 200809L
Packit 7cfc04
.\" FIXME .
Packit 7cfc04
.\" But see https://sourceware.org/bugzilla/show_bug.cgi?id=22125
Packit 7cfc04
.RE
Packit 7cfc04
.ad
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
The
Packit 7cfc04
.BR pthread_mutexattr_getrobust ()
Packit 7cfc04
function places the value of the robustness attribute of
Packit 7cfc04
the mutex attributes object referred to by
Packit 7cfc04
.I attr
Packit 7cfc04
in
Packit 7cfc04
.IR *robustness .
Packit 7cfc04
The
Packit 7cfc04
.BR pthread_mutexattr_setrobust ()
Packit 7cfc04
function sets the value of the robustness attribute of
Packit 7cfc04
the mutex attributes object referred to by
Packit 7cfc04
.I attr
Packit 7cfc04
to the value specified in
Packit 7cfc04
.IR *robustness .
Packit 7cfc04
.PP
Packit 7cfc04
The robustness attribute specifies the behavior of the mutex when
Packit 7cfc04
the owning thread dies without unlocking the mutex.
Packit 7cfc04
The following values are valid for
Packit 7cfc04
.IR robustness :
Packit 7cfc04
.TP
Packit 7cfc04
.BR PTHREAD_MUTEX_STALLED
Packit 7cfc04
This is the default value for a mutex attributes object.
Packit 7cfc04
If a mutex is initialized with the
Packit 7cfc04
.BR PTHREAD_MUTEX_STALLED
Packit 7cfc04
attribute and its owner dies without unlocking it,
Packit 7cfc04
the mutex remains locked afterwards and any future attempts to call
Packit 7cfc04
.BR pthread_mutex_lock (3)
Packit 7cfc04
on the mutex will block indefinitely.
Packit 7cfc04
.TP
Packit 7cfc04
.B PTHREAD_MUTEX_ROBUST
Packit 7cfc04
If a mutex is initialized with the
Packit 7cfc04
.BR PTHREAD_MUTEX_ROBUST
Packit 7cfc04
attribute and its owner dies without unlocking it,
Packit 7cfc04
any future attempts to call
Packit 7cfc04
.BR pthread_mutex_lock (3)
Packit 7cfc04
on this mutex will succeed and return
Packit 7cfc04
.B EOWNERDEAD
Packit 7cfc04
to indicate that the original owner no longer exists and the mutex is in
Packit 7cfc04
an inconsistent state.
Packit 7cfc04
Usually after
Packit 7cfc04
.B EOWNERDEAD
Packit 7cfc04
is returned, the next owner should call
Packit 7cfc04
.BR pthread_mutex_consistent (3)
Packit 7cfc04
on the acquired mutex to make it consistent again before using it any further.
Packit 7cfc04
.IP
Packit 7cfc04
If the next owner unlocks the mutex using
Packit 7cfc04
.BR pthread_mutex_unlock (3)
Packit 7cfc04
before making it consistent, the mutex will be permanently unusable and any
Packit 7cfc04
subsequent attempts to lock it using
Packit 7cfc04
.BR pthread_mutex_lock (3)
Packit 7cfc04
will fail with the error
Packit 7cfc04
.BR ENOTRECOVERABLE .
Packit 7cfc04
The only permitted operation on such a mutex is
Packit 7cfc04
.BR pthread_mutex_destroy (3).
Packit 7cfc04
.IP
Packit 7cfc04
If the next owner terminates before calling
Packit 7cfc04
.BR pthread_mutex_consistent (3),
Packit 7cfc04
further
Packit 7cfc04
.BR pthread_mutex_lock (3)
Packit 7cfc04
operations on this mutex will still return
Packit 7cfc04
.BR EOWNERDEAD .
Packit 7cfc04
.PP
Packit 7cfc04
Note that the
Packit 7cfc04
.IR attr
Packit 7cfc04
argument of
Packit 7cfc04
.BR pthread_mutexattr_getrobust ()
Packit 7cfc04
and
Packit 7cfc04
.BR pthread_mutexattr_setrobust ()
Packit 7cfc04
should refer to a mutex attributes object that was initialized by
Packit 7cfc04
.BR pthread_mutexattr_init (3),
Packit 7cfc04
otherwise the behavior is undefined.
Packit 7cfc04
.SH RETURN VALUE
Packit 7cfc04
On success, these functions return 0.
Packit 7cfc04
On error, they return a positive error number.
Packit 7cfc04
.PP
Packit 7cfc04
In the glibc implementation,
Packit 7cfc04
.BR pthread_mutexattr_getrobust ()
Packit 7cfc04
always return zero.
Packit 7cfc04
.SH ERRORS
Packit 7cfc04
.TP
Packit 7cfc04
.B EINVAL
Packit 7cfc04
A value other than
Packit 7cfc04
.B PTHREAD_MUTEX_STALLED
Packit 7cfc04
or
Packit 7cfc04
.B PTHREAD_MUTEX_ROBUST
Packit 7cfc04
was passed to
Packit 7cfc04
.BR pthread_mutexattr_setrobust ().
Packit 7cfc04
.SH VERSIONS
Packit 7cfc04
.BR pthread_mutexattr_getrobust ()
Packit 7cfc04
and
Packit 7cfc04
.BR pthread_mutexattr_setrobust ()
Packit 7cfc04
were added to glibc in version 2.12.
Packit 7cfc04
.SH CONFORMING TO
Packit 7cfc04
POSIX.1-2008.
Packit 7cfc04
.SH NOTES
Packit 7cfc04
In the Linux implementation,
Packit 7cfc04
when using process-shared robust mutexes, a waiting thread also receives the
Packit 7cfc04
.B EOWNERDEAD
Packit 7cfc04
notification if the owner of a robust mutex performs an
Packit 7cfc04
.BR execve (2)
Packit 7cfc04
without first unlocking the mutex.
Packit 7cfc04
POSIX.1 does not specify this detail,
Packit 7cfc04
but the same behavior also occurs in at least some
Packit 7cfc04
.\" E.g., Solaris, according to its manual page
Packit 7cfc04
other implementations.
Packit 7cfc04
.PP
Packit 7cfc04
Before the addition of
Packit 7cfc04
.BR pthread_mutexattr_getrobust ()
Packit 7cfc04
and
Packit 7cfc04
.BR pthread_mutexattr_setrobust ()
Packit 7cfc04
to POSIX,
Packit 7cfc04
glibc defined the following equivalent nonstandard functions if
Packit 7cfc04
.BR _GNU_SOURCE
Packit 7cfc04
was defined:
Packit 7cfc04
.PP
Packit 7cfc04
.nf
Packit 7cfc04
.BI "int pthread_mutexattr_getrobust_np(const pthread_mutexattr_t *" attr ,
Packit 7cfc04
.BI "                                   int *" robustness ");"
Packit 7cfc04
.BI "int pthread_mutexattr_setrobust_np(const pthread_mutexattr_t *" attr ,
Packit 7cfc04
.BI "                                   int " robustness ");"
Packit 7cfc04
.fi
Packit 7cfc04
.PP
Packit 7cfc04
Correspondingly, the constants
Packit 7cfc04
.B PTHREAD_MUTEX_STALLED_NP
Packit 7cfc04
and
Packit 7cfc04
.B PTHREAD_MUTEX_ROBUST_NP
Packit 7cfc04
were also defined.
Packit 7cfc04
.PP
Packit 7cfc04
These GNU-specific APIs, which first appeared in glibc 2.4,
Packit 7cfc04
are nowadays obsolete and should not be used in new programs.
Packit 7cfc04
.SH EXAMPLE
Packit 7cfc04
.PP
Packit 7cfc04
The program below demonstrates the use of the robustness attribute of a
Packit 7cfc04
mutex attributes object.
Packit 7cfc04
In this program, a thread holding the mutex
Packit 7cfc04
dies prematurely without unlocking the mutex.
Packit 7cfc04
The main thread subsequently acquires the mutex
Packit 7cfc04
successfully and gets the error
Packit 7cfc04
.BR EOWNERDEAD ,
Packit 7cfc04
after which it makes the mutex consistent.
Packit 7cfc04
.PP
Packit 7cfc04
The following shell session shows what we see when running this program:
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
$ \fB./a.out\fP
Packit 7cfc04
[original owner] Setting lock...
Packit 7cfc04
[original owner] Locked. Now exiting without unlocking.
Packit 7cfc04
[main thread] Attempting to lock the robust mutex.
Packit 7cfc04
[main thread] pthread_mutex_lock() returned EOWNERDEAD
Packit 7cfc04
[main thread] Now make the mutex consistent
Packit 7cfc04
[main thread] Mutex is now consistent; unlocking
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.SS Program source
Packit 7cfc04
.EX
Packit 7cfc04
#include <stdlib.h>
Packit 7cfc04
#include <stdio.h>
Packit 7cfc04
#include <unistd.h>
Packit 7cfc04
#include <pthread.h>
Packit 7cfc04
#include <errno.h>
Packit 7cfc04
Packit 7cfc04
#define handle_error_en(en, msg) \\
Packit 7cfc04
               do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
Packit 7cfc04
Packit 7cfc04
static pthread_mutex_t mtx;
Packit 7cfc04
Packit 7cfc04
static void *
Packit 7cfc04
original_owner_thread(void *ptr)
Packit 7cfc04
{
Packit 7cfc04
    printf("[original owner] Setting lock...\\n");
Packit 7cfc04
    pthread_mutex_lock(&mtx);
Packit 7cfc04
    printf("[original owner] Locked. Now exiting without unlocking.\\n");
Packit 7cfc04
    pthread_exit(NULL);
Packit 7cfc04
}
Packit 7cfc04
Packit 7cfc04
int
Packit 7cfc04
main(int argc, char *argv[])
Packit 7cfc04
{
Packit 7cfc04
    pthread_t thr;
Packit 7cfc04
    pthread_mutexattr_t attr;
Packit 7cfc04
    int s;
Packit 7cfc04
Packit 7cfc04
    pthread_mutexattr_init(&attr);
Packit 7cfc04
                                /* initialize the attributes object */
Packit 7cfc04
    pthread_mutexattr_setrobust(&attr, PTHREAD_MUTEX_ROBUST);
Packit 7cfc04
                               /* set robustness */
Packit 7cfc04
Packit 7cfc04
    pthread_mutex_init(&mtx, &attr);   /* initialize the mutex */
Packit 7cfc04
Packit 7cfc04
    pthread_create(&thr, NULL, original_owner_thread, NULL);
Packit 7cfc04
Packit 7cfc04
    sleep(2);
Packit 7cfc04
Packit 7cfc04
    /* "original_owner_thread" should have exited by now */
Packit 7cfc04
Packit 7cfc04
    printf("[main thread] Attempting to lock the robust mutex.\\n");
Packit 7cfc04
    s = pthread_mutex_lock(&mtx);
Packit 7cfc04
    if (s == EOWNERDEAD) {
Packit 7cfc04
        printf("[main thread] pthread_mutex_lock() returned EOWNERDEAD\\n");
Packit 7cfc04
        printf("[main thread] Now make the mutex consistent\\n");
Packit 7cfc04
        s = pthread_mutex_consistent(&mtx);
Packit 7cfc04
        if (s != 0)
Packit 7cfc04
            handle_error_en(s, "pthread_mutex_consistent");
Packit 7cfc04
        printf("[main thread] Mutex is now consistent; unlocking\\n");
Packit 7cfc04
        s = pthread_mutex_unlock(&mtx);
Packit 7cfc04
        if (s != 0)
Packit 7cfc04
            handle_error_en(s, "pthread_mutex_unlock");
Packit 7cfc04
Packit 7cfc04
        exit(EXIT_SUCCESS);
Packit 7cfc04
    } else if (s == 0) {
Packit 7cfc04
        printf("[main thread] pthread_mutex_lock() unexpectedly succeeded\\n");
Packit 7cfc04
        exit(EXIT_FAILURE);
Packit 7cfc04
    } else {
Packit 7cfc04
        printf("[main thread] pthread_mutex_lock() unexpectedly failed\\n");
Packit 7cfc04
        handle_error_en(s, "pthread_mutex_lock");
Packit 7cfc04
    }
Packit 7cfc04
}
Packit 7cfc04
.EE
Packit 7cfc04
.SH SEE ALSO
Packit 7cfc04
.ad l
Packit 7cfc04
.nh
Packit 7cfc04
.BR get_robust_list (2),
Packit 7cfc04
.BR set_robust_list (2),
Packit 7cfc04
.BR pthread_mutex_init (3),
Packit 7cfc04
.BR pthread_mutex_consistent (3),
Packit 7cfc04
.BR pthread_mutex_lock (3),
Packit 7cfc04
.BR pthreads (7)
Packit 7cfc04
.SH COLOPHON
Packit 7cfc04
This page is part of release 4.15 of the Linux
Packit 7cfc04
.I man-pages
Packit 7cfc04
project.
Packit 7cfc04
A description of the project,
Packit 7cfc04
information about reporting bugs,
Packit 7cfc04
and the latest version of this page,
Packit 7cfc04
can be found at
Packit 7cfc04
\%https://www.kernel.org/doc/man\-pages/.