54
54
int safe_mutex_init(safe_mutex_t *mp,
55
55
const pthread_mutexattr_t *attr __attribute__((unused)),
57
uint line, const char *name)
59
59
bzero((char*) mp,sizeof(*mp));
60
60
pthread_mutex_init(&mp->global,MY_MUTEX_INIT_ERRCHK);
94
96
int safe_mutex_lock(safe_mutex_t *mp, my_bool try_lock, const char *file, uint line)
99
DBUG_PRINT("mutex", ("%s (0x%lx) locking", mp->name ? mp->name : "Null",
151
155
if (error || (error=pthread_mutex_lock(&mp->global)))
153
fprintf(stderr,"Got error %d when trying to lock mutex at %s, line %d\n",
157
fprintf(stderr,"Got error %d when trying to lock mutex %s at %s, line %d\n",
158
error, mp->name, file, line);
158
162
mp->thread= pthread_self();
161
fprintf(stderr,"safe_mutex: Error in thread libray: Got mutex at %s, \
162
line %d more than 1 time\n", file,line);
165
fprintf(stderr,"safe_mutex: Error in thread libray: Got mutex %s at %s, "
166
"line %d more than 1 time\n", mp->name, file,line);
168
172
pthread_mutex_unlock(&mp->global);
173
DBUG_PRINT("mutex", ("%s (0x%lx) locked", mp->name, (ulong) mp));
173
178
int safe_mutex_unlock(safe_mutex_t *mp,const char *file, uint line)
181
DBUG_PRINT("mutex", ("%s (0x%lx) unlocking", mp->name, (ulong) mp));
176
182
pthread_mutex_lock(&mp->global);
177
183
if (mp->count == 0)
179
fprintf(stderr,"safe_mutex: Trying to unlock mutex that wasn't locked at %s, line %d\n Last used at %s, line: %d\n",
180
file,line,mp->file ? mp->file : "",mp->line);
185
fprintf(stderr,"safe_mutex: Trying to unlock mutex %s that wasn't locked at %s, line %d\n"
186
"Last used at %s, line: %d\n",
187
mp->name ? mp->name : "Null", file, line,
188
mp->file ? mp->file : "Null", mp->line);
184
192
if (!pthread_equal(pthread_self(),mp->thread))
186
fprintf(stderr,"safe_mutex: Trying to unlock mutex at %s, line %d that was locked by another thread at: %s, line: %d\n",
187
file,line,mp->file,mp->line);
194
fprintf(stderr,"safe_mutex: Trying to unlock mutex %s at %s, line %d that was locked by "
195
"another thread at: %s, line: %d\n",
196
mp->name, file, line, mp->file, mp->line);
197
206
error=pthread_mutex_unlock(&mp->mutex);
200
fprintf(stderr,"safe_mutex: Got error: %d (%d) when trying to unlock mutex at %s, line %d\n", error, errno, file, line);
209
fprintf(stderr,"safe_mutex: Got error: %d (%d) when trying to unlock mutex %s at %s, "
210
"line %d\n", error, errno, mp->name, file, line);
214
224
pthread_mutex_lock(&mp->global);
215
225
if (mp->count == 0)
217
fprintf(stderr,"safe_mutex: Trying to cond_wait on a unlocked mutex at %s, line %d\n",file,line);
227
fprintf(stderr,"safe_mutex: Trying to cond_wait on a unlocked mutex %s at %s, line %d\n",
228
mp->name ? mp->name : "Null", file, line);
221
232
if (!pthread_equal(pthread_self(),mp->thread))
223
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",
224
file,line,mp->file,mp->line);
234
fprintf(stderr,"safe_mutex: Trying to cond_wait on a mutex %s at %s, line %d that was "
235
"locked by another thread at: %s, line: %d\n",
236
mp->name, file, line, mp->file, mp->line);
229
241
if (mp->count-- != 1)
231
fprintf(stderr,"safe_mutex: Count was %d on locked mutex at %s, line %d\n",
232
mp->count+1, file, line);
243
fprintf(stderr,"safe_mutex: Count was %d on locked mutex %s at %s, line %d\n",
244
mp->count+1, mp->name, file, line);
238
250
pthread_mutex_lock(&mp->global);
241
fprintf(stderr,"safe_mutex: Got error: %d (%d) when doing a safe_mutex_wait at %s, line %d\n", error, errno, file, line);
253
fprintf(stderr,"safe_mutex: Got error: %d (%d) when doing a safe_mutex_wait on %s at %s, "
254
"line %d\n", error, errno, mp->name, file, line);
249
"safe_mutex: Count was %d in thread 0x%lx when locking mutex at %s, line %d\n",
250
mp->count-1, my_thread_dbug_id(), file, line);
262
"safe_mutex: Count was %d in thread 0x%lx when locking mutex %s at %s, line %d\n",
263
mp->count-1, my_thread_dbug_id(), mp->name, file, line);
266
279
pthread_mutex_lock(&mp->global);
267
280
if (mp->count != 1 || !pthread_equal(pthread_self(),mp->thread))
269
fprintf(stderr,"safe_mutex: Trying to cond_wait at %s, line %d on a not hold mutex\n",file,line);
282
fprintf(stderr,"safe_mutex: Trying to cond_wait at %s, line %d on a not hold mutex %s\n",
283
file, line, mp->name ? mp->name : "Null");
276
290
#ifdef EXTRA_DEBUG
277
291
if (error && (error != EINTR && error != ETIMEDOUT && error != ETIME))
279
fprintf(stderr,"safe_mutex: Got error: %d (%d) when doing a safe_mutex_timedwait at %s, line %d\n", error, errno, file, line);
294
"safe_mutex: Got error: %d (%d) when doing a safe_mutex_timedwait on %s at %s, "
296
error, errno, mp->name, file, line);
282
299
pthread_mutex_lock(&mp->global);
287
"safe_mutex: Count was %d in thread 0x%lx when locking mutex at %s, line %d (error: %d (%d))\n",
288
mp->count-1, my_thread_dbug_id(), file, line, error, error);
304
"safe_mutex: Count was %d in thread 0x%lx when locking mutex %s at %s, line %d "
305
"(error: %d (%d))\n",
306
mp->count-1, my_thread_dbug_id(), mp->name, file, line,
310
329
if (mp->count != 0)
312
fprintf(stderr,"safe_mutex: Trying to destroy a mutex that was locked at %s, line %d at %s, line %d\n",
313
mp->file,mp->line, file, line);
331
fprintf(stderr,"safe_mutex: Trying to destroy a mutex %s that was locked at %s, "
332
"line %d at %s, line %d\n",
333
mp->name, mp->file, mp->line, file, line);
382
402
struct st_safe_mutex_info_t *ptr;
383
403
for (ptr= safe_mutex_root ; ptr ; ptr= ptr->next)
385
fprintf(file, "\tMutex initiated at line %4u in '%s'\n",
386
ptr->init_line, ptr->init_file);
405
fprintf(file, "\tMutex %s initiated at line %4u in '%s'\n",
406
ptr->name, ptr->init_line, ptr->init_file);
387
407
(void) fflush(file);