Plasma Engine  2.0
Loading...
Searching...
No Matches
Mutex_win.h
1#ifdef PL_MUTEX_WIN_INL_H_INCLUDED
2# error "This file must not be included twice."
3#endif
4
5#define PL_MUTEX_WIN_INL_H_INCLUDED
6
7#if PL_ENABLED(PL_COMPILER_MSVC) && PL_ENABLED(PL_PLATFORM_ARCH_X86)
8
9extern "C"
10{
11 // The main purpose of this little hack here is to have Mutex::Lock and Mutex::Unlock inline-able without including windows.h
12 // The hack however does only work on the MSVC compiler. See fall back code below.
13
14 // First define two functions which are binary compatible with EnterCriticalSection and LeaveCriticalSection
15 __declspec(dllimport) void __stdcall plWinEnterCriticalSection(plMutexHandle* handle);
16 __declspec(dllimport) void __stdcall plWinLeaveCriticalSection(plMutexHandle* handle);
17 __declspec(dllimport) plMinWindows::BOOL __stdcall plWinTryEnterCriticalSection(plMutexHandle* handle);
18
19 // Now redirect them through linker flags to the correct implementation
20# if PL_ENABLED(PL_PLATFORM_32BIT)
21# pragma comment(linker, "/alternatename:__imp__plWinEnterCriticalSection@4=__imp__EnterCriticalSection@4")
22# pragma comment(linker, "/alternatename:__imp__plWinLeaveCriticalSection@4=__imp__LeaveCriticalSection@4")
23# pragma comment(linker, "/alternatename:__imp__plWinTryEnterCriticalSection@4=__imp__TryEnterCriticalSection@4")
24# else
25# pragma comment(linker, "/alternatename:__imp_plWinEnterCriticalSection=__imp_EnterCriticalSection")
26# pragma comment(linker, "/alternatename:__imp_plWinLeaveCriticalSection=__imp_LeaveCriticalSection")
27# pragma comment(linker, "/alternatename:__imp_plWinTryEnterCriticalSection=__imp_TryEnterCriticalSection")
28# endif
29}
30
31inline void plMutex::Lock()
32{
33 plWinEnterCriticalSection(&m_hHandle);
34 ++m_iLockCount;
35}
36
37inline void plMutex::Unlock()
38{
39 --m_iLockCount;
40 plWinLeaveCriticalSection(&m_hHandle);
41}
42
44{
45 if (plWinTryEnterCriticalSection(&m_hHandle) != 0)
46 {
47 ++m_iLockCount;
48 return PL_SUCCESS;
49 }
50
51 return PL_FAILURE;
52}
53
54#else
55
56# include <Foundation/Basics/Platform/Win/IncludeWindows.h>
57
58inline void plMutex::Lock()
59{
60 EnterCriticalSection((CRITICAL_SECTION*)&m_hHandle);
61 ++m_iLockCount;
62}
63
64inline void plMutex::Unlock()
65{
66 --m_iLockCount;
67 LeaveCriticalSection((CRITICAL_SECTION*)&m_hHandle);
68}
69
71{
72 if (TryEnterCriticalSection((CRITICAL_SECTION*)&m_hHandle) != 0)
73 {
74 ++m_iLockCount;
75 return PL_SUCCESS;
76 }
77
78 return PL_FAILURE;
79}
80
81#endif
void Unlock()
Releases a lock that has been previously acquired.
Definition Mutex_posix.h:34
plResult TryLock()
Attempts to acquire an exclusive lock for this mutex object. Returns true on success.
Definition Mutex_posix.h:24
void Lock()
Acquires an exclusive lock for this mutex object.
Definition Mutex_posix.h:18
Default enum for returning failure or success, instead of using a bool.
Definition Types.h:54