From b72fb21b07d5f6543a7fbc898a01f2319298d4b9 Mon Sep 17 00:00:00 2001 From: Pontus Fransson Date: Mon, 9 Dec 2013 10:48:43 +0100 Subject: [PATCH] Network - Fixed all thread related crashes. The crashes was caused by OysterMutex, so i changed all OysterMutex to std::mutex instead. --- Code/Misc/ThreadSafeQueue.h | 36 +++++++++---------- Code/Misc/Utilities.h | 4 +-- Code/Network/NetworkDependencies/Listener.cpp | 12 ++++--- Code/Network/NetworkDependencies/Listener.h | 1 + .../NetworkDependencies/OysterByte.cpp | 2 +- .../NetworkDependencies/ThreadedClient.cpp | 29 ++++++--------- .../NetworkDependencies/ThreadedClient.h | 6 ++-- .../OysterNetworkClient/ClientMain.cpp | 1 + .../OysterNetworkServer/ServerMain.cpp | 6 ++++ 9 files changed, 52 insertions(+), 45 deletions(-) diff --git a/Code/Misc/ThreadSafeQueue.h b/Code/Misc/ThreadSafeQueue.h index b26d35ff..eedd9fd9 100644 --- a/Code/Misc/ThreadSafeQueue.h +++ b/Code/Misc/ThreadSafeQueue.h @@ -46,7 +46,7 @@ namespace Oyster Node *front; Node *back; int nrOfNodes; - OysterMutex mutex; + std::mutex stdMutex; }; @@ -68,7 +68,7 @@ namespace Oyster template < typename Type > ThreadSafeQueue::~ThreadSafeQueue() { - this->mutex.LockMutex(); + stdMutex.lock(); if(this->front != NULL) { @@ -87,14 +87,14 @@ namespace Oyster this->back = NULL; } - this->mutex.UnlockMutex(); + stdMutex.unlock(); } template < typename Type > void ThreadSafeQueue::Push(Type item) { - mutex.LockMutex(); + stdMutex.lock(); Node *e = new Node(item); if(this->front != NULL) @@ -111,13 +111,13 @@ namespace Oyster this->nrOfNodes++; - mutex.UnlockMutex(); + stdMutex.unlock(); } template < typename Type > Type ThreadSafeQueue::Pop() { - mutex.LockMutex(); + stdMutex.lock(); Type item = this->front->item; Node *destroyer = this->front; @@ -132,16 +132,16 @@ namespace Oyster this->back = NULL; } - mutex.UnlockMutex(); + stdMutex.unlock(); return item; } template < typename Type > Type ThreadSafeQueue::Front() { - mutex.LockMutex(); + stdMutex.lock(); Type temp = this->front->item; - mutex.UnlockMutex(); + stdMutex.unlock(); return temp; @@ -150,9 +150,9 @@ namespace Oyster template < typename Type > Type ThreadSafeQueue::Back() { - mutex.LockMutex(); + stdMutex.lock(); Type temp = this->back->item; - mutex.UnlockMutex(); + stdMutex.unlock(); return temp; @@ -161,9 +161,9 @@ namespace Oyster template < typename Type > int ThreadSafeQueue::Size() { - mutex.LockMutex(); + stdMutex.lock(); int size = this->nrOfNodes; - mutex.UnlockMutex(); + stdMutex.unlock(); return size; @@ -172,16 +172,16 @@ namespace Oyster template < typename Type > bool ThreadSafeQueue::IsEmpty() { - mutex.LockMutex(); + stdMutex.lock(); if(nrOfNodes == 0 || this->front == NULL) { - mutex.UnlockMutex(); + stdMutex.unlock(); return true; } else { - mutex.UnlockMutex(); + stdMutex.unlock(); } return false; @@ -190,7 +190,7 @@ namespace Oyster template < typename Type > void ThreadSafeQueue::Swap(IQueue &queue ) { - mutex.LockMutex(); + stdMutex.lock(); int prevNrOfNodes = this->nrOfNodes; int size = queue.Size(); @@ -203,7 +203,7 @@ namespace Oyster { queue.Push(this->Pop()); } - mutex.UnlockMutex(); + stdMutex.unlock(); } diff --git a/Code/Misc/Utilities.h b/Code/Misc/Utilities.h index ec8b0229..a8e4b406 100644 --- a/Code/Misc/Utilities.h +++ b/Code/Misc/Utilities.h @@ -201,7 +201,6 @@ namespace Utility T *_ptr; /** Destroys the pointer and returns the memory allocated. */ - void Destroy(); public: SmartPointer(); @@ -216,7 +215,8 @@ namespace Utility T* operator-> (); operator T* (); operator bool(); - + + void Destroy(); /** * Returns the connected pointer */ diff --git a/Code/Network/NetworkDependencies/Listener.cpp b/Code/Network/NetworkDependencies/Listener.cpp index 6a9fbeb5..d606df10 100644 --- a/Code/Network/NetworkDependencies/Listener.cpp +++ b/Code/Network/NetworkDependencies/Listener.cpp @@ -60,9 +60,11 @@ void Listener::Shutdown() void Listener::SetPostBox(Oyster::Network::IPostBox>* postBox) { - mutex.LockMutex(); + stdMutex.lock(); + //mutex.LockMutex(); this->postBox = postBox; - mutex.UnlockMutex(); + //mutex.UnlockMutex(); + stdMutex.unlock(); } int Listener::Accept() @@ -72,9 +74,11 @@ int Listener::Accept() if(*clientSocket != -1) { - mutex.LockMutex(); + stdMutex.lock(); + //mutex.LockMutex(); postBox->PostMessage(clientSocket); - mutex.UnlockMutex(); + //mutex.UnlockMutex(); + stdMutex.unlock(); } return clientSocket; diff --git a/Code/Network/NetworkDependencies/Listener.h b/Code/Network/NetworkDependencies/Listener.h index 16b884dd..abf57ba9 100644 --- a/Code/Network/NetworkDependencies/Listener.h +++ b/Code/Network/NetworkDependencies/Listener.h @@ -48,6 +48,7 @@ namespace Oyster ::Oyster::Thread::OysterThread thread; OysterMutex mutex; + std::mutex stdMutex; IPostBox>* postBox; diff --git a/Code/Network/NetworkDependencies/OysterByte.cpp b/Code/Network/NetworkDependencies/OysterByte.cpp index 0ae7bf96..a0883604 100644 --- a/Code/Network/NetworkDependencies/OysterByte.cpp +++ b/Code/Network/NetworkDependencies/OysterByte.cpp @@ -18,7 +18,7 @@ OysterByte::OysterByte(int cap) OysterByte::OysterByte(const OysterByte& obj) { - delete[] this->byteArray; + //delete[] this->byteArray; this->byteArray = new unsigned char[obj.capacity]; for(int i = 0; i < obj.size; i++) diff --git a/Code/Network/NetworkDependencies/ThreadedClient.cpp b/Code/Network/NetworkDependencies/ThreadedClient.cpp index ba6b2c3b..a49caabd 100644 --- a/Code/Network/NetworkDependencies/ThreadedClient.cpp +++ b/Code/Network/NetworkDependencies/ThreadedClient.cpp @@ -51,46 +51,37 @@ ThreadedClient::~ThreadedClient() } } -int ThreadedClient::Send(SmartPointer& byte) +void ThreadedClient::Send(SmartPointer& byte) { - SmartPointer temp = new OysterByte(*byte); - - mutex.LockMutex(); - this->sendPostBox->PostMessage(temp); - mutex.UnlockMutex(); - - return 0; + this->sendPostBox->PostMessage(byte); } int ThreadedClient::Send() { int errorCode = 0; - mutex.LockMutex(); if(sendPostBox->IsFull()) { SmartPointer temp = new OysterByte; sendPostBox->FetchMessage(temp); errorCode = this->connection->Send(temp); } - mutex.UnlockMutex(); - return errorCode; } int ThreadedClient::Recv() { - int errorCode = 0; - + int errorCode = -1; + SmartPointer temp = new OysterByte; errorCode = this->connection->Recieve(temp); if(errorCode == 0) { - mutex.LockMutex(); + stdMutex.lock(); recvPostBox->PostMessage(temp); - mutex.UnlockMutex(); + stdMutex.unlock(); } return errorCode; @@ -106,6 +97,8 @@ void ThreadedClient::ThreadExit() std::cout << "Client Thread exit" << std::endl; } +#include + bool ThreadedClient::DoWork() { int errorCode; @@ -120,7 +113,7 @@ bool ThreadedClient::DoWork() { return false; }*/ - + Sleep(1); return true; } @@ -144,7 +137,7 @@ int ThreadedClient::Connect(unsigned short port, const char serverName[]) void ThreadedClient::setRecvPostBox(IPostBox> *postBox) { - this->mutex.LockMutex(); + stdMutex.lock(); this->recvPostBox = postBox; - this->mutex.UnlockMutex(); + stdMutex.unlock(); } \ No newline at end of file diff --git a/Code/Network/NetworkDependencies/ThreadedClient.h b/Code/Network/NetworkDependencies/ThreadedClient.h index 3d12ab6f..ae026a61 100644 --- a/Code/Network/NetworkDependencies/ThreadedClient.h +++ b/Code/Network/NetworkDependencies/ThreadedClient.h @@ -12,6 +12,8 @@ #include "../../Misc/Thread/OysterMutex.h" #include "../../Misc/Utilities.h" +#include + namespace Oyster { namespace Network @@ -25,7 +27,7 @@ namespace Oyster ThreadedClient(IPostBox> *postBox, unsigned int socket); virtual ~ThreadedClient(); - int Send(Utility::DynamicMemory::SmartPointer& byte); + void Send(Utility::DynamicMemory::SmartPointer& byte); int Connect(unsigned short port, const char serverName[]); @@ -44,7 +46,7 @@ namespace Oyster IPostBox> *sendPostBox; IPostBox> *recvPostBox; Oyster::Thread::OysterThread thread; - OysterMutex mutex; + std::mutex stdMutex; }; } diff --git a/Code/Network/OysterNetworkClient/ClientMain.cpp b/Code/Network/OysterNetworkClient/ClientMain.cpp index 95f966b8..72c011a2 100644 --- a/Code/Network/OysterNetworkClient/ClientMain.cpp +++ b/Code/Network/OysterNetworkClient/ClientMain.cpp @@ -95,6 +95,7 @@ void chat(ThreadedClient &client) timer.reset(); client.Send(msgSend); } + Sleep(1); } delete postBox; diff --git a/Code/Network/OysterNetworkServer/ServerMain.cpp b/Code/Network/OysterNetworkServer/ServerMain.cpp index ced434ff..9564a024 100644 --- a/Code/Network/OysterNetworkServer/ServerMain.cpp +++ b/Code/Network/OysterNetworkServer/ServerMain.cpp @@ -76,6 +76,12 @@ int main() t.Pack(test, sendBuffer); WinTimer timer; + + /* DEBUGGING: Connect 25 clients + for(int i = 0; i < 25; i++) + { + clients.push_back(new ThreadedClient(recvPostBox, 1)); + }*/ SmartPointer client = int(); while(1)