MagickCore  6.7.7
semaphore-private.h
Go to the documentation of this file.
00001 /*
00002   Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization
00003   dedicated to making software imaging solutions freely available.
00004   
00005   You may not use this file except in compliance with the License.
00006   obtain a copy of the License at
00007   
00008     http://www.imagemagick.org/script/license.php
00009   
00010   Unless required by applicable law or agreed to in writing, software
00011   distributed under the License is distributed on an "AS IS" BASIS,
00012   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013   See the License for the specific language governing permissions and
00014   limitations under the License.
00015 
00016   MagickCore private methods to lock and unlock semaphores.
00017 */
00018 #ifndef _MAGICKCORE_SEMAPHORE_PRIVATE_H
00019 #define _MAGICKCORE_SEMAPHORE_PRIVATE_H
00020 
00021 #if defined(__cplusplus) || defined(c_plusplus)
00022 extern "C" {
00023 #endif
00024 
00025 extern MagickPrivate MagickBooleanType
00026   SemaphoreComponentGenesis(void);
00027 
00028 extern MagickPrivate void
00029   SemaphoreComponentTerminus(void);
00030 
00031 #if defined(MAGICKCORE_THREAD_SUPPORT)
00032 static pthread_mutex_t
00033   semaphore_mutex = PTHREAD_MUTEX_INITIALIZER;
00034 #elif defined(MAGICKCORE_HAVE_WINTHREADS)
00035 static LONG
00036   semaphore_mutex = 0;
00037 #else
00038 static ssize_t
00039   semaphore_mutex = 0;
00040 #endif
00041 
00042 static inline void LockMagickMutex(void)
00043 {
00044 #if defined(MAGICKCORE_THREAD_SUPPORT)
00045   {
00046     int
00047       status;
00048 
00049     status=pthread_mutex_lock(&semaphore_mutex);
00050     if (status != 0)
00051       {
00052         errno=status;
00053         ThrowFatalException(ResourceLimitFatalError,"UnableToLockSemaphore");
00054       }
00055   }
00056 #elif defined(MAGICKCORE_HAVE_WINTHREADS)
00057   while (InterlockedCompareExchange(&semaphore_mutex,1L,0L) != 0)
00058     Sleep(10);
00059 #endif
00060 }
00061 
00062 static inline void UnlockMagickMutex(void)
00063 {
00064 #if defined(MAGICKCORE_THREAD_SUPPORT)
00065   {
00066     int
00067       status;
00068 
00069     status=pthread_mutex_unlock(&semaphore_mutex);
00070     if (status != 0)
00071       {
00072         errno=status;
00073         ThrowFatalException(ResourceLimitFatalError,"UnableToUnlockSemaphore");
00074       }
00075   }
00076 #elif defined(MAGICKCORE_HAVE_WINTHREADS)
00077   InterlockedExchange(&semaphore_mutex,0L);
00078 #endif
00079 }
00080 
00081 #if defined(__cplusplus) || defined(c_plusplus)
00082 }
00083 #endif
00084 
00085 #endif