]> git.ipfire.org Git - thirdparty/glibc.git/blame - nptl/sysdeps/pthread/bits/libc-lock.h
Update copyright notices with scripts/update-copyrights
[thirdparty/glibc.git] / nptl / sysdeps / pthread / bits / libc-lock.h
CommitLineData
09efc3ba 1/* libc-internal interface for mutex locks. NPTL version.
d4697bc9 2 Copyright (C) 1996-2014 Free Software Foundation, Inc.
76a50749
UD
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
28ddcea0
RM
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
76a50749
UD
8 License, or (at your option) any later version.
9
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
28ddcea0 13 Lesser General Public License for more details.
76a50749 14
28ddcea0 15 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
16 License along with the GNU C Library; see the file COPYING.LIB. If
17 not, see <http://www.gnu.org/licenses/>. */
76a50749
UD
18
19#ifndef _BITS_LIBC_LOCK_H
20#define _BITS_LIBC_LOCK_H 1
21
22#include <pthread.h>
23#define __need_NULL
24#include <stddef.h>
25
440d8bc2 26
76a50749 27/* Mutex type. */
440d8bc2 28#if defined _LIBC || defined _IO_MTSAFE_IO
d3c9f895 29# if (defined NOT_IN_libc && !defined IS_IN_libpthread) || !defined _LIBC
d3c9f895 30typedef struct { pthread_mutex_t mutex; } __libc_lock_recursive_t;
440d8bc2 31# else
d3c9f895 32typedef struct { int lock; int cnt; void *owner; } __libc_lock_recursive_t;
440d8bc2 33# endif
76a50749 34#else
76a50749 35typedef struct __libc_lock_recursive_opaque__ __libc_lock_recursive_t;
76a50749
UD
36#endif
37
76a50749
UD
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
44 of libc. */
76a50749
UD
45#define __libc_lock_define_recursive(CLASS,NAME) \
46 CLASS __libc_lock_recursive_t NAME;
76a50749
UD
47
48/* Define an initialized recursive lock variable NAME with storage
49 class CLASS. */
d3c9f895
UD
50#if defined _LIBC && (!defined NOT_IN_libc || defined IS_IN_libpthread)
51# if LLL_LOCK_INITIALIZER == 0
52# define __libc_lock_define_initialized_recursive(CLASS,NAME) \
53 CLASS __libc_lock_recursive_t NAME;
54# else
55# define __libc_lock_define_initialized_recursive(CLASS,NAME) \
56 CLASS __libc_lock_recursive_t NAME = _LIBC_LOCK_RECURSIVE_INITIALIZER;
57# endif
58# define _LIBC_LOCK_RECURSIVE_INITIALIZER \
59 { LLL_LOCK_INITIALIZER, 0, NULL }
60#else
61# define __libc_lock_define_initialized_recursive(CLASS,NAME) \
76a50749 62 CLASS __libc_lock_recursive_t NAME = _LIBC_LOCK_RECURSIVE_INITIALIZER;
d3c9f895 63# define _LIBC_LOCK_RECURSIVE_INITIALIZER \
76a50749 64 {PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP}
d3c9f895 65#endif
76a50749 66
9463518d 67/* Initialize a recursive mutex. */
d3c9f895
UD
68#if defined _LIBC && (!defined NOT_IN_libc || defined IS_IN_libpthread)
69# define __libc_lock_init_recursive(NAME) \
ced70532 70 ((NAME) = (__libc_lock_recursive_t) _LIBC_LOCK_RECURSIVE_INITIALIZER, 0)
d3c9f895
UD
71#else
72# define __libc_lock_init_recursive(NAME) \
73 do { \
74 if (__pthread_mutex_init != NULL) \
75 { \
76 pthread_mutexattr_t __attr; \
77 __pthread_mutexattr_init (&__attr); \
78 __pthread_mutexattr_settype (&__attr, PTHREAD_MUTEX_RECURSIVE_NP); \
79 __pthread_mutex_init (&(NAME).mutex, &__attr); \
80 __pthread_mutexattr_destroy (&__attr); \
81 } \
82 } while (0)
83#endif
84
76a50749 85/* Finalize recursive named lock. */
d3c9f895
UD
86#if defined _LIBC && (!defined NOT_IN_libc || defined IS_IN_libpthread)
87# define __libc_lock_fini_recursive(NAME) ((void) 0)
88#else
89# define __libc_lock_fini_recursive(NAME) \
c8886d0a 90 __libc_maybe_call (__pthread_mutex_destroy, (&(NAME).mutex), 0)
d3c9f895 91#endif
76a50749 92
76a50749 93/* Lock the recursive named lock variable. */
d3c9f895
UD
94#if defined _LIBC && (!defined NOT_IN_libc || defined IS_IN_libpthread)
95# define __libc_lock_lock_recursive(NAME) \
96 do { \
97 void *self = THREAD_SELF; \
98 if ((NAME).owner != self) \
99 { \
e51deae7 100 lll_lock ((NAME).lock, LLL_PRIVATE); \
d3c9f895
UD
101 (NAME).owner = self; \
102 } \
103 ++(NAME).cnt; \
104 } while (0)
105#else
106# define __libc_lock_lock_recursive(NAME) \
107 __libc_maybe_call (__pthread_mutex_lock, (&(NAME).mutex), 0)
108#endif
109
76a50749 110/* Try to lock the recursive named lock variable. */
d3c9f895 111#if defined _LIBC && (!defined NOT_IN_libc || defined IS_IN_libpthread)
09efc3ba
UD
112# define __libc_lock_trylock_recursive(NAME) \
113 ({ \
d3c9f895
UD
114 int result = 0; \
115 void *self = THREAD_SELF; \
116 if ((NAME).owner != self) \
117 { \
118 if (lll_trylock ((NAME).lock) == 0) \
119 { \
120 (NAME).owner = self; \
121 (NAME).cnt = 1; \
122 } \
123 else \
124 result = EBUSY; \
125 } \
126 else \
127 ++(NAME).cnt; \
09efc3ba
UD
128 result; \
129 })
d3c9f895
UD
130#else
131# define __libc_lock_trylock_recursive(NAME) \
c8886d0a 132 __libc_maybe_call (__pthread_mutex_trylock, (&(NAME).mutex), 0)
d3c9f895
UD
133#endif
134
76a50749 135/* Unlock the recursive named lock variable. */
d3c9f895
UD
136#if defined _LIBC && (!defined NOT_IN_libc || defined IS_IN_libpthread)
137/* We do no error checking here. */
138# define __libc_lock_unlock_recursive(NAME) \
139 do { \
140 if (--(NAME).cnt == 0) \
141 { \
142 (NAME).owner = NULL; \
e51deae7 143 lll_unlock ((NAME).lock, LLL_PRIVATE); \
d3c9f895
UD
144 } \
145 } while (0)
146#else
147# define __libc_lock_unlock_recursive(NAME) \
c8886d0a 148 __libc_maybe_call (__pthread_mutex_unlock, (&(NAME).mutex), 0)
d3c9f895 149#endif
76a50749 150
09d65ff3 151/* Note that for I/O cleanup handling we are using the old-style
9463518d 152 cancel handling. It does not have to be integrated with C++ since
09d65ff3
UD
153 no C++ code is called in the middle. The old-style handling is
154 faster and the support is not going away. */
09d65ff3 155extern void _pthread_cleanup_push_defer (struct _pthread_cleanup_buffer *buffer,
a784e502 156 void (*routine) (void *), void *arg);
09d65ff3 157extern void _pthread_cleanup_pop_restore (struct _pthread_cleanup_buffer *buffer,
a784e502 158 int execute);
09d65ff3 159
76a50749
UD
160/* Start critical region with cleanup. */
161#define __libc_cleanup_region_start(DOIT, FCT, ARG) \
162 { struct _pthread_cleanup_buffer _buffer; \
1fc46908
UD
163 int _avail; \
164 if (DOIT) { \
ea1533e0 165 _avail = PTFAVAIL (_pthread_cleanup_push_defer); \
1fc46908 166 if (_avail) { \
ea1533e0
UD
167 __libc_ptf_call_always (_pthread_cleanup_push_defer, (&_buffer, FCT, \
168 ARG)); \
1fc46908
UD
169 } else { \
170 _buffer.__routine = (FCT); \
171 _buffer.__arg = (ARG); \
172 } \
173 } else { \
174 _avail = 0; \
76a50749
UD
175 }
176
177/* End critical region with cleanup. */
178#define __libc_cleanup_region_end(DOIT) \
179 if (_avail) { \
ea1533e0 180 __libc_ptf_call_always (_pthread_cleanup_pop_restore, (&_buffer, DOIT));\
000160a2
UD
181 } else if (DOIT) \
182 _buffer.__routine (_buffer.__arg); \
76a50749
UD
183 }
184
76a50749 185
9463518d
UD
186/* Hide the definitions which are only supposed to be used inside libc in
187 a separate file. This file is not present in the installation! */
188#ifdef _LIBC
189# include "libc-lockP.h"
76a50749
UD
190#endif
191
76a50749 192#endif /* bits/libc-lock.h */