fixed atomics and semaphore for windows. can't test it correctly however due to that I only have a virtual box machine

This commit is contained in:
Magnus Auvinen 2012-01-01 15:56:28 +01:00
parent c31c82a584
commit e59b24d8db
3 changed files with 82 additions and 22 deletions

View file

@ -495,6 +495,21 @@ void lock_release(LOCK lock)
#endif #endif
} }
#if defined(CONF_FAMILY_UNIX)
void semaphore_init(SEMAPHORE *sem) { sem_init(sem, 0, 0); }
void semaphore_wait(SEMAPHORE *sem) { sem_wait(sem); }
void semaphore_signal(SEMAPHORE *sem) { sem_post(sem); }
void semaphore_destroy(SEMAPHORE *sem) { sem_destroy(sem); }
#elif defined(CONF_FAMILY_WINDOWS)
void semaphore_init(SEMAPHORE *sem) { (HANDLE)(*sem) = CreateSemaphore(0, 0, 10000, 0); }
void semaphore_wait(SEMAPHORE *sem) { WaitForSingleObject((HANDLE)*sem, 0L); }
void semaphore_signal(SEMAPHORE *sem) { ReleaseSemaphore((HANDLE)*sem, 1, NULL); }
void semaphore_destroy(SEMAPHORE *sem) { CloseHandle((HANDLE)*sem); }
#else
#error not implemented on this platform
#endif
/* ----- time ----- */ /* ----- time ----- */
int64 time_get() int64 time_get()
{ {

View file

@ -388,6 +388,23 @@ int lock_try(LOCK lock);
void lock_wait(LOCK lock); void lock_wait(LOCK lock);
void lock_release(LOCK lock); void lock_release(LOCK lock);
/* Group: Semaphores */
#if defined(CONF_FAMILY_UNIX)
#include <semaphore.h>
typedef sem_t SEMAPHORE;
#elif defined(CONF_FAMILY_WINDOWS)
typedef void* SEMAPHORE;
#else
#error missing sempahore implementation
#endif
void semaphore_init(SEMAPHORE *sem);
void semaphore_wait(SEMAPHORE *sem);
void semaphore_signal(SEMAPHORE *sem);
void semaphore_destroy(SEMAPHORE *sem);
/* Group: Timer */ /* Group: Timer */
#ifdef __GNUC__ #ifdef __GNUC__
/* if compiled with -pedantic-errors it will complain about long /* if compiled with -pedantic-errors it will complain about long

View file

@ -3,32 +3,60 @@
#include "../system.h" #include "../system.h"
inline unsigned atomic_inc(volatile unsigned *pValue) /*
{ atomic_inc - should return the value after increment
return __sync_fetch_and_add(pValue, 1); atomic_dec - should return the value after decrement
} atomic_compswap - should return the value before the eventual swap
*/
inline unsigned atomic_dec(volatile unsigned *pValue) #if defined(__GNUC__)
{
return __sync_fetch_and_add(pValue, -1);
}
inline unsigned atomic_compswap(volatile unsigned *pValue, unsigned comperand, unsigned value) inline unsigned atomic_inc(volatile unsigned *pValue)
{ {
return __sync_val_compare_and_swap(pValue, comperand, value); return __sync_fetch_and_add(pValue, 1);
} }
inline void sync_barrier() inline unsigned atomic_dec(volatile unsigned *pValue)
{ {
__sync_synchronize(); return __sync_fetch_and_add(pValue, -1);
} }
#include <semaphore.h> inline unsigned atomic_compswap(volatile unsigned *pValue, unsigned comperand, unsigned value)
typedef sem_t SEMAPHORE; {
inline void semaphore_init(SEMAPHORE *sem) { sem_init(sem, 0, 0); } return __sync_val_compare_and_swap(pValue, comperand, value);
inline void semaphore_wait(SEMAPHORE *sem) { sem_wait(sem); } }
inline void semaphore_signal(SEMAPHORE *sem) { sem_post(sem); }
inline void semaphore_destroy(SEMAPHORE *sem) { sem_destroy(sem); } inline void sync_barrier()
{
__sync_synchronize();
}
#elif defined(_MSC_VER)
#include <intrin.h>
inline unsigned atomic_inc(volatile unsigned *pValue)
{
return _InterlockedIncrement((volatile long *)pValue);
}
inline unsigned atomic_dec(volatile unsigned *pValue)
{
return _InterlockedDecrement((volatile long *)pValue);
}
inline unsigned atomic_compswap(volatile unsigned *pValue, unsigned comperand, unsigned value)
{
return _InterlockedCompareExchange((volatile long *)pValue, (long)value, (long)comperand);
}
inline void sync_barrier()
{
_ReadWriteBarrier();
}
#else
#error missing atomic implementation for this compiler
#endif
class semaphore class semaphore
{ {