blob: 47def9985ba3a9ad9de680fe46294e7f719ae35d [file] [log] [blame]
Jeff Thompson3d613fd2013-10-15 15:39:04 -07001#ifndef NDNBOOST_SMART_PTR_DETAIL_LWM_WIN32_CS_HPP_INCLUDED
2#define NDNBOOST_SMART_PTR_DETAIL_LWM_WIN32_CS_HPP_INCLUDED
Jeff Thompsonf7d49942013-08-01 16:47:40 -07003
4// MS compatible compilers support #pragma once
5
6#if defined(_MSC_VER) && (_MSC_VER >= 1020)
7# pragma once
8#endif
9
10//
Jeff Thompson9939dcd2013-10-15 15:12:24 -070011// ndnboost/detail/lwm_win32_cs.hpp
Jeff Thompsonf7d49942013-08-01 16:47:40 -070012//
13// Copyright (c) 2002, 2003 Peter Dimov
14//
15// Distributed under the Boost Software License, Version 1.0. (See
16// accompanying file LICENSE_1_0.txt or copy at
17// http://www.boost.org/LICENSE_1_0.txt)
18//
19
Jeff Thompson3d613fd2013-10-15 15:39:04 -070020#ifdef NDNBOOST_USE_WINDOWS_H
Jeff Thompsonf7d49942013-08-01 16:47:40 -070021# include <windows.h>
22#endif
23
24namespace ndnboost
25{
26
27namespace detail
28{
29
Jeff Thompson3d613fd2013-10-15 15:39:04 -070030#ifndef NDNBOOST_USE_WINDOWS_H
Jeff Thompsonf7d49942013-08-01 16:47:40 -070031
32struct critical_section
33{
34 struct critical_section_debug * DebugInfo;
35 long LockCount;
36 long RecursionCount;
37 void * OwningThread;
38 void * LockSemaphore;
39#if defined(_WIN64)
40 unsigned __int64 SpinCount;
41#else
42 unsigned long SpinCount;
43#endif
44};
45
46extern "C" __declspec(dllimport) void __stdcall InitializeCriticalSection(critical_section *);
47extern "C" __declspec(dllimport) void __stdcall EnterCriticalSection(critical_section *);
48extern "C" __declspec(dllimport) void __stdcall LeaveCriticalSection(critical_section *);
49extern "C" __declspec(dllimport) void __stdcall DeleteCriticalSection(critical_section *);
50
51#else
52
53typedef ::CRITICAL_SECTION critical_section;
54
Jeff Thompson3d613fd2013-10-15 15:39:04 -070055#endif // #ifndef NDNBOOST_USE_WINDOWS_H
Jeff Thompsonf7d49942013-08-01 16:47:40 -070056
57class lightweight_mutex
58{
59private:
60
61 critical_section cs_;
62
63 lightweight_mutex(lightweight_mutex const &);
64 lightweight_mutex & operator=(lightweight_mutex const &);
65
66public:
67
68 lightweight_mutex()
69 {
70 InitializeCriticalSection(&cs_);
71 }
72
73 ~lightweight_mutex()
74 {
75 DeleteCriticalSection(&cs_);
76 }
77
78 class scoped_lock;
79 friend class scoped_lock;
80
81 class scoped_lock
82 {
83 private:
84
85 lightweight_mutex & m_;
86
87 scoped_lock(scoped_lock const &);
88 scoped_lock & operator=(scoped_lock const &);
89
90 public:
91
92 explicit scoped_lock(lightweight_mutex & m): m_(m)
93 {
94 EnterCriticalSection(&m_.cs_);
95 }
96
97 ~scoped_lock()
98 {
99 LeaveCriticalSection(&m_.cs_);
100 }
101 };
102};
103
104} // namespace detail
105
106} // namespace ndnboost
107
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700108#endif // #ifndef NDNBOOST_SMART_PTR_DETAIL_LWM_WIN32_CS_HPP_INCLUDED