1 /* Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; version 2 of the License.
6
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
11
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15
16 /* This makes a wrapper for mutex handling to make it easier to debug mutex */
17
18 #include <my_global.h>
19 #if defined(TARGET_OS_LINUX) && !defined (__USE_UNIX98)
20 #define __USE_UNIX98 /* To get rw locks under Linux */
21 #endif
22 #if defined(SAFE_MUTEX)
23 #undef SAFE_MUTEX /* Avoid safe_mutex redefinitions */
24 #include "mysys_priv.h"
25 #include "my_static.h"
26 #include <m_string.h>
27
28 #ifndef DO_NOT_REMOVE_THREAD_WRAPPERS
29 /* Remove wrappers */
30 #undef pthread_mutex_t
31 #undef pthread_mutex_init
32 #undef pthread_mutex_lock
33 #undef pthread_mutex_unlock
34 #undef pthread_mutex_destroy
35 #undef pthread_cond_wait
36 #undef pthread_cond_timedwait
37 #ifdef HAVE_NONPOSIX_PTHREAD_MUTEX_INIT
38 #define pthread_mutex_init(a,b) my_pthread_mutex_init((a),(b))
39 #endif
40 #endif /* DO_NOT_REMOVE_THREAD_WRAPPERS */
41
42 /* Not instrumented */
43 static pthread_mutex_t THR_LOCK_mutex;
44 static ulong safe_mutex_count= 0; /* Number of mutexes created */
45 #ifdef SAFE_MUTEX_DETECT_DESTROY
46 static struct st_safe_mutex_info_t *safe_mutex_root= NULL;
47 #endif
48
safe_mutex_global_init(void)49 void safe_mutex_global_init(void)
50 {
51 pthread_mutex_init(&THR_LOCK_mutex,MY_MUTEX_INIT_FAST);
52 }
53
54
safe_mutex_init(safe_mutex_t * mp,const pthread_mutexattr_t * attr,const char * file,uint line)55 int safe_mutex_init(safe_mutex_t *mp,
56 const pthread_mutexattr_t *attr __attribute__((unused)),
57 const char *file,
58 uint line)
59 {
60 bzero((char*) mp,sizeof(*mp));
61 pthread_mutex_init(&mp->global,MY_MUTEX_INIT_ERRCHK);
62 pthread_mutex_init(&mp->mutex,attr);
63 /* Mark that mutex is initialized */
64 mp->file= file;
65 mp->line= line;
66
67 #ifdef SAFE_MUTEX_DETECT_DESTROY
68 /*
69 Monitor the freeing of mutexes. This code depends on single thread init
70 and destroy
71 */
72 if ((mp->info= (safe_mutex_info_t *) malloc(sizeof(safe_mutex_info_t))))
73 {
74 struct st_safe_mutex_info_t *info =mp->info;
75
76 info->init_file= file;
77 info->init_line= line;
78 info->prev= NULL;
79 info->next= NULL;
80
81 pthread_mutex_lock(&THR_LOCK_mutex);
82 if ((info->next= safe_mutex_root))
83 safe_mutex_root->prev= info;
84 safe_mutex_root= info;
85 safe_mutex_count++;
86 pthread_mutex_unlock(&THR_LOCK_mutex);
87 }
88 #else
89 pthread_mutex_lock(&THR_LOCK_mutex);
90 safe_mutex_count++;
91 pthread_mutex_unlock(&THR_LOCK_mutex);
92 #endif /* SAFE_MUTEX_DETECT_DESTROY */
93 return 0;
94 }
95
96
safe_mutex_lock(safe_mutex_t * mp,my_bool try_lock,const char * file,uint line)97 int safe_mutex_lock(safe_mutex_t *mp, my_bool try_lock, const char *file, uint line)
98 {
99 int error;
100 if (!mp->file)
101 {
102 fprintf(stderr,
103 "safe_mutex: Trying to lock unitialized mutex at %s, line %d\n",
104 file, line);
105 fflush(stderr);
106 abort();
107 }
108
109 pthread_mutex_lock(&mp->global);
110 if (mp->count > 0)
111 {
112 if (try_lock)
113 {
114 pthread_mutex_unlock(&mp->global);
115 return EBUSY;
116 }
117 else if (pthread_equal(pthread_self(),mp->thread))
118 {
119 fprintf(stderr,
120 "safe_mutex: Trying to lock mutex at %s, line %d, when the"
121 " mutex was already locked at %s, line %d in thread %s\n",
122 file,line,mp->file, mp->line, my_thread_name());
123 fflush(stderr);
124 abort();
125 }
126 }
127 pthread_mutex_unlock(&mp->global);
128
129 /*
130 If we are imitating trylock(), we need to take special
131 precautions.
132
133 - We cannot use pthread_mutex_lock() only since another thread can
134 overtake this thread and take the lock before this thread
135 causing pthread_mutex_trylock() to hang. In this case, we should
136 just return EBUSY. Hence, we use pthread_mutex_trylock() to be
137 able to return immediately.
138
139 - We cannot just use trylock() and continue execution below, since
140 this would generate an error and abort execution if the thread
141 was overtaken and trylock() returned EBUSY . In this case, we
142 instead just return EBUSY, since this is the expected behaviour
143 of trylock().
144 */
145 if (try_lock)
146 {
147 error= pthread_mutex_trylock(&mp->mutex);
148 if (error == EBUSY)
149 return error;
150 }
151 else
152 error= pthread_mutex_lock(&mp->mutex);
153
154 if (error || (error=pthread_mutex_lock(&mp->global)))
155 {
156 fprintf(stderr,"Got error %d when trying to lock mutex at %s, line %d\n",
157 error, file, line);
158 fflush(stderr);
159 abort();
160 }
161 mp->thread= pthread_self();
162 if (mp->count++)
163 {
164 fprintf(stderr,"safe_mutex: Error in thread libray: Got mutex at %s, \
165 line %d more than 1 time\n", file,line);
166 fflush(stderr);
167 abort();
168 }
169 mp->file= file;
170 mp->line=line;
171 pthread_mutex_unlock(&mp->global);
172 return error;
173 }
174
175
safe_mutex_unlock(safe_mutex_t * mp,const char * file,uint line)176 int safe_mutex_unlock(safe_mutex_t *mp,const char *file, uint line)
177 {
178 int error;
179 pthread_mutex_lock(&mp->global);
180 if (mp->count == 0)
181 {
182 fprintf(stderr,"safe_mutex: Trying to unlock mutex that wasn't locked at %s, line %d\n Last used at %s, line: %d\n",
183 file,line,mp->file ? mp->file : "",mp->line);
184 fflush(stderr);
185 abort();
186 }
187 if (!pthread_equal(pthread_self(),mp->thread))
188 {
189 fprintf(stderr,"safe_mutex: Trying to unlock mutex at %s, line %d that was locked by another thread at: %s, line: %d\n",
190 file,line,mp->file,mp->line);
191 fflush(stderr);
192 abort();
193 }
194 mp->thread= 0;
195 mp->count--;
196 #ifdef __WIN__
197 pthread_mutex_unlock(&mp->mutex);
198 error=0;
199 #else
200 error=pthread_mutex_unlock(&mp->mutex);
201 if (error)
202 {
203 fprintf(stderr,"safe_mutex: Got error: %d (%d) when trying to unlock mutex at %s, line %d\n", error, errno, file, line);
204 fflush(stderr);
205 abort();
206 }
207 #endif /* __WIN__ */
208 pthread_mutex_unlock(&mp->global);
209 return error;
210 }
211
212
safe_cond_wait(pthread_cond_t * cond,safe_mutex_t * mp,const char * file,uint line)213 int safe_cond_wait(pthread_cond_t *cond, safe_mutex_t *mp, const char *file,
214 uint line)
215 {
216 int error;
217 pthread_mutex_lock(&mp->global);
218 if (mp->count == 0)
219 {
220 fprintf(stderr,"safe_mutex: Trying to cond_wait on a unlocked mutex at %s, line %d\n",file,line);
221 fflush(stderr);
222 abort();
223 }
224 if (!pthread_equal(pthread_self(),mp->thread))
225 {
226 fprintf(stderr,"safe_mutex: Trying to cond_wait on a mutex at %s, line %d that was locked by another thread at: %s, line: %d\n",
227 file,line,mp->file,mp->line);
228 fflush(stderr);
229 abort();
230 }
231
232 if (mp->count-- != 1)
233 {
234 fprintf(stderr,"safe_mutex: Count was %d on locked mutex at %s, line %d\n",
235 mp->count+1, file, line);
236 fflush(stderr);
237 abort();
238 }
239 pthread_mutex_unlock(&mp->global);
240 error=pthread_cond_wait(cond,&mp->mutex);
241 pthread_mutex_lock(&mp->global);
242 if (error)
243 {
244 fprintf(stderr,"safe_mutex: Got error: %d (%d) when doing a safe_mutex_wait at %s, line %d\n", error, errno, file, line);
245 fflush(stderr);
246 abort();
247 }
248 mp->thread=pthread_self();
249 if (mp->count++)
250 {
251 fprintf(stderr,
252 "safe_mutex: Count was %d in thread 0x%lx when locking mutex at %s, line %d\n",
253 mp->count-1, my_thread_dbug_id(), file, line);
254 fflush(stderr);
255 abort();
256 }
257 mp->file= file;
258 mp->line=line;
259 pthread_mutex_unlock(&mp->global);
260 return error;
261 }
262
263
safe_cond_timedwait(pthread_cond_t * cond,safe_mutex_t * mp,const struct timespec * abstime,const char * file,uint line)264 int safe_cond_timedwait(pthread_cond_t *cond, safe_mutex_t *mp,
265 const struct timespec *abstime,
266 const char *file, uint line)
267 {
268 int error;
269 pthread_mutex_lock(&mp->global);
270 if (mp->count != 1 || !pthread_equal(pthread_self(),mp->thread))
271 {
272 fprintf(stderr,"safe_mutex: Trying to cond_wait at %s, line %d on a not hold mutex\n",file,line);
273 fflush(stderr);
274 abort();
275 }
276 mp->count--; /* Mutex will be released */
277 pthread_mutex_unlock(&mp->global);
278 error=pthread_cond_timedwait(cond,&mp->mutex,abstime);
279 #ifdef EXTRA_DEBUG
280 if (error && (error != EINTR && error != ETIMEDOUT && error != ETIME))
281 {
282 fprintf(stderr,"safe_mutex: Got error: %d (%d) when doing a safe_mutex_timedwait at %s, line %d\n", error, errno, file, line);
283 }
284 #endif
285 pthread_mutex_lock(&mp->global);
286 mp->thread=pthread_self();
287 if (mp->count++)
288 {
289 fprintf(stderr,
290 "safe_mutex: Count was %d in thread 0x%lx when locking mutex at %s, line %d (error: %d (%d))\n",
291 mp->count-1, my_thread_dbug_id(), file, line, error, error);
292 fflush(stderr);
293 abort();
294 }
295 mp->file= file;
296 mp->line=line;
297 pthread_mutex_unlock(&mp->global);
298 return error;
299 }
300
301
safe_mutex_destroy(safe_mutex_t * mp,const char * file,uint line)302 int safe_mutex_destroy(safe_mutex_t *mp, const char *file, uint line)
303 {
304 int error=0;
305 if (!mp->file)
306 {
307 fprintf(stderr,
308 "safe_mutex: Trying to destroy unitialized mutex at %s, line %d\n",
309 file, line);
310 fflush(stderr);
311 abort();
312 }
313 if (mp->count != 0)
314 {
315 fprintf(stderr,"safe_mutex: Trying to destroy a mutex that was locked at %s, line %d at %s, line %d\n",
316 mp->file,mp->line, file, line);
317 fflush(stderr);
318 abort();
319 }
320 #ifdef __WIN__
321 pthread_mutex_destroy(&mp->global);
322 pthread_mutex_destroy(&mp->mutex);
323 #else
324 if (pthread_mutex_destroy(&mp->global))
325 error=1;
326 if (pthread_mutex_destroy(&mp->mutex))
327 error=1;
328 #endif
329 mp->file= 0; /* Mark destroyed */
330
331 #ifdef SAFE_MUTEX_DETECT_DESTROY
332 if (mp->info)
333 {
334 struct st_safe_mutex_info_t *info= mp->info;
335 pthread_mutex_lock(&THR_LOCK_mutex);
336
337 if (info->prev)
338 info->prev->next = info->next;
339 else
340 safe_mutex_root = info->next;
341 if (info->next)
342 info->next->prev = info->prev;
343 safe_mutex_count--;
344
345 pthread_mutex_unlock(&THR_LOCK_mutex);
346 free(info);
347 mp->info= NULL; /* Get crash if double free */
348 }
349 #else
350 pthread_mutex_lock(&THR_LOCK_mutex);
351 safe_mutex_count--;
352 pthread_mutex_unlock(&THR_LOCK_mutex);
353 #endif /* SAFE_MUTEX_DETECT_DESTROY */
354 return error;
355 }
356
357
358 /*
359 Free global resources and check that all mutex has been destroyed
360
361 SYNOPSIS
362 safe_mutex_end()
363 file Print errors on this file
364
365 NOTES
366 We can't use DBUG_PRINT() here as we have in my_end() disabled
367 DBUG handling before calling this function.
368
369 In MySQL one may get one warning for a mutex created in my_thr_init.c
370 This is ok, as this thread may not yet have been exited.
371 */
372
safe_mutex_end(FILE * file)373 void safe_mutex_end(FILE *file __attribute__((unused)))
374 {
375 if (!safe_mutex_count) /* safetly */
376 pthread_mutex_destroy(&THR_LOCK_mutex);
377 #ifdef SAFE_MUTEX_DETECT_DESTROY
378 if (!file)
379 return;
380
381 if (safe_mutex_count)
382 {
383 fprintf(file, "Warning: Not destroyed mutex: %lu\n", safe_mutex_count);
384 (void) fflush(file);
385 }
386 {
387 struct st_safe_mutex_info_t *ptr;
388 for (ptr= safe_mutex_root ; ptr ; ptr= ptr->next)
389 {
390 fprintf(file, "\tMutex initiated at line %4u in '%s'\n",
391 ptr->init_line, ptr->init_file);
392 (void) fflush(file);
393 }
394 }
395 #endif /* SAFE_MUTEX_DETECT_DESTROY */
396 }
397
398 #endif /* SAFE_MUTEX */
399
400 #if defined(MY_PTHREAD_FASTMUTEX) && !defined(SAFE_MUTEX)
401
402 #include "mysys_priv.h"
403 #include "my_static.h"
404 #include <m_string.h>
405
406 #include <m_ctype.h>
407 #include <hash.h>
408 #include <myisampack.h>
409 #include <mysys_err.h>
410 #include <my_sys.h>
411
412 #undef pthread_mutex_t
413 #undef pthread_mutex_init
414 #undef pthread_mutex_lock
415 #undef pthread_mutex_trylock
416 #undef pthread_mutex_unlock
417 #undef pthread_mutex_destroy
418 #undef pthread_cond_wait
419 #undef pthread_cond_timedwait
420
mutex_delay(ulong delayloops)421 ulong mutex_delay(ulong delayloops)
422 {
423 ulong i;
424 volatile ulong j;
425
426 j = 0;
427
428 for (i = 0; i < delayloops * 50; i++)
429 j += i;
430
431 return(j);
432 }
433
434 #define MY_PTHREAD_FASTMUTEX_SPINS 8
435 #define MY_PTHREAD_FASTMUTEX_DELAY 4
436
437 static int cpu_count= 0;
438
my_pthread_fastmutex_init(my_pthread_fastmutex_t * mp,const pthread_mutexattr_t * attr)439 int my_pthread_fastmutex_init(my_pthread_fastmutex_t *mp,
440 const pthread_mutexattr_t *attr)
441 {
442 if ((cpu_count > 1) && (attr == MY_MUTEX_INIT_FAST))
443 mp->spins= MY_PTHREAD_FASTMUTEX_SPINS;
444 else
445 mp->spins= 0;
446 mp->rng_state= 1;
447 return pthread_mutex_init(&mp->mutex, attr);
448 }
449
450 /**
451 Park-Miller random number generator. A simple linear congruential
452 generator that operates in multiplicative group of integers modulo n.
453
454 x_{k+1} = (x_k g) mod n
455
456 Popular pair of parameters: n = 2^32 − 5 = 4294967291 and g = 279470273.
457 The period of the generator is about 2^31.
458 Largest value that can be returned: 2147483646 (RAND_MAX)
459
460 Reference:
461
462 S. K. Park and K. W. Miller
463 "Random number generators: good ones are hard to find"
464 Commun. ACM, October 1988, Volume 31, No 10, pages 1192-1201.
465 */
466
park_rng(my_pthread_fastmutex_t * mp)467 static double park_rng(my_pthread_fastmutex_t *mp)
468 {
469 mp->rng_state= ((my_ulonglong)mp->rng_state * 279470273U) % 4294967291U;
470 return (mp->rng_state / 2147483647.0);
471 }
472
my_pthread_fastmutex_lock(my_pthread_fastmutex_t * mp)473 int my_pthread_fastmutex_lock(my_pthread_fastmutex_t *mp)
474 {
475 int res;
476 uint i;
477 uint maxdelay= MY_PTHREAD_FASTMUTEX_DELAY;
478
479 for (i= 0; i < mp->spins; i++)
480 {
481 res= pthread_mutex_trylock(&mp->mutex);
482
483 if (res == 0)
484 return 0;
485
486 if (res != EBUSY)
487 return res;
488
489 mutex_delay(maxdelay);
490 maxdelay += park_rng(mp) * MY_PTHREAD_FASTMUTEX_DELAY + 1;
491 }
492 return pthread_mutex_lock(&mp->mutex);
493 }
494
495
fastmutex_global_init(void)496 void fastmutex_global_init(void)
497 {
498 #ifdef _SC_NPROCESSORS_CONF
499 cpu_count= sysconf(_SC_NPROCESSORS_CONF);
500 #endif
501 }
502
503 #endif /* defined(MY_PTHREAD_FASTMUTEX) && !defined(SAFE_MUTEX) */
504