Loading...
Searching...
No Matches
OmniThreads.hh
Go to the documentation of this file.
/*
* OmniThreads.hh
*
* Copyright 2002, LifeLine Networks BV (www.lifeline.nl). All rights reserved.
* Copyright 2002, Bastiaan Bakker. All rights reserved.
*
* See the COPYING file for the terms of usage and distribution.
*/
#ifndef _LOG4SHIB_THREADING_OMNITHREADS_HH
#define _LOG4SHIB_THREADING_OMNITHREADS_HH
#include <log4shib/Portability.hh>
#include <omnithread.h>
#include <stdio.h>
#include <string>
namespace log4shib {
namespace threading {
std::string getThreadId();
typedef omni_mutex Mutex;
typedef omni_mutex_lock ScopedLock;
template<typename T> class ThreadLocalDataHolder {
public:
typedef T data_type;
inline ThreadLocalDataHolder() :
_key(omni_thread::allocate_key()) {};
inline ~ThreadLocalDataHolder() {};
inline T* get() const {
Holder* holder = dynamic_cast<Holder*>(
::omni_thread::self()->get_value(_key));
return (holder) ? holder->data : NULL;
};
inline T* operator->() const { return get(); };
inline T& operator*() const { return *get(); };
inline T* release() {
T* result = NULL;
Holder* holder = dynamic_cast<Holder*>(
::omni_thread::self()->get_value(_key));
if (holder) {
result = holder->data;
holder->data = NULL;
}
return result;
};
inline void reset(T* p = NULL) {
Holder* holder = dynamic_cast<Holder*>(
::omni_thread::self()->get_value(_key));
if (holder) {
if (holder->data)
delete holder->data;
holder->data = p;
} else {
holder = new Holder(p);
::omni_thread::self()->set_value(_key, holder);
}
};
private:
class Holder : public omni_thread::value_t {
public:
Holder(data_type* data) : data(data) {};
virtual ~Holder() { if (data) delete (data); };
data_type* data;
private:
Holder(const Holder& other);
Holder& operator=(const Holder& other);
};
omni_thread::key_t _key;
};
}
}
#endif
Portability.hh
log4shib::threading::Mutex
Definition PThreads.hh:29
log4shib::threading::ScopedLock
definition of ScopedLock;
Definition PThreads.hh:58
log4shib::threading::ThreadLocalDataHolder
This class holds Thread local data of type T, i.e.
Definition BoostThreads.hh:32
log4shib::threading::ThreadLocalDataHolder::operator->
T * operator->() const
Obtains the Object held for the current thread.
Definition OmniThreads.hh:74
log4shib::threading::ThreadLocalDataHolder::operator*
T & operator*() const
Obtains the Object held for the current thread.
Definition OmniThreads.hh:81
log4shib::threading::ThreadLocalDataHolder::~ThreadLocalDataHolder
~ThreadLocalDataHolder()
Definition OmniThreads.hh:55
log4shib::threading::ThreadLocalDataHolder::reset
void reset(T *p=NULL)
Sets a new Object to be held for the current thread.
Definition OmniThreads.hh:108
log4shib::threading::ThreadLocalDataHolder::get
T * get() const
Obtains the Object held for the current thread.
Definition OmniThreads.hh:62
log4shib::threading::ThreadLocalDataHolder::data_type
T data_type
Definition DummyThreads.hh:34
log4shib::threading::ThreadLocalDataHolder::release
T * release()
Releases the Object held for the current thread.
Definition OmniThreads.hh:89
log4shib::threading::ThreadLocalDataHolder::ThreadLocalDataHolder
ThreadLocalDataHolder()
Definition OmniThreads.hh:52
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