Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: src/plugin/PluginMutex.cpp

Issue 5750789393874944: [IE] First round of ATL removal (Closed)
Patch Set: Created June 20, 2014, 9:22 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
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), mutex_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( "Mutex::Create name:" + mutex_name )
13 } 14 }
14 15 m_hMutex = ::CreateMutexW( NULL, FALSE, mutex_name.c_str() );
15 m_hMutex = ::CreateMutex(NULL, FALSE, "Global\\AdblockPlus" + name);
16
17 if (m_hMutex == NULL) 16 if (m_hMutex == NULL)
18 { 17 {
19 DWORD error = GetLastError(); 18 DWORD error = GetLastError();
20 m_hMutex = OpenMutex(MUTEX_ALL_ACCESS, FALSE, "Global\\AdblockPlus" + name); 19 m_hMutex = OpenMutexW( MUTEX_ALL_ACCESS, FALSE, mutex_name.c_str() );
21 if (m_hMutex == NULL) 20 if (m_hMutex == NULL)
22 { 21 {
23 m_hMutex = ::CreateMutex(NULL, FALSE, "Local\\AdblockPlus" + name); 22 m_hMutex = ::CreateMutexW( NULL, FALSE, mutex_name.c_str() );
Oleksandr 2014/06/26 00:48:43 We should try to open a mutex in Local namespace i
Eric 2014/06/26 15:13:56 This a defect I introduced. I don't think I notice
24 if (m_hMutex == NULL) 23 if (m_hMutex == NULL)
25 { 24 {
26 m_hMutex = OpenMutex(NULL, FALSE, "Local\\AdblockPlus" + name); 25 m_hMutex = OpenMutexW( NULL, FALSE, mutex_name.c_str() );
27 if (m_hMutex == NULL) 26 if (m_hMutex == NULL)
28 { 27 {
29 DWORD error = GetLastError(); 28 DWORD error = GetLastError();
30 DEBUG_ERROR_LOG(::GetLastError(), PLUGIN_ERROR_MUTEX, PLUGIN_ERROR_MUT EX_CREATE + m_errorSubidBase, "Mutex::CreateMutex"); 29 DEBUG_ERROR_LOG(::GetLastError(), PLUGIN_ERROR_MUTEX, PLUGIN_ERROR_MUT EX_CREATE + m_errorSubidBase, L"Mutex::CreateMutex");
31 } 30 }
32 } 31 }
33 else 32 else
34 { 33 {
35 switch (::WaitForSingleObject(m_hMutex, 3000)) 34 switch (::WaitForSingleObject(m_hMutex, 3000))
36 { 35 {
37 // The thread got ownership of the mutex 36 // The thread got ownership of the mutex
38 case WAIT_OBJECT_0: 37 case WAIT_OBJECT_0:
39 m_isLocked = true; 38 m_isLocked = true;
40 break; 39 break;
41 40
42 case WAIT_TIMEOUT: 41 case WAIT_TIMEOUT:
43 DEBUG_ERROR_LOG(::GetLastError(), PLUGIN_ERROR_MUTEX, PLUGIN_ERROR_MUT EX_WAIT_TIMEOUT + m_errorSubidBase, "Mutex::CreateMutex - Timeout"); 42 DEBUG_ERROR_LOG(::GetLastError(), PLUGIN_ERROR_MUTEX, PLUGIN_ERROR_MUT EX_WAIT_TIMEOUT + m_errorSubidBase, L"Mutex::CreateMutex - Timeout");
44 m_hMutex = NULL; 43 m_hMutex = NULL;
45 break; 44 break;
46 45
47 case WAIT_FAILED: 46 case WAIT_FAILED:
48 DEBUG_ERROR_LOG(::GetLastError(), PLUGIN_ERROR_MUTEX, PLUGIN_ERROR_MUT EX_WAIT + m_errorSubidBase, "Mutex::CreateMutex - Wait error"); 47 DEBUG_ERROR_LOG(::GetLastError(), PLUGIN_ERROR_MUTEX, PLUGIN_ERROR_MUT EX_WAIT + m_errorSubidBase, L"Mutex::CreateMutex - Wait error");
49 break; 48 break;
50 } 49 }
51 } 50 }
52 51
53 } 52 }
54 } 53 }
55 else 54 else
56 { 55 {
57 switch (::WaitForSingleObject(m_hMutex, 3000)) 56 switch (::WaitForSingleObject(m_hMutex, 3000))
58 { 57 {
59 // The thread got ownership of the mutex 58 // The thread got ownership of the mutex
60 case WAIT_OBJECT_0: 59 case WAIT_OBJECT_0:
61 m_isLocked = true; 60 m_isLocked = true;
62 break; 61 break;
63 62
64 case WAIT_TIMEOUT: 63 case WAIT_TIMEOUT:
65 DEBUG_ERROR_LOG(::GetLastError(), PLUGIN_ERROR_MUTEX, PLUGIN_ERROR_MUTEX_W AIT_TIMEOUT + m_errorSubidBase, "Mutex::CreateMutex - Timeout"); 64 DEBUG_ERROR_LOG(::GetLastError(), PLUGIN_ERROR_MUTEX, PLUGIN_ERROR_MUTEX_W AIT_TIMEOUT + m_errorSubidBase, L"Mutex::CreateMutex - Timeout");
66 m_hMutex = NULL; 65 m_hMutex = NULL;
67 break; 66 break;
68 67
69 case WAIT_FAILED: 68 case WAIT_FAILED:
70 DEBUG_ERROR_LOG(::GetLastError(), PLUGIN_ERROR_MUTEX, PLUGIN_ERROR_MUTEX_W AIT + m_errorSubidBase, "Mutex::CreateMutex - Wait error"); 69 DEBUG_ERROR_LOG(::GetLastError(), PLUGIN_ERROR_MUTEX, PLUGIN_ERROR_MUTEX_W AIT + m_errorSubidBase, L"Mutex::CreateMutex - Wait error");
71 break; 70 break;
72 } 71 }
73 } 72 }
74 } 73 }
75 74
76 CPluginMutex::~CPluginMutex() 75 CPluginMutex::~CPluginMutex()
77 { 76 {
78 if (m_errorSubidBase != PLUGIN_ERROR_MUTEX_DEBUG_FILE) 77 if (m_errorSubidBase != PLUGIN_ERROR_MUTEX_DEBUG_FILE)
79 { 78 {
80 DEBUG_MUTEX("Mutex::Release name:" + m_name) 79 DEBUG_MUTEX("Mutex::Release name:" + mutex_name)
81 } 80 }
82 81
83 if (m_isLocked) 82 if (m_isLocked)
84 { 83 {
85 m_isLocked = false; 84 m_isLocked = false;
86 } 85 }
87 86
88 if (m_hMutex) 87 if (m_hMutex)
89 { 88 {
90 if (!::ReleaseMutex(m_hMutex)) 89 if (!::ReleaseMutex(m_hMutex))
91 { 90 {
92 DEBUG_ERROR_LOG(::GetLastError(), PLUGIN_ERROR_MUTEX, PLUGIN_ERROR_MUTEX_R ELEASE + m_errorSubidBase, "Mutex::ReleaseMutex"); 91 DEBUG_ERROR_LOG(::GetLastError(), PLUGIN_ERROR_MUTEX, PLUGIN_ERROR_MUTEX_R ELEASE + m_errorSubidBase, L"Mutex::ReleaseMutex");
93 } 92 }
94 } 93 }
95 94
96 m_hMutex = NULL; 95 m_hMutex = NULL;
97 } 96 }
98 97
99 bool CPluginMutex::IsLocked() const 98 bool CPluginMutex::IsLocked() const
100 { 99 {
101 return m_isLocked; 100 return m_isLocked;
102 } 101 }
OLDNEW

Powered by Google App Engine
This is Rietveld