00001 /* 00002 * OmniThreads.hh 00003 * 00004 * Copyright 2002, LifeLine Networks BV (www.lifeline.nl). All rights reserved. 00005 * Copyright 2002, Bastiaan Bakker. All rights reserved. 00006 * 00007 * See the COPYING file for the terms of usage and distribution. 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 LOG4CPP_NS_BEGIN 00019 namespace threading { 00025 std::string getThreadId(); 00026 00031 typedef omni_mutex Mutex; 00032 00038 typedef omni_mutex_lock ScopedLock; 00039 00048 template<typename T> class ThreadLocalDataHolder { 00049 public: 00050 typedef T data_type; 00051 00052 inline ThreadLocalDataHolder() : 00053 _key(omni_thread::allocate_key()) {}; 00054 00055 inline ~ThreadLocalDataHolder() {}; 00056 00062 inline T* get() const { 00063 Holder* holder = dynamic_cast<Holder*>( 00064 ::omni_thread::self()->get_value(_key)); 00065 return (holder) ? holder->data : NULL; 00066 }; 00067 00074 inline T* operator->() const { return get(); }; 00075 00081 inline T& operator*() const { return *get(); }; 00082 00089 inline T* release() { 00090 T* result = NULL; 00091 Holder* holder = dynamic_cast<Holder*>( 00092 ::omni_thread::self()->get_value(_key)); 00093 00094 if (holder) { 00095 result = holder->data; 00096 holder->data = NULL; 00097 } 00098 00099 return result; 00100 }; 00101 00108 inline void reset(T* p = NULL) { 00109 Holder* holder = dynamic_cast<Holder*>( 00110 ::omni_thread::self()->get_value(_key)); 00111 if (holder) { 00112 if (holder->data) 00113 delete holder->data; 00114 00115 holder->data = p; 00116 } 00117 else { 00118 holder = new Holder(p); 00119 ::omni_thread::self()->set_value(_key, holder); 00120 } 00121 }; 00122 00123 private: 00124 class Holder : public omni_thread::value_t { 00125 public: 00126 Holder(data_type* data) : data(data) {}; 00127 virtual ~Holder() { if (data) delete (data); }; 00128 data_type* data; 00129 private: 00130 Holder(const Holder& other); 00131 Holder& operator=(const Holder& other); 00132 }; 00133 00134 omni_thread::key_t _key; 00135 }; 00136 } 00137 LOG4CPP_NS_END 00138 #endif