summaryrefslogtreecommitdiff
path: root/libclapi/cl_mutex.c
blob: b18990c2fdb71691990d8845e552e699948ef36d (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
/*
 * 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"

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;
  } holder;
  struct {
    char* file;
    int line;
  } waiter[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;

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_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;
    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;
        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 i;
  cl_mutex_log_item item = NULL;

  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);
    item->waiter[nth].file = NULL;
    item->waiter[nth].line = 0;
  }

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

  pthread_mutex_unlock(&cl_mutex_log_lock);
}

static void after_release_the_mutex(pthread_mutex_t* mutex)
{
  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;
    }
  }

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

  still_used = 0;
  for (j = 0; j < 8; j++) {
    if (item->waiter[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 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;
}

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.
  }

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