blob: bbee7762ea21de4155dab6247c0055458e41473f [file] [log] [blame]
cristy7828daa2010-05-13 01:27:43 +00001/*
2 Copyright 1999-2010 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)
22extern "C" {
23#endif
24
25#if defined(MAGICKCORE_HAVE_PTHREAD)
26static pthread_mutex_t
27 semaphore_mutex = PTHREAD_MUTEX_INITIALIZER;
28#elif defined(MAGICKCORE_HAVE_WINTHREADS)
29static LONG
30 semaphore_mutex = 0;
31#else
cristybb503372010-05-27 20:51:26 +000032static ssize_t
cristy7828daa2010-05-13 01:27:43 +000033 semaphore_mutex = 0;
34#endif
35
36static inline void LockMagickMutex(void)
37{
38#if defined(MAGICKCORE_HAVE_PTHREAD)
39 {
40 int
41 status;
42
43 status=pthread_mutex_lock(&semaphore_mutex);
44 if (status != 0)
45 {
46 errno=status;
47 ThrowFatalException(ResourceLimitFatalError,"UnableToLockSemaphore");
48 }
49 }
50#elif defined(MAGICKCORE_HAVE_WINTHREADS)
51 while (InterlockedCompareExchange(&semaphore_mutex,1L,0L) != 0)
52 Sleep(10);
53#endif
54}
55
56static inline void UnlockMagickMutex(void)
57{
58#if defined(MAGICKCORE_HAVE_PTHREAD)
59 {
60 int
61 status;
62
63 status=pthread_mutex_unlock(&semaphore_mutex);
64 if (status != 0)
65 {
66 errno=status;
67 ThrowFatalException(ResourceLimitFatalError,"UnableToUnlockSemaphore");
68 }
69 }
70#elif defined(MAGICKCORE_HAVE_WINTHREADS)
71 InterlockedExchange(&semaphore_mutex,0L);
72#endif
73}
74
75#if defined(__cplusplus) || defined(c_plusplus)
76}
77#endif
78
79#endif