1 /*
2 Copyright 1999-2019 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 omp_lock_t
35 translation_unit_mutex;
36 #elif defined(MAGICKCORE_THREAD_SUPPORT)
37 static pthread_mutex_t
38 translation_unit_mutex = PTHREAD_MUTEX_INITIALIZER;
39 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
40 static LONG
41 translation_unit_mutex = 0;
42 #endif
43
DestroyMagickMutex(void)44 static inline void DestroyMagickMutex(void)
45 {
46 #if defined(MAGICKCORE_OPENMP_SUPPORT)
47 omp_destroy_lock(&translation_unit_mutex);
48 #endif
49 }
50
InitializeMagickMutex(void)51 static inline void InitializeMagickMutex(void)
52 {
53 #if defined(MAGICKCORE_OPENMP_SUPPORT)
54 omp_init_lock(&translation_unit_mutex);
55 #endif
56 }
57
LockMagickMutex(void)58 static inline void LockMagickMutex(void)
59 {
60 #if defined(MAGICKCORE_OPENMP_SUPPORT)
61 omp_set_lock(&translation_unit_mutex);
62 #elif defined(MAGICKCORE_THREAD_SUPPORT)
63 {
64 int
65 status;
66
67 status=pthread_mutex_lock(&translation_unit_mutex);
68 if (status != 0)
69 {
70 errno=status;
71 ThrowFatalException(ResourceLimitFatalError,"UnableToLockSemaphore");
72 }
73 }
74 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
75 while (InterlockedCompareExchange(&translation_unit_mutex,1L,0L) != 0)
76 Sleep(10);
77 #endif
78 }
79
UnlockMagickMutex(void)80 static inline void UnlockMagickMutex(void)
81 {
82 #if defined(MAGICKCORE_OPENMP_SUPPORT)
83 omp_unset_lock(&translation_unit_mutex);
84 #elif defined(MAGICKCORE_THREAD_SUPPORT)
85 {
86 int
87 status;
88
89 status=pthread_mutex_unlock(&translation_unit_mutex);
90 if (status != 0)
91 {
92 errno=status;
93 ThrowFatalException(ResourceLimitFatalError,"UnableToUnlockSemaphore");
94 }
95 }
96 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
97 InterlockedExchange(&translation_unit_mutex,0L);
98 #endif
99 }
100
101 #if defined(__cplusplus) || defined(c_plusplus)
102 }
103 #endif
104
105 #endif
106