~jlukas79/+junk/mysql-server

« back to all changes in this revision

Viewing changes to mysys/thr_mutex.c

manual merge 6.0-main --> 6.0-bka-review

Show diffs side-by-side

added added

removed removed

Lines of Context:
54
54
int safe_mutex_init(safe_mutex_t *mp,
55
55
                    const pthread_mutexattr_t *attr __attribute__((unused)),
56
56
                    const char *file,
57
 
                    uint line)
 
57
                    uint line, const char *name)
58
58
{
59
59
  bzero((char*) mp,sizeof(*mp));
60
60
  pthread_mutex_init(&mp->global,MY_MUTEX_INIT_ERRCHK);
62
62
  /* Mark that mutex is initialized */
63
63
  mp->file= file;
64
64
  mp->line= line;
 
65
  /* Skip the very common '&' prefix from the autogenerated name */
 
66
  mp->name= name[0] == '&' ? name + 1 : name;
65
67
 
66
68
#ifdef SAFE_MUTEX_DETECT_DESTROY
67
69
  /*
94
96
int safe_mutex_lock(safe_mutex_t *mp, my_bool try_lock, const char *file, uint line)
95
97
{
96
98
  int error;
 
99
  DBUG_PRINT("mutex", ("%s (0x%lx) locking", mp->name ? mp->name : "Null",
 
100
                       (ulong) mp));
97
101
  if (!mp->file)
98
102
  {
99
103
    fprintf(stderr,
150
154
 
151
155
  if (error || (error=pthread_mutex_lock(&mp->global)))
152
156
  {
153
 
    fprintf(stderr,"Got error %d when trying to lock mutex at %s, line %d\n",
154
 
            error, file, line);
 
157
    fprintf(stderr,"Got error %d when trying to lock mutex %s at %s, line %d\n",
 
158
            error, mp->name, file, line);
155
159
    fflush(stderr);
156
160
    abort();
157
161
  }
158
162
  mp->thread= pthread_self();
159
163
  if (mp->count++)
160
164
  {
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);
163
167
    fflush(stderr);
164
168
    abort();
165
169
  }
166
170
  mp->file= file;
167
 
  mp->line=line;
 
171
  mp->line= line;
168
172
  pthread_mutex_unlock(&mp->global);
 
173
  DBUG_PRINT("mutex", ("%s (0x%lx) locked", mp->name, (ulong) mp));
169
174
  return error;
170
175
}
171
176
 
173
178
int safe_mutex_unlock(safe_mutex_t *mp,const char *file, uint line)
174
179
{
175
180
  int error;
 
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)
178
184
  {
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);
181
189
    fflush(stderr);
182
190
    abort();
183
191
  }
184
192
  if (!pthread_equal(pthread_self(),mp->thread))
185
193
  {
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);
188
197
    fflush(stderr);
189
198
    abort();
190
199
  }
197
206
  error=pthread_mutex_unlock(&mp->mutex);
198
207
  if (error)
199
208
  {
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);
201
211
    fflush(stderr);
202
212
    abort();
203
213
  }
214
224
  pthread_mutex_lock(&mp->global);
215
225
  if (mp->count == 0)
216
226
  {
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);
218
229
    fflush(stderr);
219
230
    abort();
220
231
  }
221
232
  if (!pthread_equal(pthread_self(),mp->thread))
222
233
  {
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);
225
237
    fflush(stderr);
226
238
    abort();
227
239
  }
228
240
 
229
241
  if (mp->count-- != 1)
230
242
  {
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);
233
245
    fflush(stderr);
234
246
    abort();
235
247
  }
238
250
  pthread_mutex_lock(&mp->global);
239
251
  if (error)
240
252
  {
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);
242
255
    fflush(stderr);
243
256
    abort();
244
257
  }
246
259
  if (mp->count++)
247
260
  {
248
261
    fprintf(stderr,
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);
251
264
    fflush(stderr);
252
265
    abort();
253
266
  }
266
279
  pthread_mutex_lock(&mp->global);
267
280
  if (mp->count != 1 || !pthread_equal(pthread_self(),mp->thread))
268
281
  {
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");
270
284
    fflush(stderr);
271
285
    abort();
272
286
  }
276
290
#ifdef EXTRA_DEBUG
277
291
  if (error && (error != EINTR && error != ETIMEDOUT && error != ETIME))
278
292
  {
279
 
    fprintf(stderr,"safe_mutex: Got error: %d (%d) when doing a safe_mutex_timedwait at %s, line %d\n", error, errno, file, line);
 
293
    fprintf(stderr,
 
294
            "safe_mutex: Got error: %d (%d) when doing a safe_mutex_timedwait on %s at %s, "
 
295
            "line %d\n",
 
296
            error, errno, mp->name, file, line);
280
297
  }
281
298
#endif
282
299
  pthread_mutex_lock(&mp->global);
284
301
  if (mp->count++)
285
302
  {
286
303
    fprintf(stderr,
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,
 
307
            error, error);
289
308
    fflush(stderr);
290
309
    abort();
291
310
  }
309
328
  }
310
329
  if (mp->count != 0)
311
330
  {
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);
314
334
    fflush(stderr);
315
335
    abort();
316
336
  }
382
402
    struct st_safe_mutex_info_t *ptr;
383
403
    for (ptr= safe_mutex_root ; ptr ; ptr= ptr->next)
384
404
    {
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);
388
408
    }
389
409
  }