$search
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 namespace log4cpp { 00019 namespace threading { 00025 std::string getThreadId(); 00026 00035 char* getThreadId(char* buffer); 00036 00041 typedef omni_mutex Mutex; 00042 00048 typedef omni_mutex_lock ScopedLock; 00049 00058 template<typename T> class ThreadLocalDataHolder { 00059 public: 00060 typedef T data_type; 00061 00062 inline ThreadLocalDataHolder() : 00063 _key(omni_thread::allocate_key()) {}; 00064 00065 inline ~ThreadLocalDataHolder() {}; 00066 00072 inline T* get() const { 00073 Holder* holder = dynamic_cast<Holder*>( 00074 ::omni_thread::self()->get_value(_key)); 00075 return (holder) ? holder->data : NULL; 00076 }; 00077 00084 inline T* operator->() const { return get(); }; 00085 00091 inline T& operator*() const { return *get(); }; 00092 00099 inline T* release() { 00100 T* result = NULL; 00101 Holder* holder = dynamic_cast<Holder*>( 00102 ::omni_thread::self()->get_value(_key)); 00103 00104 if (holder) { 00105 result = holder->data; 00106 holder->data = NULL; 00107 } 00108 00109 return result; 00110 }; 00111 00118 inline void reset(T* p = NULL) { 00119 Holder* holder = dynamic_cast<Holder*>( 00120 ::omni_thread::self()->get_value(_key)); 00121 if (holder) { 00122 if (holder->data) 00123 delete holder->data; 00124 00125 holder->data = p; 00126 } else { 00127 holder = new Holder(p); 00128 ::omni_thread::self()->set_value(_key, holder); 00129 } 00130 }; 00131 00132 private: 00133 class Holder : public omni_thread::value_t { 00134 public: 00135 Holder(data_type* data) : data(data) {}; 00136 virtual ~Holder() { if (data) delete (data); }; 00137 data_type* data; 00138 private: 00139 Holder(const Holder& other); 00140 Holder& operator=(const Holder& other); 00141 }; 00142 00143 omni_thread::key_t _key; 00144 }; 00145 } 00146 } 00147 #endif