RCFProto
 All Classes Functions Typedefs
win_mutex.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_mutex.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_MUTEX_HPP
30 #define RCF_DETAIL_WIN_MUTEX_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/scoped_lock.hpp>
39 
40 #include <RCF/thread/push_options.hpp>
41 
42 namespace RCF {
43 namespace detail {
44 
45 class RCF_EXPORT win_mutex
46  : private noncopyable
47 {
48 public:
49  typedef RCF::detail::scoped_lock<win_mutex> scoped_lock;
50 
51  // Constructor.
52  win_mutex();
53  win_mutex(bool allowRecursiveLocking);
54 
55  // Destructor.
56  ~win_mutex()
57  {
58  ::DeleteCriticalSection(&crit_section_);
59  }
60 
61  // Lock the mutex.
62  void lock()
63  {
64 
65 #ifndef NDEBUG
66  int threadId = GetCurrentThreadId();
67  if (!mAllowRecursiveLocking)
68  {
69  assert(threadId != mThreadId && "Recursive locking detected.");
70  }
71 #endif
72 
73  ::EnterCriticalSection(&crit_section_);
74 
75 #ifndef NDEBUG
76  mThreadId = threadId;
77 #endif
78 
79  }
80 
81  // Unlock the mutex.
82  void unlock()
83  {
84 #ifndef NDEBUG
85  mThreadId = 0;
86 #endif
87 
88  ::LeaveCriticalSection(&crit_section_);
89  }
90 
91 private:
92 
93  void commonCtor(bool allowRecursiveLocking);
94 
95  // Initialisation must be performed in a separate function to the constructor
96  // since the compiler does not support the use of structured exceptions and
97  // C++ exceptions in the same function.
98  int do_init();
99 
100  ::CRITICAL_SECTION crit_section_;
101 
102 #ifndef NDEBUG
103  // Debug tool to detect recursive locking.
104  int mThreadId;
105  bool mAllowRecursiveLocking;
106 #endif
107 
108 
109 };
110 
111 } // namespace detail
112 } // namespace RCF
113 
114 #include <RCF/thread/pop_options.hpp>
115 
116 #endif // defined(BOOST_WINDOWS)
117 
118 #endif // RCF_DETAIL_WIN_MUTEX_HPP