CppCMS
thread.h
1 //
2 // Copyright (C) 2009-2012 Artyom Beilis (Tonkikh)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //
8 #ifndef BOOSTER_UTIL_THREAD_H
9 #define BOOSTER_UTIL_THREAD_H
10 
11 #include <booster/hold_ptr.h>
12 #include <booster/noncopyable.h>
13 #include <booster/refcounted.h>
14 #include <booster/intrusive_ptr.h>
15 #include <booster/function.h>
16 #include <booster/config.h>
17 
18 #include <thread>
19 #include <mutex>
20 #include <condition_variable>
21 namespace booster {
22 
23 
24  using std::thread;
25  using std::mutex;
26  using std::recursive_mutex;
27  using std::condition_variable;
28  using std::unique_lock;
29 
30 
37  class BOOSTER_API recursive_shared_mutex : public noncopyable {
38  public:
46  void lock() { unique_lock(); }
52  void unique_lock();
59  void shared_lock();
63  void unlock();
64  private:
65  struct data;
67  };
74  class BOOSTER_API shared_mutex : public noncopyable {
75  public:
76  shared_mutex();
77  ~shared_mutex();
81  void lock() { unique_lock(); }
85  void unique_lock();
91  void shared_lock();
95  void unlock();
96  private:
97  struct data;
99  };
100 
102  namespace details {
103  struct tls_object;
104 
105  class key : public refcounted {
106  public:
107  key(void (*d)(void *)) :
108  dtor_(d)
109  {
110  }
111  virtual ~key()
112  {
113  }
114  void *get();
115  void set(void *);
116 
117  void destroy(void *p)
118  {
119  dtor_(p);
120  }
121  virtual tls_object *get_object() = 0;
122  private:
123  void (*dtor_)(void *);
124  };
125 
126  BOOSTER_API intrusive_ptr<key> make_key(void (*dtor)(void *));
127 
128  struct tls_object {
129  tls_object(intrusive_ptr<key> p) :
130  the_key(p),
131  obj(0)
132  {
133  }
134  ~tls_object()
135  {
136  the_key->destroy(obj);
137  obj = 0;
138  }
139  intrusive_ptr<key> the_key;
140  void *obj;
141  };
142 
143  inline void key::set(void *p)
144  {
145  get_object()->obj = p;
146  }
147  inline void *key::get()
148  {
149  return get_object()->obj;
150  }
151 
152  } // details
153 
155 
179  template<typename T>
181  public:
185  thread_specific_ptr() : key_(details::make_key(destructor))
186  {
187  }
188 
193  {
194  }
198  T *get() const
199  {
200  return static_cast<T*>(key_->get());
201  }
205  T* operator->() const
206  {
207  return get();
208  }
212  T& operator*() const
213  {
214  return *get();
215  }
221  void reset(T *new_val = 0)
222  {
223  T *p = get();
224  if(p)
225  destructor(p);
226  key_->set(static_cast<void *>(new_val));
227  }
233  T *release()
234  {
235  T *p = get();
236  key_->set(0);
237  return p;
238  }
239  private:
240  static void destructor(void *ptr)
241  {
242  delete static_cast<T*>(ptr);
243  }
245  };
246 
247 
253  template<typename Mutex>
254  class shared_lock : public noncopyable {
255  public:
257  shared_lock(Mutex &m) : m_(&m)
258  {
259  m_->shared_lock();
260  }
263  {
264  m_->unlock();
265  }
267  Mutex *mutex() const
268  {
269  return m_;
270  }
271  private:
272  Mutex *m_;
273  };
274 #ifdef BOOSTER_POSIX
275  class BOOSTER_API fork_shared_mutex : public noncopyable {
281  public:
285  fork_shared_mutex();
286  ~fork_shared_mutex();
287 
291  bool try_lock() { return try_unique_lock(); }
295  bool try_unique_lock();
299  bool try_shared_lock();
300 
304  void lock() { return unique_lock(); }
308  void unique_lock();
312  void shared_lock();
313 
317  void unlock();
318  private:
319  struct data;
320  hold_ptr<data> d;
321  };
322 #endif
323 
324 }//booster
325 
326 
327 #endif
T * operator->() const
Definition: thread.h:205
void lock()
Definition: thread.h:81
thread_specific_ptr()
Definition: thread.h:185
Thread specific pointer.
Definition: thread.h:180
Recursuve Shared mutex or a.k.a. Read-Write Lock that can be recursively locked by readers...
Definition: thread.h:37
~thread_specific_ptr()
Definition: thread.h:192
void lock()
Definition: thread.h:46
This class is used as base class for reference counted objects that use intrusive_ptr. Deriving from this class allows simple way to manage reference counting for single object.
Definition: refcounted.h:25
shared_lock(Mutex &m)
Acquire the lock.
Definition: thread.h:257
T & operator*() const
Definition: thread.h:212
~shared_lock()
Release the lock.
Definition: thread.h:262
Shared mutex or a.k.a. Read-Write Lock.
Definition: thread.h:74
a Shared lock guard.
Definition: thread.h:254
intrusive_ptr is the class taken as-is from boost.
Definition: intrusive_ptr.h:42
void reset(T *new_val=0)
Definition: thread.h:221
Mutex * mutex() const
Get the reference to the mutex object.
Definition: thread.h:267
Booster library namespace. The library that implements Boost Like API in ABI backward compatible way...
Definition: application.h:23
This class makes impossible to copy any class derived from this one.
Definition: noncopyable.h:15
T * release()
Definition: thread.h:233