1 //
2 //  boost/signals2/detail/lwm_win32_cs.hpp
3 //
4 //  Copyright (c) 2002, 2003 Peter Dimov
5 //  Copyright (c) 2008 Frank Mori Hess
6 //  Copyright (c) Microsoft Corporation 2014
7 //
8 // Distributed under the Boost Software License, Version 1.0. (See
9 // accompanying file LICENSE_1_0.txt or copy at
10 // http://www.boost.org/LICENSE_1_0.txt)
11 //
12 
13 #ifndef BOOST_SIGNALS2_LWM_WIN32_CS_HPP
14 #define BOOST_SIGNALS2_LWM_WIN32_CS_HPP
15 
16 // MS compatible compilers support #pragma once
17 
18 #if defined(_MSC_VER)
19 # pragma once
20 #endif
21 
22 #include <boost/assert.hpp>
23 
24 #ifdef BOOST_USE_WINDOWS_H
25 #  include <windows.h>
26 #endif
27 
28 #include <boost/predef/platform.h>
29 
30 namespace boost
31 {
32 
33 namespace signals2
34 {
35 
36 #ifndef BOOST_USE_WINDOWS_H
37 
38 struct critical_section
39 {
40     struct critical_section_debug * DebugInfo;
41     long LockCount;
42     long RecursionCount;
43     void * OwningThread;
44     void * LockSemaphore;
45 #if defined(_WIN64)
46     unsigned __int64 SpinCount;
47 #else
48     unsigned long SpinCount;
49 #endif
50 };
51 
52 #if BOOST_PLAT_WINDOWS_RUNTIME
53 extern "C" __declspec(dllimport) void __stdcall InitializeCriticalSectionEx(critical_section *, unsigned long, unsigned long);
54 #else
55 extern "C" __declspec(dllimport) void __stdcall InitializeCriticalSection(critical_section *);
56 #endif
57 extern "C" __declspec(dllimport) void __stdcall EnterCriticalSection(critical_section *);
58 extern "C" __declspec(dllimport) int __stdcall TryEnterCriticalSection(critical_section *);
59 extern "C" __declspec(dllimport) void __stdcall LeaveCriticalSection(critical_section *);
60 extern "C" __declspec(dllimport) void __stdcall DeleteCriticalSection(critical_section *);
61 
62 #else
63 
64 typedef ::CRITICAL_SECTION critical_section;
65 
66 #endif // #ifndef BOOST_USE_WINDOWS_H
67 
68 class mutex
69 {
70 private:
71 
72     critical_section cs_;
73 
74     mutex(mutex const &);
75     mutex & operator=(mutex const &);
76 
77 public:
78 
mutex()79     mutex()
80     {
81 #if BOOST_PLAT_WINDOWS_RUNTIME
82         InitializeCriticalSectionEx(&cs_, 4000, 0);
83 #else
84         InitializeCriticalSection(&cs_);
85 #endif
86     }
87 
~mutex()88     ~mutex()
89     {
90         DeleteCriticalSection(&cs_);
91     }
92 
lock()93     void lock()
94     {
95         EnterCriticalSection(&cs_);
96     }
97 // TryEnterCriticalSection only exists on Windows NT 4.0 and later
98 #if (defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0400))
try_lock()99     bool try_lock()
100     {
101         return TryEnterCriticalSection(&cs_) != 0;
102     }
103 #else
try_lock()104     bool try_lock()
105     {
106         BOOST_ASSERT(false);
107         return false;
108     }
109 #endif
unlock()110     void unlock()
111     {
112         LeaveCriticalSection(&cs_);
113     }
114 };
115 
116 } // namespace signals2
117 
118 } // namespace boost
119 
120 #endif // #ifndef BOOST_SIGNALS2_LWM_WIN32_CS_HPP
121