source: NonGTP/Boost/boost/detail/lwm_win32_cs.hpp @ 857

Revision 857, 2.2 KB checked in by igarcia, 18 years ago (diff)
Line 
1#ifndef BOOST_DETAIL_LWM_WIN32_CS_HPP_INCLUDED
2#define BOOST_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//
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
20#ifdef BOOST_USE_WINDOWS_H
21#  include <windows.h>
22#endif
23
24namespace boost
25{
26
27namespace detail
28{
29
30#ifndef BOOST_USE_WINDOWS_H
31
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#endif // #ifndef BOOST_USE_WINDOWS_H
52
53class lightweight_mutex
54{
55private:
56
57    CRITICAL_SECTION cs_;
58
59    lightweight_mutex(lightweight_mutex const &);
60    lightweight_mutex & operator=(lightweight_mutex const &);
61
62public:
63
64    lightweight_mutex()
65    {
66        InitializeCriticalSection(&cs_);
67    }
68
69    ~lightweight_mutex()
70    {
71        DeleteCriticalSection(&cs_);
72    }
73
74    class scoped_lock;
75    friend class scoped_lock;
76
77    class scoped_lock
78    {
79    private:
80
81        lightweight_mutex & m_;
82
83        scoped_lock(scoped_lock const &);
84        scoped_lock & operator=(scoped_lock const &);
85
86    public:
87
88        explicit scoped_lock(lightweight_mutex & m): m_(m)
89        {
90            EnterCriticalSection(&m_.cs_);
91        }
92
93        ~scoped_lock()
94        {
95            LeaveCriticalSection(&m_.cs_);
96        }
97    };
98};
99
100} // namespace detail
101
102} // namespace boost
103
104#endif // #ifndef BOOST_DETAIL_LWM_WIN32_CS_HPP_INCLUDED
Note: See TracBrowser for help on using the repository browser.