summaryrefslogtreecommitdiff
path: root/libclapi/cl_mutex.c
blob: e25022ba2d0f832d55cd1a9d9d5d9a67defcb08a (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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
 * Copyright © 2012 Intel Corporation
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 */
#include <stdlib.h>
#include <assert.h>
#include <malloc.h>
#include <pthread.h>
#include <string.h>
#include "cl_mutex.h"
#include "cl_internals.h"

#ifdef CL_DEBUG_MUTEX

static pthread_mutex_t cl_mutex_log_lock;
static unsigned int cl_mutex_log_num;

typedef struct _cl_mutex_log_item {
  pthread_mutex_t* mutex;
  struct {
    char* file;
    int line;
    pthread_t tid;
  } holder;
  struct {
    char* file;
    int line;
    pthread_t tid;
  } waiter[8];
  struct {
    char* file;
    int line;
    pthread_t tid;
    pthread_cond_t* cond;
  } sleeper[8];
} _cl_mutex_log_item;
typedef struct _cl_mutex_log_item*  cl_mutex_log_item;

static cl_mutex_log_item* cl_mutex_log_map;
static int cl_mutex_log_map_size;

LOCAL void cl_mutex_debug_init(void)
{
  static int inited = 0;
  if (inited)
    return;

  pthread_mutex_init(&cl_mutex_log_lock, NULL);

  cl_mutex_log_map_size = 128;
  cl_mutex_log_map = malloc(cl_mutex_log_map_size*sizeof(cl_mutex_log_item));
  memset(cl_mutex_log_map, 0, cl_mutex_log_map_size*sizeof(cl_mutex_log_item));
  cl_mutex_log_num = 0;

  atexit(cl_mutex_report);

  inited = 1;
}

static int before_get_the_mutex(pthread_mutex_t* mutex, char* file, int line)
{
  int i;
  int ret = -1;
  cl_mutex_log_item item = NULL;
  pthread_t tid = pthread_self();

  pthread_mutex_lock(&cl_mutex_log_lock);
  for (i = 0; i < cl_mutex_log_map_size; i++) {
    if (cl_mutex_log_map[i]->mutex == mutex) {
      item = cl_mutex_log_map[i];
      break;
    }
  }

  if (item == NULL) {
    for (i = 0; i < cl_mutex_log_map_size; i++) {
      if (cl_mutex_log_map[i] == NULL) {
        break;
      }
    }

    if (i == cl_mutex_log_map_size) {
      cl_mutex_log_map = realloc(cl_mutex_log_map, 2*cl_mutex_log_map_size*sizeof(cl_mutex_log_item));
      memset(cl_mutex_log_map + cl_mutex_log_map_size, 0, cl_mutex_log_map_size*sizeof(cl_mutex_log_item));
      cl_mutex_log_map_size = cl_mutex_log_map_size*2;
    }

    item = malloc(sizeof(_cl_mutex_log_item));
    assert(item);
    memset(item, 0, sizeof(_cl_mutex_log_item));
    item->waiter[0].file = file;
    item->waiter[0].line = line;
    item->waiter[0].tid = tid;
    cl_mutex_log_map[i] = item;
    ret = 0;
    cl_mutex_log_num++;
  } else {
    for (i = 0; i < 8; i++) {
      if (item->waiter[i].file == NULL) {
        item->waiter[i].file = file;
        item->waiter[i].line = line;
        item->waiter[i].tid = tid;
        ret = i;
        break;
      }
    }
    /* If no waiter slot avaible, just skip, do not record. */
  }

  pthread_mutex_unlock(&cl_mutex_log_lock);
  return ret;
}

static void after_get_the_mutex(pthread_mutex_t* mutex, char* file, int line, int nth, int is_from_sleep)
{
  int i;
  cl_mutex_log_item item = NULL;
  pthread_t tid = pthread_self();

  pthread_mutex_lock(&cl_mutex_log_lock);
  for (i = 0; i < cl_mutex_log_map_size; i++) {
    if (cl_mutex_log_map[i]->mutex == mutex) {
      item = cl_mutex_log_map[i];
      break;
    }
  }
  assert(item);

  if (nth >= 0) {
    assert(nth < 8);
    if (is_from_sleep == 0) {
      assert(item->waiter[nth].file == file);
      assert(item->waiter[nth].line == line);
      assert(item->waiter[nth].tid == tid);
      item->waiter[nth].file = NULL;
      item->waiter[nth].line = 0;
      item->waiter[nth].tid = -1;
    } else {
      assert(item->sleeper[nth].file == file);
      assert(item->sleeper[nth].line == line);
      assert(item->sleeper[nth].tid == tid);
      item->sleeper[nth].file = NULL;
      item->sleeper[nth].line = 0;
      item->sleeper[nth].tid = -1;
      item->sleeper[nth].cond = NULL;
    }
  }

  item->holder.file = file;
  item->holder.line = line;
  item->holder.tid = tid;

  pthread_mutex_unlock(&cl_mutex_log_lock);
}

static void before_release_the_mutex(pthread_mutex_t* mutex,  char* file, int line)
{
  int i, j;
  cl_mutex_log_item item = NULL;
  int still_used;

  pthread_mutex_lock(&cl_mutex_log_lock);
  for (i = 0; i < cl_mutex_log_map_size; i++) {
    if (cl_mutex_log_map[i]->mutex == mutex) {
      item = cl_mutex_log_map[i];
      break;
    }
  }

  if (item == NULL) {
    printf("At unlock point file: %s, line: %d, we can not find the locked mutex:%p item, fatal\n",
           file, line, mutex);
    assert(0);
    return;
  }

  if (item->holder.file == NULL) {
    printf("At unlock point file: %s, line: %d, we can not find the locked mutex:%p info, fatal\n",
           file, line, mutex);
    assert(0);
    return;
  }

  item->holder.file = NULL;
  item->holder.line = 0;
  item->holder.tid = -1;

  still_used = 0;
  for (j = 0; j < 8; j++) {
    if (item->waiter[j].file != NULL) {
      still_used = 1;
      break;
    }
    if (item->sleeper[j].file != NULL) {
      still_used = 1;
      break;
    }
  }

  if (still_used == 0) {
    cl_mutex_log_map[i] = NULL;
    free(item);
    cl_mutex_log_num--;
  }

  pthread_mutex_unlock(&cl_mutex_log_lock);
}

static int before_sleep_on_the_mutex(pthread_cond_t* cond, pthread_mutex_t* mutex,  char* file, int line)
{
  int i;
  int ret;
  cl_mutex_log_item item = NULL;
  pthread_t tid = pthread_self();

  pthread_mutex_lock(&cl_mutex_log_lock);
  for (i = 0; i < cl_mutex_log_map_size; i++) {
    if (cl_mutex_log_map[i]->mutex == mutex) {
      item = cl_mutex_log_map[i];
      break;
    }
  }

  if (item == NULL) {
    printf("At unlock point file: %s, line: %d, we can not find the locked mutex:%p item, fatal\n",
           file, line, mutex);
    assert(0);
    return -1;
  }

  if (item->holder.file == NULL) {
    printf("At unlock point file: %s, line: %d, we can not find the locked mutex:%p info, fatal\n",
           file, line, mutex);
    assert(0);
    return -1;
  }

  assert(item->holder.file == file);
  assert(item->holder.line == line);
  assert(item->holder.tid == tid);
  item->holder.file = NULL;
  item->holder.line = 0;
  item->holder.tid = -1;

  ret = -1;
  for (i = 0; i < 8; i++) {
    if (item->sleeper[i].file == NULL) {
      item->sleeper[i].file = file;
      item->sleeper[i].line = line;
      item->sleeper[i].tid = tid;
      item->sleeper[i].cond = cond;
      ret = i;
      break;
    }
  }

  pthread_mutex_unlock(&cl_mutex_log_lock);
  return ret;
}

static int try_to_get_the_mutex(pthread_mutex_t* mutex)
{
  struct timespec abs_time;
  clock_gettime(CLOCK_REALTIME , &abs_time);
  // so many seconds, still not get, we think deadlock happened.
  abs_time.tv_sec += 25;
  if (pthread_mutex_timedlock(mutex, &abs_time) != 0) {
    return 0;
  }
  return 1;
}

static void handle_deadlock(pthread_mutex_t* mutex, char* file, int line)
{
  cl_mutex_log_item item = NULL;
  int i;
  int print_others;
  int num;
  pthread_t tid = pthread_self();

  pthread_mutex_lock(&cl_mutex_log_lock);
  for (i = 0; i < cl_mutex_log_map_size; i++) {
    if (cl_mutex_log_map[i]->mutex == mutex) {
      item = cl_mutex_log_map[i];
      break;
    }
  }
  printf("------------------------------------------------------\n");
  if (item == NULL) {
    printf("We may dead lock at a unknown mutex, call lock point is"
           "file: %s, line: %d\n", file, line);
  } else {
    print_others = 1;
    printf("We may dead lock at a mutex:%p, call lock point is"
           "file: %s, line: %d, thread id is %d.\nThe mutex is held by"
           "file: %s, line: %d, thread is is %d\n",
           mutex, file, line, (int)tid, item->holder.file, item->holder.line,
           (int)item->holder.tid);

    for (i = 0; i < 8; i++) {
      if (item->waiter[i].file) {
        if (print_others) {
          print_others = 0;
          printf("There are other waiters:\n");
        }

        printf("    file: %s, line: %d, thread id: %d\n",
               item->waiter[i].file, item->waiter[i].line, (int)item->waiter[i].tid);
      }
    }

    print_others = 0;
    for (i = 0; i < 8; i++) {
      if (item->sleeper[i].file) {
        if (print_others) {
          print_others = 0;
          printf("There are other sleepers:\n");
        }

        printf("    file: %s, line: %d, thread id: %d, cond: %p\n",
               item->sleeper[i].file, item->sleeper[i].line,
               (int)item->sleeper[i].tid, item->sleeper[i].cond);
      }
    }
  }
  printf("------------------------------------------------------\n");

  num = 0;
  for (i = 0; i < cl_mutex_log_map_size; i++) {
    if (cl_mutex_log_map[i]) {
      num++;
    }
  }
  assert(num == cl_mutex_log_num);

  exit(0);
  pthread_mutex_unlock(&cl_mutex_log_lock);
}

LOCAL void cl_mutex_lock(pthread_mutex_t* mutex, char* file, int line)
{
  assert(mutex);
  int nth = before_get_the_mutex(mutex, file, line);
  if (try_to_get_the_mutex(mutex) == 0) {
    // Consider to be dead locked.
    handle_deadlock(mutex, file, line);
  }

  after_get_the_mutex(mutex, file, line, nth, 0);
}

LOCAL void cl_mutex_unlock(pthread_mutex_t* mutex, char* file, int line)
{
  assert(mutex);
  before_release_the_mutex(mutex, file, line);
  pthread_mutex_unlock(mutex);
}

LOCAL int cl_cond_wait(pthread_cond_t* cond, pthread_mutex_t* mutex, char* file, int line)
{
  int ret;
  int nth;
  assert(mutex);
  assert(cond);

  nth = before_sleep_on_the_mutex(cond, mutex, file, line);
  ret = pthread_cond_wait(cond, mutex);
  after_get_the_mutex(mutex, file, line, nth, 1);

  return ret;
}

LOCAL int cl_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
                            const struct timespec *abstime, char* file, int line)
{
  int ret;
  int nth;
  assert(mutex);
  assert(cond);

  nth = before_sleep_on_the_mutex(cond, mutex, file, line);
  ret = pthread_cond_timedwait(cond, mutex, abstime);
  after_get_the_mutex(mutex, file, line, nth, 1);

  return ret;
}

