Network - Fixed all thread related crashes.

The crashes was caused by OysterMutex, so i changed all OysterMutex to
std::mutex instead.
This commit is contained in:
Pontus Fransson 2013-12-09 10:48:43 +01:00
parent a79dc61159
commit b72fb21b07
9 changed files with 52 additions and 45 deletions

View File

@ -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<Type>::~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<Type>::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<Type>::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<Type>::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<Type>::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<Type>::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<Type>::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<Type>::Swap(IQueue<Type> &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();
}

View File

@ -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
*/

View File

@ -60,9 +60,11 @@ void Listener::Shutdown()
void Listener::SetPostBox(Oyster::Network::IPostBox<SmartPointer<int>>* 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;

View File

@ -48,6 +48,7 @@ namespace Oyster
::Oyster::Thread::OysterThread thread;
OysterMutex mutex;
std::mutex stdMutex;
IPostBox<Utility::DynamicMemory::SmartPointer<int>>* postBox;

View File

@ -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++)

View File

@ -51,46 +51,37 @@ ThreadedClient::~ThreadedClient()
}
}
int ThreadedClient::Send(SmartPointer<OysterByte>& byte)
void ThreadedClient::Send(SmartPointer<OysterByte>& byte)
{
SmartPointer<OysterByte> 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<OysterByte> 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<OysterByte> 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 <Windows.h>
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<SmartPointer<OysterByte>> *postBox)
{
this->mutex.LockMutex();
stdMutex.lock();
this->recvPostBox = postBox;
this->mutex.UnlockMutex();
stdMutex.unlock();
}

View File

@ -12,6 +12,8 @@
#include "../../Misc/Thread/OysterMutex.h"
#include "../../Misc/Utilities.h"
#include <mutex>
namespace Oyster
{
namespace Network
@ -25,7 +27,7 @@ namespace Oyster
ThreadedClient(IPostBox<Utility::DynamicMemory::SmartPointer<OysterByte>> *postBox, unsigned int socket);
virtual ~ThreadedClient();
int Send(Utility::DynamicMemory::SmartPointer<OysterByte>& byte);
void Send(Utility::DynamicMemory::SmartPointer<OysterByte>& byte);
int Connect(unsigned short port, const char serverName[]);
@ -44,7 +46,7 @@ namespace Oyster
IPostBox<Utility::DynamicMemory::SmartPointer<OysterByte>> *sendPostBox;
IPostBox<Utility::DynamicMemory::SmartPointer<OysterByte>> *recvPostBox;
Oyster::Thread::OysterThread thread;
OysterMutex mutex;
std::mutex stdMutex;
};
}

View File

@ -95,6 +95,7 @@ void chat(ThreadedClient &client)
timer.reset();
client.Send(msgSend);
}
Sleep(1);
}
delete postBox;

View File

@ -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<int> client = int();
while(1)