Loading...
Searching...
No Matches
PThreads.hh
Go to the documentation of this file.
/*
* PThreads.hh
*
* Copyright 2002, Emiliano Martin emilianomc@terra.es All rights reserved.
*
* See the COPYING file for the terms of usage and distribution.
*/
#ifndef _LOG4SHIB_THREADING_PTHREADS_HH
#define _LOG4SHIB_THREADING_PTHREADS_HH
#include <log4shib/Portability.hh>
#include <stdio.h>
#include <pthread.h>
#include <string>
#include <assert.h>
namespace log4shib {
namespace threading {
std::string getThreadId();
class Mutex {
private:
pthread_mutex_t mutex;
public:
inline Mutex() {
::pthread_mutex_init(&mutex, NULL);
}
inline void lock() {
::pthread_mutex_lock(&mutex);
}
inline void unlock() {
::pthread_mutex_unlock(&mutex);
}
inline ~Mutex() {
::pthread_mutex_destroy(&mutex);
}
private:
Mutex(const Mutex& m);
Mutex& operator=(const Mutex &m);
};
class ScopedLock {
private:
Mutex& _mutex;
public:
inline ScopedLock(Mutex& mutex) :
_mutex(mutex) {
_mutex.lock();
}
inline ~ScopedLock() {
_mutex.unlock();
}
};
template<typename T> class ThreadLocalDataHolder {
private:
pthread_key_t _key;
public:
typedef T data_type;
inline ThreadLocalDataHolder() {
::pthread_key_create(&_key, freeHolder);
}
inline static void freeHolder(void *p) {
assert(p != NULL);
delete reinterpret_cast<T *>(p);
}
inline ~ThreadLocalDataHolder() {
T *data = get();
if (data != NULL) {
delete data;
}
::pthread_key_delete(_key);
}
inline T* get() const {
return reinterpret_cast<T *>(::pthread_getspecific(_key));
}
inline T* operator->() const { return get(); }
inline T& operator*() const { return *get(); }
inline T* release() {
T* result = get();
::pthread_setspecific(_key, NULL);
return result;
}
inline void reset(T* p = NULL) {
T *data = get();
if (data != NULL) {
delete data;
}
::pthread_setspecific(_key, p);
}
};
}
}
#endif
Portability.hh
log4shib::threading::Mutex
Definition PThreads.hh:29
log4shib::threading::Mutex::Mutex
Mutex()
Definition PThreads.hh:34
log4shib::threading::Mutex::~Mutex
~Mutex()
Definition PThreads.hh:46
log4shib::threading::Mutex::lock
void lock()
Definition PThreads.hh:38
log4shib::threading::Mutex::unlock
void unlock()
Definition PThreads.hh:42
log4shib::threading::ScopedLock::~ScopedLock
~ScopedLock()
Definition PThreads.hh:68
log4shib::threading::ScopedLock::ScopedLock
ScopedLock(Mutex &mutex)
Definition PThreads.hh:63
log4shib::threading::ThreadLocalDataHolder::operator->
T * operator->() const
Definition PThreads.hh:104
log4shib::threading::ThreadLocalDataHolder::operator*
T & operator*() const
Definition PThreads.hh:105
log4shib::threading::ThreadLocalDataHolder::~ThreadLocalDataHolder
~ThreadLocalDataHolder()
Definition PThreads.hh:92
log4shib::threading::ThreadLocalDataHolder::reset
void reset(T *p=NULL)
Definition PThreads.hh:114
log4shib::threading::ThreadLocalDataHolder::get
T * get() const
Definition BoostThreads.hh:34
log4shib::threading::ThreadLocalDataHolder::data_type
T data_type
Definition DummyThreads.hh:34
log4shib::threading::ThreadLocalDataHolder::freeHolder
static void freeHolder(void *p)
Definition PThreads.hh:87
log4shib::threading::ThreadLocalDataHolder::release
T * release()
Definition PThreads.hh:107
log4shib::threading::ThreadLocalDataHolder::ThreadLocalDataHolder
ThreadLocalDataHolder()
Definition PThreads.hh:83
log4shib::threading
Definition BoostThreads.hh:21
log4shib::threading::getThreadId
static std::string getThreadId()
Return an identifier for the current thread.
Definition BoostThreads.hh:22
log4shib
The top level namespace for all 'Log for C++' types and classes.
Definition AbortAppender.hh:16