diff options
author | George Hazan <george.hazan@gmail.com> | 2014-07-13 10:50:59 +0000 |
---|---|---|
committer | George Hazan <george.hazan@gmail.com> | 2014-07-13 10:50:59 +0000 |
commit | 5df94ff719eccd04c7450df35fb5e3f2b36e2ee7 (patch) | |
tree | 440d736b199ff849ca0619adf355ffc73750ca7f /protocols/IRCG/src | |
parent | 70496fb739225b559a251f4518c946110aef718c (diff) |
wrapping the most evident critical sections into a class
git-svn-id: http://svn.miranda-ng.org/main/trunk@9785 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
Diffstat (limited to 'protocols/IRCG/src')
-rw-r--r-- | protocols/IRCG/src/commandmonitor.cpp | 26 | ||||
-rw-r--r-- | protocols/IRCG/src/irc.h | 10 | ||||
-rw-r--r-- | protocols/IRCG/src/irclib.cpp | 97 | ||||
-rw-r--r-- | protocols/IRCG/src/ircproto.cpp | 23 | ||||
-rw-r--r-- | protocols/IRCG/src/main.cpp | 2 | ||||
-rw-r--r-- | protocols/IRCG/src/services.cpp | 14 | ||||
-rw-r--r-- | protocols/IRCG/src/tools.cpp | 38 |
7 files changed, 77 insertions, 133 deletions
diff --git a/protocols/IRCG/src/commandmonitor.cpp b/protocols/IRCG/src/commandmonitor.cpp index 52d4b03d20..ad1a374ce4 100644 --- a/protocols/IRCG/src/commandmonitor.cpp +++ b/protocols/IRCG/src/commandmonitor.cpp @@ -237,22 +237,22 @@ int CIrcProto::AddOutgoingMessageToDB(MCONTACT hContact, TCHAR* msg) void __cdecl CIrcProto::ResolveIPThread(LPVOID di)
{
IPRESOLVE* ipr = (IPRESOLVE *)di;
-
- EnterCriticalSection(&m_resolve);
-
- if (ipr != NULL && (ipr->iType == IP_AUTO && lstrlenA(m_myHost) == 0 || ipr->iType == IP_MANUAL)) {
- hostent* myhost = gethostbyname(ipr->sAddr.c_str());
- if (myhost) {
- IN_ADDR in;
- memcpy(&in, myhost->h_addr, 4);
- if (ipr->iType == IP_AUTO)
- mir_snprintf(m_myHost, sizeof(m_myHost), "%s", inet_ntoa(in));
- else
- mir_snprintf(m_mySpecifiedHostIP, sizeof(m_mySpecifiedHostIP), "%s", inet_ntoa(in));
+ {
+ mir_cslock lock(m_resolve);
+
+ if (ipr != NULL && (ipr->iType == IP_AUTO && lstrlenA(m_myHost) == 0 || ipr->iType == IP_MANUAL)) {
+ hostent* myhost = gethostbyname(ipr->sAddr.c_str());
+ if (myhost) {
+ IN_ADDR in;
+ memcpy(&in, myhost->h_addr, 4);
+ if (ipr->iType == IP_AUTO)
+ mir_snprintf(m_myHost, sizeof(m_myHost), "%s", inet_ntoa(in));
+ else
+ mir_snprintf(m_mySpecifiedHostIP, sizeof(m_mySpecifiedHostIP), "%s", inet_ntoa(in));
+ }
}
}
- LeaveCriticalSection(&m_resolve);
delete ipr;
}
diff --git a/protocols/IRCG/src/irc.h b/protocols/IRCG/src/irc.h index 89f2edd82d..b74ff0b710 100644 --- a/protocols/IRCG/src/irc.h +++ b/protocols/IRCG/src/irc.h @@ -348,10 +348,10 @@ struct CIrcProto : public PROTO<CIrcProto> OBJLIST<CMString> vUserhostReasons;
OBJLIST<CMString> vWhoInProgress;
- CRITICAL_SECTION cs;
- CRITICAL_SECTION m_gchook;
- CRITICAL_SECTION m_resolve;
- HANDLE m_evWndCreate;
+ mir_cs cs;
+ mir_cs m_gchook;
+ mir_cs m_resolve;
+ HANDLE m_evWndCreate;
CMString m_statusMessage;
int m_iTempCheckTime;
@@ -536,7 +536,7 @@ protected : LIST<CDccSession> m_dcc_xfers;
private :
- CRITICAL_SECTION m_dcc; // protect the dcc objects
+ mir_cs m_dcc; // protect the dcc objects
void createMessageFromPchar( const char* p );
void Notify(const CIrcMessage* pmsg);
diff --git a/protocols/IRCG/src/irclib.cpp b/protocols/IRCG/src/irclib.cpp index f6a57f3121..d824dbd080 100644 --- a/protocols/IRCG/src/irclib.cpp +++ b/protocols/IRCG/src/irclib.cpp @@ -459,99 +459,82 @@ void __cdecl CIrcProto::ThreadProc(void*) void CIrcProto::AddDCCSession(MCONTACT, CDccSession* dcc)
{
- EnterCriticalSection(&m_dcc);
+ mir_cslock lck(m_dcc);
CDccSession* p = m_dcc_chats.find(dcc);
if (p)
m_dcc_chats.remove(p);
m_dcc_chats.insert(dcc);
-
- LeaveCriticalSection(&m_dcc);
}
void CIrcProto::AddDCCSession(DCCINFO*, CDccSession* dcc)
{
- EnterCriticalSection(&m_dcc);
-
+ mir_cslock lck(m_dcc);
m_dcc_xfers.insert(dcc);
-
- LeaveCriticalSection(&m_dcc);
}
void CIrcProto::RemoveDCCSession(MCONTACT hContact)
{
- EnterCriticalSection(&m_dcc);
+ mir_cslock lck(m_dcc);
for (int i = 0; i < m_dcc_chats.getCount(); i++)
- if (m_dcc_chats[i]->di->hContact == hContact) {
- m_dcc_chats.remove(i);
- break;
- }
-
- LeaveCriticalSection(&m_dcc);
+ if (m_dcc_chats[i]->di->hContact == hContact) {
+ m_dcc_chats.remove(i);
+ break;
+ }
}
void CIrcProto::RemoveDCCSession(DCCINFO* pdci)
{
- EnterCriticalSection(&m_dcc);
+ mir_cslock lck(m_dcc);
- for (int i = 0; i < m_dcc_xfers.getCount(); i++)
- if (m_dcc_xfers[i]->di == pdci) {
- m_dcc_xfers.remove(i);
- break;
+ for (int i = 0; i < m_dcc_xfers.getCount(); i++) {
+ if (m_dcc_xfers[i]->di == pdci) {
+ m_dcc_xfers.remove(i);
+ break;
+ }
}
-
- LeaveCriticalSection(&m_dcc);
}
CDccSession* CIrcProto::FindDCCSession(MCONTACT hContact)
{
- EnterCriticalSection(&m_dcc);
+ mir_cslock lck(m_dcc);
for (int i = 0; i < m_dcc_chats.getCount(); i++)
- if (m_dcc_chats[i]->di->hContact == hContact) {
- LeaveCriticalSection(&m_dcc);
- return m_dcc_chats[i];
- }
+ if (m_dcc_chats[i]->di->hContact == hContact)
+ return m_dcc_chats[i];
- LeaveCriticalSection(&m_dcc);
return 0;
}
CDccSession* CIrcProto::FindDCCSession(DCCINFO* pdci)
{
- EnterCriticalSection(&m_dcc);
+ mir_cslock lck(m_dcc);
for (int i = 0; i < m_dcc_xfers.getCount(); i++)
- if (m_dcc_xfers[i]->di == pdci) {
- LeaveCriticalSection(&m_dcc);
- return m_dcc_xfers[i];
- }
+ if (m_dcc_xfers[i]->di == pdci)
+ return m_dcc_xfers[i];
- LeaveCriticalSection(&m_dcc);
return 0;
}
CDccSession* CIrcProto::FindDCCSendByPort(int iPort)
{
- EnterCriticalSection(&m_dcc);
+ mir_cslock lck(m_dcc);
for (int i = 0; i < m_dcc_xfers.getCount(); i++) {
- CDccSession* p = m_dcc_xfers[i];
- if (p->di->iType == DCC_SEND && p->di->bSender && iPort == p->di->iPort) {
- LeaveCriticalSection(&m_dcc);
+ CDccSession *p = m_dcc_xfers[i];
+ if (p->di->iType == DCC_SEND && p->di->bSender && iPort == p->di->iPort)
return p;
- }
}
- LeaveCriticalSection(&m_dcc);
return 0;
}
CDccSession* CIrcProto::FindDCCRecvByPortAndName(int iPort, const TCHAR* szName)
{
- EnterCriticalSection(&m_dcc);
+ mir_cslock lck(m_dcc);
for (int i = 0; i < m_dcc_xfers.getCount(); i++) {
CDccSession* p = m_dcc_xfers[i];
@@ -559,61 +542,51 @@ CDccSession* CIrcProto::FindDCCRecvByPortAndName(int iPort, const TCHAR* szName) if (!getTString(p->di->hContact, "Nick", &dbv)) {
if (p->di->iType == DCC_SEND && !p->di->bSender && !lstrcmpi(szName, dbv.ptszVal) && iPort == p->di->iPort) {
db_free(&dbv);
- LeaveCriticalSection(&m_dcc);
return p;
}
db_free(&dbv);
}
}
- LeaveCriticalSection(&m_dcc);
return 0;
}
CDccSession* CIrcProto::FindPassiveDCCSend(int iToken)
{
- EnterCriticalSection(&m_dcc);
+ mir_cslock lck(m_dcc);
- for (int i = 0; i < m_dcc_xfers.getCount(); i++) {
- if (m_dcc_xfers[i]->iToken == iToken) {
- LeaveCriticalSection(&m_dcc);
+ for (int i = 0; i < m_dcc_xfers.getCount(); i++)
+ if (m_dcc_xfers[i]->iToken == iToken)
return m_dcc_xfers[i];
- }
- }
- LeaveCriticalSection(&m_dcc);
return 0;
}
CDccSession* CIrcProto::FindPassiveDCCRecv(CMString sName, CMString sToken)
{
- EnterCriticalSection(&m_dcc);
+ mir_cslock lck(m_dcc);
for (int i = 0; i < m_dcc_xfers.getCount(); i++) {
- CDccSession* p = m_dcc_xfers[i];
- if (sToken == p->di->sToken && sName == p->di->sContactName) {
- LeaveCriticalSection(&m_dcc);
+ CDccSession *p = m_dcc_xfers[i];
+ if (sToken == p->di->sToken && sName == p->di->sContactName)
return p;
- }
}
- LeaveCriticalSection(&m_dcc);
+
return 0;
}
void CIrcProto::DisconnectAllDCCSessions(bool Shutdown)
{
- EnterCriticalSection(&m_dcc);
+ mir_cslock lck(m_dcc);
for (int i = 0; i < m_dcc_chats.getCount(); i++)
- if (m_disconnectDCCChats || Shutdown)
- m_dcc_chats[i]->Disconnect();
-
- LeaveCriticalSection(&m_dcc);
+ if (m_disconnectDCCChats || Shutdown)
+ m_dcc_chats[i]->Disconnect();
}
void CIrcProto::CheckDCCTimeout(void)
{
- EnterCriticalSection(&m_dcc);
+ mir_cslock lck(m_dcc);
for (int i = 0; i < m_dcc_chats.getCount(); i++) {
CDccSession* p = m_dcc_chats[i];
@@ -626,8 +599,6 @@ void CIrcProto::CheckDCCTimeout(void) if (time(0) > p->tLastActivity + DCCSENDTIMEOUT)
p->Disconnect();
}
-
- LeaveCriticalSection(&m_dcc);
}
////////////////////////////////////////////////////////////////////
diff --git a/protocols/IRCG/src/ircproto.cpp b/protocols/IRCG/src/ircproto.cpp index a212afdfdb..8b036c14c8 100644 --- a/protocols/IRCG/src/ircproto.cpp +++ b/protocols/IRCG/src/ircproto.cpp @@ -32,15 +32,13 @@ static int CompareSessions(const CDccSession* p1, const CDccSession* p2) }
CIrcProto::CIrcProto(const char* szModuleName, const TCHAR* tszUserName) :
-PROTO<CIrcProto>(szModuleName, tszUserName),
-m_dcc_chats(10, CompareSessions),
-m_dcc_xfers(10, CompareSessions),
-m_ignoreItems(10),
-vUserhostReasons(10),
-vWhoInProgress(10)
-{
- InitializeCriticalSection(&cs);
- InitializeCriticalSection(&m_gchook);
+ PROTO<CIrcProto>(szModuleName, tszUserName),
+ m_dcc_chats(10, CompareSessions),
+ m_dcc_xfers(10, CompareSessions),
+ m_ignoreItems(10),
+ vUserhostReasons(10),
+ vWhoInProgress(10)
+{
m_evWndCreate = ::CreateEvent(NULL, FALSE, FALSE, NULL);
CreateProtoService(PS_GETMYAWAYMSG, &CIrcProto::GetMyAwayMsg);
@@ -67,8 +65,6 @@ vWhoInProgress(10) CreateProtoService("/GetIrcData", &CIrcProto::Scripting_GetIrcData);
codepage = CP_ACP;
- InitializeCriticalSection(&m_resolve);
- InitializeCriticalSection(&m_dcc);
InitPrefs();
@@ -152,17 +148,12 @@ CIrcProto::~CIrcProto() Netlib_CloseHandle(m_hNetlibUser); m_hNetlibUser = NULL;
Netlib_CloseHandle(hNetlibDCC); hNetlibDCC = NULL;
- DeleteCriticalSection(&cs);
- DeleteCriticalSection(&m_gchook);
-
if (hMenuRoot)
CallService(MS_CLIST_REMOVEMAINMENUITEM, (WPARAM)hMenuRoot, 0);
mir_free(m_alias);
CloseHandle(m_evWndCreate);
- DeleteCriticalSection(&m_resolve);
- DeleteCriticalSection(&m_dcc);
KillChatTimer(OnlineNotifTimer);
KillChatTimer(OnlineNotifTimer3);
}
diff --git a/protocols/IRCG/src/main.cpp b/protocols/IRCG/src/main.cpp index 5dd5f1ec82..f7e10448e5 100644 --- a/protocols/IRCG/src/main.cpp +++ b/protocols/IRCG/src/main.cpp @@ -40,7 +40,6 @@ static int sttCompareProtocols(const CIrcProto *p1, const CIrcProto *p2) LIST<CIrcProto> g_Instances(1, sttCompareProtocols);
-void InitTimers( void );
void UninitTimers( void );
// Information about the plugin
@@ -92,7 +91,6 @@ extern "C" int __declspec(dllexport) Load() mir_getLP(&pluginInfo);
InitIcons();
- InitTimers();
InitServers();
InitContactMenus();
diff --git a/protocols/IRCG/src/services.cpp b/protocols/IRCG/src/services.cpp index 02e7fdbc0d..ae0ecb7267 100644 --- a/protocols/IRCG/src/services.cpp +++ b/protocols/IRCG/src/services.cpp @@ -499,7 +499,7 @@ int __cdecl CIrcProto::GCEventHook(WPARAM wParam, LPARAM lParam) GCHOOK *gch = (GCHOOK*)lParam;
CMString S = _T("");
- EnterCriticalSection(&m_gchook);
+ mir_cslock lock(m_gchook);
// handle the hook
if (gch) {
@@ -769,7 +769,6 @@ int __cdecl CIrcProto::GCEventHook(WPARAM wParam, LPARAM lParam) }
}
- LeaveCriticalSection(&m_gchook);
return 0;
}
@@ -906,7 +905,7 @@ int __cdecl CIrcProto::GCMenuHook(WPARAM, LPARAM lParam) int __cdecl CIrcProto::OnPreShutdown(WPARAM, LPARAM)
{
- EnterCriticalSection(&cs);
+ mir_cslock lock(cs);
if (m_perform && IsConnected())
if (DoPerform("Event: Disconnect"))
@@ -922,8 +921,6 @@ int __cdecl CIrcProto::OnPreShutdown(WPARAM, LPARAM) m_nickDlg->Close();
if (m_joinDlg)
m_joinDlg->Close();
-
- LeaveCriticalSection(&cs);
return 0;
}
@@ -1011,9 +1008,10 @@ void __cdecl CIrcProto::ConnectServerThread(void*) nickflag = true;
ProtoBroadcastAck(NULL, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)Temp, ID_STATUS_CONNECTING);
Sleep(100);
- EnterCriticalSection(&cs);
- Connect(si);
- LeaveCriticalSection(&cs);
+ {
+ mir_cslock lock(cs);
+ Connect(si);
+ }
if (IsConnected()) {
if (m_mySpecifiedHost[0])
ForkThread(&CIrcProto::ResolveIPThread, new IPRESOLVE(m_mySpecifiedHost, IP_MANUAL));
diff --git a/protocols/IRCG/src/tools.cpp b/protocols/IRCG/src/tools.cpp index 5d07573291..8046eebf0d 100644 --- a/protocols/IRCG/src/tools.cpp +++ b/protocols/IRCG/src/tools.cpp @@ -513,34 +513,21 @@ static int CompareTimers(const TimerPair* p1, const TimerPair* p2) }
static OBJLIST<TimerPair> timers(10, CompareTimers);
-static CRITICAL_SECTION timers_cs;
-
-void InitTimers(void)
-{
- InitializeCriticalSection(&timers_cs);
-}
+static mir_cs timers_cs;
void UninitTimers(void)
{
- EnterCriticalSection(&timers_cs);
+ mir_cslock lck(timers_cs);
timers.destroy();
- LeaveCriticalSection(&timers_cs);
- DeleteCriticalSection(&timers_cs);
}
CIrcProto* GetTimerOwner(UINT_PTR nIDEvent)
{
- CIrcProto* result;
+ mir_cslock lck(timers_cs);
- EnterCriticalSection(&timers_cs);
TimerPair temp(NULL, nIDEvent);
int idx = timers.getIndex(&temp);
- if (idx == -1)
- result = NULL;
- else
- result = timers[idx].ppro;
- LeaveCriticalSection(&timers_cs);
- return result;
+ return (idx == -1) ? NULL : timers[idx].ppro;
}
void CIrcProto::SetChatTimer(UINT_PTR &nIDEvent, UINT uElapse, TIMERPROC lpTimerFunc)
@@ -550,25 +537,24 @@ void CIrcProto::SetChatTimer(UINT_PTR &nIDEvent, UINT uElapse, TIMERPROC lpTimer nIDEvent = SetTimer(NULL, NULL, uElapse, lpTimerFunc);
- EnterCriticalSection(&timers_cs);
+ mir_cslock lck(timers_cs);
timers.insert(new TimerPair(this, nIDEvent));
- LeaveCriticalSection(&timers_cs);
}
void CIrcProto::KillChatTimer(UINT_PTR &nIDEvent)
{
- if (nIDEvent) {
- EnterCriticalSection(&timers_cs);
+ if (nIDEvent == 0)
+ return;
+ {
+ mir_cslock lck(timers_cs);
TimerPair temp(this, nIDEvent);
int idx = timers.getIndex(&temp);
if (idx != -1)
timers.remove(idx);
-
- LeaveCriticalSection(&timers_cs);
-
- KillTimer(NULL, nIDEvent);
- nIDEvent = NULL;
}
+
+ KillTimer(NULL, nIDEvent);
+ nIDEvent = NULL;
}
/////////////////////////////////////////////////////////////////////////////////////////
|