Deprecated custom containers

This commit is contained in:
matsukaze 2014-02-11 20:12:44 -05:00
parent 5ce1b650e8
commit 095f5292cd
4 changed files with 0 additions and 580 deletions

View file

@ -1,84 +0,0 @@
/**
* @file AutoLock.h
* A simple locking class featuring templated mutex policy and mutex barrier
* activation/deactivation constructor/destructor.
*
* @author Norman Crafts
* @copyright 2014, All rights reserved.
*/
#ifndef __AUTOLOCK_H__
#define __AUTOLOCK_H__
#include "Mutex.h"
/**
* @class AutoLock
*/
template
<
class MutexPolicy = Mutex
>
class AutoLock
{
public:
/** Dependency injection constructor
* AutoLock constructor with client mutex injection
*/
AutoLock(
MutexPolicy & client ///< Client mutex
);
/** Dependency injection constructor
* AutoLock constructor with client mutex injection
*/
AutoLock(
MutexPolicy *client ///< Client mutex
);
~AutoLock();
private:
/** Default constructor prohibited to API user
*/
AutoLock();
/** Copy constructor prohibited to API user
*/
AutoLock(
AutoLock const & copy
);
private:
MutexPolicy &_mutex; ///< Client mutex
};
template<class MutexPolicy>
inline
AutoLock<MutexPolicy>::AutoLock(
MutexPolicy &mutex
) :
_mutex(mutex)
{
_mutex.lock();
}
template<class MutexPolicy>
inline
AutoLock<MutexPolicy>::AutoLock(
MutexPolicy *mutex
) :
_mutex(*mutex)
{
_mutex.lock();
}
template<class MutexPolicy>
inline
AutoLock<MutexPolicy>::~AutoLock()
{
_mutex.unlock();
}
#endif // __AUTOLOCK_H__

View file

@ -1,146 +0,0 @@
/**
* @file Mutex.h
* An operating system independent simple wrapper to mutex services.
*
* @author: Norman Crafts
* @copyright 2014, All rights reserved.
*/
#ifndef __MUTEX_H__
#define __MUTEX_H__
#ifdef LINUX
#include <errno.h>
#include <pthread.h>
#endif
/**
* @class Mutex
*
* This class is an OS independent delegate pattern providing access
* to simple OS dependent mutex services. Assume the mutex service is
* non-reentrant.
*/
class Mutex
{
public:
/** Default constructor
*/
Mutex();
~Mutex();
/** Lock the mutex barrier
* First competing thread will capture the mutex and block all
* other threads.
*/
void lock();
/** Unlock the mutex barrier
* Mutex owner releases the mutex and allow competing threads to try to capture.
*/
void unlock();
private:
/** Copy constructor prohibited to API user
*/
Mutex(
Mutex const & copy
);
private:
#ifdef _DEBUG
int _lockCt;
#endif //_DEBUG
#ifdef WIN32
CRITICAL_SECTION _mutex;
#endif // WIN32
#ifdef LINUX
pthread_mutex_t _mutex;
#endif // LINUX
};
#ifdef WIN32
inline
Mutex::Mutex()
#ifdef _DEBUG
:
_lockCt(0)
#endif
{
InitializeCriticalSection(&_mutex);
}
inline
Mutex::~Mutex()
{
DeleteCriticalSection(&_mutex);
}
inline
void Mutex::lock()
{
EnterCriticalSection(&_mutex);
#ifdef _DEBUG
_lockCt++;
#endif // _DEBUG
}
inline
void Mutex::unlock()
{
#ifdef _DEBUG
_lockCt--;
#endif // _DEBUG
LeaveCriticalSection(&_mutex);
}
#endif // WIN32
#ifdef LINUX
inline
Mutex::Mutex()
#ifdef _DEBUG
:
_lockCt(0)
#endif
{
int err;
pthread_mutexattr_t attrib;
err = pthread_mutexattr_init(&attrib);
err = pthread_mutex_init(&_mutex, &attrib);
err = pthread_mutexattr_destroy(&attrib);
}
inline
Mutex::~Mutex()
{
pthread_mutex_destroy(&_mutex);
}
inline
void Mutex::lock()
{
pthread_mutex_lock(&_mutex);
#ifdef _DEBUG
_lockCt++;
#endif // _DEBUG
}
inline
void Mutex::unlock()
{
#ifdef _DEBUG
_lockCt--;
#endif // _DEBUG
pthread_mutex_unlock(&_mutex);
}
#endif // LINUX
#endif // __MUTEX_H__

