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