1 | // Copyright (C) 2001-2003 |
---|
2 | // William E. Kempf |
---|
3 | // |
---|
4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
---|
5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
---|
6 | // boostinspect:nounnamed |
---|
7 | |
---|
8 | namespace { |
---|
9 | |
---|
10 | #if defined(BOOST_HAS_WINTHREADS) |
---|
11 | //:PREVENT THIS FROM BEING DUPLICATED |
---|
12 | typedef BOOL (WINAPI* TryEnterCriticalSection_type)(LPCRITICAL_SECTION lpCriticalSection); |
---|
13 | TryEnterCriticalSection_type g_TryEnterCriticalSection = 0; |
---|
14 | boost::once_flag once_init_TryEnterCriticalSection = BOOST_ONCE_INIT; |
---|
15 | |
---|
16 | void init_TryEnterCriticalSection() |
---|
17 | { |
---|
18 | //TryEnterCriticalSection is only available on WinNT 4.0 or later; |
---|
19 | //it is not available on Win9x. |
---|
20 | |
---|
21 | OSVERSIONINFO version_info = {sizeof(OSVERSIONINFO)}; |
---|
22 | ::GetVersionEx(&version_info); |
---|
23 | if (version_info.dwPlatformId == VER_PLATFORM_WIN32_NT && |
---|
24 | version_info.dwMajorVersion >= 4) |
---|
25 | { |
---|
26 | if (HMODULE kernel_module = GetModuleHandle(TEXT("KERNEL32.DLL"))) |
---|
27 | { |
---|
28 | g_TryEnterCriticalSection = reinterpret_cast<TryEnterCriticalSection_type>( |
---|
29 | #if defined(BOOST_NO_ANSI_APIS) |
---|
30 | GetProcAddressW(kernel_module, L"TryEnterCriticalSection") |
---|
31 | #else |
---|
32 | GetProcAddress(kernel_module, "TryEnterCriticalSection") |
---|
33 | #endif |
---|
34 | ); |
---|
35 | } |
---|
36 | } |
---|
37 | } |
---|
38 | |
---|
39 | inline bool has_TryEnterCriticalSection() |
---|
40 | { |
---|
41 | boost::call_once(init_TryEnterCriticalSection, once_init_TryEnterCriticalSection); |
---|
42 | return g_TryEnterCriticalSection != 0; |
---|
43 | } |
---|
44 | |
---|
45 | inline HANDLE mutex_cast(void* p) |
---|
46 | { |
---|
47 | return reinterpret_cast<HANDLE>(p); |
---|
48 | } |
---|
49 | |
---|
50 | inline LPCRITICAL_SECTION critical_section_cast(void* p) |
---|
51 | { |
---|
52 | return reinterpret_cast<LPCRITICAL_SECTION>(p); |
---|
53 | } |
---|
54 | |
---|
55 | inline void* new_critical_section() |
---|
56 | { |
---|
57 | try |
---|
58 | { |
---|
59 | LPCRITICAL_SECTION critical_section = new CRITICAL_SECTION; |
---|
60 | if (critical_section == 0) throw boost::thread_resource_error(); |
---|
61 | InitializeCriticalSection(critical_section); |
---|
62 | return critical_section; |
---|
63 | } |
---|
64 | catch(...) |
---|
65 | { |
---|
66 | throw boost::thread_resource_error(); |
---|
67 | } |
---|
68 | } |
---|
69 | |
---|
70 | inline void* new_mutex(const char* name) |
---|
71 | { |
---|
72 | #if defined(BOOST_NO_ANSI_APIS) |
---|
73 | USES_CONVERSION; |
---|
74 | HANDLE mutex = CreateMutexW(0, 0, A2CW(name)); |
---|
75 | #else |
---|
76 | HANDLE mutex = CreateMutexA(0, 0, name); |
---|
77 | #endif |
---|
78 | if (mutex == 0 || mutex == INVALID_HANDLE_VALUE) //:xxx (check for both values?) |
---|
79 | throw boost::thread_resource_error(); |
---|
80 | return reinterpret_cast<void*>(mutex); |
---|
81 | } |
---|
82 | |
---|
83 | inline void delete_critical_section(void* mutex) |
---|
84 | { |
---|
85 | DeleteCriticalSection(critical_section_cast(mutex)); |
---|
86 | delete critical_section_cast(mutex); |
---|
87 | } |
---|
88 | |
---|
89 | inline void delete_mutex(void* mutex) |
---|
90 | { |
---|
91 | int res = 0; |
---|
92 | res = CloseHandle(mutex_cast(mutex)); |
---|
93 | assert(res); |
---|
94 | } |
---|
95 | |
---|
96 | inline void wait_critical_section_infinite(void* mutex) |
---|
97 | { |
---|
98 | EnterCriticalSection(critical_section_cast(mutex)); //:xxx Can throw an exception under low memory conditions |
---|
99 | } |
---|
100 | |
---|
101 | inline bool wait_critical_section_try(void* mutex) |
---|
102 | { |
---|
103 | BOOL res = g_TryEnterCriticalSection(critical_section_cast(mutex)); |
---|
104 | return res != 0; |
---|
105 | } |
---|
106 | |
---|
107 | inline int wait_mutex(void* mutex, int time) |
---|
108 | { |
---|
109 | unsigned int res = 0; |
---|
110 | res = WaitForSingleObject(mutex_cast(mutex), time); |
---|
111 | //:xxx assert(res != WAIT_FAILED && res != WAIT_ABANDONED); |
---|
112 | return res; |
---|
113 | } |
---|
114 | |
---|
115 | inline void release_critical_section(void* mutex) |
---|
116 | { |
---|
117 | LeaveCriticalSection(critical_section_cast(mutex)); |
---|
118 | } |
---|
119 | |
---|
120 | inline void release_mutex(void* mutex) |
---|
121 | { |
---|
122 | BOOL res = FALSE; |
---|
123 | res = ReleaseMutex(mutex_cast(mutex)); |
---|
124 | assert(res); |
---|
125 | } |
---|
126 | #endif |
---|
127 | |
---|
128 | } |
---|