log4cpp: OmniThreads.hh Source File

- Main Page
- Namespaces
- Classes
- Files
- Related Pages

- File List
- File Members

# OmniThreads.hh

Go to the documentation of this file.

    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
    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                 } else {
    00117                     holder = new Holder(p);
    00118                     ::omni_thread::self()->set_value(_key, holder);
    00119                 }
    00120             };
    00121
    00122             private:
    00123             class Holder : public omni_thread::value_t {
    00124                 public:
    00125                 Holder(data_type* data) : data(data) {};
    00126                 virtual ~Holder() { if (data) delete (data); };
    00127                 data_type* data;
    00128                 private:
    00129                 Holder(const Holder& other);
    00130                 Holder& operator=(const Holder& other);
    00131             };
    00132
    00133             omni_thread::key_t _key;
    00134         };
    00135     }
    00136 }
    00137 #endif