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 extern MagickPrivate MagickBooleanType
00026 SemaphoreComponentGenesis(void);
00027
00028 extern MagickPrivate void
00029 SemaphoreComponentTerminus(void);
00030
00031 #if defined(MAGICKCORE_OPENMP_SUPPORT)
00032 static omp_lock_t
00033 semaphore_mutex;
00034 #elif defined(MAGICKCORE_THREAD_SUPPORT)
00035 static pthread_mutex_t
00036 semaphore_mutex = PTHREAD_MUTEX_INITIALIZER;
00037 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
00038 static LONG
00039 semaphore_mutex = 0;
00040 #else
00041 static ssize_t
00042 semaphore_mutex = 0;
00043 #endif
00044
00045 static MagickBooleanType
00046 active_mutex = MagickFalse;
00047
00048 static inline void DestroyMagickMutex(void)
00049 {
00050 #if defined(MAGICKCORE_OPENMP_SUPPORT)
00051 if (active_mutex != MagickFalse)
00052 omp_destroy_lock(&semaphore_mutex);
00053 #endif
00054 active_mutex=MagickFalse;
00055 }
00056
00057 static inline void InitializeMagickMutex(void)
00058 {
00059 #if defined(MAGICKCORE_OPENMP_SUPPORT)
00060 if (active_mutex == MagickFalse)
00061 omp_init_lock(&semaphore_mutex);
00062 #endif
00063 active_mutex=MagickTrue;
00064 }
00065
00066 static inline void LockMagickMutex(void)
00067 {
00068 #if defined(MAGICKCORE_OPENMP_SUPPORT)
00069 omp_set_lock(&semaphore_mutex);
00070 #elif defined(MAGICKCORE_THREAD_SUPPORT)
00071 {
00072 int
00073 status;
00074
00075 status=pthread_mutex_lock(&semaphore_mutex);
00076 if (status != 0)
00077 {
00078 errno=status;
00079 ThrowFatalException(ResourceLimitFatalError,"UnableToLockSemaphore");
00080 }
00081 }
00082 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
00083 while (InterlockedCompareExchange(&semaphore_mutex,1L,0L) != 0)
00084 Sleep(10);
00085 #endif
00086 }
00087
00088 static inline void UnlockMagickMutex(void)
00089 {
00090 #if defined(MAGICKCORE_OPENMP_SUPPORT)
00091 omp_unset_lock(&semaphore_mutex);
00092 #elif defined(MAGICKCORE_THREAD_SUPPORT)
00093 {
00094 int
00095 status;
00096
00097 status=pthread_mutex_unlock(&semaphore_mutex);
00098 if (status != 0)
00099 {
00100 errno=status;
00101 ThrowFatalException(ResourceLimitFatalError,"UnableToUnlockSemaphore");
00102 }
00103 }
00104 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
00105 InterlockedExchange(&semaphore_mutex,0L);
00106 #endif
00107 }
00108
00109 #if defined(__cplusplus) || defined(c_plusplus)
00110 }
00111 #endif
00112
00113 #endif