From 15d4346a1e3ada4bd14739b63fc239109dc96a0c Mon Sep 17 00:00:00 2001
From: George Hazan <george.hazan@gmail.com>
Date: Thu, 30 Apr 2015 18:47:28 +0000
Subject: custom critical section class replaced with mir_cs/mir_cslock

git-svn-id: http://svn.miranda-ng.org/main/trunk@13300 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
---
 plugins/Boltun/src/Engine/CriticalSection.h | 54 -----------------------------
 plugins/Boltun/src/Engine/PerContactData.h  | 36 ++++++-------------
 plugins/Boltun/src/Engine/TalkEngine.cpp    | 11 +++---
 plugins/Boltun/src/Engine/TalkEngine.h      |  2 ++
 plugins/Boltun/src/Engine/ValueChooser.h    |  3 ++
 plugins/Boltun/src/actionQueue.cpp          | 41 ++++++++++------------
 plugins/Boltun/src/config.h                 |  8 +++++
 7 files changed, 47 insertions(+), 108 deletions(-)
 delete mode 100644 plugins/Boltun/src/Engine/CriticalSection.h

(limited to 'plugins')

diff --git a/plugins/Boltun/src/Engine/CriticalSection.h b/plugins/Boltun/src/Engine/CriticalSection.h
deleted file mode 100644
index 550aa32696..0000000000
--- a/plugins/Boltun/src/Engine/CriticalSection.h
+++ /dev/null
@@ -1,54 +0,0 @@
-//***********************************************************
-//	Copyright � 2008 Valentin Pavlyuchenko
-//
-//	This file is part of Boltun.
-//
-//    Boltun is free software: you can redistribute it and/or modify
-//    it under the terms of the GNU General Public License as published by
-//    the Free Software Foundation, either version 2 of the License, or
-//    (at your option) any later version.
-//
-//    Boltun is distributed in the hope that it will be useful,
-//    but WITHOUT ANY WARRANTY; without even the implied warranty of
-//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-//    GNU General Public License for more details.
-//
-//    You should have received a copy of the GNU General Public License
-//	  along with Boltun. If not, see <http://www.gnu.org/licenses/>.
-//
-//***********************************************************
-
-#ifndef CriticalSectionH
-#define CriticalSectionH
-
-class CriticalSection
-{
-	CRITICAL_SECTION csQueue;
-public:
-	inline CriticalSection()
-	{
-		InitializeCriticalSection(&csQueue);
-	}
-
-	inline ~CriticalSection()
-	{
-		DeleteCriticalSection(&csQueue);
-	}
-
-	inline void Enter()
-	{
-		EnterCriticalSection(&csQueue);
-	}
-
-	inline void Leave()
-	{
-		LeaveCriticalSection(&csQueue);
-	}
-
-	inline bool TryEnter()
-	{
-		return TryEnterCriticalSection(&csQueue) != 0;
-	}
-};
-
-#endif /* CriticalSectionH */
\ No newline at end of file
diff --git a/plugins/Boltun/src/Engine/PerContactData.h b/plugins/Boltun/src/Engine/PerContactData.h
index e81192053f..75bc321b66 100644
--- a/plugins/Boltun/src/Engine/PerContactData.h
+++ b/plugins/Boltun/src/Engine/PerContactData.h
@@ -21,17 +21,16 @@
 #ifndef PerContactDataH
 #define PerContactDataH
 
-#include "CriticalSection.h"
-
 static std::map<unsigned, void*> perContactDataObjects;
 
 template <class Source, class Data, class ContactHandle>
 class PerContactData
 {
+	PerContactData& operator=(const PerContactData&);
+
 	template <class Source, class Data>
 	struct InternalData
 	{
-		CriticalSection lock;
 		Data *data;
 		time_t time;
 		inline InternalData(const Source& src)
@@ -51,7 +50,7 @@ class PerContactData
 			delete data;
 		}
 	};
-	CriticalSection mapLock;
+	mir_cs mapLock;
 	unsigned timerID;
 	std::map<ContactHandle, InternalData<Source, Data>* > datas;
 	typedef typename std::map<ContactHandle, InternalData<Source, Data>* >::iterator mapIt;
@@ -59,6 +58,7 @@ class PerContactData
 	void CleanupData();
 	template <class Source, class Data, class ContactHandle>
 	friend VOID CALLBACK RunTimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime);
+
 public:
 	PerContactData(const Source& src);
 	~PerContactData();
