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