Mercurial > hg > dmlib
annotate src/dmlib.c @ 958:985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Fri, 27 Feb 2015 03:58:25 +0200 |
parents | b66653c9acb3 |
children | ada47e30d0c9 |
rev | line source |
---|---|
0 | 1 #include "dmlib.h" |
2 #include <errno.h> | |
3 | |
4 | |
5 int dmVerbosity = 0; | |
6 char *dmProgName = NULL, | |
7 *dmProgDesc = NULL, | |
8 *dmProgVersion = NULL, | |
9 *dmProgAuthor = NULL, | |
10 *dmProgLicense = NULL; | |
11 | |
12 | |
13 void dmInitProg(char *name, char *desc, char *version, char *author, char *license) | |
14 { | |
15 dmProgName = name; | |
16 dmProgDesc = desc; | |
17 dmProgVersion = version; | |
804 | 18 dmProgAuthor = author ? author : DM_PROG_AUTHOR; |
19 dmProgLicense = license ? license : DM_PROG_LICENSE; | |
0 | 20 } |
21 | |
22 | |
23 void dmPrintBanner(FILE *outFile, const char *name, const char *usage) | |
24 { | |
25 fprintf(outFile, | |
804 | 26 "\n%s v%s (%s)\n" |
27 "%s\n" | |
28 "%s\n" | |
29 "Usage: %s %s\n", | |
30 dmProgName, dmProgVersion, dmProgDesc, | |
31 dmProgAuthor, dmProgLicense, name, usage); | |
0 | 32 } |
33 | |
34 | |
35 void dmMsgVA(int level, const char *fmt, va_list ap) | |
36 { | |
37 if (dmVerbosity >= level) | |
38 { | |
39 fprintf(stderr, "%s: ", dmProgName); | |
40 vfprintf(stderr, fmt, ap); | |
41 } | |
42 } | |
43 | |
44 | |
45 void dmPrintVA(int level, const char *fmt, va_list ap) | |
46 { | |
47 if (dmVerbosity >= level) | |
48 { | |
49 vfprintf(stderr, fmt, ap); | |
50 } | |
51 } | |
52 | |
53 | |
54 void dmMsg(int level, const char *fmt, ...) | |
55 { | |
56 va_list ap; | |
57 | |
58 va_start(ap, fmt); | |
59 dmMsgVA(level, fmt, ap); | |
60 va_end(ap); | |
61 } | |
62 | |
63 | |
64 void dmPrint(int level, const char *fmt, ...) | |
65 { | |
66 va_list ap; | |
67 | |
68 va_start(ap, fmt); | |
69 dmPrintVA(level, fmt, ap); | |
70 va_end(ap); | |
71 } | |
72 | |
73 | |
74 /* Memory handling routines | |
75 */ | |
76 void *dmMalloc(size_t l) | |
77 { | |
78 return malloc(l); | |
79 } | |
80 | |
81 | |
82 void *dmMalloc0(size_t l) | |
83 { | |
84 return calloc(1, l); | |
85 } | |
86 | |
87 | |
88 void *dmCalloc(size_t n, size_t l) | |
89 { | |
90 return calloc(n, l); | |
91 } | |
92 | |
93 | |
94 void *dmRealloc(void *p, size_t l) | |
95 { | |
96 return realloc(p, l); | |
97 } | |
98 | |
99 | |
100 void dmFree(void *p) | |
101 { | |
102 /* Check for NULL pointers for portability due to some libc | |
103 * implementations not handling free(NULL) too well. | |
104 */ | |
105 if (p) free(p); | |
106 } | |
107 | |
108 | |
871
fc272f5f2d15
Make value type of dmGetIntVal() unsigned.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
109 BOOL dmGetIntVal(const char *s, unsigned int *i) |
0 | 110 { |
111 if (s[0] == '$') | |
112 { | |
113 if (sscanf(&s[1], "%x", i) < 1) | |
114 return FALSE; | |
115 } | |
116 else if (s[0] == '0' && s[1] == 'x') | |
117 { | |
118 if (sscanf(&s[2], "%x", i) < 1) | |
119 return FALSE; | |
120 } | |
121 else | |
122 { | |
123 if (sscanf(s, "%u", i) < 1) | |
124 return FALSE; | |
125 } | |
126 return TRUE; | |
127 } | |
128 | |
129 | |
955 | 130 /* |
131 * Error handling and messages | |
0 | 132 */ |
133 #define DM_SYSTEM_ERRORS 100000 | |
134 | |
955 | 135 |
0 | 136 int dmGetErrno() |
137 { | |
138 return DM_SYSTEM_ERRORS + errno; | |
139 } | |
140 | |
141 | |
957
b66653c9acb3
Oops, forgot to add const in the actual function .. 100L.
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
142 const char *dmErrorStr(const int error) |
0 | 143 { |
144 if (error >= DM_SYSTEM_ERRORS) | |
145 return strerror(error - DM_SYSTEM_ERRORS); | |
146 | |
147 switch (error) | |
148 { | |
149 case DMERR_OK: return "No error"; | |
150 case DMERR_FOPEN: return "File open error"; | |
151 case DMERR_FREAD: return "Read error"; | |
152 case DMERR_FWRITE: return "Write error"; | |
153 case DMERR_FSEEK: return "Seek error"; | |
154 case DMERR_NOT_FOUND: return "Resource not found"; | |
155 | |
156 case DMERR_INVALID_DATA: return "Invalid data"; | |
157 case DMERR_MALLOC: return "Memory allocation failure"; | |
158 case DMERR_ALREADY_INIT: return "Already initialized"; | |
159 case DMERR_INIT_FAIL: return "Initialization failed"; | |
160 case DMERR_INVALID_ARGS: return "Invalid arguments"; | |
161 | |
162 case DMERR_NULLPTR: return "NULL pointer"; | |
163 case DMERR_NOT_SUPPORTED: return "Operation not supported"; | |
164 case DMERR_OUT_OF_DATA: return "Out of data"; | |
165 case DMERR_EXTRA_DATA: return "Extra data"; | |
166 case DMERR_BOUNDS: return "Bounds check failed"; | |
167 | |
908
03bda6477ad4
Add new error code DMERR_DATA_ERROR.
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
168 case DMERR_DATA_ERROR: return "Data decoding/encoding/parsing error"; |
03bda6477ad4
Add new error code DMERR_DATA_ERROR.
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
169 |
0 | 170 case DMERR_NOTPACK: return "File is not a PACK"; |
171 case DMERR_VERSION: return "Unsupported PACK version"; | |
172 case DMERR_INVALID: return "Invalid data, corrupted file"; | |
173 case DMERR_COMPRESSION: return "Error in compression"; | |
174 | |
175 default: return "Unknown error"; | |
176 } | |
177 } | |
294
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
178 |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
179 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
957
diff
changeset
|
180 int dmErrorVA(const int error, const char *fmt, va_list ap) |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
957
diff
changeset
|
181 { |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
957
diff
changeset
|
182 if (dmProgName != NULL) |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
957
diff
changeset
|
183 fprintf(stderr, "%s: ", dmProgName); |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
957
diff
changeset
|
184 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
957
diff
changeset
|
185 vfprintf(stderr, fmt, ap); |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
957
diff
changeset
|
186 return error; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
957
diff
changeset
|
187 } |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
957
diff
changeset
|
188 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
957
diff
changeset
|
189 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
957
diff
changeset
|
190 int dmError(const int error, const char *fmt, ...) |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
957
diff
changeset
|
191 { |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
957
diff
changeset
|
192 int ret; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
957
diff
changeset
|
193 va_list ap; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
957
diff
changeset
|
194 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
957
diff
changeset
|
195 va_start(ap, fmt); |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
957
diff
changeset
|
196 ret = dmErrorVA(error, fmt, ap); |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
957
diff
changeset
|
197 va_end(ap); |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
957
diff
changeset
|
198 return ret; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
957
diff
changeset
|
199 } |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
957
diff
changeset
|
200 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
957
diff
changeset
|
201 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
957
diff
changeset
|
202 void dmErrorMsg(const char *fmt, ...) |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
957
diff
changeset
|
203 { |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
957
diff
changeset
|
204 va_list ap; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
957
diff
changeset
|
205 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
957
diff
changeset
|
206 va_start(ap, fmt); |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
957
diff
changeset
|
207 dmErrorVA(DMERR_INTERNAL, fmt, ap); |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
957
diff
changeset
|
208 va_end(ap); |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
957
diff
changeset
|
209 } |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
957
diff
changeset
|
210 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
957
diff
changeset
|
211 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
957
diff
changeset
|
212 /* |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
957
diff
changeset
|
213 * Mutex debugging |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
957
diff
changeset
|
214 */ |
294
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
215 #ifdef DM_MUTEX_DEBUG |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
216 |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
217 static DMMutexLock * dmGetMutexThreadIDLock(DMMutex *mutex) |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
218 { |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
219 Uint32 id = SDL_ThreadID(); |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
220 int i; |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
221 for (i = 0; i < 8; i++) |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
222 { |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
223 DMMutexLock *lock = &(mutex->locks[i]); |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
224 if (lock->used && lock->id == id) |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
225 return lock; |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
226 } |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
227 return NULL; |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
228 } |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
229 |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
230 static void dmPrintMutexLocks(DMMutex *mutex, const char *state, const char *file, const int line) |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
231 { |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
232 int i; |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
233 |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
234 fprintf(stderr, |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
235 "----------------------\n" |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
236 "%s --> %p @ %s:%d\n" |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
237 "Current thread: %d\n" |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
238 "Mutex : %p (created @ %s:%d)\n", |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
239 state, mutex, file, line, |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
240 SDL_ThreadID(), mutex, mutex->cr_file, mutex->cr_line); |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
241 |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
242 for (i = 0; i < 8; i++) |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
243 { |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
244 DMMutexLock *lock = &(mutex->locks[i]); |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
245 if (lock->used) |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
246 { |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
247 fprintf(stderr, |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
248 "Lock #%d: thread=%d, state=%d\n", |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
249 i, lock->id, lock->state); |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
250 } |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
251 } |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
252 } |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
253 |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
254 int dmDOMutexLock(DMMutex *mutex, const char *file, const int line) |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
255 { |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
256 if (mutex != NULL) |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
257 { |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
258 dmPrintMutexLocks(mutex, "LOCKING", file, line); |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
259 |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
260 DMMutexLock *lock = dmGetMutexThreadIDLock(mutex); |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
261 if (lock != NULL) |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
262 { |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
263 int res; |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
264 if (lock->state == 0) |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
265 res = SDL_mutexP(mutex->m); |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
266 else |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
267 res = 1; |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
268 lock->state++; |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
269 fprintf(stderr, "LOCKING %p @ thread=%d done [1].\n", mutex, SDL_ThreadID()); |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
270 return res; |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
271 } |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
272 else |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
273 { |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
274 int i; |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
275 for (i = 0; i < 8; i++) |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
276 { |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
277 DMMutexLock *lock = &(mutex->locks[i]); |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
278 if (!lock->used) |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
279 { |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
280 int res; |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
281 lock->used = TRUE; |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
282 lock->id = SDL_ThreadID(); |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
283 lock->state++; |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
284 res = SDL_mutexP(mutex->m); |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
285 fprintf(stderr, "LOCKING %p @ thread=%d done [2].\n", mutex, SDL_ThreadID()); |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
286 return res; |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
287 } |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
288 } |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
289 return -2; |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
290 } |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
291 } |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
292 return -1; |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
293 } |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
294 |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
295 |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
296 int dmDOMutexUnlock(DMMutex *mutex, const char *file, const int line) |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
297 { |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
298 if (mutex != NULL) |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
299 { |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
300 dmPrintMutexLocks(mutex, "UN-LOCKING", file, line); |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
301 |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
302 DMMutexLock *lock = dmGetMutexThreadIDLock(mutex); |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
303 if (lock != NULL) |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
304 { |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
305 int res; |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
306 lock->state--; |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
307 if (lock->state == 0) |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
308 res = SDL_mutexV(mutex->m); |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
309 else |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
310 res = lock->state; |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
311 return res; |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
312 } |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
313 else |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
314 { |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
315 return -2; |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
316 } |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
317 } |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
318 return -1; |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
319 } |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
320 |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
321 |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
322 DMMutex * dmDOCreateMutex(const char *file, const int line) |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
323 { |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
324 DMMutex *mutex = dmMalloc0(sizeof(DMMutex)); |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
325 if (mutex == NULL) |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
326 return NULL; |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
327 mutex->cr_file = dm_strdup(file); |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
328 mutex->cr_line = line; |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
329 mutex->m = SDL_CreateMutex(); |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
330 return mutex; |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
331 } |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
332 |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
333 |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
334 void dmDestroyMutex(DMMutex *mutex) |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
335 { |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
336 if (mutex != NULL) |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
337 { |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
338 SDL_DestroyMutex(mutex->m); |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
339 dmFree(mutex); |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
340 } |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
341 } |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
342 |
dd9809a93425
Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
343 #endif |