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