summaryrefslogtreecommitdiff
path: root/debian/patches/libpthread_recursive_mutex_initializer.patch
blob: c6e3e5361651ab4daec1a69909e95672f9b2fccd (plain)
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
---
 libpthread/include/pthread/pthread.h              |    2 ++
 libpthread/sysdeps/generic/bits/mutex-attr.h      |    1 +
 libpthread/sysdeps/generic/bits/mutex.h           |   10 +++++++++-
 libpthread/sysdeps/generic/pt-mutex-destroy.c     |    3 ++-
 libpthread/sysdeps/generic/pt-mutex-init.c        |   11 ++++-------
 libpthread/sysdeps/generic/pt-mutex-timedlock.c   |   22 ++++++++++++++--------
 libpthread/sysdeps/generic/pt-mutex-transfer-np.c |   11 +++++++++--
 libpthread/sysdeps/generic/pt-mutex-trylock.c     |   14 ++++++++++----
 libpthread/sysdeps/generic/pt-mutex-unlock.c      |   16 +++++++++++-----
 libpthread/sysdeps/generic/pt-mutexattr.c         |    8 ++++++++
 10 files changed, 70 insertions(+), 28 deletions(-)

--- a/libpthread/include/pthread/pthread.h
+++ b/libpthread/include/pthread/pthread.h
@@ -313,6 +313,8 @@ extern int pthread_mutexattr_settype(pth
 /* Static initializer for recursive mutexes.  */
 
 #ifdef __USE_GNU
+# define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP \
+  __PTHREAD_ERRORCHECK_MUTEX_INITIALIZER
 # define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP \
   __PTHREAD_RECURSIVE_MUTEX_INITIALIZER
 #endif
--- a/libpthread/sysdeps/generic/pt-mutex-destroy.c
+++ b/libpthread/sysdeps/generic/pt-mutex-destroy.c
@@ -26,7 +26,8 @@
 int
 _pthread_mutex_destroy (pthread_mutex_t *mutex)
 {
-  if (mutex->attr == &__pthread_recursive_mutexattr)
+  if (mutex->attr == __PTHREAD_ERRORCHECK_MUTEXATTR
+   || mutex->attr == __PTHREAD_RECURSIVE_MUTEXATTR)
     /* Static attributes.  */
     ;
   else
--- a/libpthread/sysdeps/generic/pt-mutex-init.c
+++ b/libpthread/sysdeps/generic/pt-mutex-init.c
@@ -35,14 +35,11 @@ _pthread_mutex_init (pthread_mutex_t *mu
     /* The default attributes.  */
     return 0;
 
-  if (attr == &__pthread_recursive_mutexattr)
-    /* Non-default but known attributes.  */
-    {
-      mutex->attr = attr;
-      return 0;
-    }
+  if (! mutex->attr
+      || mutex->attr == __PTHREAD_ERRORCHECK_MUTEXATTR
+      || mutex->attr == __PTHREAD_RECURSIVE_MUTEXATTR)
+    mutex->attr = malloc (sizeof *attr);
 
-  mutex->attr = malloc (sizeof *attr);
   if (! mutex->attr)
     return ENOMEM;
 
--- a/libpthread/sysdeps/generic/pt-mutex-timedlock.c
+++ b/libpthread/sysdeps/generic/pt-mutex-timedlock.c
@@ -31,6 +31,12 @@ __pthread_mutex_timedlock_internal (stru
 				    const struct timespec *abstime)
 {
   struct __pthread *self;
+  const struct __pthread_mutexattr *attr = mutex->attr;
+
+  if (attr == __PTHREAD_ERRORCHECK_MUTEXATTR)
+    attr = &__pthread_errorcheck_mutexattr;
+  if (attr == __PTHREAD_RECURSIVE_MUTEXATTR)
+    attr = &__pthread_recursive_mutexattr;
 
   __pthread_spin_lock (&mutex->__lock);
   if (__pthread_spin_trylock (&mutex->__held) == 0)
@@ -50,8 +56,8 @@ __pthread_mutex_timedlock_internal (stru
 #endif
 #endif
 
-      if (mutex->attr)
-	switch (mutex->attr->mutex_type)
+      if (attr)
+	switch (attr->mutex_type)
 	  {
 	  case PTHREAD_MUTEX_NORMAL:
 	    break;
@@ -75,7 +81,7 @@ __pthread_mutex_timedlock_internal (stru
   self = _pthread_self ();
   assert (self);
 
-  if (! mutex->attr || mutex->attr->mutex_type == PTHREAD_MUTEX_NORMAL)
+  if (! attr || attr->mutex_type == PTHREAD_MUTEX_NORMAL)
     {
 #if defined(ALWAYS_TRACK_MUTEX_OWNER)
       assert (mutex->owner != self);
@@ -83,7 +89,7 @@ __pthread_mutex_timedlock_internal (stru
     }
   else
     {
-      switch (mutex->attr->mutex_type)
+      switch (attr->mutex_type)
 	{
 	case PTHREAD_MUTEX_ERRORCHECK:
 	  if (mutex->owner == self)
@@ -108,7 +114,7 @@ __pthread_mutex_timedlock_internal (stru
     }
 
 #if !defined(ALWAYS_TRACK_MUTEX_OWNER)
-  if (mutex->attr && mutex->attr->mutex_type != PTHREAD_MUTEX_NORMAL)
+  if (attr && attr->mutex_type != PTHREAD_MUTEX_NORMAL)
 #endif
     assert (mutex->owner);
 
@@ -147,14 +153,14 @@ __pthread_mutex_timedlock_internal (stru
     __pthread_block (self);
 
 #if !defined(ALWAYS_TRACK_MUTEX_OWNER)
-  if (mutex->attr && mutex->attr->mutex_type != PTHREAD_MUTEX_NORMAL)
+  if (attr && attr->mutex_type != PTHREAD_MUTEX_NORMAL)
 #endif
     {
       assert (mutex->owner == self);
     }
 
-  if (mutex->attr)
-    switch (mutex->attr->mutex_type)
+  if (attr)
+    switch (attr->mutex_type)
       {
       case PTHREAD_MUTEX_NORMAL:
 	break;
--- a/libpthread/sysdeps/generic/pt-mutex-transfer-np.c
+++ b/libpthread/sysdeps/generic/pt-mutex-transfer-np.c
@@ -29,13 +29,20 @@ __pthread_mutex_transfer_np (struct __pt
   assert (mutex->owner == _pthread_self ());
 
   struct __pthread *thread = __pthread_getid (tid);
+  const struct __pthread_mutexattr *attr = mutex->attr;
+
   if (! thread)
     return ESRCH;
 
   if (thread == _pthread_self ())
     return 0;
 
-  if (mutex->attr && mutex->attr->mutex_type == PTHREAD_MUTEX_ERRORCHECK)
+  if (attr == __PTHREAD_ERRORCHECK_MUTEXATTR)
+    attr = &__pthread_errorcheck_mutexattr;
+  if (attr == __PTHREAD_RECURSIVE_MUTEXATTR)
+    attr = &__pthread_recursive_mutexattr;
+
+  if (attr && attr->mutex_type == PTHREAD_MUTEX_ERRORCHECK)
     {
 
       if (mutex->owner != _pthread_self ())
@@ -46,7 +53,7 @@ __pthread_mutex_transfer_np (struct __pt
 
 #ifndef NDEBUG
 # if !defined(ALWAYS_TRACK_MUTEX_OWNER)
-  if (mutex->attr && mutex->attr->mutex_type != PTHREAD_MUTEX_NORMAL)
+  if (attr && attr->mutex_type != PTHREAD_MUTEX_NORMAL)
 # endif
     {
       mutex->owner = thread;
--- a/libpthread/sysdeps/generic/pt-mutex-trylock.c
+++ b/libpthread/sysdeps/generic/pt-mutex-trylock.c
@@ -29,6 +29,12 @@ __pthread_mutex_trylock (struct __pthrea
 {
   int err;
   struct __pthread *self;
+  const struct __pthread_mutexattr *attr = mutex->attr;
+
+  if (attr == __PTHREAD_ERRORCHECK_MUTEXATTR)
+    attr = &__pthread_errorcheck_mutexattr;
+  if (attr == __PTHREAD_RECURSIVE_MUTEXATTR)
+    attr = &__pthread_recursive_mutexattr;
 
   __pthread_spin_lock (&mutex->__lock);
   if (__pthread_spin_trylock (&mutex->__held) == 0)
@@ -48,8 +54,8 @@ __pthread_mutex_trylock (struct __pthrea
 #endif
 #endif
 
-      if (mutex->attr)
-	switch (mutex->attr->mutex_type)
+      if (attr)
+	switch (attr->mutex_type)
 	  {
 	  case PTHREAD_MUTEX_NORMAL:
 	    break;
@@ -70,10 +76,10 @@ __pthread_mutex_trylock (struct __pthrea
 
   err = EBUSY;
 
-  if (mutex->attr)
+  if (attr)
     {
       self = _pthread_self ();
-      switch (mutex->attr->mutex_type)
+      switch (attr->mutex_type)
 	{
 	case PTHREAD_MUTEX_NORMAL:
 	  break;
--- a/libpthread/sysdeps/generic/pt-mutex-unlock.c
+++ b/libpthread/sysdeps/generic/pt-mutex-unlock.c
@@ -28,10 +28,16 @@ int
 __pthread_mutex_unlock (pthread_mutex_t *mutex)
 {
   struct __pthread *wakeup;
-  
+  const struct __pthread_mutexattr *attr = mutex->attr;
+
+  if (attr == __PTHREAD_ERRORCHECK_MUTEXATTR)
+    attr = &__pthread_errorcheck_mutexattr;
+  if (attr == __PTHREAD_RECURSIVE_MUTEXATTR)
+    attr = &__pthread_recursive_mutexattr;
+
   __pthread_spin_lock (&mutex->__lock);
 
-  if (! mutex->attr || mutex->attr->mutex_type == PTHREAD_MUTEX_NORMAL)
+  if (! attr || attr->mutex_type == PTHREAD_MUTEX_NORMAL)
     {
 #if defined(ALWAYS_TRACK_MUTEX_OWNER)
 # ifndef NDEBUG
@@ -45,7 +51,7 @@ __pthread_mutex_unlock (pthread_mutex_t 
 #endif
     }
   else
-    switch (mutex->attr->mutex_type)
+    switch (attr->mutex_type)
       {
       case PTHREAD_MUTEX_ERRORCHECK:
       case PTHREAD_MUTEX_RECURSIVE:
@@ -55,7 +61,7 @@ __pthread_mutex_unlock (pthread_mutex_t 
 	    return EPERM;
 	  }
 
-	if (mutex->attr->mutex_type == PTHREAD_MUTEX_RECURSIVE)
+	if (attr->mutex_type == PTHREAD_MUTEX_RECURSIVE)
 	  if (--mutex->locks > 0)
 	    {
 	      __pthread_spin_unlock (&mutex->__lock);
@@ -82,7 +88,7 @@ __pthread_mutex_unlock (pthread_mutex_t 
 
 #ifndef NDEBUG
 # if !defined (ALWAYS_TRACK_MUTEX_OWNER)
-  if (mutex->attr && mutex->attr->mutex_type != PTHREAD_MUTEX_NORMAL)
+  if (attr && attr->mutex_type != PTHREAD_MUTEX_NORMAL)
 # endif
     {
       mutex->owner = wakeup;
--- a/libpthread/sysdeps/generic/pt-mutexattr.c
+++ b/libpthread/sysdeps/generic/pt-mutexattr.c
@@ -28,6 +28,14 @@ const struct __pthread_mutexattr __pthre
   mutex_type: PTHREAD_MUTEX_DEFAULT
 };
 
+const struct __pthread_mutexattr __pthread_errorcheck_mutexattr =
+{
+  prioceiling: 0,
+  protocol: PTHREAD_PRIO_NONE,
+  pshared: PTHREAD_PROCESS_PRIVATE,
+  mutex_type: PTHREAD_MUTEX_ERRORCHECK
+};
+
 const struct __pthread_mutexattr __pthread_recursive_mutexattr =
 {
   prioceiling: 0,
--- a/libpthread/sysdeps/generic/bits/mutex.h
+++ b/libpthread/sysdeps/generic/bits/mutex.h
@@ -57,9 +57,17 @@ struct __pthread_mutex
 #  define __PTHREAD_MUTEX_INITIALIZER \
     { __PTHREAD_SPIN_LOCK_INITIALIZER, __PTHREAD_SPIN_LOCK_INITIALIZER, 0, 0, 0, 0, 0, 0 }
 
+#  define __PTHREAD_ERRORCHECK_MUTEXATTR ((struct __pthread_mutexattr *) ((unsigned long) __PTHREAD_MUTEX_ERRORCHECK + 1))
+
+#  define __PTHREAD_ERRORCHECK_MUTEX_INITIALIZER \
+    { __PTHREAD_SPIN_LOCK_INITIALIZER, __PTHREAD_SPIN_LOCK_INITIALIZER, 0, 0,	\
+	__PTHREAD_ERRORCHECK_MUTEXATTR, 0, 0, 0 }
+
+#  define __PTHREAD_RECURSIVE_MUTEXATTR ((struct __pthread_mutexattr *) ((unsigned long) __PTHREAD_MUTEX_RECURSIVE + 1))
+
 #  define __PTHREAD_RECURSIVE_MUTEX_INITIALIZER \
     { __PTHREAD_SPIN_LOCK_INITIALIZER, __PTHREAD_SPIN_LOCK_INITIALIZER, 0, 0,	\
-	(struct __pthread_mutexattr *) &__pthread_recursive_mutexattr, 0, 0, 0 }
+	__PTHREAD_RECURSIVE_MUTEXATTR, 0, 0, 0 }
 
 # endif
 #endif /* Not __pthread_mutex_defined.  */
--- a/libpthread/sysdeps/generic/bits/mutex-attr.h
+++ b/libpthread/sysdeps/generic/bits/mutex-attr.h
@@ -35,6 +35,7 @@ struct __pthread_mutexattr
 };
 
 /* Attributes for a recursive mutex.  */
+extern const struct __pthread_mutexattr __pthread_errorcheck_mutexattr;
 extern const struct __pthread_mutexattr __pthread_recursive_mutexattr;
 
 #endif /* bits/mutex-attr.h */