OLD | NEW |
1 #include "PluginStdAfx.h" | 1 #include "PluginStdAfx.h" |
2 | 2 |
3 #include "PluginMutex.h" | 3 #include "PluginMutex.h" |
4 #include "PluginClient.h" | 4 #include "PluginClient.h" |
5 #include "sddl.h" | 5 #include "sddl.h" |
6 | 6 |
7 | 7 |
8 CPluginMutex::CPluginMutex(const CString& name, int errorSubidBase) : m_isLocked
(false), m_errorSubidBase(errorSubidBase), m_name(name) | 8 CPluginMutex::CPluginMutex(const std::wstring& name, int errorSubidBase) |
| 9 : m_isLocked(false), m_errorSubidBase(errorSubidBase), system_name(L"Global\\A
dblockPlus" + name) |
9 { | 10 { |
10 if (m_errorSubidBase != PLUGIN_ERROR_MUTEX_DEBUG_FILE) | 11 if (m_errorSubidBase != PLUGIN_ERROR_MUTEX_DEBUG_FILE) |
11 { | 12 { |
12 DEBUG_MUTEX("Mutex::Create name:" + name) | 13 DEBUG_MUTEX(L"Mutex::Create name:" + name) |
13 } | 14 } |
14 | 15 m_hMutex = CreateMutexW(NULL, FALSE, system_name.c_str()); |
15 m_hMutex = ::CreateMutex(NULL, FALSE, "Global\\AdblockPlus" + name); | |
16 | 16 |
17 if (m_hMutex == NULL) | 17 if (m_hMutex == NULL) |
18 { | 18 { |
19 DWORD error = GetLastError(); | 19 DWORD error = GetLastError(); |
20 m_hMutex = OpenMutex(MUTEX_ALL_ACCESS, FALSE, "Global\\AdblockPlus" + name); | 20 m_hMutex = OpenMutexW(MUTEX_ALL_ACCESS, FALSE, system_name.c_str()); |
21 if (m_hMutex == NULL) | 21 if (m_hMutex == NULL) |
22 { | 22 { |
23 m_hMutex = ::CreateMutex(NULL, FALSE, "Local\\AdblockPlus" + name); | 23 system_name = L"Local\\AdblockPlus" + name; |
| 24 m_hMutex = CreateMutexW(NULL, FALSE, system_name.c_str()); |
24 if (m_hMutex == NULL) | 25 if (m_hMutex == NULL) |
25 { | 26 { |
26 m_hMutex = OpenMutex(NULL, FALSE, "Local\\AdblockPlus" + name); | 27 m_hMutex = OpenMutexW(NULL, FALSE, system_name.c_str()); |
27 if (m_hMutex == NULL) | 28 if (m_hMutex == NULL) |
28 { | 29 { |
29 DWORD error = GetLastError(); | 30 DWORD error = GetLastError(); |
30 DEBUG_ERROR_LOG(::GetLastError(), PLUGIN_ERROR_MUTEX, PLUGIN_ERROR_MUT
EX_CREATE + m_errorSubidBase, "Mutex::CreateMutex"); | 31 DEBUG_ERROR_LOG(::GetLastError(), PLUGIN_ERROR_MUTEX, PLUGIN_ERROR_MUT
EX_CREATE + m_errorSubidBase, "Mutex::CreateMutex"); |
31 } | 32 } |
32 } | 33 } |
33 else | 34 else |
| 35 // TODO: Combine this block with identical one below. |
34 { | 36 { |
35 switch (::WaitForSingleObject(m_hMutex, 3000)) | 37 switch (::WaitForSingleObject(m_hMutex, 3000)) |
36 { | 38 { |
37 // The thread got ownership of the mutex | 39 // The thread got ownership of the mutex |
38 case WAIT_OBJECT_0: | 40 case WAIT_OBJECT_0: |
39 m_isLocked = true; | 41 m_isLocked = true; |
40 break; | 42 break; |
41 | 43 |
42 case WAIT_TIMEOUT: | 44 case WAIT_TIMEOUT: |
43 DEBUG_ERROR_LOG(::GetLastError(), PLUGIN_ERROR_MUTEX, PLUGIN_ERROR_MUT
EX_WAIT_TIMEOUT + m_errorSubidBase, "Mutex::CreateMutex - Timeout"); | 45 DEBUG_ERROR_LOG(::GetLastError(), PLUGIN_ERROR_MUTEX, PLUGIN_ERROR_MUT
EX_WAIT_TIMEOUT + m_errorSubidBase, "Mutex::CreateMutex - Timeout"); |
44 m_hMutex = NULL; | 46 m_hMutex = NULL; |
45 break; | 47 break; |
46 | 48 |
47 case WAIT_FAILED: | 49 case WAIT_FAILED: |
48 DEBUG_ERROR_LOG(::GetLastError(), PLUGIN_ERROR_MUTEX, PLUGIN_ERROR_MUT
EX_WAIT + m_errorSubidBase, "Mutex::CreateMutex - Wait error"); | 50 DEBUG_ERROR_LOG(::GetLastError(), PLUGIN_ERROR_MUTEX, PLUGIN_ERROR_MUT
EX_WAIT + m_errorSubidBase, "Mutex::CreateMutex - Wait error"); |
49 break; | 51 break; |
50 } | 52 } |
51 } | 53 } |
52 | 54 |
53 } | 55 } |
54 } | 56 } |
55 else | 57 else |
| 58 // TODO: Combine this block with identical one above. |
56 { | 59 { |
57 switch (::WaitForSingleObject(m_hMutex, 3000)) | 60 switch (::WaitForSingleObject(m_hMutex, 3000)) |
58 { | 61 { |
59 // The thread got ownership of the mutex | 62 // The thread got ownership of the mutex |
60 case WAIT_OBJECT_0: | 63 case WAIT_OBJECT_0: |
61 m_isLocked = true; | 64 m_isLocked = true; |
62 break; | 65 break; |
63 | 66 |
64 case WAIT_TIMEOUT: | 67 case WAIT_TIMEOUT: |
65 DEBUG_ERROR_LOG(::GetLastError(), PLUGIN_ERROR_MUTEX, PLUGIN_ERROR_MUTEX_W
AIT_TIMEOUT + m_errorSubidBase, "Mutex::CreateMutex - Timeout"); | 68 DEBUG_ERROR_LOG(::GetLastError(), PLUGIN_ERROR_MUTEX, PLUGIN_ERROR_MUTEX_W
AIT_TIMEOUT + m_errorSubidBase, "Mutex::CreateMutex - Timeout"); |
66 m_hMutex = NULL; | 69 m_hMutex = NULL; |
67 break; | 70 break; |
68 | 71 |
69 case WAIT_FAILED: | 72 case WAIT_FAILED: |
70 DEBUG_ERROR_LOG(::GetLastError(), PLUGIN_ERROR_MUTEX, PLUGIN_ERROR_MUTEX_W
AIT + m_errorSubidBase, "Mutex::CreateMutex - Wait error"); | 73 DEBUG_ERROR_LOG(::GetLastError(), PLUGIN_ERROR_MUTEX, PLUGIN_ERROR_MUTEX_W
AIT + m_errorSubidBase, "Mutex::CreateMutex - Wait error"); |
71 break; | 74 break; |
72 } | 75 } |
73 } | 76 } |
74 } | 77 } |
75 | 78 |
76 CPluginMutex::~CPluginMutex() | 79 CPluginMutex::~CPluginMutex() |
77 { | 80 { |
78 if (m_errorSubidBase != PLUGIN_ERROR_MUTEX_DEBUG_FILE) | 81 if (m_errorSubidBase != PLUGIN_ERROR_MUTEX_DEBUG_FILE) |
79 { | 82 { |
80 DEBUG_MUTEX("Mutex::Release name:" + m_name) | 83 DEBUG_MUTEX(L"Mutex::Release name:" + system_name) |
81 } | 84 } |
82 | 85 |
83 if (m_isLocked) | 86 if (m_isLocked) |
84 { | 87 { |
85 m_isLocked = false; | 88 m_isLocked = false; |
86 } | 89 } |
87 | 90 |
88 if (m_hMutex) | 91 if (m_hMutex) |
89 { | 92 { |
90 if (!::ReleaseMutex(m_hMutex)) | 93 if (!::ReleaseMutex(m_hMutex)) |
91 { | 94 { |
92 DEBUG_ERROR_LOG(::GetLastError(), PLUGIN_ERROR_MUTEX, PLUGIN_ERROR_MUTEX_R
ELEASE + m_errorSubidBase, "Mutex::ReleaseMutex"); | 95 DEBUG_ERROR_LOG(::GetLastError(), PLUGIN_ERROR_MUTEX, PLUGIN_ERROR_MUTEX_R
ELEASE + m_errorSubidBase, "Mutex::ReleaseMutex"); |
93 } | 96 } |
94 } | 97 } |
95 | 98 |
96 m_hMutex = NULL; | 99 m_hMutex = NULL; |
97 } | 100 } |
98 | 101 |
99 bool CPluginMutex::IsLocked() const | 102 bool CPluginMutex::IsLocked() const |
100 { | 103 { |
101 return m_isLocked; | 104 return m_isLocked; |
102 } | 105 } |
OLD | NEW |