Added sharedSynchronization library

This commit is contained in:
Anonymous
2014-01-14 00:45:55 -07:00
parent 1f331006e1
commit 9cbb2cfecb
47 changed files with 1439 additions and 0 deletions
@@ -0,0 +1,50 @@
// ======================================================================
//
// ConditionVariable.cpp
// Acy Stapp
//
// Copyright 6/19/2001 Sony Online Entertainment
//
// ======================================================================
#include "sharedSynchronization/FirstSharedSynchronization.h"
#include "sharedSynchronization/ConditionVariable.h"
#include "sharedSynchronization/Mutex.h"
ConditionVariable::ConditionVariable(Mutex &m)
: sem(1), _mutex(m), count(0)
{
}
ConditionVariable::~ConditionVariable()
{
}
void ConditionVariable::wait()
{
++count;
_mutex.leave();
sem.wait();
_mutex.enter();
}
void ConditionVariable::signal()
{
if (count)
{
--count;
sem.signal();
}
}
void ConditionVariable::broadcast()
{
if (count)
{
int oldcount = count;
count = 0;
sem.signal(oldcount);
}
}
@@ -0,0 +1,42 @@
// ======================================================================
//
// ConditionVariable.h
// Acy Stapp
//
// Copyright 6/19/2001 Sony Online Entertainment
//
// ======================================================================
#ifndef INCLUDED_ConditionVariable_h
#define INCLUDED_ConditionVariable_h
class Mutex;
#include "sharedSynchronization/Semaphore.h"
/* Condition variable semantics follow pthreads semantics. You
must have the mutex locked before calling signal or broadcast.
When wait returns, the mutex is locked and you must unlock it.
*/
class ConditionVariable
{
public:
ConditionVariable(Mutex &m);
~ConditionVariable();
void wait();
// You must own the mutex before calling signal.
void signal();
void broadcast();
Mutex &mutex() { return _mutex; }
private:
ConditionVariable(const ConditionVariable &o);
ConditionVariable &operator =(const ConditionVariable &o);
Semaphore sem;
Mutex &_mutex;
int count;
};
#endif
@@ -0,0 +1,8 @@
// ======================================================================
//
// FirstSynchronization.cpp
// copyright (c) 2001 Sony Online Entertainment
//
// ======================================================================
#include "sharedSynchronization/FirstSharedSynchronization.h"
@@ -0,0 +1,60 @@
// ======================================================================
//
// Gate.cpp
//
// Copyright 2001-2002 Sony Online Entertainment
// All Rights Reserved.
//
// ======================================================================
#include "sharedSynchronization/FirstSharedSynchronization.h"
#include "sharedSynchronization/Gate.h"
// ======================================================================
Gate::Gate(bool open)
{
handle = CreateEvent(0, true, open, 0);
DEBUG_FATAL(handle == NULL, ("CreateEvent failed"));
}
// ----------------------------------------------------------------------
Gate::~Gate()
{
CloseHandle(handle);
}
// ----------------------------------------------------------------------
void Gate::wait()
{
int errors = 0;
for (;;)
{
DWORD result = WaitForSingleObject(handle, INFINITE);
if (result == WAIT_OBJECT_0)
return;
++errors;
FATAL(errors >= 3, ("WaitForSingleObject failed multiple times"));
}
}
// ----------------------------------------------------------------------
void Gate::close()
{
const BOOL result = ResetEvent(handle);
FATAL(result == 0, ("ResetEvent failed"));
}
// ----------------------------------------------------------------------
void Gate::open()
{
const BOOL result = SetEvent(handle);
FATAL(result == 0, ("SetEvent failed"));
}
// ======================================================================
@@ -0,0 +1,39 @@
// ======================================================================
//
// Gate.h
//
// Copyright 2001-2002 Sony Online Entertainment
// All Rights Reseved.
//
// ======================================================================
#ifndef INCLUDED_Gate_h
#define INCLUDED_Gate_h
// ======================================================================
class Gate
{
public:
Gate(bool initiallyOpen);
~Gate();
void wait();
void close();
void open();
private:
Gate(const Gate &o);
Gate &operator =(const Gate &o);
private:
HANDLE handle;
};
// ======================================================================
#endif
@@ -0,0 +1,52 @@
// ======================================================================
//
// InterlockedInteger.h
// Acy Stapp
//
// Copyright 6/19/2001 Sony Online Entertainment
//
// ======================================================================
#ifndef INCLUDED_InterlockedInteger_h
#define INCLUDED_InterlockedInteger_h
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
class InterlockedInteger
{
public:
explicit InterlockedInteger(int initialValue=0);
int operator =(int); // returns prior value (exchange)
int operator ++(); // returns new value
int operator --(); // returns new value
operator int() const { return static_cast<int>(value); }
private:
InterlockedInteger(const InterlockedInteger &o);
InterlockedInteger &operator =(const InterlockedInteger &o);
volatile int value;
};
inline InterlockedInteger::InterlockedInteger(int i_value)
: value(i_value)
{
}
inline int InterlockedInteger::operator =(int i_value)
{
long * ptr = (long *)&value;
return static_cast<int>(InterlockedExchange(ptr, static_cast<long>(i_value)));
}
inline int InterlockedInteger::operator ++()
{
long * ptr = (long *)&value;
return static_cast<int>(InterlockedIncrement(ptr));
}
inline int InterlockedInteger::operator --()
{
long * ptr = (long *)&value;
return static_cast<int>(InterlockedDecrement(ptr));
}
#endif
@@ -0,0 +1,45 @@
// ======================================================================
//
// InterlockedVoidPointer.h
// Acy Stapp
//
// Copyright 6/19/2001 Sony Online Entertainment
//
// ======================================================================
#ifndef INCLUDED_InterlockedVoidPointer_h
#define INCLUDED_InterlockedVoidPointer_h
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
class InterlockedVoidPointer
{
public:
explicit InterlockedVoidPointer(void * initialValue=0);
void * compareExchange(void * compare, void * exchange); // compare and exchange if same
operator void * () const { return reinterpret_cast<void *>(value); }
private:
InterlockedVoidPointer(const InterlockedVoidPointer &o);
InterlockedVoidPointer &operator =(const InterlockedVoidPointer &o);
void * volatile value;
};
template <class T>
class InterlockedPointer: public InterlockedVoidPointer
{
public:
explicit InterlockedPointer(T * initialValue): InterlockedVoidPointer(initialValue) {}
operator T * () const { return static_cast<const T *>(value); }
};
inline InterlockedVoidPointer::InterlockedVoidPointer(void * initialValue)
{
value = initialValue;
}
inline void * InterlockedVoidPointer::compareExchange(void * compare, void * exchange)
{
return (void *)InterlockedCompareExchange((void **)&value, exchange, compare);
}
#endif
@@ -0,0 +1,53 @@
// ======================================================================
//
// Mutex.cpp
//
// Copyright 2001-2002 Sony Online Entertainment
// All Rights Reserved
//
// ======================================================================
#include "sharedSynchronization/FirstSharedSynchronization.h"
#include "sharedSynchronization/Mutex.h"
// ======================================================================
void Mutex::install()
{
}
// ----------------------------------------------------------------------
void Mutex::remove()
{
}
// ======================================================================
Mutex::Mutex()
{
InitializeCriticalSection(&m_criticalSection);
}
// ----------------------------------------------------------------------
Mutex::~Mutex()
{
DeleteCriticalSection(&m_criticalSection);
}
// ----------------------------------------------------------------------
void Mutex::enter()
{
EnterCriticalSection(&m_criticalSection);
}
// ----------------------------------------------------------------------
void Mutex::leave()
{
LeaveCriticalSection(&m_criticalSection);
}
// ======================================================================
@@ -0,0 +1,41 @@
// ======================================================================
//
// Mutex.h
// Copyright 2001-2002 Sony Online Entertainment
// All Rights Reserved.
//
// ======================================================================
#ifndef INCLUDED_Mutex_H
#define INCLUDED_Mutex_H
// ======================================================================
class Mutex
{
public:
static void install();
static void remove();
public:
Mutex();
~Mutex();
void enter();
void leave();
private:
Mutex(const Mutex &);
Mutex &operator =(const Mutex &);
private:
CRITICAL_SECTION m_criticalSection;
};
// ======================================================================
#endif
@@ -0,0 +1,53 @@
// ======================================================================
//
// RecursiveMutex.cpp
//
// Copyright 2001-2002 Sony Online Entertainment
// All Rights Reserved
//
// ======================================================================
#include "sharedSynchronization/FirstSharedSynchronization.h"
#include "sharedSynchronization/RecursiveMutex.h"
// ======================================================================
void RecursiveMutex::install()
{
}
// ----------------------------------------------------------------------
void RecursiveMutex::remove()
{
}
// ======================================================================
RecursiveMutex::RecursiveMutex()
{
InitializeCriticalSection(&m_criticalSection);
}
// ----------------------------------------------------------------------
RecursiveMutex::~RecursiveMutex()
{
DeleteCriticalSection(&m_criticalSection);
}
// ----------------------------------------------------------------------
void RecursiveMutex::enter()
{
EnterCriticalSection(&m_criticalSection);
}
// ----------------------------------------------------------------------
void RecursiveMutex::leave()
{
LeaveCriticalSection(&m_criticalSection);
}
// ======================================================================
@@ -0,0 +1,41 @@
// ======================================================================
//
// RecursiveMutex.h
// Copyright 2001-2002 Sony Online Entertainment
// All Rights Reserved.
//
// ======================================================================
#ifndef INCLUDED_RecursiveMutex_H
#define INCLUDED_RecursiveMutex_H
// ======================================================================
class RecursiveMutex
{
public:
static void install();
static void remove();
public:
RecursiveMutex();
~RecursiveMutex();
void enter();
void leave();
private:
RecursiveMutex(const RecursiveMutex &);
RecursiveMutex &operator =(const RecursiveMutex &);
private:
CRITICAL_SECTION m_criticalSection;
};
// ======================================================================
#endif
@@ -0,0 +1,37 @@
// ======================================================================
//
// Semaphore.cpp
// Acy Stapp
//
// Copyright 6/19/2001 Sony Online Entertainment
//
// ======================================================================
#include "sharedSynchronization/FirstSharedSynchronization.h"
#include "sharedSynchronization/Semaphore.h"
Semaphore::Semaphore(int count, int initial)
{
handle = CreateSemaphore(0, initial, count, 0);
}
Semaphore::~Semaphore()
{
CloseHandle(handle);
}
void Semaphore::wait()
{
WaitForSingleObject(handle, INFINITE);
}
void Semaphore::wait(unsigned int maxDurationMs)
{
WaitForSingleObject(handle, maxDurationMs);
}
void Semaphore::signal(int count)
{
ReleaseSemaphore(handle, count, 0);
}
@@ -0,0 +1,28 @@
// ======================================================================
//
// Semaphore.h
// Acy Stapp
//
// Copyright 6/19/2001 Sony Online Entertainment
//
// ======================================================================
#ifndef INCLUDED_Semaphore_h
#define INCLUDED_Semaphore_h
class Semaphore
{
public:
Semaphore(int count=0x7FFFFFFF, int initial=0);
~Semaphore();
void wait();
void wait(unsigned int maxDurationMs);
void signal(int count=1);
private:
Semaphore(const Semaphore &o);
Semaphore &operator =(const Semaphore &o);
HANDLE handle;
};
#endif