semaphore-private.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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