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

Generated on 28 Jan 2020 for MagickCore by  doxygen 1.6.1