~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000-2003 MySQL AB
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
16
/* This makes a wrapper for mutex handling to make it easier to debug mutex */
17
212.5.39 by Monty Taylor
Phew. Moved my_base and my_global.
18
#include "mysys_priv.h"
19
1 by brian
clean slate
20
#if defined(TARGET_OS_LINUX) && !defined (__USE_UNIX98)
21
#define __USE_UNIX98			/* To get rw locks under Linux */
22
#endif
28.1.35 by Monty Taylor
Removed all references to THREAD.
23
#if defined(SAFE_MUTEX)
1 by brian
clean slate
24
#undef SAFE_MUTEX			/* Avoid safe_mutex redefinitions */
25
#include "mysys_priv.h"
26
#include "my_static.h"
27
#include <m_string.h>
28
29
#ifndef DO_NOT_REMOVE_THREAD_WRAPPERS
30
/* Remove wrappers */
31
#undef pthread_mutex_t
32
#undef pthread_mutex_init
33
#undef pthread_mutex_lock
34
#undef pthread_mutex_unlock
35
#undef pthread_mutex_destroy
36
#undef pthread_cond_wait
37
#undef pthread_cond_timedwait
38
#ifdef HAVE_NONPOSIX_PTHREAD_MUTEX_INIT
39
#define pthread_mutex_init(a,b) my_pthread_mutex_init((a),(b))
40
#endif
41
#endif /* DO_NOT_REMOVE_THREAD_WRAPPERS */
42
43
static pthread_mutex_t THR_LOCK_mutex;
298 by Brian Aker
ulong conversion.
44
static uint32_t safe_mutex_count= 0;		/* Number of mutexes created */
1 by brian
clean slate
45
#ifdef SAFE_MUTEX_DETECT_DESTROY
46
static struct st_safe_mutex_info_t *safe_mutex_root= NULL;
47
#endif
48
49
void safe_mutex_global_init(void)
50
{
51
  pthread_mutex_init(&THR_LOCK_mutex,MY_MUTEX_INIT_FAST);
52
}
53
54
55
int safe_mutex_init(safe_mutex_t *mp,
657 by Brian Aker
Random cleanup, removed dead mysys call for hw address creation.
56
		    const pthread_mutexattr_t *, const char *file, uint32_t line)
