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