1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
|
m4_changecom()
m4_define([upcase], [m4_translit([$*], [a-z], [A-Z])])
m4_define([alias], [dnl
#ifndef upcase(HAVE_[$3])
#define upcase(NEED_[$1]_STUB)
# ifdef SUPPORT_ATTRIBUTE_ALIAS
[$2] [$3]() __attribute__ ((weak, [alias] ("__pthread_[$1]_stub")));
# else
# pragma weak [$3] = __pthread_[$1]_stub
# endif
#endif
])
# dependency_group(<group_name>, [<functions>...])
# Declare that if any function in the function list needs
# to be stubbed using our implementation, then all of them do.
# If our implementations are used, then additionally #define
# NEED_<GROUP_NAME>.
m4_define([dependency_group],[dnl
#if m4_foreach([x], [$2], [!defined(HAVE_[]upcase(x)) || ])0
# define NEED_[]upcase($1) 1
m4_foreach([x], [$2], [dnl
# undef HAVE_[]upcase(x)
])dnl
#endif])
m4_divert(0)dnl
/* Copyright (C) 2006 Diego Pettenò
* Copyright (C) 2010 M Joonas Pihlaja
* Inspired by libX11 code copyright (c) 1995 David E. Wexelblat.
*
* This is an automatically created file from stubs.c.m4. Do not edit.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* Except as contained in this notice, the names of the authors or their
* institutions shall not be used in advertising or otherwise to promote the
* sale, use or other dealings in this Software without prior written
* authorization from the authors.
*/
#include <pthread.h>
#include <stdlib.h>
#include <errno.h>
#include "config.h"
/* Gah.. linux pthreads doesn't give us the posix mutex types by
* default. Map them manually. */
#if defined(__linux__)
# define PTHREAD_MUTEX_NORMAL PTHREAD_MUTEX_TIMED_NP
# define PTHREAD_MUTEX_RECURSIVE PTHREAD_MUTEX_RECURSIVE_NP
# define PTHREAD_MUTEX_ERRORCHECK PTHREAD_MUTEX_ERRORCHECK_NP
# define PTHREAD_MUTEX_DEFAULT PTHREAD_MUTEX_NORMAL
#endif
dependency_group(MUTEXATTR_GROUP, [
pthread_mutex_init,
pthread_mutex_destroy,
pthread_mutex_lock,
pthread_mutex_unlock,
pthread_mutexattr_init,
pthread_mutexattr_destroy,
pthread_mutexattr_settype,
pthread_mutexattr_gettype])
m4_include([list.m4])
#ifdef NEED_ZERO_STUB
static int __pthread_zero_stub()
{
return 0;
}
#endif
#ifdef NEED_ABORT_STUB
static int __pthread_abort_stub()
{
abort();
}
#endif
#ifdef NEED_EQUAL_STUB
static int __pthread_equal_stub(pthread_t t1, pthread_t t2)
{
return (t1 == t2);
}
#endif
#ifdef NEED_EXIT_STUB
static void __pthread_exit_stub(void *ret)
{
exit(EXIT_SUCCESS);
}
#endif
#if NEED_MUTEXATTR_GROUP
typedef struct {
int mutex_type;
} __pthread_mutexattr_stub_t;
typedef struct {
unsigned locked_count;
__pthread_mutexattr_stub_t attr;
} __pthread_mutex_stub_t;
#endif
#ifdef NEED_MUTEXATTR_INIT_STUB
static int __pthread_mutexattr_init_stub(__pthread_mutexattr_stub_t *attr)
{
if (!attr) return EINVAL;
attr->mutex_type = PTHREAD_MUTEX_DEFAULT;
return 0;
}
#endif
#ifdef NEED_MUTEXATTR_SETTYPE_STUB
static int __pthread_mutexattr_settype_stub(__pthread_mutexattr_stub_t *attr,
int type)
{
if (!attr) return EINVAL;
if (type == PTHREAD_MUTEX_NORMAL ||
type == PTHREAD_MUTEX_ERRORCHECK ||
type == PTHREAD_MUTEX_RECURSIVE ||
type == PTHREAD_MUTEX_DEFAULT)
{
attr->mutex_type = type;
return 0;
}
return EINVAL;
}
#endif
#ifdef NEED_MUTEXATTR_GETTYPE_STUB
static int __pthread_mutexattr_gettype_stub(__pthread_mutexattr_stub_t *attr,
int *type)
{
if (!attr || !type) return EINVAL;
*type = attr->mutex_type;
return 0;
}
#endif
#ifdef NEED_MUTEX_INIT_STUB
static int __pthread_mutex_init_stub(__pthread_mutex_stub_t *mutex,
__pthread_mutexattr_stub_t *attr)
{
if (!mutex) return EINVAL;
mutex->locked_count = 0;
if (attr) {
mutex->attr = *attr;
} else {
__pthread_mutexattr_init_stub(&mutex->attr);
}
return 0;
}
#endif
#ifdef NEED_MUTEX_LOCK_STUB
static int __pthread_mutex_lock_stub(__pthread_mutex_stub_t *mutex)
{
int type;
if (!mutex) return EINVAL;
if (mutex->locked_count == 0) {
mutex->locked_count = 1;
return 0;
}
type = mutex->attr.mutex_type;
if (type == PTHREAD_MUTEX_RECURSIVE) {
if (mutex->locked_count+1 == 0)
/* "The mutex could not be acquired because the maximum
* number of recursive locks for mutex has been
* exceeded." */
return EAGAIN;
++mutex->locked_count;
return 0;
}
if (type == PTHREAD_MUTEX_NORMAL) {
/* "If the mutex type is PTHREAD_MUTEX_NORMAL, deadlock
* detection shall not be provided. Attempting to relock the
* mutex causes deadlock." Uh... okay. :| */
while (1) {}
return EDEADLK;
}
if (type == PTHREAD_MUTEX_DEFAULT ||
type == PTHREAD_MUTEX_ERRORCHECK)
{
/* "If the mutex type is PTHREAD_MUTEX_DEFAULT, attempting to
* recursively lock the mutex results in undefined
* behavior." */
/* "If a thread attempts to relock a
* [PTHREAD_MUTEX_ERRORCHECK] mutex that it has already
* locked, an error shall be returned." */
return EDEADLK;
}
/* "The value specified by mutex does not refer to an
* initialized mutex object." */
return EINVAL;
}
#endif
#ifdef NEED_MUTEX_UNLOCK_STUB
static int __pthread_mutex_unlock_stub(__pthread_mutex_stub_t *mutex)
{
if (!mutex) return EINVAL;
if (mutex->locked_count == 0)
/* "The current thread does not own the mutex." */
return EPERM;
if (mutex->locked_count == 1) {
mutex->locked_count = 0;
return 0;
}
if (mutex->attr.mutex_type == PTHREAD_MUTEX_RECURSIVE) {
--mutex->locked_count;
return 0;
}
/* "The value specified by mutex does not refer to an initialized
* mutex object." */
/* or "undefined behaviour." */
/* or "an error shall be returned." */
return EINVAL;
}
#endif
|