1 by brian
clean slate
57
{
212.6.14 by Mats Kindahl
Removing redundant use of casts in mysys for memcmp(), memcpy(), memset(), and memmove().
58
  memset(mp, 0, sizeof(*mp));
1 by brian
clean slate
59
  pthread_mutex_init(&mp->global,MY_MUTEX_INIT_ERRCHK);
60
  pthread_mutex_init(&mp->mutex,attr);
61
  /* Mark that mutex is initialized */
62
  mp->file= file;
63
  mp->line= line;
64
65
#ifdef SAFE_MUTEX_DETECT_DESTROY
66
  /*
67
    Monitor the freeing of mutexes.  This code depends on single thread init
68
    and destroy
69
  */
70
  if ((mp->info= (safe_mutex_info_t *) malloc(sizeof(safe_mutex_info_t))))
71
  {
72
    struct st_safe_mutex_info_t *info =mp->info;
73
74
    info->init_file= file;
75
    info->init_line= line;
76
    info->prev= NULL;
77
    info->next= NULL;
78
79
    pthread_mutex_lock(&THR_LOCK_mutex);
80
    if ((info->next= safe_mutex_root))
81
      safe_mutex_root->prev= info;
82
    safe_mutex_root= info;
83
    safe_mutex_count++;
84
    pthread_mutex_unlock(&THR_LOCK_mutex);
85
  }
86
#else
87
  thread_safe_increment(safe_mutex_count, &THR_LOCK_mutex);
88
#endif /* SAFE_MUTEX_DETECT_DESTROY */
89
  return 0;
90
}
91
92
482 by Brian Aker
Remove uint.
93
int safe_mutex_lock(safe_mutex_t *mp, bool try_lock, const char *file, uint32_t line)
1 by brian
clean slate
94
{
95
  int error;
96
  if (!mp->file)
97
  {
98
    fprintf(stderr,
99
	    "safe_mutex: Trying to lock unitialized mutex at %s, line %d\n",
100
	    file, line);
101
    fflush(stderr);
102
    abort();
103
  }
104
105
  pthread_mutex_lock(&mp->global);
106
  if (mp->count > 0)
107
  {
108
    if (try_lock)
109
    {
110
      pthread_mutex_unlock(&mp->global);
111
      return EBUSY;
112
    }
113
    else if (pthread_equal(pthread_self(),mp->thread))
114
    {
115
      fprintf(stderr,
116
              "safe_mutex: Trying to lock mutex at %s, line %d, when the"
117
              " mutex was already locked at %s, line %d in thread %s\n",
118
              file,line,mp->file, mp->line, my_thread_name());
119
      fflush(stderr);
120
      abort();
121
    }
122
  }
123
  pthread_mutex_unlock(&mp->global);
124
125
  /*
126
    If we are imitating trylock(), we need to take special
127
    precautions.
128
129
    - We cannot use pthread_mutex_lock() only since another thread can
130
      overtake this thread and take the lock before this thread
131
      causing pthread_mutex_trylock() to hang. In this case, we should
132
      just return EBUSY. Hence, we use pthread_mutex_trylock() to be
133
      able to return immediately.
134
135
    - We cannot just use trylock() and continue execution below, since
136
      this would generate an error and abort execution if the thread
137
      was overtaken and trylock() returned EBUSY . In this case, we
138
      instead just return EBUSY, since this is the expected behaviour
139
      of trylock().
140
   */
141
  if (try_lock)
142
  {
143
    error= pthread_mutex_trylock(&mp->mutex);
144
    if (error == EBUSY)
145
      return error;
146
  }
147
  else
148
    error= pthread_mutex_lock(&mp->mutex);
149
150
  if (error || (error=pthread_mutex_lock(&mp->global)))
151
  {
152
    fprintf(stderr,"Got error %d when trying to lock mutex at %s, line %d\n",
153
	    error, file, line);
154
    fflush(stderr);
155
    abort();
156
  }
157
  mp->thread= pthread_self();
158
  if (mp->count++)
159
  {
160
    fprintf(stderr,"safe_mutex: Error in thread libray: Got mutex at %s, \
161
line %d more than 1 time\n", file,line);
162
    fflush(stderr);
163
    abort();
164
  }
165
  mp->file= file;
166
  mp->line=line;
167
  pthread_mutex_unlock(&mp->global);
168
  return error;
169
}
170
171
482 by Brian Aker
Remove uint.
172
int safe_mutex_unlock(safe_mutex_t *mp,const char *file, uint32_t line)
1 by brian
clean slate
173
{
174
  int error;
175
  pthread_mutex_lock(&mp->global);
176
  if (mp->count == 0)
177
  {
178
    fprintf(stderr,"safe_mutex: Trying to unlock mutex that wasn't locked at %s, line %d\n            Last used at %s, line: %d\n",
179
	    file,line,mp->file ? mp->file : "",mp->line);
180
    fflush(stderr);
181
    abort();
182
  }
183
  if (!pthread_equal(pthread_self(),mp->thread))
184
  {
185
    fprintf(stderr,"safe_mutex: Trying to unlock mutex at %s, line %d  that was locked by another thread at: %s, line: %d\n",
186
	    file,line,mp->file,mp->line);
187
    fflush(stderr);
188
    abort();
189
  }
190
  mp->thread= 0;
191
  mp->count--;
192
  error=pthread_mutex_unlock(&mp->mutex);
193
  if (error)
194
  {
195
    fprintf(stderr,"safe_mutex: Got error: %d (%d) when trying to unlock mutex at %s, line %d\n", error, errno, file, line);
196
    fflush(stderr);
197
    abort();
198
  }
199
  pthread_mutex_unlock(&mp->global);
200
  return error;
201
}
202
203
204
int safe_cond_wait(pthread_cond_t *cond, safe_mutex_t *mp, const char *file,
482 by Brian Aker
Remove uint.
205
		   uint32_t line)
1 by brian
clean slate
206
{
207
  int error;
208
  pthread_mutex_lock(&mp->global);
209
  if (mp->count == 0)
210
  {
211
    fprintf(stderr,"safe_mutex: Trying to cond_wait on a unlocked mutex at %s, line %d\n",file,line);
212
    fflush(stderr);
213
    abort();
214
  }
215
  if (!pthread_equal(pthread_self(),mp->thread))
216
  {
217
    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",
218
	    file,line,mp->file,mp->line);
219
    fflush(stderr);
220
    abort();
221
  }
222
223
  if (mp->count-- != 1)
224
  {
225
    fprintf(stderr,"safe_mutex:  Count was %d on locked mutex at %s, line %d\n",
226
	    mp->count+1, file, line);
227
    fflush(stderr);
228
    abort();
229
  }
230
  pthread_mutex_unlock(&mp->global);
231
  error=pthread_cond_wait(cond,&mp->mutex);
232
  pthread_mutex_lock(&mp->global);
233
  if (error)
234
  {
235
    fprintf(stderr,"safe_mutex: Got error: %d (%d) when doing a safe_mutex_wait at %s, line %d\n", error, errno, file, line);
236
    fflush(stderr);
237
    abort();
238
  }
239
  mp->thread=pthread_self();
240
  if (mp->count++)
241
  {
242
    fprintf(stderr,
243
	    "safe_mutex:  Count was %d in thread 0x%lx when locking mutex at %s, line %d\n",
244
	    mp->count-1, my_thread_dbug_id(), file, line);
245
    fflush(stderr);
246
    abort();
247
  }
248
  mp->file= file;
249
  mp->line=line;
250
  pthread_mutex_unlock(&mp->global);
251
  return error;
252
}
253
254
255
int safe_cond_timedwait(pthread_cond_t *cond, safe_mutex_t *mp,
256
			struct timespec *abstime,
482 by Brian Aker
Remove uint.
257
			const char *file, uint32_t line)
