semaphore-private.h

Go to the documentation of this file.
00001 /*
00002   Copyright 1999-2019 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 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

Generated on 26 Aug 2019 for MagickCore by  doxygen 1.6.1