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