1 by brian
clean slate
258
{
259
  int error;
260
  pthread_mutex_lock(&mp->global);
261
  if (mp->count != 1 || !pthread_equal(pthread_self(),mp->thread))
262
  {
263
    fprintf(stderr,"safe_mutex: Trying to cond_wait at %s, line %d on a not hold mutex\n",file,line);
264
    fflush(stderr);
265
    abort();
266
  }
267
  mp->count--;					/* Mutex will be released */
268
  pthread_mutex_unlock(&mp->global);
269
  error=pthread_cond_timedwait(cond,&mp->mutex,abstime);
270
#ifdef EXTRA_DEBUG
271
  if (error && (error != EINTR && error != ETIMEDOUT && error != ETIME))
272
  {
273
    fprintf(stderr,"safe_mutex: Got error: %d (%d) when doing a safe_mutex_timedwait at %s, line %d\n", error, errno, file, line);
274
  }
275
#endif
276
  pthread_mutex_lock(&mp->global);
277
  mp->thread=pthread_self();
278
  if (mp->count++)
279
  {
280
    fprintf(stderr,
281
	    "safe_mutex:  Count was %d in thread 0x%lx when locking mutex at %s, line %d (error: %d (%d))\n",
282
	    mp->count-1, my_thread_dbug_id(), file, line, error, error);
283
    fflush(stderr);
284
    abort();
285
  }
286
  mp->file= file;
287
  mp->line=line;
288
  pthread_mutex_unlock(&mp->global);
289
  return error;
290
}
291
292
482 by Brian Aker
Remove uint.
293
int safe_mutex_destroy(safe_mutex_t *mp, const char *file, uint32_t line)
1 by brian
clean slate
294
{
295
  int error=0;
296
  if (!mp->file)
297
  {
298
    fprintf(stderr,
299
	    "safe_mutex: Trying to destroy unitialized mutex at %s, line %d\n",
300
	    file, line);
301
    fflush(stderr);
302
    abort();
303
  }
304
  if (mp->count != 0)
305
  {
306
    fprintf(stderr,"safe_mutex: Trying to destroy a mutex that was locked at %s, line %d at %s, line %d\n",
307
	    mp->file,mp->line, file, line);
308
    fflush(stderr);
309
    abort();
310
  }
311
  if (pthread_mutex_destroy(&mp->global))
312
    error=1;
313
  if (pthread_mutex_destroy(&mp->mutex))
314
    error=1;
315
  mp->file= 0;					/* Mark destroyed */
316
317
#ifdef SAFE_MUTEX_DETECT_DESTROY
318
  if (mp->info)
319
  {
320
    struct st_safe_mutex_info_t *info= mp->info;
321
    pthread_mutex_lock(&THR_LOCK_mutex);
322
323
    if (info->prev)
324
      info->prev->next = info->next;
325
    else
326
      safe_mutex_root = info->next;
327
    if (info->next)
328
      info->next->prev = info->prev;
329
    safe_mutex_count--;
330
331
    pthread_mutex_unlock(&THR_LOCK_mutex);
332
    free(info);
333
    mp->info= NULL;				/* Get crash if double free */
334
  }
335
#else
336
  thread_safe_sub(safe_mutex_count, 1, &THR_LOCK_mutex);
337
#endif /* SAFE_MUTEX_DETECT_DESTROY */
338
  return error;
339
}
340
341
342
/*
343
  Free global resources and check that all mutex has been destroyed
344
345
  SYNOPSIS
346
    safe_mutex_end()
347
    file		Print errors on this file
348
349
  NOTES
350
351
   In MySQL one may get one warning for a mutex created in my_thr_init.c
352
   This is ok, as this thread may not yet have been exited.
353
*/
354
657 by Brian Aker
Random cleanup, removed dead mysys call for hw address creation.
355
void safe_mutex_end(int *)
1 by brian
clean slate
356
{
357
  if (!safe_mutex_count)			/* safetly */
358
    pthread_mutex_destroy(&THR_LOCK_mutex);
359
#ifdef SAFE_MUTEX_DETECT_DESTROY
360
  if (!file)
361
    return;
362
363
  if (safe_mutex_count)
364
  {
365
    fprintf(file, "Warning: Not destroyed mutex: %lu\n", safe_mutex_count);
366
    (void) fflush(file);
367
  }
368
  {
369
    struct st_safe_mutex_info_t *ptr;
370
    for (ptr= safe_mutex_root ; ptr ; ptr= ptr->next)
371
    {
372
      fprintf(file, "\tMutex initiated at line %4u in '%s'\n",
373
	      ptr->init_line, ptr->init_file);
374
      (void) fflush(file);
375
    }
376
  }
377
#endif /* SAFE_MUTEX_DETECT_DESTROY */
378
}
379
380
#endif /* THREAD && SAFE_MUTEX */
381