Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/thread/src/mutex.inl @ 29

Last change on this file since 29 was 29, checked in by landauf, 16 years ago

updated boost from 1_33_1 to 1_34_1

File size: 3.4 KB
Line 
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
8namespace {
9
10#if defined(BOOST_HAS_WINTHREADS)
11//:PREVENT THIS FROM BEING DUPLICATED
12typedef BOOL (WINAPI* TryEnterCriticalSection_type)(LPCRITICAL_SECTION lpCriticalSection);
13TryEnterCriticalSection_type g_TryEnterCriticalSection = 0;
14boost::once_flag once_init_TryEnterCriticalSection = BOOST_ONCE_INIT;
15
16void 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
39inline bool has_TryEnterCriticalSection()
40{
41    boost::call_once(init_TryEnterCriticalSection, once_init_TryEnterCriticalSection);
42    return g_TryEnterCriticalSection != 0;
43}
44
45inline HANDLE mutex_cast(void* p)
46{
47    return reinterpret_cast<HANDLE>(p);
48}
49
50inline LPCRITICAL_SECTION critical_section_cast(void* p)
51{
52    return reinterpret_cast<LPCRITICAL_SECTION>(p);
53}
54
55inline 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
70inline 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
83inline void delete_critical_section(void* mutex)
84{
85    DeleteCriticalSection(critical_section_cast(mutex));
86    delete critical_section_cast(mutex);
87}
88
89inline void delete_mutex(void* mutex)
90{
91    int res = 0;
92    res = CloseHandle(mutex_cast(mutex));
93    assert(res);
94}
95
96inline 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
101inline bool wait_critical_section_try(void* mutex)
102{
103    BOOL res = g_TryEnterCriticalSection(critical_section_cast(mutex));
104    return res != 0;
105}
106
107inline 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
115inline void release_critical_section(void* mutex)
116{
117    LeaveCriticalSection(critical_section_cast(mutex));
118}
119
120inline void release_mutex(void* mutex)
121{
122    BOOL res = FALSE;
123    res = ReleaseMutex(mutex_cast(mutex));
124    assert(res);
125}
126#endif
127
128}
Note: See TracBrowser for help on using the repository browser.