]> granicus.if.org Git - imagemagick/blob - MagickCore/semaphore-private.h
(no commit message)
[imagemagick] / MagickCore / semaphore-private.h
1 /*
2   Copyright 1999-2014 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.
6   obtain a copy of the License at
7   
8     http://www.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 private methods to lock and unlock semaphores.
17 */
18 #ifndef _MAGICKCORE_SEMAPHORE_PRIVATE_H
19 #define _MAGICKCORE_SEMAPHORE_PRIVATE_H
20
21 #if defined(__cplusplus) || defined(c_plusplus)
22 extern "C" {
23 #endif
24
25 extern MagickPrivate MagickBooleanType
26   SemaphoreComponentGenesis(void);
27
28 extern MagickPrivate void
29   SemaphoreComponentTerminus(void);
30
31 #if defined(MAGICKCORE_THREAD_SUPPORT)
32 static pthread_mutex_t
33   semaphore_mutex = PTHREAD_MUTEX_INITIALIZER;
34 #elif defined(MAGICKCORE_HAVE_WINTHREADS)
35 static LONG
36   semaphore_mutex = 0;
37 #elif defined(MAGICKCORE_OPENMP_SUPPORT)
38 static omp_lock_t
39   semaphore_mutex;
40 #else
41 static ssize_t
42   semaphore_mutex = 0;
43 #endif
44
45 static MagickBooleanType
46   active_mutex = MagickFalse;
47
48 static inline void DestroyMagickMutex(void)
49 {
50   if (active_mutex != MagickFalse)
51     {
52 #if defined(MAGICKCORE_THREAD_SUPPORT)
53       ;
54 #elif defined(MAGICKCORE_HAVE_WINTHREADS)
55       DeleteCriticalSection(&semaphore_mutex);
56 #elif defined(MAGICKCORE_OPENMP_SUPPORT)
57       omp_destroy_lock(&semaphore_mutex);
58 #endif
59     }
60   active_mutex=MagickFalse;
61 }
62
63 static inline void InitializeMagickMutex(void)
64 {
65   if (active_mutex == MagickFalse)
66     {
67 #if defined(MAGICKCORE_THREAD_SUPPORT)
68       ;
69 #elif defined(MAGICKCORE_HAVE_WINTHREADS)
70       InitializeCriticalSection(&semaphore_mutex);
71 #elif defined(MAGICKCORE_OPENMP_SUPPORT)
72       omp_init_lock(&semaphore_mutex);
73 #endif
74     }
75   active_mutex=MagickTrue;
76 }
77
78 static inline void LockMagickMutex(void)
79 {
80 #if defined(MAGICKCORE_THREAD_SUPPORT)
81   {
82     int
83       status;
84
85     status=pthread_mutex_lock(&semaphore_mutex);
86     if (status != 0)
87       {
88         errno=status;
89         ThrowFatalException(ResourceLimitFatalError,"UnableToLockSemaphore");
90       }
91   }
92 #elif defined(MAGICKCORE_HAVE_WINTHREADS)
93   while (InterlockedCompareExchange(&semaphore_mutex,1L,0L) != 0)
94     Sleep(10);
95 #if defined(MAGICKCORE_OPENMP_SUPPORT)
96   omp_set_lock(&semaphore_mutex);
97 #endif
98 }
99
100 static inline void UnlockMagickMutex(void)
101 {
102 #if defined(MAGICKCORE_THREAD_SUPPORT)
103   {
104     int
105       status;
106
107     status=pthread_mutex_unlock(&semaphore_mutex);
108     if (status != 0)
109       {
110         errno=status;
111         ThrowFatalException(ResourceLimitFatalError,"UnableToUnlockSemaphore");
112       }
113   }
114 #elif defined(MAGICKCORE_HAVE_WINTHREADS)
115   InterlockedExchange(&semaphore_mutex,0L);
116 #elif defined(MAGICKCORE_OPENMP_SUPPORT)
117   omp_unset_lock(&semaphore_mutex);
118 #endif
119 }
120
121 #if defined(__cplusplus) || defined(c_plusplus)
122 }
123 #endif
124
125 #endif