00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef MAGICKCORE_MUTEX_H
00019 #define MAGICKCORE_MUTEX_H
00020
00021 #if defined(__cplusplus) || defined(c_plusplus)
00022 extern "C" {
00023 #endif
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #if defined(MAGICKCORE_OPENMP_SUPPORT)
00034 static omp_lock_t
00035 translation_unit_mutex;
00036 #elif defined(MAGICKCORE_THREAD_SUPPORT)
00037 static pthread_mutex_t
00038 translation_unit_mutex = PTHREAD_MUTEX_INITIALIZER;
00039 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
00040 static LONG
00041 translation_unit_mutex = 0;
00042 #endif
00043
00044 static inline void DestroyMagickMutex(void)
00045 {
00046 #if defined(MAGICKCORE_OPENMP_SUPPORT)
00047 omp_destroy_lock(&translation_unit_mutex);
00048 #endif
00049 }
00050
00051 static inline void InitializeMagickMutex(void)
00052 {
00053 #if defined(MAGICKCORE_OPENMP_SUPPORT)
00054 omp_init_lock(&translation_unit_mutex);
00055 #endif
00056 }
00057
00058 static inline void LockMagickMutex(void)
00059 {
00060 #if defined(MAGICKCORE_OPENMP_SUPPORT)
00061 omp_set_lock(&translation_unit_mutex);
00062 #elif defined(MAGICKCORE_THREAD_SUPPORT)
00063 {
00064 int
00065 status;
00066
00067 status=pthread_mutex_lock(&translation_unit_mutex);
00068 if (status != 0)
00069 {
00070 errno=status;
00071 ThrowFatalException(ResourceLimitFatalError,"UnableToLockSemaphore");
00072 }
00073 }
00074 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
00075 while (InterlockedCompareExchange(&translation_unit_mutex,1L,0L) != 0)
00076 Sleep(10);
00077 #endif
00078 }
00079
00080 static inline void UnlockMagickMutex(void)
00081 {
00082 #if defined(MAGICKCORE_OPENMP_SUPPORT)
00083 omp_unset_lock(&translation_unit_mutex);
00084 #elif defined(MAGICKCORE_THREAD_SUPPORT)
00085 {
00086 int
00087 status;
00088
00089 status=pthread_mutex_unlock(&translation_unit_mutex);
00090 if (status != 0)
00091 {
00092 errno=status;
00093 ThrowFatalException(ResourceLimitFatalError,"UnableToUnlockSemaphore");
00094 }
00095 }
00096 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
00097 InterlockedExchange(&translation_unit_mutex,0L);
00098 #endif
00099 }
00100
00101 #if defined(__cplusplus) || defined(c_plusplus)
00102 }
00103 #endif
00104
00105 #endif