void cl_mutex_report(void)
{
  int i, j, num;
  pthread_mutex_lock(&cl_mutex_log_lock);
  if (cl_mutex_log_num == 0) {
    pthread_mutex_unlock(&cl_mutex_log_lock);
    return;
  }

  printf("-------------------------------------------------------------------\n");
  num = 0;
  for (i = 0; i < cl_mutex_log_map_size; i++) {
    if (cl_mutex_log_map[i]) {
      printf("The mutex %p still has users.\n", cl_mutex_log_map[i]->mutex);
      if (cl_mutex_log_map[i]->holder.file) {
        printf("    Holder: At file:%s, line: %d, thread id is: %d\n",
               cl_mutex_log_map[i]->holder.file, cl_mutex_log_map[i]->holder.line,
               (int)cl_mutex_log_map[i]->holder.tid);
      }

      for (j = 0; j < 8; j++) {
        printf("    Waiter: At file:%s, line: %d, thread id is: %d\n",
               cl_mutex_log_map[i]->waiter[i].file, cl_mutex_log_map[i]->waiter[i].line,
               (int)cl_mutex_log_map[i]->waiter[i].tid);
      }

      for (j = 0; j < 8; j++) {
        printf("    Sleeper: At file:%s, line: %d, thread id is: %d\n",
               cl_mutex_log_map[i]->sleeper[i].file, cl_mutex_log_map[i]->sleeper[i].line,
               (int)cl_mutex_log_map[i]->sleeper[i].tid);
      }
      num++;
    }
  }
  printf("-------------------------------------------------------------------\n");
  assert(num == cl_mutex_log_num);
  pthread_mutex_unlock(&cl_mutex_log_lock);
}

#endif