@@ -75,7 +75,7 @@ PerContactData<Source, Data, ContactHandle>::PerContactData(const Source& src)
 template <class Source, class Data, class ContactHandle>
 PerContactData<Source, Data, ContactHandle>::~PerContactData()
 {
-	mapLock.Enter();
+	mir_cslock mlck(mapLock);
 	if (timerID)
 	{
 		KillTimer(NULL, timerID);
@@ -83,40 +83,26 @@ PerContactData<Source, Data, ContactHandle>::~PerContactData()
 	}
 	while (!datas.empty())
 	{
-		while (!(*datas.begin()).second->lock.TryEnter())
-		{
-			mapLock.Leave();
-			Sleep(10);
-			mapLock.Enter();
-		}
-		//Now we know exactly that no-one onws a contact lock
-		InternalData<Source, Data>* data = (*datas.begin()).second;
-		data->lock.Leave();
-		delete data;
+		delete (*datas.begin()).second;
 		datas.erase(datas.begin());
 	}
-	mapLock.Leave();
 }
 
 template <class Source, class Data, class ContactHandle>
 Data* PerContactData<Source, Data, ContactHandle>::GetData(ContactHandle Contact)
 {
-	mapLock.Enter();
+	mir_cslock mlck(mapLock);
 	mapIt it;
 	if ((it = datas.find(Contact)) == datas.end())
 		it = datas.insert(make_pair(Contact, new InternalData<Source, Data>(source))).first;
-	(*it).second->lock.Enter();
 	(*it).second->time = 0;
-	Data* data = (*it).second->data;
-	mapLock.Leave();
-	return data;
+	return (*it).second->data;
 }
 
 template <class Source, class Data, class ContactHandle>
 void PerContactData<Source, Data, ContactHandle>::PutData(ContactHandle Contact)
 {
-	mapLock.Enter();
-	datas[Contact]->lock.Leave();
+	mir_cslock mlck(mapLock);
 	::time(&(datas[Contact]->time));
 	if (!timerID)
 	{
@@ -124,13 +110,12 @@ void PerContactData<Source, Data, ContactHandle>::PutData(ContactHandle Contact)
 		assert(timerID);
 		perContactDataObjects[timerID] = this;
 	}
-	mapLock.Leave();
 }
 
 template <class Source, class Data, class ContactHandle>
 void PerContactData<Source, Data, ContactHandle>::CleanupData()
 {
-	mapLock.Enter();
+	mir_cslock mlck(mapLock);
 	time_t now;
 	time(&now);
 	for (mapIt it = datas.begin(); it != datas.end();)
@@ -156,7 +141,6 @@ void PerContactData<Source, Data, ContactHandle>::CleanupData()
 		KillTimer(NULL, timerID);
 		perContactDataObjects.erase(timerID);
 	}
-	mapLock.Leave();
 }
 
 template <class Source, class Data, class ContactHandle>
diff --git a/plugins/Boltun/src/Engine/TalkEngine.cpp b/plugins/Boltun/src/Engine/TalkEngine.cpp
index 0391c11112..8d4efbf476 100644
--- a/plugins/Boltun/src/Engine/TalkEngine.cpp
+++ b/plugins/Boltun/src/Engine/TalkEngine.cpp
@@ -97,8 +97,7 @@ tstring TalkBot::ReplaceAliases(const tstring &message)
 	const TCHAR dividers[] = _T(" \t\n\r,./?\\|;:'\"~!#^&*()_-+=[{]}�\1");
 	tstring sentence = message;
 	tstring result;
-	int len = (int)sentence.length();
-	map<int, tstring> sm;
+	map<size_t, tstring> sm;
 	//Find smiles
 	for (size_t i = 0; i < sentence.length() - 1; i++)
 	{
@@ -116,7 +115,7 @@ tstring TalkBot::ReplaceAliases(const tstring &message)
 			}
 		}
 	}
-	len = (int)sentence.length();
+	int len = (int)sentence.length();
 	bool hadQuestionSigns = false;
 	int it = 0;
 	while (it != len)