View file

@ -1,272 +0,0 @@
/**
* @file Queue.h
* An efficient FIFO queue featuring lock-free interaction between
* producer and consumer.
*
* @author: Norman Crafts
* @copyright 2014, All rights reserved.
*/
#ifndef __QUEUE_H__
#define __QUEUE_H__
#include "AutoLock.h"
#include "Mutex.h"
#include "Threading.h"
/**
* @class Queue
*
* A template based, efficient first-in/first-out queue featuring lock free
* access between producer and consumer.
*/
template
<
class Client, // Managed client class
template <class> class ProducerThreadingPolicy = MultiThreaded,
template <class> class ConsumerThreadingPolicy = MultiThreaded,
class ProducerMutexPolicy = Mutex,
class ConsumerMutexPolicy = Mutex
>
class Queue
{
public:
/** Default constructor
*/
Queue();
~Queue();
/** Add an object of managed client class to the tail of the queue
*/
void add(
Client item
);
/** Remove an object of managed client class from the head of the queue
*/
Client remove();
/** Peek at an object of managed client class at the head of the queue
* but do not remove it.
*/
Client peek();
/** Is the queue empty?
*/
bool isEmpty();
private:
struct __node
{
/** Default node constructor
*/
__node() :
_item(),
_next(0)
{}
/** Item injection constructor
*/
__node(
Client item
) :
_item(item),
_next(0)
{}
Client _item;
struct __node *_next;
};
static const int s_cacheLineSize = 64; ///< Cache line size (in bytes) of standard Intel architecture
char pad0[s_cacheLineSize]; ///< Padding to eliminate cache line contention between threads
ProducerThreadingPolicy<ProducerMutexPolicy> _producerGuard;
char pad1[s_cacheLineSize - sizeof(ProducerThreadingPolicy<ProducerMutexPolicy>)];
ConsumerThreadingPolicy<ConsumerMutexPolicy> _consumerGuard;
char pad2[s_cacheLineSize - sizeof(ConsumerThreadingPolicy<ConsumerMutexPolicy>)];
struct __node *_first; ///< Queue management
char pad3[s_cacheLineSize - sizeof(struct __node *)];
struct __node *_last; ///< Queue management
char pad4[s_cacheLineSize - sizeof(struct __node *)];
};
template
<
class Client,
template <class> class ProducerThreadingPolicy,
template <class> class ConsumerThreadingPolicy,
class ProducerMutexPolicy,
class ConsumerMutexPolicy
>
Queue
<
Client,
ProducerThreadingPolicy,
ConsumerThreadingPolicy,
ProducerMutexPolicy,
ConsumerMutexPolicy
>::Queue()
{
_first = new __node();
_last = _first;
memset(pad0, 0, sizeof(pad0));
memset(pad1, 0, sizeof(pad1));
memset(pad2, 0, sizeof(pad2));
memset(pad3, 0, sizeof(pad3));
memset(pad4, 0, sizeof(pad4));
}
template
<
class Client,
template <class> class ProducerThreadingPolicy,
template <class> class ConsumerThreadingPolicy,
class ProducerMutexPolicy,
class ConsumerMutexPolicy
>
Queue
<
Client,
ProducerThreadingPolicy,
ConsumerThreadingPolicy,
ProducerMutexPolicy,
ConsumerMutexPolicy
>::~Queue()
{
AutoLock<ConsumerThreadingPolicy<ConsumerMutexPolicy> > lock(_consumerGuard);
while (_first)
{
struct __node *t = _first;
_first = t->_next;
delete t;
}
}
template
<
class Client,
template <class> class ProducerThreadingPolicy,
template <class> class ConsumerThreadingPolicy,
class ProducerMutexPolicy,
class ConsumerMutexPolicy
>
void Queue
<
Client,
ProducerThreadingPolicy,
ConsumerThreadingPolicy,
ProducerMutexPolicy,
ConsumerMutexPolicy
>::add(
Client item
)
{
struct __node *n = new __node(item);
AutoLock<ProducerThreadingPolicy<ProducerMutexPolicy> > lock(_producerGuard);
_last->_next = n;
_last = n;
}
template
<
class Client,
template <class> class ProducerThreadingPolicy,
template <class> class ConsumerThreadingPolicy,
class ProducerMutexPolicy,
class ConsumerMutexPolicy
>
Client Queue
<
Client,
ProducerThreadingPolicy,
ConsumerThreadingPolicy,
ProducerMutexPolicy,
ConsumerMutexPolicy
>::remove()
{
AutoLock<ConsumerThreadingPolicy<ConsumerMutexPolicy> > lock(_consumerGuard);
struct __node *next = _first->_next;
if (next)
{
struct __node *first = _first;
Client item = next->_item;
next->_item = 0;
_first = next;
delete first;
return item;
}
return 0;
}
template
<
class Client,
template <class> class ProducerThreadingPolicy,
template <class> class ConsumerThreadingPolicy,
class ProducerMutexPolicy,
class ConsumerMutexPolicy
>
Client Queue
<
Client,
ProducerThreadingPolicy,
ConsumerThreadingPolicy,
ProducerMutexPolicy,
ConsumerMutexPolicy
>::peek()
{
AutoLock<ConsumerThreadingPolicy<ConsumerMutexPolicy> > lock(_consumerGuard);
struct __node *next = _first->_next;
if (next)
{
Client item = next->_item;
return item;
}
return 0;
}
template
<
class Client,
template <class> class ProducerThreadingPolicy,
template <class> class ConsumerThreadingPolicy,
class ProducerMutexPolicy,
class ConsumerMutexPolicy
>
bool Queue
<
Client,
ProducerThreadingPolicy,
ConsumerThreadingPolicy,
ProducerMutexPolicy,
ConsumerMutexPolicy
>::isEmpty()
{
bool empty = true;
AutoLock<ConsumerThreadingPolicy<ConsumerMutexPolicy> > lock(_consumerGuard);
struct __node *next = _first->_next;
if (next)
empty = false;
return empty;
}
#endif // __QUEUE_H__

View file

@ -1,78 +0,0 @@
/**
* @file Threading.h
* A delegate pattern to encapsulate threading policy semantics.
*
* @author: Norman Crafts
* @copyright 2014, All rights reserved.
*/
#ifndef __THREADING_H__
#define __THREADING_H__
/**
* @class SingleThreaded
*/
template
<
class MutexPolicy = Mutex ///< Mutex policy
>
class SingleThreaded
{
public:
void lock();
void unlock();
};
/**
* @class MultiThreaded
*/
template
<
class MutexPolicy = Mutex ///< Mutex policy
>
class MultiThreaded
{
public:
void lock();
void unlock();
private:
MutexPolicy _mutex;
};
template <class MutexPolicy>
inline
void SingleThreaded<MutexPolicy>::lock()
{}
template <class MutexPolicy>
inline
void SingleThreaded<MutexPolicy>::unlock()
{}
template <class MutexPolicy>
inline
void MultiThreaded<MutexPolicy>::lock()
{
_mutex.lock();
}
template <class MutexPolicy>
inline
void MultiThreaded<MutexPolicy>::unlock()
{
_mutex.unlock();
}
#endif // __THREADING_H__