1 /* libc-internal interface for mutex locks. NPTL version.
2 Copyright (C) 1996-2016 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If
17 not, see <http://www.gnu.org/licenses/>. */
20 #define _LIBC_LOCK_H 1
28 #if defined _LIBC || defined _IO_MTSAFE_IO
29 # if (!IS_IN (libc) && !IS_IN (libpthread)) || !defined _LIBC
30 typedef struct { pthread_mutex_t mutex
; } __libc_lock_recursive_t
;
32 typedef struct { int lock
; int cnt
; void *owner
; } __libc_lock_recursive_t
;
35 typedef struct __libc_lock_recursive_opaque__ __libc_lock_recursive_t
;
38 /* Define a lock variable NAME with storage class CLASS. The lock must be
39 initialized with __libc_lock_init before it can be used (or define it
40 with __libc_lock_define_initialized, below). Use `extern' for CLASS to
41 declare a lock defined in another module. In public structure
42 definitions you must use a pointer to the lock structure (i.e., NAME
43 begins with a `*'), because its storage size will not be known outside
45 #define __libc_lock_define_recursive(CLASS,NAME) \
46 CLASS __libc_lock_recursive_t NAME;
48 /* Define an initialized recursive lock variable NAME with storage
50 #if defined _LIBC && (IS_IN (libc) || IS_IN (libpthread))
51 # define __libc_lock_define_initialized_recursive(CLASS, NAME) \
52 CLASS __libc_lock_recursive_t NAME = _LIBC_LOCK_RECURSIVE_INITIALIZER;
53 # define _LIBC_LOCK_RECURSIVE_INITIALIZER \
54 { LLL_LOCK_INITIALIZER, 0, NULL }
56 # define __libc_lock_define_initialized_recursive(CLASS,NAME) \
57 CLASS __libc_lock_recursive_t NAME = _LIBC_LOCK_RECURSIVE_INITIALIZER;
58 # define _LIBC_LOCK_RECURSIVE_INITIALIZER \
59 {PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP}
62 /* Initialize a recursive mutex. */
63 #if defined _LIBC && (IS_IN (libc) || IS_IN (libpthread))
64 # define __libc_lock_init_recursive(NAME) \
65 ((void) ((NAME) = (__libc_lock_recursive_t) _LIBC_LOCK_RECURSIVE_INITIALIZER))
67 # define __libc_lock_init_recursive(NAME) \
69 if (__pthread_mutex_init != NULL) \
71 pthread_mutexattr_t __attr; \
72 __pthread_mutexattr_init (&__attr); \
73 __pthread_mutexattr_settype (&__attr, PTHREAD_MUTEX_RECURSIVE_NP); \
74 __pthread_mutex_init (&(NAME).mutex, &__attr); \
75 __pthread_mutexattr_destroy (&__attr); \
80 /* Finalize recursive named lock. */
81 #if defined _LIBC && (IS_IN (libc) || IS_IN (libpthread))
82 # define __libc_lock_fini_recursive(NAME) ((void) 0)
84 # define __libc_lock_fini_recursive(NAME) \
85 __libc_maybe_call (__pthread_mutex_destroy, (&(NAME).mutex), 0)
88 /* Lock the recursive named lock variable. */
89 #if defined _LIBC && (IS_IN (libc) || IS_IN (libpthread))
90 # define __libc_lock_lock_recursive(NAME) \
92 void *self = THREAD_SELF; \
93 if ((NAME).owner != self) \
95 lll_lock ((NAME).lock, LLL_PRIVATE); \
96 (NAME).owner = self; \
101 # define __libc_lock_lock_recursive(NAME) \
102 __libc_maybe_call (__pthread_mutex_lock, (&(NAME).mutex), 0)
105 /* Try to lock the recursive named lock variable. */
106 #if defined _LIBC && (IS_IN (libc) || IS_IN (libpthread))
107 # define __libc_lock_trylock_recursive(NAME) \
110 void *self = THREAD_SELF; \
111 if ((NAME).owner != self) \
113 if (lll_trylock ((NAME).lock) == 0) \
115 (NAME).owner = self; \
126 # define __libc_lock_trylock_recursive(NAME) \
127 __libc_maybe_call (__pthread_mutex_trylock, (&(NAME).mutex), 0)
130 /* Unlock the recursive named lock variable. */
131 #if defined _LIBC && (IS_IN (libc) || IS_IN (libpthread))
132 /* We do no error checking here. */
133 # define __libc_lock_unlock_recursive(NAME) \
135 if (--(NAME).cnt == 0) \
137 (NAME).owner = NULL; \
138 lll_unlock ((NAME).lock, LLL_PRIVATE); \
142 # define __libc_lock_unlock_recursive(NAME) \
143 __libc_maybe_call (__pthread_mutex_unlock, (&(NAME).mutex), 0)
146 /* Note that for I/O cleanup handling we are using the old-style
147 cancel handling. It does not have to be integrated with C++ since
148 no C++ code is called in the middle. The old-style handling is
149 faster and the support is not going away. */
150 extern void _pthread_cleanup_push_defer (struct _pthread_cleanup_buffer
*buffer
,
151 void (*routine
) (void *), void *arg
);
152 extern void _pthread_cleanup_pop_restore (struct _pthread_cleanup_buffer
*buffer
,
155 /* Start critical region with cleanup. */
156 #define __libc_cleanup_region_start(DOIT, FCT, ARG) \
157 { struct _pthread_cleanup_buffer _buffer; \
160 _avail = PTFAVAIL (_pthread_cleanup_push_defer); \
162 __libc_ptf_call_always (_pthread_cleanup_push_defer, (&_buffer, FCT, \
165 _buffer.__routine = (FCT); \
166 _buffer.__arg = (ARG); \
172 /* End critical region with cleanup. */
173 #define __libc_cleanup_region_end(DOIT) \
175 __libc_ptf_call_always (_pthread_cleanup_pop_restore, (&_buffer, DOIT));\
177 _buffer.__routine (_buffer.__arg); \
181 /* Hide the definitions which are only supposed to be used inside libc in
182 a separate file. This file is not present in the installation! */
184 # include "libc-lockP.h"
187 #endif /* libc-lock.h */