/src/imagemagick/MagickCore/mutex.h
Line | Count | Source (jump to first uncovered line) |
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) |
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 | | |
47 | | static inline void DestroyMagickMutex(void) |
48 | 0 | { |
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 | 0 | } Unexecuted instantiation: magick.c:DestroyMagickMutex Unexecuted instantiation: semaphore.c:DestroyMagickMutex |
55 | | |
56 | | static inline void InitializeMagickMutex(void) |
57 | 588 | { |
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 | 588 | } magick.c:InitializeMagickMutex Line | Count | Source | 57 | 294 | { | 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 | 294 | } |
semaphore.c:InitializeMagickMutex Line | Count | Source | 57 | 294 | { | 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 | 294 | } |
|
64 | | |
65 | | static inline void LockMagickMutex(void) |
66 | 210M | { |
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 | 210M | int |
74 | 210M | status; |
75 | | |
76 | 210M | status=pthread_mutex_lock(&translation_unit_mutex); |
77 | 210M | if (status != 0) |
78 | 0 | { |
79 | 0 | errno=status; |
80 | 0 | ThrowFatalException(ResourceLimitFatalError,"UnableToLockSemaphore"); |
81 | 0 | } |
82 | 210M | } |
83 | | #elif defined(MAGICKCORE_WINDOWS_SUPPORT) |
84 | | while (InterlockedCompareExchange(&translation_unit_mutex,1L,0L) != 0) |
85 | | Sleep(10); |
86 | | #endif |
87 | 210M | } Line | Count | Source | 66 | 294 | { | 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 | 294 | int | 74 | 294 | status; | 75 | | | 76 | 294 | status=pthread_mutex_lock(&translation_unit_mutex); | 77 | 294 | if (status != 0) | 78 | 0 | { | 79 | 0 | errno=status; | 80 | 0 | ThrowFatalException(ResourceLimitFatalError,"UnableToLockSemaphore"); | 81 | 0 | } | 82 | 294 | } | 83 | | #elif defined(MAGICKCORE_WINDOWS_SUPPORT) | 84 | | while (InterlockedCompareExchange(&translation_unit_mutex,1L,0L) != 0) | 85 | | Sleep(10); | 86 | | #endif | 87 | 294 | } |
semaphore.c:LockMagickMutex Line | Count | Source | 66 | 210M | { | 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 | 210M | int | 74 | 210M | status; | 75 | | | 76 | 210M | status=pthread_mutex_lock(&translation_unit_mutex); | 77 | 210M | if (status != 0) | 78 | 0 | { | 79 | 0 | errno=status; | 80 | 0 | ThrowFatalException(ResourceLimitFatalError,"UnableToLockSemaphore"); | 81 | 0 | } | 82 | 210M | } | 83 | | #elif defined(MAGICKCORE_WINDOWS_SUPPORT) | 84 | | while (InterlockedCompareExchange(&translation_unit_mutex,1L,0L) != 0) | 85 | | Sleep(10); | 86 | | #endif | 87 | 210M | } |
|
88 | | |
89 | | static inline void UnlockMagickMutex(void) |
90 | 210M | { |
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 | 210M | int |
98 | 210M | status; |
99 | | |
100 | 210M | status=pthread_mutex_unlock(&translation_unit_mutex); |
101 | 210M | if (status != 0) |
102 | 0 | { |
103 | 0 | errno=status; |
104 | 0 | ThrowFatalException(ResourceLimitFatalError,"UnableToUnlockSemaphore"); |
105 | 0 | } |
106 | 210M | } |
107 | | #elif defined(MAGICKCORE_WINDOWS_SUPPORT) |
108 | | InterlockedExchange(&translation_unit_mutex,0L); |
109 | | #endif |
110 | 210M | } magick.c:UnlockMagickMutex Line | Count | Source | 90 | 294 | { | 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 | 294 | int | 98 | 294 | status; | 99 | | | 100 | 294 | status=pthread_mutex_unlock(&translation_unit_mutex); | 101 | 294 | if (status != 0) | 102 | 0 | { | 103 | 0 | errno=status; | 104 | 0 | ThrowFatalException(ResourceLimitFatalError,"UnableToUnlockSemaphore"); | 105 | 0 | } | 106 | 294 | } | 107 | | #elif defined(MAGICKCORE_WINDOWS_SUPPORT) | 108 | | InterlockedExchange(&translation_unit_mutex,0L); | 109 | | #endif | 110 | 294 | } |
semaphore.c:UnlockMagickMutex Line | Count | Source | 90 | 210M | { | 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 | 210M | int | 98 | 210M | status; | 99 | | | 100 | 210M | status=pthread_mutex_unlock(&translation_unit_mutex); | 101 | 210M | if (status != 0) | 102 | 0 | { | 103 | 0 | errno=status; | 104 | 0 | ThrowFatalException(ResourceLimitFatalError,"UnableToUnlockSemaphore"); | 105 | 0 | } | 106 | 210M | } | 107 | | #elif defined(MAGICKCORE_WINDOWS_SUPPORT) | 108 | | InterlockedExchange(&translation_unit_mutex,0L); | 109 | | #endif | 110 | 210M | } |
|
111 | | |
112 | | #if defined(__cplusplus) || defined(c_plusplus) |
113 | | } |
114 | | #endif |
115 | | |
116 | | #endif |