• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2   Copyright 1999-2021 ImageMagick Studio LLC, a non-profit organization
3   dedicated to making software imaging solutions freely available.
4 
5   You may not use this file except in compliance with the License.  You may
6   obtain a copy of the License at
7 
8     https://imagemagick.org/script/license.php
9 
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License.
15 
16   MagickCore methods to synchronize code within a translation unit.
17 */
18 #ifndef MAGICKCORE_MUTEX_H
19 #define MAGICKCORE_MUTEX_H
20 
21 #if defined(__cplusplus) || defined(c_plusplus)
22 extern "C" {
23 #endif
24 
25 /*
26   When included in a translation unit, the following code provides the
27   translation unit a means by which to synchronize multiple threads that might
28   try to enter the same critical section or to access a shared resource; it can
29   be included in multiple translation units, and thereby provide a separate,
30   independent means of synchronization to each such translation unit.
31 */
32 
33 #if defined(MAGICKCORE_OPENMP_SUPPORT)
34 static MagickBooleanType
35   translation_unit_initialized = MagickFalse;
36 
37 static omp_lock_t
38   translation_unit_mutex;
39 #elif defined(MAGICKCORE_THREAD_SUPPORT)
40 static pthread_mutex_t
41   translation_unit_mutex = PTHREAD_MUTEX_INITIALIZER;
42 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
43 static LONG
44   translation_unit_mutex = 0;
45 #endif
46 
DestroyMagickMutex(void)47 static inline void DestroyMagickMutex(void)
48 {
49 #if defined(MAGICKCORE_OPENMP_SUPPORT)
50   omp_destroy_lock(&translation_unit_mutex);
51   translation_unit_initialized=MagickFalse;
52 #endif
53 }
54 
InitializeMagickMutex(void)55 static inline void InitializeMagickMutex(void)
56 {
57 #if defined(MAGICKCORE_OPENMP_SUPPORT)
58   omp_init_lock(&translation_unit_mutex);
59   translation_unit_initialized=MagickTrue;
60 #endif
61 }
62 
LockMagickMutex(void)63 static inline void LockMagickMutex(void)
64 {
65 #if defined(MAGICKCORE_OPENMP_SUPPORT)
66   if (translation_unit_initialized == MagickFalse)
67     InitializeMagickMutex();
68   omp_set_lock(&translation_unit_mutex);
69 #elif defined(MAGICKCORE_THREAD_SUPPORT)
70   {
71     int
72       status;
73 
74     status=pthread_mutex_lock(&translation_unit_mutex);
75     if (status != 0)
76       {
77         errno=status;
78         ThrowFatalException(ResourceLimitFatalError,"UnableToLockSemaphore");
79       }
80   }
81 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
82   while (InterlockedCompareExchange(&translation_unit_mutex,1L,0L) != 0)
83     Sleep(10);
84 #endif
85 }
86 
UnlockMagickMutex(void)87 static inline void UnlockMagickMutex(void)
88 {
89 #if defined(MAGICKCORE_OPENMP_SUPPORT)
90   omp_unset_lock(&translation_unit_mutex);
91 #elif defined(MAGICKCORE_THREAD_SUPPORT)
92   {
93     int
94       status;
95 
96     status=pthread_mutex_unlock(&translation_unit_mutex);
97     if (status != 0)
98       {
99         errno=status;
100         ThrowFatalException(ResourceLimitFatalError,"UnableToUnlockSemaphore");
101       }
102   }
103 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
104   InterlockedExchange(&translation_unit_mutex,0L);
105 #endif
106 }
107 
108 #if defined(__cplusplus) || defined(c_plusplus)
109 }
110 #endif
111 
112 #endif
113