OmniThreads.hh
Go to the documentation of this file.
1 /*
2  * OmniThreads.hh
3  *
4  * Copyright 2002, LifeLine Networks BV (www.lifeline.nl). All rights reserved.
5  * Copyright 2002, Bastiaan Bakker. All rights reserved.
6  *
7  * See the COPYING file for the terms of usage and distribution.
8  */
9 
10 #ifndef _LOG4CPP_THREADING_OMNITHREADS_HH
11 #define _LOG4CPP_THREADING_OMNITHREADS_HH
12 
13 #include <log4cpp/Portability.hh>
14 #include <omnithread.h>
15 #include <stdio.h>
16 #include <string>
17 
18 namespace log4cpp {
19  namespace threading {
25  std::string getThreadId();
26 
35  char* getThreadId(char* buffer);
36 
41  typedef omni_mutex Mutex;
42 
48  typedef omni_mutex_lock ScopedLock;
49 
58  template<typename T> class ThreadLocalDataHolder {
59  public:
60  typedef T data_type;
61 
63  _key(omni_thread::allocate_key()) {};
64 
66 
72  inline T* get() const {
73  Holder* holder = dynamic_cast<Holder*>(
74  ::omni_thread::self()->get_value(_key));
75  return (holder) ? holder->data : NULL;
76  };
77 
84  inline T* operator->() const { return get(); };
85 
91  inline T& operator*() const { return *get(); };
92 
99  inline T* release() {
100  T* result = NULL;
101  Holder* holder = dynamic_cast<Holder*>(
102  ::omni_thread::self()->get_value(_key));
103 
104  if (holder) {
105  result = holder->data;
106  holder->data = NULL;
107  }
108 
109  return result;
110  };
111 
118  inline void reset(T* p = NULL) {
119  Holder* holder = dynamic_cast<Holder*>(
120  ::omni_thread::self()->get_value(_key));
121  if (holder) {
122  if (holder->data)
123  delete holder->data;
124 
125  holder->data = p;
126  } else {
127  holder = new Holder(p);
128  ::omni_thread::self()->set_value(_key, holder);
129  }
130  };
131 
132  private:
133  class Holder : public omni_thread::value_t {
134  public:
135  Holder(data_type* data) : data(data) {};
136  virtual ~Holder() { if (data) delete (data); };
137  data_type* data;
138  private:
139  Holder(const Holder& other);
140  Holder& operator=(const Holder& other);
141  };
142 
143  omni_thread::key_t _key;
144  };
145  }
146 }
147 #endif
static std::string getThreadId()
Definition: BoostThreads.hh:22
boost::mutex Mutex
Definition: BoostThreads.hh:37
boost::mutex::scoped_lock ScopedLock
Definition: BoostThreads.hh:40


log4cpp
Author(s): Stephen Roderick, Bastiaan Bakker, Cedric Le Goater, Steve Ostlind, Marcel Harkema, Walter Stroebel, Glenn Scott and Tony Cheung
autogenerated on Sun Jun 23 2019 19:14:17