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,38 @@
// ======================================================================
//
// ConditionVariable.cpp
// Acy Stapp
//
// Copyright 6/19/2001 Sony Online Entertainment
//
// ======================================================================
#include "sharedFoundation/FirstSharedFoundation.h"
#include "sharedSynchronization/ConditionVariable.h"
ConditionVariable::ConditionVariable(Mutex &m)
: _mutex(m)
{
pthread_cond_init(&cond, 0);
}
ConditionVariable::~ConditionVariable()
{
pthread_cond_destroy(&cond);
}
void ConditionVariable::wait()
{
pthread_cond_wait(&cond, &_mutex.getInternalMutex());
}
void ConditionVariable::signal()
{
pthread_cond_signal(&cond);
}
void ConditionVariable::broadcast()
{
pthread_cond_broadcast(&cond);
}
@@ -0,0 +1,34 @@
// ======================================================================
//
// ConditionVariable.h
// Acy Stapp
//
// Copyright 6/19/2001 Sony Online Entertainment
//
// ======================================================================
#ifndef INCLUDED_ConditionVariable_h
#define INCLUDED_ConditionVariable_h
#include "sharedSynchronization/Mutex.h"
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);
Mutex &_mutex;
pthread_cond_t cond;
};
#endif
@@ -0,0 +1,45 @@
// ======================================================================
//
// Gate.cpp
// Acy Stapp
//
// Copyright 6/19/2001 Sony Online Entertainment
//
// ======================================================================
#include "sharedFoundation/FirstSharedFoundation.h"
#include "sharedSynchronization/Gate.h"
Gate::Gate(bool open)
: cond(lock)
{
opened = open;
}
Gate::~Gate()
{
}
void Gate::wait()
{
lock.enter();
while (!opened)
cond.wait();
lock.leave();
}
void Gate::close()
{
lock.enter();
opened = false;
lock.leave();
}
void Gate::open()
{
lock.enter();
opened = true;
cond.broadcast();
lock.leave();
}
@@ -0,0 +1,35 @@
// ======================================================================
//
// Gate.h
// Acy Stapp
//
// Copyright 6/19/2001 Sony Online Entertainment
//
// ======================================================================
#ifndef INCLUDED_Gate_h
#define INCLUDED_Gate_h
#include "sharedSynchronization/ConditionVariable.h"
class Gate
{
public:
Gate(bool open);
~Gate();
void wait();
void close();
void open();
private:
Gate(const Gate &o);
Gate &operator =(const Gate &o);
Mutex lock;
bool opened;
ConditionVariable cond;
};
#endif
@@ -0,0 +1,63 @@
// ======================================================================
//
// 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 value; }
private:
InterlockedInteger(const InterlockedInteger &o);
InterlockedInteger &operator =(const InterlockedInteger &o);
pthread_mutex_t lock;
volatile int value;
};
inline InterlockedInteger::InterlockedInteger(int i_value)
: value(i_value)
{
pthread_mutex_init(&lock, 0);
}
inline int InterlockedInteger::operator =(int i_value)
{
pthread_mutex_lock(&lock);
int oldvalue = value;
value = i_value;
pthread_mutex_unlock(&lock);
return oldvalue;
}
inline int InterlockedInteger::operator ++()
{
pthread_mutex_lock(&lock);
int newvalue = value+1;
value = newvalue;
pthread_mutex_unlock(&lock);
return newvalue;
}
inline int InterlockedInteger::operator --()
{
pthread_mutex_lock(&lock);
int newvalue = value-1;
value = newvalue;
pthread_mutex_unlock(&lock);
return newvalue;
}
#endif
@@ -0,0 +1,53 @@
// ======================================================================
//
// InterlockedVoidPointer.h
// Acy Stapp
//
// Copyright 6/19/2001 Sony Online Entertainment
//
// ======================================================================
#ifndef INCLUDED_InterlockedVoidPointer_h
#define INCLUDED_InterlockedVoidPointer_h
#include "sharedSynchronization/Mutex.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);
Mutex lock;
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)
{
lock.enter();
void * temp = value;
if (temp == compare)
value = exchange;
lock.leave();
return temp;
}
#endif
@@ -0,0 +1,32 @@
// ======================================================================
//
// Mutex.cpp
// Acy Stapp
//
// Copyright 6/19/2001 Sony Online Entertainment
//
// ======================================================================
#include "sharedFoundation/FirstSharedFoundation.h"
#include "sharedSynchronization/Mutex.h"
Mutex::Mutex()
{
pthread_mutex_init(&mutex, 0);
}
Mutex::~Mutex()
{
pthread_mutex_destroy(&mutex);
}
void Mutex::enter()
{
pthread_mutex_lock(&mutex);
}
void Mutex::leave()
{
pthread_mutex_unlock(&mutex);
}
@@ -0,0 +1,31 @@
// ======================================================================
//
// Mutex.h
// Acy Stapp
//
// Copyright 6/19/2001 Sony Online Entertainment
//
// ======================================================================
#ifndef INCLUDED_Mutex_h
#define INCLUDED_Mutex_h
class Mutex
{
public:
Mutex();
~Mutex();
void enter();
void leave();
pthread_mutex_t &getInternalMutex() { return mutex; }
private:
Mutex(const Mutex &o);
Mutex &operator =(const Mutex &o);
pthread_mutex_t mutex;
};
#endif
@@ -0,0 +1,37 @@
// ======================================================================
//
// RecursiveMutex.cpp
// Acy Stapp
//
// Copyright 6/19/2001 Sony Online Entertainment
//
// ======================================================================
#include "sharedFoundation/FirstSharedFoundation.h"
#include "sharedSynchronization/RecursiveMutex.h"
RecursiveMutex::RecursiveMutex()
{
pthread_mutexattr_t attributes;
pthread_mutexattr_init(&attributes);
pthread_mutexattr_settype(&attributes,PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&mutex,&attributes);
pthread_mutexattr_destroy(&attributes);
}
RecursiveMutex::~RecursiveMutex()
{
pthread_mutex_destroy(&mutex);
}
void RecursiveMutex::enter()
{
pthread_mutex_lock(&mutex);
}
void RecursiveMutex::leave()
{
pthread_mutex_unlock(&mutex);
}
@@ -0,0 +1,31 @@
// ======================================================================
//
// RecursiveMutex.h
// Acy Stapp
//
// Copyright 6/19/2001 Sony Online Entertainment
//
// ======================================================================
#ifndef INCLUDED_RecursiveMutex_h
#define INCLUDED_RecursiveMutex_h
#include "sharedSynchronization/InterlockedInteger.h"
class RecursiveMutex
{
public:
RecursiveMutex();
~RecursiveMutex();
void enter();
void leave();
private:
RecursiveMutex(const RecursiveMutex &o);
RecursiveMutex &operator =(const RecursiveMutex &o);
pthread_mutex_t mutex;
};
#endif
@@ -0,0 +1,40 @@
// ======================================================================
//
// Semaphore.cpp
// Acy Stapp
//
// Copyright 6/19/2001 Sony Online Entertainment
//
// ======================================================================
#include "sharedFoundation/FirstSharedFoundation.h"
#include "sharedSynchronization/Semaphore.h"
Semaphore::Semaphore(int count, int initial)
{
sem_init(&sem, false, initial);
}
Semaphore::~Semaphore()
{
sem_destroy(&sem);
}
void Semaphore::wait()
{
sem_wait(&sem);
}
void Semaphore::wait(unsigned int maxDurationMs)
{
timespec ts;
ts.tv_sec = maxDurationMs/1000000;
ts.tv_nsec = maxDurationMs%1000000;
sem_timedwait(&sem, &ts);
}
void Semaphore::signal(int count)
{
sem_post(&sem);
}
@@ -0,0 +1,30 @@
// ======================================================================
//
// 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);
sem_t sem;
};
#endif