@@ -125,7 +124,7 @@ tstring TalkBot::ReplaceAliases(const tstring &message)
 		{
 			if (sentence[it] == _T('?'))
 				hadQuestionSigns = true;
-			map<int, tstring>::iterator smit;
+			map<size_t, tstring>::iterator smit;
 			if (sentence[it] == '\1')
 			{
 				smit = sm.find(it);
@@ -515,7 +514,7 @@ void TalkBot::SplitAndSortWords(tstring sentence, vector<tstring>& keywords,
 	const TCHAR dividers[] = _T(" \t\n\r,./?\\|;:'\"~!#^&*()_-+=[{]}�");
 	int len = (int)sentence.length();
 	vector<tstring> words;
-	map<int, tstring> sm;
+	map<size_t, tstring> sm;
 	//Find smiles
 	for (size_t i = 0; i < sentence.length() - 1; i++)
 	{
@@ -543,7 +542,7 @@ void TalkBot::SplitAndSortWords(tstring sentence, vector<tstring>& keywords,
 		{
 			if (sentence[it] == _T('?'))
 				hadQuestionSigns = true;
-			map<int, tstring>::iterator smit;
+			map<size_t, tstring>::iterator smit;
 			if (_istspace(sentence[it]) && (smit = sm.find(it)) != sm.end())
 				words.push_back((*smit).second);
 			it++;
diff --git a/plugins/Boltun/src/Engine/TalkEngine.h b/plugins/Boltun/src/Engine/TalkEngine.h
index 6f95541941..c18f3df1c1 100644
--- a/plugins/Boltun/src/Engine/TalkEngine.h
+++ b/plugins/Boltun/src/Engine/TalkEngine.h
@@ -28,6 +28,8 @@
 
 class TalkBot
 {
+	TalkBot& operator=(const TalkBot&);
+
 public:
 	struct MessageInfo
 	{
diff --git a/plugins/Boltun/src/Engine/ValueChooser.h b/plugins/Boltun/src/Engine/ValueChooser.h
index 10256a5b70..0018ac8ff2 100644
--- a/plugins/Boltun/src/Engine/ValueChooser.h
+++ b/plugins/Boltun/src/Engine/ValueChooser.h
@@ -29,6 +29,9 @@ private:
 	bool *numbers;
 	const container data;
 	bool notifyOnReset;
+
+	ValueChooser& operator=(const ValueChooser&);
+
 public:
 	ValueChooser(const container& vec, bool NotifyOnReset = false)
 		:data(vec), notifyOnReset(NotifyOnReset)
diff --git a/plugins/Boltun/src/actionQueue.cpp b/plugins/Boltun/src/actionQueue.cpp
index 8625d3bde0..f9d2166001 100644
--- a/plugins/Boltun/src/actionQueue.cpp
+++ b/plugins/Boltun/src/actionQueue.cpp
@@ -47,18 +47,19 @@ static list<QueueElement> actionQueue;
 static set<MCONTACT> typingContacts;
 UINT_PTR timerID = 0;
 
-CriticalSection cs;
-CriticalSection typingContactsLock;
+mir_cs cs;
+mir_cs typingContactsLock;
 
 void UpdateTimer();
 
 VOID CALLBACK TimerProc(HWND, UINT, UINT_PTR, DWORD)
 {
-	cs.Enter();
+	mir_cslockfull lck(cs);
 	QueueElement q = actionQueue.front();
 	actionQueue.pop_front();
 	UpdateTimer();
-	cs.Leave();
+	lck.unlock();
+
 	q.Handler(q.hContact, q.inf);
 }
 
@@ -90,8 +91,7 @@ static void TimerAnswer(MCONTACT hContact, const TalkBot::MessageInfo* info)
 	bufsize *= sizeof(TCHAR) + 1;
 	msg = new char[bufsize];
 
-	if (!WideCharToMultiByte(CP_ACP, 0, info->Answer.c_str(), -1, msg, size,
-		NULL, NULL))
+	if (!WideCharToMultiByte(CP_ACP, 0, info->Answer.c_str(), -1, msg, (int)size, NULL, NULL))
 		memset(msg, '-', (size - 1)); //In case of fault return "----" in ANSI part
 	memcpy(msg + size, info->Answer.c_str(), size * 2);
 
@@ -100,7 +100,7 @@ static void TimerAnswer(MCONTACT hContact, const TalkBot::MessageInfo* info)
 	memset(&ldbei, 0, sizeof(ldbei));
 	ldbei.cbSize = sizeof(ldbei);
 	//FIXME: Error may happen
-	ldbei.cbBlob = bufsize;
+	ldbei.cbBlob = (int)bufsize;
 	ldbei.pBlob = (PBYTE)(void*)msg;
 	ldbei.eventType = EVENTTYPE_MESSAGE;
 	ldbei.flags = DBEF_SENT;
@@ -113,18 +113,16 @@ static void TimerAnswer(MCONTACT hContact, const TalkBot::MessageInfo* info)
 
 	delete[] msg;
 
-	typingContactsLock.Enter();
+	mir_cslock lck(typingContactsLock);
 	typingContacts.erase(hContact);
-	typingContactsLock.Leave();
 }
 
 static void StartTyping(MCONTACT hContact, const TalkBot::MessageInfo*)
 {
-	CallService(MS_PROTO_SELFISTYPING, hContact,
-		(LPARAM)PROTOTYPE_SELFTYPING_ON);
-	typingContactsLock.Enter();
+	CallService(MS_PROTO_SELFISTYPING, hContact, PROTOTYPE_SELFTYPING_ON);
+
+	mir_cslock lck(typingContactsLock);
 	typingContacts.insert(hContact);
-	typingContactsLock.Leave();
 }
 
 void DoAnswer(MCONTACT hContact, const TalkBot::MessageInfo *info, bool sticky = false)
@@ -156,15 +154,14 @@ void DoAnswer(MCONTACT hContact, const TalkBot::MessageInfo *info, bool sticky =
 			thinkTime = thinkTime * (rand() % 300) / 100 + thinkTime;
 		}
 	}
-	cs.Enter();
+	
+	mir_cslock lck(cs);
 	//Check if this contact's timer handler is now waiting for a cs.
 	bool needTimerRearrange = false;
 	if (!actionQueue.empty() && actionQueue.front().hContact == hContact)
 	{
 		needTimerRearrange = true;
 		KillTimer(NULL, timerID);
-		cs.Leave();
-		cs.Enter();
 	}
 	if (!actionQueue.empty())
 	{
@@ -189,13 +186,14 @@ void DoAnswer(MCONTACT hContact, const TalkBot::MessageInfo *info, bool sticky =
 			--it;
 		}
 	}
-	typingContactsLock.Enter();
-	if (typingContacts.find(hContact) != typingContacts.end())
 	{
-		CallService(MS_PROTO_SELFISTYPING, hContact, (LPARAM)PROTOTYPE_SELFTYPING_OFF);
-		typingContacts.erase(hContact);
+		mir_cslock tcl(typingContactsLock);
+		if (typingContacts.find(hContact) != typingContacts.end())
+		{
+			CallService(MS_PROTO_SELFISTYPING, hContact, (LPARAM)PROTOTYPE_SELFTYPING_OFF);
+			typingContacts.erase(hContact);
+		}
 	}
-	typingContactsLock.Leave();
 	if (actionQueue.empty())
 		needTimerRearrange = true;
 	if (thinkTime)
@@ -203,7 +201,6 @@ void DoAnswer(MCONTACT hContact, const TalkBot::MessageInfo *info, bool sticky =
 	actionQueue.push_back(QueueElement(hContact, TimerAnswer, waitTime, info, sticky));
 	if (needTimerRearrange)
 		UpdateTimer();
-	cs.Leave();
 }
 
 void AnswerToContact(MCONTACT hContact, const TCHAR* messageToAnswer)
diff --git a/plugins/Boltun/src/config.h b/plugins/Boltun/src/config.h
index a1cd72c7de..3cee967d4f 100644
--- a/plugins/Boltun/src/config.h
+++ b/plugins/Boltun/src/config.h
@@ -29,12 +29,16 @@ class Property
 public:
 	typedef const T(__thiscall BaseClass::*Getter)();
 	typedef const T(__thiscall BaseClass::*Setter)(const T);
+
 private:
 	const Getter getter;
 	const Setter setter;
 	BaseClass* owner;
 	bool cacheValid;
 	T cached;
+
+	Property& operator=(const Property&);
+
 public:
 	Property(Getter g, Setter s)
 		:getter(g), setter(s), cacheValid(false)
@@ -68,12 +72,16 @@ class PtrProperty
 public:
 	typedef const T* (__thiscall BaseClass::*Getter)();
 	typedef const T* (__thiscall BaseClass::*Setter)(const T*);
+
 private:
 	const Getter getter;
 	const Setter setter;
 	BaseClass* owner;
 	bool cacheValid;
 	const T* cached;
+
+	PtrProperty& operator=(const PtrProperty&);
+
 public:
 	PtrProperty(Getter g, Setter s)
 		:getter(g), setter(s), cacheValid(false), cached(NULL)
-- 
cgit v1.2.3