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:
parent
a79dc61159
commit
b72fb21b07
|
@ -46,7 +46,7 @@ namespace Oyster
|
||||||
Node *front;
|
Node *front;
|
||||||
Node *back;
|
Node *back;
|
||||||
int nrOfNodes;
|
int nrOfNodes;
|
||||||
OysterMutex mutex;
|
std::mutex stdMutex;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -68,7 +68,7 @@ namespace Oyster
|
||||||
template < typename Type >
|
template < typename Type >
|
||||||
ThreadSafeQueue<Type>::~ThreadSafeQueue()
|
ThreadSafeQueue<Type>::~ThreadSafeQueue()
|
||||||
{
|
{
|
||||||
this->mutex.LockMutex();
|
stdMutex.lock();
|
||||||
|
|
||||||
if(this->front != NULL)
|
if(this->front != NULL)
|
||||||
{
|
{
|
||||||
|
@ -87,14 +87,14 @@ namespace Oyster
|
||||||
this->back = NULL;
|
this->back = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
this->mutex.UnlockMutex();
|
stdMutex.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template < typename Type >
|
template < typename Type >
|
||||||
void ThreadSafeQueue<Type>::Push(Type item)
|
void ThreadSafeQueue<Type>::Push(Type item)
|
||||||
{
|
{
|
||||||
mutex.LockMutex();
|
stdMutex.lock();
|
||||||
Node *e = new Node(item);
|
Node *e = new Node(item);
|
||||||
|
|
||||||
if(this->front != NULL)
|
if(this->front != NULL)
|
||||||
|
@ -111,13 +111,13 @@ namespace Oyster
|
||||||
|
|
||||||
this->nrOfNodes++;
|
this->nrOfNodes++;
|
||||||
|
|
||||||
mutex.UnlockMutex();
|
stdMutex.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
template < typename Type >
|
template < typename Type >
|
||||||
Type ThreadSafeQueue<Type>::Pop()
|
Type ThreadSafeQueue<Type>::Pop()
|
||||||
{
|
{
|
||||||
mutex.LockMutex();
|
stdMutex.lock();
|
||||||
|
|
||||||
Type item = this->front->item;
|
Type item = this->front->item;
|
||||||
Node *destroyer = this->front;
|
Node *destroyer = this->front;
|
||||||
|
@ -132,16 +132,16 @@ namespace Oyster
|
||||||
this->back = NULL;
|
this->back = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
mutex.UnlockMutex();
|
stdMutex.unlock();
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
|
|
||||||
template < typename Type >
|
template < typename Type >
|
||||||
Type ThreadSafeQueue<Type>::Front()
|
Type ThreadSafeQueue<Type>::Front()
|
||||||
{
|
{
|
||||||
mutex.LockMutex();
|
stdMutex.lock();
|
||||||
Type temp = this->front->item;
|
Type temp = this->front->item;
|
||||||
mutex.UnlockMutex();
|
stdMutex.unlock();
|
||||||
|
|
||||||
return temp;
|
return temp;
|
||||||
|
|
||||||
|
@ -150,9 +150,9 @@ namespace Oyster
|
||||||
template < typename Type >
|
template < typename Type >
|
||||||
Type ThreadSafeQueue<Type>::Back()
|
Type ThreadSafeQueue<Type>::Back()
|
||||||
{
|
{
|
||||||
mutex.LockMutex();
|
stdMutex.lock();
|
||||||
Type temp = this->back->item;
|
Type temp = this->back->item;
|
||||||
mutex.UnlockMutex();
|
stdMutex.unlock();
|
||||||
|
|
||||||
return temp;
|
return temp;
|
||||||
|
|
||||||
|
@ -161,9 +161,9 @@ namespace Oyster
|
||||||
template < typename Type >
|
template < typename Type >
|
||||||
int ThreadSafeQueue<Type>::Size()
|
int ThreadSafeQueue<Type>::Size()
|
||||||
{
|
{
|
||||||
mutex.LockMutex();
|
stdMutex.lock();
|
||||||
int size = this->nrOfNodes;
|
int size = this->nrOfNodes;
|
||||||
mutex.UnlockMutex();
|
stdMutex.unlock();
|
||||||
|
|
||||||
return size;
|
return size;
|
||||||
|
|
||||||
|
@ -172,16 +172,16 @@ namespace Oyster
|
||||||
template < typename Type >
|
template < typename Type >
|
||||||
bool ThreadSafeQueue<Type>::IsEmpty()
|
bool ThreadSafeQueue<Type>::IsEmpty()
|
||||||
{
|
{
|
||||||
mutex.LockMutex();
|
stdMutex.lock();
|
||||||
if(nrOfNodes == 0 || this->front == NULL)
|
if(nrOfNodes == 0 || this->front == NULL)
|
||||||
{
|
{
|
||||||
mutex.UnlockMutex();
|
stdMutex.unlock();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
mutex.UnlockMutex();
|
stdMutex.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
@ -190,7 +190,7 @@ namespace Oyster
|
||||||
template < typename Type >
|
template < typename Type >
|
||||||
void ThreadSafeQueue<Type>::Swap(IQueue<Type> &queue )
|
void ThreadSafeQueue<Type>::Swap(IQueue<Type> &queue )
|
||||||
{
|
{
|
||||||
mutex.LockMutex();
|
stdMutex.lock();
|
||||||
int prevNrOfNodes = this->nrOfNodes;
|
int prevNrOfNodes = this->nrOfNodes;
|
||||||
int size = queue.Size();
|
int size = queue.Size();
|
||||||
|
|
||||||
|
@ -203,7 +203,7 @@ namespace Oyster
|
||||||
{
|
{
|
||||||
queue.Push(this->Pop());
|
queue.Push(this->Pop());
|
||||||
}
|
}
|
||||||
mutex.UnlockMutex();
|
stdMutex.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -201,7 +201,6 @@ namespace Utility
|
||||||
T *_ptr;
|
T *_ptr;
|
||||||
|
|
||||||
/** Destroys the pointer and returns the memory allocated. */
|
/** Destroys the pointer and returns the memory allocated. */
|
||||||
void Destroy();
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
SmartPointer();
|
SmartPointer();
|
||||||
|
@ -216,7 +215,8 @@ namespace Utility
|
||||||
T* operator-> ();
|
T* operator-> ();
|
||||||
operator T* ();
|
operator T* ();
|
||||||
operator bool();
|
operator bool();
|
||||||
|
|
||||||
|
void Destroy();
|
||||||
/**
|
/**
|
||||||
* Returns the connected pointer
|
* Returns the connected pointer
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -60,9 +60,11 @@ void Listener::Shutdown()
|
||||||
|
|
||||||
void Listener::SetPostBox(Oyster::Network::IPostBox<SmartPointer<int>>* postBox)
|
void Listener::SetPostBox(Oyster::Network::IPostBox<SmartPointer<int>>* postBox)
|
||||||
{
|
{
|
||||||
mutex.LockMutex();
|
stdMutex.lock();
|
||||||
|
//mutex.LockMutex();
|
||||||
this->postBox = postBox;
|
this->postBox = postBox;
|
||||||
mutex.UnlockMutex();
|
//mutex.UnlockMutex();
|
||||||
|
stdMutex.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
int Listener::Accept()
|
int Listener::Accept()
|
||||||
|
@ -72,9 +74,11 @@ int Listener::Accept()
|
||||||
|
|
||||||
if(*clientSocket != -1)
|
if(*clientSocket != -1)
|
||||||
{
|
{
|
||||||
mutex.LockMutex();
|
stdMutex.lock();
|
||||||
|
//mutex.LockMutex();
|
||||||
postBox->PostMessage(clientSocket);
|
postBox->PostMessage(clientSocket);
|
||||||
mutex.UnlockMutex();
|
//mutex.UnlockMutex();
|
||||||
|
stdMutex.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
return clientSocket;
|
return clientSocket;
|
||||||
|
|
|
@ -48,6 +48,7 @@ namespace Oyster
|
||||||
|
|
||||||
::Oyster::Thread::OysterThread thread;
|
::Oyster::Thread::OysterThread thread;
|
||||||
OysterMutex mutex;
|
OysterMutex mutex;
|
||||||
|
std::mutex stdMutex;
|
||||||
|
|
||||||
IPostBox<Utility::DynamicMemory::SmartPointer<int>>* postBox;
|
IPostBox<Utility::DynamicMemory::SmartPointer<int>>* postBox;
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,7 @@ OysterByte::OysterByte(int cap)
|
||||||
|
|
||||||
OysterByte::OysterByte(const OysterByte& obj)
|
OysterByte::OysterByte(const OysterByte& obj)
|
||||||
{
|
{
|
||||||
delete[] this->byteArray;
|
//delete[] this->byteArray;
|
||||||
this->byteArray = new unsigned char[obj.capacity];
|
this->byteArray = new unsigned char[obj.capacity];
|
||||||
|
|
||||||
for(int i = 0; i < obj.size; i++)
|
for(int i = 0; i < obj.size; i++)
|
||||||
|
|
|
@ -51,46 +51,37 @@ ThreadedClient::~ThreadedClient()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int ThreadedClient::Send(SmartPointer<OysterByte>& byte)
|
void ThreadedClient::Send(SmartPointer<OysterByte>& byte)
|
||||||
{
|
{
|
||||||
SmartPointer<OysterByte> temp = new OysterByte(*byte);
|
this->sendPostBox->PostMessage(byte);
|
||||||
|
|
||||||
mutex.LockMutex();
|
|
||||||
this->sendPostBox->PostMessage(temp);
|
|
||||||
mutex.UnlockMutex();
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int ThreadedClient::Send()
|
int ThreadedClient::Send()
|
||||||
{
|
{
|
||||||
int errorCode = 0;
|
int errorCode = 0;
|
||||||
|
|
||||||
mutex.LockMutex();
|
|
||||||
if(sendPostBox->IsFull())
|
if(sendPostBox->IsFull())
|
||||||
{
|
{
|
||||||
SmartPointer<OysterByte> temp = new OysterByte;
|
SmartPointer<OysterByte> temp = new OysterByte;
|
||||||
sendPostBox->FetchMessage(temp);
|
sendPostBox->FetchMessage(temp);
|
||||||
errorCode = this->connection->Send(temp);
|
errorCode = this->connection->Send(temp);
|
||||||
}
|
}
|
||||||
mutex.UnlockMutex();
|
|
||||||
|
|
||||||
|
|
||||||
return errorCode;
|
return errorCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ThreadedClient::Recv()
|
int ThreadedClient::Recv()
|
||||||
{
|
{
|
||||||
int errorCode = 0;
|
int errorCode = -1;
|
||||||
|
|
||||||
SmartPointer<OysterByte> temp = new OysterByte;
|
SmartPointer<OysterByte> temp = new OysterByte;
|
||||||
errorCode = this->connection->Recieve(temp);
|
errorCode = this->connection->Recieve(temp);
|
||||||
|
|
||||||
if(errorCode == 0)
|
if(errorCode == 0)
|
||||||
{
|
{
|
||||||
mutex.LockMutex();
|
stdMutex.lock();
|
||||||
recvPostBox->PostMessage(temp);
|
recvPostBox->PostMessage(temp);
|
||||||
mutex.UnlockMutex();
|
stdMutex.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
return errorCode;
|
return errorCode;
|
||||||
|
@ -106,6 +97,8 @@ void ThreadedClient::ThreadExit()
|
||||||
std::cout << "Client Thread exit" << std::endl;
|
std::cout << "Client Thread exit" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#include <Windows.h>
|
||||||
|
|
||||||
bool ThreadedClient::DoWork()
|
bool ThreadedClient::DoWork()
|
||||||
{
|
{
|
||||||
int errorCode;
|
int errorCode;
|
||||||
|
@ -120,7 +113,7 @@ bool ThreadedClient::DoWork()
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}*/
|
}*/
|
||||||
|
Sleep(1);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -144,7 +137,7 @@ int ThreadedClient::Connect(unsigned short port, const char serverName[])
|
||||||
|
|
||||||
void ThreadedClient::setRecvPostBox(IPostBox<SmartPointer<OysterByte>> *postBox)
|
void ThreadedClient::setRecvPostBox(IPostBox<SmartPointer<OysterByte>> *postBox)
|
||||||
{
|
{
|
||||||
this->mutex.LockMutex();
|
stdMutex.lock();
|
||||||
this->recvPostBox = postBox;
|
this->recvPostBox = postBox;
|
||||||
this->mutex.UnlockMutex();
|
stdMutex.unlock();
|
||||||
}
|
}
|
|
@ -12,6 +12,8 @@
|
||||||
#include "../../Misc/Thread/OysterMutex.h"
|
#include "../../Misc/Thread/OysterMutex.h"
|
||||||
#include "../../Misc/Utilities.h"
|
#include "../../Misc/Utilities.h"
|
||||||
|
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
namespace Oyster
|
namespace Oyster
|
||||||
{
|
{
|
||||||
namespace Network
|
namespace Network
|
||||||
|
@ -25,7 +27,7 @@ namespace Oyster
|
||||||
ThreadedClient(IPostBox<Utility::DynamicMemory::SmartPointer<OysterByte>> *postBox, unsigned int socket);
|
ThreadedClient(IPostBox<Utility::DynamicMemory::SmartPointer<OysterByte>> *postBox, unsigned int socket);
|
||||||
virtual ~ThreadedClient();
|
virtual ~ThreadedClient();
|
||||||
|
|
||||||
int Send(Utility::DynamicMemory::SmartPointer<OysterByte>& byte);
|
void Send(Utility::DynamicMemory::SmartPointer<OysterByte>& byte);
|
||||||
|
|
||||||
int Connect(unsigned short port, const char serverName[]);
|
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>> *sendPostBox;
|
||||||
IPostBox<Utility::DynamicMemory::SmartPointer<OysterByte>> *recvPostBox;
|
IPostBox<Utility::DynamicMemory::SmartPointer<OysterByte>> *recvPostBox;
|
||||||
Oyster::Thread::OysterThread thread;
|
Oyster::Thread::OysterThread thread;
|
||||||
OysterMutex mutex;
|
std::mutex stdMutex;
|
||||||
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -95,6 +95,7 @@ void chat(ThreadedClient &client)
|
||||||
timer.reset();
|
timer.reset();
|
||||||
client.Send(msgSend);
|
client.Send(msgSend);
|
||||||
}
|
}
|
||||||
|
Sleep(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
delete postBox;
|
delete postBox;
|
||||||
|
|
|
@ -76,6 +76,12 @@ int main()
|
||||||
t.Pack(test, sendBuffer);
|
t.Pack(test, sendBuffer);
|
||||||
|
|
||||||
WinTimer timer;
|
WinTimer timer;
|
||||||
|
|
||||||
|
/* DEBUGGING: Connect 25 clients
|
||||||
|
for(int i = 0; i < 25; i++)
|
||||||
|
{
|
||||||
|
clients.push_back(new ThreadedClient(recvPostBox, 1));
|
||||||
|
}*/
|
||||||
|
|
||||||
SmartPointer<int> client = int();
|
SmartPointer<int> client = int();
|
||||||
while(1)
|
while(1)
|
||||||
|
|
Loading…
Reference in New Issue