semaphore-private.h

Go to the documentation of this file.
00001 /*
00002   Copyright 1999-2020 ImageMagick Studio LLC, a non-profit organization
00003   dedicated to making software imaging solutions freely available.
00004   
00005   You may not use this file except in compliance with the License.  You may
00006   obtain a copy of the License at
00007   
00008     https://imagemagick.org/script/license.php
00009   
00010   Unless required by applicable law or agreed to in writing, software
00011   distributed under the License is distributed on an "AS IS" BASIS,
00012   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013   See the License for the specific language governing permissions and
00014   limitations under the License.
00015 
00016   MagickCore private methods to lock and unlock semaphores.
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

Generated on 9 Dec 2019 for MagickCore by  doxygen 1.6.1