RCFProto
 All Classes Functions Typedefs
win_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/win_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_WIN_TSS_PTR_HPP
30 #define RCF_DETAIL_WIN_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_WINDOWS)
37 
38 #include <RCF/thread/push_options.hpp>
39 
40 namespace RCF {
41 namespace detail {
42 
43 // Helper function to create thread-specific storage.
44 RCF_EXPORT DWORD win_tss_ptr_create();
45 
46 template <typename T>
47 class win_tss_ptr
48  : private noncopyable
49 {
50 public:
51  // Constructor.
52  win_tss_ptr()
53  : tss_key_(win_tss_ptr_create())
54  {
55  }
56 
57  // Destructor.
58  ~win_tss_ptr()
59  {
60  ::TlsFree(tss_key_);
61  }
62 
63  // Get the value.
64  operator T*() const
65  {
66  return static_cast<T*>(::TlsGetValue(tss_key_));
67  }
68 
69  // Set the value.
70  void operator=(T* value)
71  {
72  ::TlsSetValue(tss_key_, value);
73  }
74 
75 private:
76  // Thread-specific storage to allow unlocked access to determine whether a
77  // thread is a member of the pool.
78  DWORD tss_key_;
79 };
80 
81 } // namespace detail
82 } // namespace RCF
83 
84 #include <RCF/thread/pop_options.hpp>
85 
86 #endif // defined(BOOST_WINDOWS)
87 
88 #endif // RCF_DETAIL_WIN_TSS_PTR_HPP