]> 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_OPENMP_SUPPORT)
32 static omp_lock_t
33   semaphore_mutex;
34 #elif defined(MAGICKCORE_THREAD_SUPPORT)
35 static pthread_mutex_t
36   semaphore_mutex = PTHREAD_MUTEX_INITIALIZER;
37 #elif defined(MAGICKCORE_HAVE_WINTHREADS)
38 static LONG
39   semaphore_mutex = 0;
40 #else
41 static ssize_t
42   semaphore_mutex = 0;
43 #endif
44
45 static inline void LockMagickMutex(void)
46 {
47 #if defined(MAGICKCORE_OPENMP_SUPPORT)
48   omp_set_lock(&semaphore_mutex);
49 #elif defined(MAGICKCORE_THREAD_SUPPORT)
50   {
51     int
52       status;
53
54     status=pthread_mutex_lock(&semaphore_mutex);
55     if (status != 0)
56       {
57         errno=status;
58         ThrowFatalException(ResourceLimitFatalError,"UnableToLockSemaphore");
59       }
60   }
61 #elif defined(MAGICKCORE_HAVE_WINTHREADS)
62   while (InterlockedCompareExchange(&semaphore_mutex,1L,0L) != 0)
63     Sleep(10);
64 #endif
65 }
66
67 static inline void UnlockMagickMutex(void)
68 {
69 #if defined(MAGICKCORE_OPENMP_SUPPORT)
70   omp_unset_lock(&semaphore_mutex);
71 #elif defined(MAGICKCORE_THREAD_SUPPORT)
72   {
73     int
74       status;
75
76     status=pthread_mutex_unlock(&semaphore_mutex);
77     if (status != 0)
78       {
79         errno=status;
80         ThrowFatalException(ResourceLimitFatalError,"UnableToUnlockSemaphore");
81       }
82   }
83 #elif defined(MAGICKCORE_HAVE_WINTHREADS)
84   InterlockedExchange(&semaphore_mutex,0L);
85 #endif
86 }
87
88 #if defined(__cplusplus) || defined(c_plusplus)
89 }
90 #endif
91
92 #endif