File indexing completed on 2025-01-19 03:55:07

0001 /*****************************************************************************/
0002 // Copyright 2006-2019 Adobe Systems Incorporated
0003 // All Rights Reserved.
0004 //
0005 // NOTICE:  Adobe permits you to use, modify, and distribute this file in
0006 // accordance with the terms of the Adobe license agreement accompanying it.
0007 /*****************************************************************************/
0008 
0009 #ifndef __dng_mutex__
0010 #define __dng_mutex__
0011 
0012 /******************************************************************************/
0013 
0014 #include "dng_flags.h"
0015 #include "dng_types.h"
0016 #include "dng_uncopyable.h"
0017 
0018 #if qDNGThreadSafe
0019 #include "dng_pthread.h"
0020 #endif
0021 
0022 #include <mutex>
0023 
0024 typedef std::mutex                   dng_std_mutex;
0025 typedef std::lock_guard<std::mutex>  dng_lock_std_mutex;
0026 typedef std::unique_lock<std::mutex> dng_unique_lock;
0027 
0028 // We should try to phase out use of dng_mutex over time.
0029 //
0030 // Note that dng_mutex differs from dng_std_mutex (std::mutex) in that
0031 // dng_mutex supports recursive locking (hierarchical mutex).
0032 
0033 /******************************************************************************/
0034 
0035 class dng_mutex: private dng_uncopyable
0036     {
0037 
0038     public:
0039 
0040         enum
0041             {
0042             kDNGMutexLevelLeaf   = 0x70000000u,
0043             kDNGMutexLevelIgnore = 0x7FFFFFFFu
0044             };
0045 
0046         dng_mutex (const char *mutexName,
0047                    uint32 mutexLevel = kDNGMutexLevelLeaf);
0048 
0049         virtual ~dng_mutex ();
0050 
0051         void Lock ();
0052 
0053         void Unlock ();
0054 
0055         const char *MutexName () const;
0056 
0057     protected:
0058 
0059         #if qDNGThreadSafe
0060 
0061         pthread_mutex_t fPthreadMutex;
0062 
0063         const uint32 fMutexLevel;
0064 
0065         uint32 fRecursiveLockCount;
0066 
0067         dng_mutex *fPrevHeldMutex;
0068 
0069         const char * const fMutexName;
0070 
0071         friend class dng_condition;
0072 
0073         #endif
0074 
0075     };
0076 
0077 /*****************************************************************************/
0078 
0079 class dng_lock_mutex: private dng_uncopyable
0080     {
0081 
0082     private:
0083 
0084         dng_mutex *fMutex;
0085 
0086     public:
0087 
0088         dng_lock_mutex (dng_mutex *mutex);
0089 
0090         dng_lock_mutex (dng_mutex &mutex);
0091 
0092         ~dng_lock_mutex ();
0093 
0094     };
0095 
0096 /*****************************************************************************/
0097 
0098 class dng_unlock_mutex: private dng_uncopyable
0099     {
0100 
0101     private:
0102 
0103         dng_mutex *fMutex;
0104 
0105     public:
0106 
0107         dng_unlock_mutex (dng_mutex *mutex);
0108 
0109         dng_unlock_mutex (dng_mutex &mutex);
0110 
0111         ~dng_unlock_mutex ();
0112 
0113     };
0114 
0115 /*****************************************************************************/
0116 
0117 class dng_condition: private dng_uncopyable
0118     {
0119 
0120     public:
0121 
0122         dng_condition ();
0123 
0124         ~dng_condition ();
0125 
0126         bool Wait (dng_mutex &mutex, double timeoutSecs = -1.0);
0127 
0128         void Signal ();
0129 
0130         void Broadcast ();
0131 
0132     protected:
0133 
0134 
0135 #if qDNGThreadSafe
0136         pthread_cond_t fPthreadCondition;
0137 #endif // qDNGThreadSafe
0138 
0139     };
0140 
0141 /*****************************************************************************/
0142 
0143 #endif
0144 
0145 /*****************************************************************************/