1 /* mutexDebug.c - NeonServ v5.6
2 * Copyright (C) 2011-2012 Philipp Kreil (pk910)
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include "mutexDebug.h"
20 #ifdef ENABLE_MUTEX_DEBUG
22 struct MutexLockEvent {
26 struct MutexLockEvent *next;
32 struct MutexLockEvent *first_event, *last_event;
33 struct MutexLock *prev, *next;
37 pthread_mutex_t *mutex;
38 struct MutexLock *first_lock, *last_lock;
39 struct MutexNode *next;
42 static pthread_mutex_t synchronized;
43 static struct MutexNode *mutex_nodes = NULL;
45 static struct MutexNode *getMutexNode(pthread_mutex_t *mutex, int create);
46 static void lockMutex(struct MutexNode *node, const char *file, unsigned int line);
47 static void unlockMutex(struct MutexNode *node, const char *file, unsigned int line);
49 void xmutex(int lock, pthread_mutex_t *mutex, const char *file, unsigned int line) {
50 pthread_mutex_lock(&synchronized);
51 struct MutexNode *node = getMutexNode(mutex, 1);
53 lockMutex(node, file, line);
55 unlockMutex(node, file, line);
56 pthread_mutex_unlock(&synchronized);
59 void initMutexDebug() {
60 THREAD_MUTEX_INIT(synchronized);
63 static struct MutexNode *getMutexNode(pthread_mutex_t *mutex, int create) {
64 struct MutexNode *node;
65 for(node = mutex_nodes; node; node = node->next) {
66 if(node->mutex == mutex)
71 node = malloc(sizeof(*node));
72 node->first_lock = NULL;
73 node->last_lock = NULL;
75 node->next = mutex_nodes;
80 static void lockMutex(struct MutexNode *node, const char *file, unsigned int line) {
81 struct MutexLock *lock;
82 int thread = getCurrentThreadID();
83 for(lock = node->first_lock; lock; lock = lock->next) {
84 if(lock->thread == thread)
88 lock = malloc(sizeof(*lock));
89 lock->thread = thread;
91 lock->first_event = NULL;
92 lock->last_event = NULL;
93 lock->prev = node->last_lock;
95 node->last_lock = lock;
97 node->first_lock = lock;
101 struct MutexLockEvent *event = malloc(sizeof(*event));
106 if(lock->last_event) {
107 lock->last_event->next = event;
108 lock->last_event = event;
110 lock->first_event = event;
111 lock->last_event = event;
115 static void unlockMutex(struct MutexNode *node, const char *file, unsigned int line) {
116 struct MutexLock *lock;
117 int thread = getCurrentThreadID();
118 for(lock = node->first_lock; lock; lock = lock->next) {
119 if(lock->thread == thread)
125 if(lock->count <= 0) {
128 lock->prev->next = lock->next;
130 node->first_lock = lock->next;
132 lock->next->prev = lock->prev;
134 node->last_lock = lock->prev;
135 //recursive free all events
136 struct MutexLockEvent *event, *next_event;
137 for(event = lock->first_event; event; event = next_event) {
138 next_event = event->next;
144 struct MutexLockEvent *event = malloc(sizeof(*event));
149 if(lock->last_event) {
150 lock->last_event->next = event;
151 lock->last_event = event;
153 lock->first_event = event;
154 lock->last_event = event;
159 void mutex_debug(pthread_mutex_t *mutex) {
160 //replay mutex events to stdout
161 struct MutexNode *node = getMutexNode(mutex, 0);
163 printf("[MUTEX_DEBUG] unknown mutex!\n");
166 printf("[MUTEX_DEBUG] mutex replay:\n");
167 struct MutexLock *lock;
168 struct MutexLockEvent *event;
169 for(lock = node->first_lock; lock; lock = lock->next) {
170 printf("[MUTEX_DEBUG] THREAD %d (%d locks):\n", lock->thread, lock->count);
171 for(event = lock->first_event; event; event = event->next) {
172 printf("[MUTEX_DEBUG] %s in %s:%d\n", (event->locked ? "lock " : "unlock"), event->file, event->line);
175 printf("[MUTEX_DEBUG] end of mutex replay.\n");