1 /* ///////////////////////////////////////////////////////////////////////
2 * File: critical_section.h
7 * Brief: The Critical Section
10 * Copyright (c) 2008-2020, Waruqi All rights reserved.
11 * //////////////////////////////////////////////////////////////////// */
12 #ifndef EXTL_PLATFORM_WINDOWS_SYNCH_CRITICAL_SECTION_H
13 #define EXTL_PLATFORM_WINDOWS_SYNCH_CRITICAL_SECTION_H
15 /*!\file critical_section.h
16 * \brief The Critical Section
19 # error critical_section.h need be supported by c++.
22 /* ///////////////////////////////////////////////////////////////////////
26 #include "../../utility/uncopyable.h"
28 /* ///////////////////////////////////////////////////////////////////////
29 * ::extl::platform::windows namespace
31 EXTL_WINDOWS_BEGIN_WHOLE_NAMESPACE
34 /*!\brief critical_section class
36 * \ingroup extl_group_synch
38 class critical_section
39 : private uncopyable
<critical_section
>
44 typedef critical_section class_type
;
45 typedef LPCRITICAL_SECTION handle_type
;
46 typedef CRITICAL_SECTION crsec_type
;
47 typedef e_bool_t bool_type
;
56 /// \name Constructors
60 : m_cs(initialize_critical_section())
63 virtual ~critical_section() EXTL_THROW_0()
65 ::DeleteCriticalSection(&m_cs
);
72 /// initialize critical section
73 static crsec_type
initialize_critical_section()
76 ::InitializeCriticalSection(&cs
);
79 /// enter critical section
82 ::EnterCriticalSection(&m_cs
);
85 /// try entering critical section
88 #if(_WIN_WINNT >= 0x0400)
89 return (BOOL2bool(::TryEnterCriticalSection(&m_cs
)));
95 /// leave critical section
98 ::LeaveCriticalSection(&m_cs
);
105 /// gets object handle
106 operator handle_type() const { return const_cast<handle_type
>(&m_cs
); }
107 /// gets object handle
108 handle_type
handle() const { return const_cast<handle_type
>(&m_cs
); }
112 /* ///////////////////////////////////////////////////////////////////////
113 * ::extl::platform::windows namespace
115 EXTL_WINDOWS_END_WHOLE_NAMESPACE
117 /* //////////////////////////////////////////////////////////////////// */
118 #endif /* EXTL_PLATFORM_WINDOWS_SYNCH_CRITICAL_SECTION_H */
119 /* //////////////////////////////////////////////////////////////////// */