]> granicus.if.org Git - imagemagick/blob - MagickCore/semaphore-private.h
d89b2fd79e912573e6786cb23b169668b6af7786
[imagemagick] / MagickCore / semaphore-private.h
1 /*
2   Copyright 1999-2012 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 #else
38 static ssize_t
39   semaphore_mutex = 0;
40 #endif
41
42 static inline void LockMagickMutex(void)
43 {
44 #if defined(MAGICKCORE_THREAD_SUPPORT)
45   {
46     int
47       status;
48
49     status=pthread_mutex_lock(&semaphore_mutex);
50     if (status != 0)
51       {
52         errno=status;
53         ThrowFatalException(ResourceLimitFatalError,"UnableToLockSemaphore");
54       }
55   }
56 #elif defined(MAGICKCORE_HAVE_WINTHREADS)
57   while (InterlockedCompareExchange(&semaphore_mutex,1L,0L) != 0)
58     Sleep(10);
59 #endif
60 }
61
62 static inline void UnlockMagickMutex(void)
63 {
64 #if defined(MAGICKCORE_THREAD_SUPPORT)
65   {
66     int
67       status;
68
69     status=pthread_mutex_unlock(&semaphore_mutex);
70     if (status != 0)
71       {
72         errno=status;
73         ThrowFatalException(ResourceLimitFatalError,"UnableToUnlockSemaphore");
74       }
75   }
76 #elif defined(MAGICKCORE_HAVE_WINTHREADS)
77   InterlockedExchange(&semaphore_mutex,0L);
78 #endif
79 }
80
81 #if defined(__cplusplus) || defined(c_plusplus)
82 }
83 #endif
84
85 #endif