mutex.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 methods to synchronize code within a translation unit.
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   When included in a translation unit, the following code provides the
00027   translation unit a means by which to synchronize multiple threads that might
00028   try to enter the same critical section or to access a shared resource; it can
00029   be included in multiple translation units, and thereby provide a separate,
00030   independent means of synchronization to each such translation unit.
00031 */
00032 
00033 #if defined(MAGICKCORE_OPENMP_SUPPORT)
00034 static MagickBooleanType
00035   translation_unit_initialized = MagickFalse;
00036 
00037 static omp_lock_t
00038   translation_unit_mutex;
00039 #elif defined(MAGICKCORE_THREAD_SUPPORT)
00040 static pthread_mutex_t
00041   translation_unit_mutex = PTHREAD_MUTEX_INITIALIZER;
00042 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
00043 static LONG
00044   translation_unit_mutex = 0;
00045 #endif
00046 
00047 static inline void DestroyMagickMutex(void)
00048 {
00049 #if defined(MAGICKCORE_OPENMP_SUPPORT)
00050   omp_destroy_lock(&translation_unit_mutex);
00051   translation_unit_initialized=MagickFalse;
00052 #endif
00053 }
00054 
00055 static inline void InitializeMagickMutex(void)
00056 {
00057 #if defined(MAGICKCORE_OPENMP_SUPPORT)
00058   omp_init_lock(&translation_unit_mutex);
00059   translation_unit_initialized=MagickTrue;
00060 #endif
00061 }
00062 
00063 static inline void LockMagickMutex(void)
00064 {
00065 #if defined(MAGICKCORE_OPENMP_SUPPORT)
00066   if (translation_unit_initialized == MagickFalse)
00067     InitializeMagickMutex();
00068   omp_set_lock(&translation_unit_mutex);
00069 #elif defined(MAGICKCORE_THREAD_SUPPORT)
00070   {
00071     int
00072       status;
00073 
00074     status=pthread_mutex_lock(&translation_unit_mutex);
00075     if (status != 0)
00076       {
00077         errno=status;
00078         ThrowFatalException(ResourceLimitFatalError,"UnableToLockSemaphore");
00079       }
00080   }
00081 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
00082   while (InterlockedCompareExchange(&translation_unit_mutex,1L,0L) != 0)
00083     Sleep(10);
00084 #endif
00085 }
00086 
00087 static inline void UnlockMagickMutex(void)
00088 {
00089 #if defined(MAGICKCORE_OPENMP_SUPPORT)
00090   omp_unset_lock(&translation_unit_mutex);
00091 #elif defined(MAGICKCORE_THREAD_SUPPORT)
00092   {
00093     int
00094       status;
00095 
00096     status=pthread_mutex_unlock(&translation_unit_mutex);
00097     if (status != 0)
00098       {
00099         errno=status;
00100         ThrowFatalException(ResourceLimitFatalError,"UnableToUnlockSemaphore");
00101       }
00102   }
00103 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
00104   InterlockedExchange(&translation_unit_mutex,0L);
00105 #endif
00106 }
00107 
00108 #if defined(__cplusplus) || defined(c_plusplus)
00109 }
00110 #endif
00111 
00112 #endif

Generated on 20 Apr 2020 for MagickCore by  doxygen 1.6.1