semaphore-private.h

Go to the documentation of this file.
00001 /*
00002   Copyright 1999-2010 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.wizards-toolkit.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   Wizard's Toolkit private methods to lock and unlock semaphores.
00017 */
00018 #ifndef _WIZARDSTOOLKIT_SEMAPHORE_PRIVATE_H
00019 #define _WIZARDSTOOLKIT_SEMAPHORE_PRIVATE_H
00020 
00021 #if defined(__cplusplus) || defined(c_plusplus)
00022 extern "C" {
00023 #endif
00024 
00025 #include "wizard/exception-private.h"
00026 
00027 #if defined(WIZARDSTOOLKIT_HAVE_PTHREAD)
00028 static pthread_mutex_t
00029   semaphore_mutex = PTHREAD_MUTEX_INITIALIZER;
00030 #elif defined(WIZARDSTOOLKIT_HAVE_WINTHREADS)
00031 static LONG
00032   semaphore_mutex = 0;
00033 #else
00034 static ssize_t
00035   semaphore_mutex = 0;
00036 #endif
00037 
00038 static inline void LockWizardMutex(void)
00039 {
00040 #if defined(WIZARDSTOOLKIT_HAVE_PTHREAD)
00041   {
00042     int
00043       status;
00044 
00045     status=pthread_mutex_lock(&semaphore_mutex);
00046     if (status != 0)
00047       { 
00048         errno=status;
00049         ThrowFatalException(ResourceFatalError,
00050           "unable to lock semaphore `%s'");
00051       }
00052   }
00053 #elif defined(WIZARDSTOOLKIT_HAVE_WINTHREADS)
00054   while (InterlockedCompareExchange(&semaphore_mutex,1L,0L) != 0)
00055     Sleep(10);
00056 #endif
00057 }
00058 
00059 static inline void UnlockWizardMutex(void)
00060 {
00061 #if defined(WIZARDSTOOLKIT_HAVE_PTHREAD)
00062   {
00063     int
00064       status;
00065 
00066     status=pthread_mutex_unlock(&semaphore_mutex);
00067     if (status != 0)
00068       {
00069         errno=status;
00070         ThrowFatalException(ResourceFatalError,
00071           "unable to unlock semaphore `%s'");
00072       }
00073   }
00074 #elif defined(WIZARDSTOOLKIT_HAVE_WINTHREADS)
00075   InterlockedExchange(&semaphore_mutex,0L);
00076 #endif
00077 }
00078 
00079 #if defined(__cplusplus) || defined(c_plusplus)
00080 }
00081 #endif
00082 
00083 #endif
Generated by  doxygen 1.6.2-20100208