RCFProto
 All Classes Functions Typedefs
posix_tss_ptr.hpp
1 
2 //******************************************************************************
3 // RCF - Remote Call Framework
4 //
5 // Copyright (c) 2005 - 2013, Delta V Software. All rights reserved.
6 // http://www.deltavsoft.com
7 //
8 // RCF is distributed under dual licenses - closed source or GPL.
9 // Consult your particular license for conditions of use.
10 //
11 // If you have not purchased a commercial license, you are using RCF
12 // under GPL terms.
13 //
14 // Version: 2.0
15 // Contact: support <at> deltavsoft.com
16 //
17 //******************************************************************************
18 
19 //
20 // detail/posix_tss_ptr.hpp
21 // ~~~~~~~~~~~~~~~~~~~~~~~~
22 //
23 // Copyright (c) 2003-2011 Christopher M. Kohlhoff (chris at kohlhoff dot com)
24 //
25 // Distributed under the Boost Software License, Version 1.0. (See accompanying
26 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
27 //
28 
29 #ifndef RCF_DETAIL_POSIX_TSS_PTR_HPP
30 #define RCF_DETAIL_POSIX_TSS_PTR_HPP
31 
32 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
33 # pragma once
34 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
35 
36 #if defined(BOOST_HAS_PTHREADS)
37 
38 #include <pthread.h>
39 
40 #include <RCF/thread/push_options.hpp>
41 
42 namespace RCF {
43 namespace detail {
44 
45 // Helper function to create thread-specific storage.
46 RCF_EXPORT void posix_tss_ptr_create(pthread_key_t& key);
47 
48 template <typename T>
49 class posix_tss_ptr
50  : private noncopyable
51 {
52 public:
53  // Constructor.
54  posix_tss_ptr()
55  {
56  posix_tss_ptr_create(tss_key_);
57  }
58 
59  // Destructor.
60  ~posix_tss_ptr()
61  {
62  ::pthread_key_delete(tss_key_);
63  }
64 
65  // Get the value.
66  operator T*() const
67  {
68  return static_cast<T*>(::pthread_getspecific(tss_key_));
69  }
70 
71  // Set the value.
72  void operator=(T* value)
73  {
74  ::pthread_setspecific(tss_key_, value);
75  }
76 
77 private:
78  // Thread-specific storage to allow unlocked access to determine whether a
79  // thread is a member of the pool.
80  pthread_key_t tss_key_;
81 
82 };
83 
84 } // namespace detail
85 } // namespace RCF
86 
87 #include <RCF/thread/pop_options.hpp>
88 
89 #endif // defined(BOOST_HAS_PTHREADS)
90 
91 #endif // RCF_DETAIL_POSIX_TSS_PTR_HPP