00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifndef _LOG4CPP_THREADING_OMNITHREADS_HH
00011 #define _LOG4CPP_THREADING_OMNITHREADS_HH
00012
00013 #include <log4cpp/Portability.hh>
00014 #include <omnithread.h>
00015 #include <stdio.h>
00016 #include <string>
00017
00018 namespace log4cpp {
00019 namespace threading {
00025 static std::string getThreadId() {
00026 char buffer[16];
00027 sprintf(buffer, "%d", ::omni_thread::self()->id());
00028 return std::string(buffer);
00029 };
00030
00035 typedef omni_mutex Mutex;
00036
00042 typedef omni_mutex_lock ScopedLock;
00043
00052 template<typename T> class ThreadLocalDataHolder {
00053 public:
00054 typedef T data_type;
00055
00056 inline ThreadLocalDataHolder() :
00057 _key(omni_thread::allocate_key()) {};
00058
00059 inline ~ThreadLocalDataHolder() {};
00060
00066 inline T* get() const {
00067 Holder* holder = dynamic_cast<Holder*>(
00068 ::omni_thread::self()->get_value(_key));
00069 return (holder) ? holder->data : NULL;
00070 };
00071
00078 inline T* operator->() const { return get(); };
00079
00085 inline T& operator*() const { return *get(); };
00086
00093 inline T* release() {
00094 T* result = NULL;
00095 Holder* holder = dynamic_cast<Holder*>(
00096 ::omni_thread::self()->get_value(_key));
00097
00098 if (holder) {
00099 result = holder->data;
00100 holder->data = NULL;
00101 }
00102
00103 return result;
00104 };
00105
00112 inline void reset(T* p = NULL) {
00113 Holder* holder = dynamic_cast<Holder*>(
00114 ::omni_thread::self()->get_value(_key));
00115 if (holder) {
00116 if (holder->data)
00117 delete holder->data;
00118
00119 holder->data = p;
00120 } else {
00121 holder = new Holder(p);
00122 ::omni_thread::self()->set_value(_key, holder);
00123 }
00124 };
00125
00126 private:
00127 class Holder : public omni_thread::value_t {
00128 public:
00129 Holder(data_type* data) : data(data) {};
00130 virtual ~Holder() { if (data) delete (data); };
00131 data_type* data;
00132 private:
00133 Holder(const Holder& other);
00134 Holder& operator=(const Holder& other);
00135 };
00136
00137 omni_thread::key_t _key;
00138 };
00139 }
00140 }
00141 #endif