|
Packit |
6c4009 |
/* Internal definitions for pthreads library.
|
|
Packit |
6c4009 |
Copyright (C) 2016-2018 Free Software Foundation, Inc.
|
|
Packit |
6c4009 |
This file is part of the GNU C Library.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit |
6c4009 |
modify it under the terms of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License as published by the Free Software Foundation; either
|
|
Packit |
6c4009 |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit |
6c4009 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
6c4009 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
6c4009 |
Lesser General Public License for more details.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
You should have received a copy of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License along with the GNU C Library; if not, see
|
|
Packit |
6c4009 |
<http://www.gnu.org/licenses/>. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#ifndef _PT_MUTEX_H
|
|
Packit |
6c4009 |
#define _PT_MUTEX_H 1
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Special ID used to signal an unrecoverable robust mutex. */
|
|
Packit |
6c4009 |
#define NOTRECOVERABLE_ID (1U << 31)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Common path for robust mutexes. Assumes the variable 'ret'
|
|
Packit |
6c4009 |
* is bound in the function this is called from. */
|
|
Packit |
6c4009 |
#define ROBUST_LOCK(self, mtxp, cb, ...) \
|
|
Packit |
6c4009 |
if (mtxp->__owner_id == NOTRECOVERABLE_ID) \
|
|
Packit |
6c4009 |
return ENOTRECOVERABLE; \
|
|
Packit |
6c4009 |
else if (mtxp->__owner_id == self->thread && \
|
|
Packit |
6c4009 |
__getpid () == (int)(mtxp->__lock & LLL_OWNER_MASK)) \
|
|
Packit |
6c4009 |
{ \
|
|
Packit |
6c4009 |
if (mtxp->__type == PT_MTX_RECURSIVE) \
|
|
Packit |
6c4009 |
{ \
|
|
Packit |
6c4009 |
if (__glibc_unlikely (mtxp->__cnt + 1 == 0)) \
|
|
Packit |
6c4009 |
return EAGAIN; \
|
|
Packit |
6c4009 |
\
|
|
Packit |
6c4009 |
++mtxp->__cnt; \
|
|
Packit |
6c4009 |
return 0; \
|
|
Packit |
6c4009 |
} \
|
|
Packit |
6c4009 |
else if (mtxp->__type == PT_MTX_ERRORCHECK) \
|
|
Packit |
6c4009 |
return EDEADLK; \
|
|
Packit |
6c4009 |
} \
|
|
Packit |
6c4009 |
\
|
|
Packit |
6c4009 |
ret = cb (&mtxp->__lock, ##__VA_ARGS__); \
|
|
Packit |
6c4009 |
if (ret == 0 || ret == EOWNERDEAD) \
|
|
Packit |
6c4009 |
{ \
|
|
Packit |
6c4009 |
if (mtxp->__owner_id == ENOTRECOVERABLE) \
|
|
Packit |
6c4009 |
ret = ENOTRECOVERABLE; \
|
|
Packit |
6c4009 |
else \
|
|
Packit |
6c4009 |
{ \
|
|
Packit |
6c4009 |
mtxp->__owner_id = self->thread; \
|
|
Packit |
6c4009 |
mtxp->__cnt = 1; \
|
|
Packit |
6c4009 |
if (ret == EOWNERDEAD) \
|
|
Packit |
6c4009 |
{ \
|
|
Packit |
6c4009 |
mtxp->__lock = mtxp->__lock | LLL_DEAD_OWNER; \
|
|
Packit |
6c4009 |
atomic_write_barrier (); \
|
|
Packit |
6c4009 |
} \
|
|
Packit |
6c4009 |
} \
|
|
Packit |
6c4009 |
} \
|
|
Packit |
6c4009 |
(void)0
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Check that a thread owns the mutex. For non-robust, task-shared
|
|
Packit |
6c4009 |
* objects, we have to check the thread *and* process-id. */
|
|
Packit |
6c4009 |
#define mtx_owned_p(mtx, pt, flags) \
|
|
Packit |
6c4009 |
((mtx)->__owner_id == (pt)->thread && \
|
|
Packit |
6c4009 |
(((flags) & GSYNC_SHARED) == 0 || \
|
|
Packit |
6c4009 |
(mtx)->__shpid == __getpid ()))
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Record a thread as the owner of the mutex. */
|
|
Packit |
6c4009 |
#define mtx_set_owner(mtx, pt, flags) \
|
|
Packit |
6c4009 |
(void) \
|
|
Packit |
6c4009 |
({ \
|
|
Packit |
6c4009 |
(mtx)->__owner_id = (pt)->thread; \
|
|
Packit |
6c4009 |
if ((flags) & GSYNC_SHARED) \
|
|
Packit |
6c4009 |
(mtx)->__shpid = __getpid (); \
|
|
Packit |
6c4009 |
})
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Redefined mutex types. The +1 is for binary compatibility. */
|
|
Packit |
6c4009 |
#define PT_MTX_NORMAL __PTHREAD_MUTEX_TIMED
|
|
Packit |
6c4009 |
#define PT_MTX_RECURSIVE (__PTHREAD_MUTEX_RECURSIVE + 1)
|
|
Packit |
6c4009 |
#define PT_MTX_ERRORCHECK (__PTHREAD_MUTEX_ERRORCHECK + 1)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Mutex type, including robustness. */
|
|
Packit |
6c4009 |
#define MTX_TYPE(mtxp) \
|
|
Packit |
6c4009 |
((mtxp)->__type | ((mtxp)->__flags & PTHREAD_MUTEX_ROBUST))
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
extern int __getpid (void) __attribute__ ((const));
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#endif /* pt-mutex.h */
|