Debian Codegraph @

core/libs/dngwriter/extra/dng_sdk/dng_mutex.cpp

raw
1 /*****************************************************************************/
2 // Copyright 2006-2019 Adobe Systems Incorporated
3 // All Rights Reserved.
4 //
5 // NOTICE: Adobe permits you to use, modify, and distribute this file in
6 // accordance with the terms of the Adobe license agreement accompanying it.
7 /*****************************************************************************/
8
9 #include "dng_abort_sniffer.h"
10 #include "dng_mutex.h"
11
12 #include "dng_assertions.h"
13 #include "dng_exceptions.h"
14
15 #include <stdlib.h>
16
17 /*****************************************************************************/
18
19 // do mutex lock level tracking, asserts stripped in non-debug so don't track there
20 #ifndef qDNGThreadTestMutexLevels
21 #define qDNGThreadTestMutexLevels (qDNGThreadSafe && qDNGDebug)
22 #endif
23
24 #if qDNGThreadTestMutexLevels
25 namespace
26 {
27
28 class InnermostMutexHolder
29 {
30
31 private:
32
33 pthread_key_t fInnermostMutexKey;
34
35 public:
36
37 InnermostMutexHolder ()
38
39 : fInnermostMutexKey ()
40
41 {
42
43 int result = pthread_key_create (&fInnermostMutexKey, NULL);
44
45 DNG_ASSERT (result == 0, "pthread_key_create failed.");
46
47 if (result != 0)
48 ThrowProgramError ();
49
50 }
51
52 ~InnermostMutexHolder ()
53 {
54
55 pthread_key_delete (fInnermostMutexKey);
56
57 }
58
59 void SetInnermostMutex (dng_mutex *mutex)
60 {
61
62 int result;
63
64 result = pthread_setspecific (fInnermostMutexKey, (void *)mutex);
65
66 DNG_ASSERT (result == 0, "pthread_setspecific failed.");
67
68 (void) result;
69
70 #if 0 // Hard failure here was causing crash on quit.
71
72 if (result != 0)
73 ThrowProgramError ();
74
75 #endif
76
77 }
78
79 dng_mutex *GetInnermostMutex ()
80 {
81
82 void *result = pthread_getspecific (fInnermostMutexKey);
83
84 return reinterpret_cast<dng_mutex *> (result);
85
86 }
87
88 };
89
90 InnermostMutexHolder gInnermostMutexHolder;
91
92 }
93
94 #endif
95
96 /*****************************************************************************/
97
98 dng_mutex::dng_mutex (const char *mutexName, uint32 mutexLevel)
99
100 #if qDNGThreadSafe
101
102 : fPthreadMutex ()
103 , fMutexLevel (mutexLevel)
104 , fRecursiveLockCount (0)
105 , fPrevHeldMutex (NULL)
106 , fMutexName (mutexName)
107
108 #endif
109
110 {
111
112 #if qDNGThreadSafe
113
114 #if qWinOS
115
116 // Win is already a recursive mutex by default
117 if (pthread_mutex_init (&fPthreadMutex, NULL) != 0)
118 {
119 ThrowMemoryFull ();
120 }
121
122 #else
123
124 // make recursive mutex, can lock within itself
125 pthread_mutexattr_t mta;
126 pthread_mutexattr_init(&mta);
127 pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_RECURSIVE);
128
129 if (pthread_mutex_init (&fPthreadMutex, &mta) != 0)
130 {
131 ThrowMemoryFull ();
132 }
133 #endif
134
135 #endif
136
137 }
138
139 /*****************************************************************************/
140
141 dng_mutex::~dng_mutex ()
142 {
143
144 #if qDNGThreadSafe
145
146 pthread_mutex_destroy (&fPthreadMutex);
147
148 #endif
149
150 }
151
152 /*****************************************************************************/
153
154 void dng_mutex::Lock ()
155 {
156
157 #if qDNGThreadSafe
158 #if qDNGThreadTestMutexLevels
159
160 dng_mutex *innermostMutex = gInnermostMutexHolder.GetInnermostMutex ();
161
162 if (innermostMutex != NULL)
163 {
164
165 if (innermostMutex == this)
166 {
167
168 int result = pthread_mutex_lock (&fPthreadMutex);
169
170 if (result != 0)
171 {
172
173 DNG_ASSERT (result == 0, "pthread_mutex_lock failed.");
174
175 ThrowProgramError ();
176
177 }
178
179 fRecursiveLockCount++;
180
181 return;
182
183 }
184
185 bool lockOrderPreserved = fMutexLevel > innermostMutex->fMutexLevel;
186
187 // to allow cloning of class internals both with a dng_mutex and get closer to the C++ mutex,
188 // test for MutexLevelIgnore and don't generate level violations
189 if (!lockOrderPreserved)
190 {
191
192 if ((fMutexLevel == kDNGMutexLevelIgnore) || (innermostMutex->fMutexLevel == kDNGMutexLevelIgnore))
193 lockOrderPreserved = true;
194
195 }
196
197 if (!lockOrderPreserved)
198 {
199
200 char msg [1024];
201
202 snprintf (msg,
203 1024,
204 "Lock order violation: This mutex: %s v Innermost mutex: %s",
205 this->MutexName (),
206 innermostMutex->MutexName ());
207
208 DNG_REPORT(msg); // asserts inside of mutex lock, any locks within that must be lower
209
210 }
211
212 }
213
214 int result = pthread_mutex_lock (&fPthreadMutex);
215
216 if (result != 0)
217 {
218
219 DNG_ASSERT (result == 0, "pthread_mutex_lock failed.");
220
221 ThrowProgramError ();
222
223 }
224
225 fPrevHeldMutex = innermostMutex;
226
227 gInnermostMutexHolder.SetInnermostMutex (this);
228
229 #else
230
231 // Register the fact that we're trying to lock this mutex.
232
233 int result = pthread_mutex_lock (&fPthreadMutex);
234
235 if (result != 0)
236 {
237
238 DNG_REPORT ("pthread_mutex_lock failed");
239
240 ThrowProgramError ();
241
242 }
243
244 // Register the fact that we've now successfully acquired the mutex.
245
246 #endif
247 #endif
248
249 }
250
251 /*****************************************************************************/
252
253 void dng_mutex::Unlock ()
254 {
255
256 #if qDNGThreadSafe
257 #if qDNGThreadTestMutexLevels
258
259 DNG_ASSERT (gInnermostMutexHolder.GetInnermostMutex () == this, "Mutexes unlocked out of order!!!");
260
261 if (fRecursiveLockCount > 0)
262 {
263
264 fRecursiveLockCount--;
265
266 pthread_mutex_unlock (&fPthreadMutex);
267
268 return;
269
270 }
271
272 gInnermostMutexHolder.SetInnermostMutex (fPrevHeldMutex);
273
274 fPrevHeldMutex = NULL;
275
276 #endif
277
278 pthread_mutex_unlock (&fPthreadMutex);
279
280 #endif
281
282 }
283
284 /*****************************************************************************/
285
286 const char *dng_mutex::MutexName () const
287 {
288
289 #if qDNGThreadSafe
290
291 if (fMutexName)
292 return fMutexName;
293
294 #endif
295
296 return "< unknown >";
297
298 }
299
300 /*****************************************************************************/
301
302 dng_lock_mutex::dng_lock_mutex (dng_mutex *mutex)
303
304 : fMutex (mutex)
305
306 {
307
308 if (fMutex)
309 fMutex->Lock ();
310
311 }
312
313 /*****************************************************************************/
314
315 dng_lock_mutex::dng_lock_mutex (dng_mutex &mutex)
316
317 : fMutex (&mutex)
318
319 {
320
321 if (fMutex)
322 fMutex->Lock ();
323
324 }
325
326 /*****************************************************************************/
327
328 dng_lock_mutex::~dng_lock_mutex ()
329 {
330
331 if (fMutex)
332 fMutex->Unlock ();
333
334 }
335
336 /*****************************************************************************/
337
338 dng_unlock_mutex::dng_unlock_mutex (dng_mutex *mutex)
339
340 : fMutex (mutex)
341
342 {
343
344 if (fMutex)
345 fMutex->Unlock ();
346
347 }
348
349 /*****************************************************************************/
350
351 dng_unlock_mutex::dng_unlock_mutex (dng_mutex &mutex)
352
353 : fMutex (&mutex)
354
355 {
356
357 if (fMutex)
358 fMutex->Unlock ();
359
360 }
361
362 /*****************************************************************************/
363
364 dng_unlock_mutex::~dng_unlock_mutex ()
365 {
366
367 if (fMutex)
368 fMutex->Lock ();
369
370 }
371
372 /*****************************************************************************/
373
374 dng_condition::dng_condition ()
375
376 #if qDNGThreadSafe
377 : fPthreadCondition ()
378 #endif
379
380 {
381
382 #if qDNGThreadSafe
383 int result;
384
385 result = pthread_cond_init (&fPthreadCondition, NULL);
386
387 DNG_ASSERT (result == 0, "pthread_cond_init failed.");
388
389 if (result != 0)
390 {
391 ThrowProgramError ();
392 }
393 #endif
394
395 }
396
397 /*****************************************************************************/
398
399 dng_condition::~dng_condition ()
400 {
401 #if qDNGThreadSafe
402 pthread_cond_destroy (&fPthreadCondition);
403 #endif
404 }
405
406 /*****************************************************************************/
407
408 bool dng_condition::Wait (dng_mutex &mutex, double timeoutSecs)
409 {
410
411 #if qDNGThreadSafe
412 bool timedOut = false;
413
414 #if qDNGThreadTestMutexLevels
415
416 dng_mutex *innermostMutex = gInnermostMutexHolder.GetInnermostMutex ();
417
418 DNG_ASSERT (innermostMutex == &mutex, "Attempt to wait on non-innermost mutex.");
419
420 (void) innermostMutex;
421
422 innermostMutex = mutex.fPrevHeldMutex;
423
424 gInnermostMutexHolder.SetInnermostMutex (innermostMutex);
425
426 mutex.fPrevHeldMutex = NULL;
427
428 #endif
429
430 if (timeoutSecs < 0)
431 {
432
433 pthread_cond_wait (&fPthreadCondition, &mutex.fPthreadMutex);
434
435 }
436
437 else
438 {
439
440 struct timespec now;
441
442 dng_pthread_now (&now);
443
444 timeoutSecs += now.tv_sec;
445 timeoutSecs += now.tv_nsec / 1000000000.0;
446
447 now.tv_sec = (long) timeoutSecs;
448 now.tv_nsec = (long) ((timeoutSecs - now.tv_sec) * 1000000000);
449
450 #if defined(_MSC_VER) && _MSC_VER >= 1900
451
452 struct dng_timespec tempNow;
453
454 tempNow.tv_sec = (long) now.tv_sec;
455 tempNow.tv_nsec = now.tv_nsec;
456
457 timedOut = (pthread_cond_timedwait (&fPthreadCondition, &mutex.fPthreadMutex, &tempNow) == ETIMEDOUT);
458
459 #else
460
461 timedOut = (pthread_cond_timedwait (&fPthreadCondition, &mutex.fPthreadMutex, &now) == ETIMEDOUT);
462
463 #endif
464
465 }
466
467 #if qDNGThreadTestMutexLevels
468
469 mutex.fPrevHeldMutex = innermostMutex;
470
471 gInnermostMutexHolder.SetInnermostMutex (&mutex);
472
473 #endif
474
475 return !timedOut;
476 #else
477 return true;
478 #endif
479
480 }
481
482 /*****************************************************************************/
483
484 void dng_condition::Signal ()
485 {
486
487 #if qDNGThreadSafe
488 int result;
489
490 result = pthread_cond_signal (&fPthreadCondition);
491
492 DNG_ASSERT (result == 0, "pthread_cond_signal failed.");
493
494 if (result != 0)
495 ThrowProgramError ();
496 #endif
497
498 }
499
500 /*****************************************************************************/
501
502 void dng_condition::Broadcast ()
503 {
504 #if qDNGThreadSafe
505
506 int result;
507
508 result = pthread_cond_broadcast (&fPthreadCondition);
509
510 DNG_ASSERT (result == 0, "pthread_cond_broadcast failed.");
511
512 if (result != 0)
513 ThrowProgramError ();
514 #endif
515 }
516
517 /*****************************************************************************/