forked from oerkki/voxelands
Revert "remove jthread pt1: reimplement SimpleThread, and add SimpleMutex"
This reverts commit 3abcae28f1
.
This commit is contained in:
parent
f7f60fac02
commit
919c6ed62e
|
@ -131,6 +131,8 @@ QueuedMeshUpdate * MeshUpdateQueue::pop()
|
|||
|
||||
void * MeshUpdateThread::Thread()
|
||||
{
|
||||
ThreadStarted();
|
||||
|
||||
log_register_thread("MeshUpdateThread");
|
||||
|
||||
DSTACK(__FUNCTION_NAME);
|
||||
|
@ -211,7 +213,7 @@ Client::Client(
|
|||
|
||||
m_httpclient = new HTTPClient(this);
|
||||
|
||||
m_mesh_update_thread.start();
|
||||
m_mesh_update_thread.Start();
|
||||
|
||||
/*
|
||||
Add local player
|
||||
|
@ -239,7 +241,9 @@ Client::~Client()
|
|||
if (g_settings->getBool("enable_http"))
|
||||
m_httpclient->stop();
|
||||
|
||||
m_mesh_update_thread.stop();
|
||||
m_mesh_update_thread.setRun(false);
|
||||
while(m_mesh_update_thread.IsRunning())
|
||||
sleep_ms(100);
|
||||
}
|
||||
|
||||
void Client::connect(Address address)
|
||||
|
|
|
@ -515,7 +515,7 @@ Connection::Connection(u32 protocol_id, u32 max_packet_size, float timeout):
|
|||
{
|
||||
m_socket.setTimeoutMs(5);
|
||||
|
||||
start();
|
||||
Start();
|
||||
}
|
||||
|
||||
Connection::Connection(u32 protocol_id, u32 max_packet_size, float timeout,
|
||||
|
@ -530,7 +530,7 @@ Connection::Connection(u32 protocol_id, u32 max_packet_size, float timeout,
|
|||
{
|
||||
m_socket.setTimeoutMs(5);
|
||||
|
||||
start();
|
||||
Start();
|
||||
}
|
||||
|
||||
|
||||
|
@ -543,6 +543,7 @@ Connection::~Connection()
|
|||
|
||||
void * Connection::Thread()
|
||||
{
|
||||
ThreadStarted();
|
||||
log_register_thread("Connection");
|
||||
|
||||
dout_con<<"Connection thread started"<<std::endl;
|
||||
|
@ -995,7 +996,7 @@ void Connection::serve(u16 port)
|
|||
m_socket.Bind(port);
|
||||
#ifndef SERVER
|
||||
}catch(SocketException &e) {
|
||||
stop();
|
||||
setRun(false);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
|
10
src/http.cpp
10
src/http.cpp
|
@ -66,6 +66,8 @@ static std::string http_player_interface(Player *player, HTTPServer *server)
|
|||
/* server thread main loop */
|
||||
void * HTTPServerThread::Thread()
|
||||
{
|
||||
ThreadStarted();
|
||||
|
||||
log_register_thread("HTTPServerThread");
|
||||
|
||||
DSTACK(__FUNCTION_NAME);
|
||||
|
@ -115,7 +117,8 @@ void HTTPServer::start(u16 port)
|
|||
m_socket->Bind(port);
|
||||
|
||||
// Start thread
|
||||
m_thread.start();
|
||||
m_thread.setRun(true);
|
||||
m_thread.Start();
|
||||
|
||||
infostream<<"HTTPServer: Started on port "<<port<<std::endl;
|
||||
}
|
||||
|
@ -532,6 +535,8 @@ void HTTPRemoteClient::sendHeaders()
|
|||
/* main loop for the client http fetcher */
|
||||
void * HTTPClientThread::Thread()
|
||||
{
|
||||
ThreadStarted();
|
||||
|
||||
log_register_thread("HTTPClientThread");
|
||||
|
||||
DSTACK(__FUNCTION_NAME);
|
||||
|
@ -580,7 +585,8 @@ void HTTPClient::start(const Address &address)
|
|||
m_connection_failures = 0;
|
||||
|
||||
// Start thread
|
||||
m_thread.start();
|
||||
m_thread.setRun(true);
|
||||
m_thread.Start();
|
||||
|
||||
infostream<<"HTTPClient: Started"<<std::endl;
|
||||
}
|
||||
|
|
|
@ -40,10 +40,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||
#include <windows.h>
|
||||
#define sleep_ms(x) Sleep(x)
|
||||
#else
|
||||
# include <unistd.h>
|
||||
# include <pthread.h>
|
||||
# include <signal.h>
|
||||
# define sleep_ms(x) usleep(x*1000)
|
||||
#include <unistd.h>
|
||||
#define sleep_ms(x) usleep(x*1000)
|
||||
#endif
|
||||
|
||||
namespace porting
|
||||
|
|
|
@ -73,6 +73,8 @@ private:
|
|||
|
||||
void * ServerThread::Thread()
|
||||
{
|
||||
ThreadStarted();
|
||||
|
||||
log_register_thread("ServerThread");
|
||||
|
||||
DSTACK(__FUNCTION_NAME);
|
||||
|
@ -108,7 +110,8 @@ void * ServerThread::Thread()
|
|||
|
||||
void * EmergeThread::Thread()
|
||||
{
|
||||
printf("EmergeThread::Thread()\n");
|
||||
ThreadStarted();
|
||||
|
||||
log_register_thread("EmergeThread");
|
||||
|
||||
DSTACK(__FUNCTION_NAME);
|
||||
|
@ -1079,7 +1082,8 @@ void Server::start(unsigned short port)
|
|||
return;
|
||||
|
||||
// Start thread
|
||||
m_thread.start();
|
||||
m_thread.setRun(true);
|
||||
m_thread.Start();
|
||||
|
||||
infostream<<"Server: Started on port "<<port<<std::endl;
|
||||
}
|
||||
|
@ -1090,7 +1094,9 @@ void Server::stop()
|
|||
|
||||
infostream<<"Server: Stopping and waiting threads"<<std::endl;
|
||||
|
||||
// Stop threads
|
||||
// Stop threads (set run=false first so both start stopping)
|
||||
m_thread.setRun(false);
|
||||
m_emergethread.setRun(false);
|
||||
m_thread.stop();
|
||||
m_emergethread.stop();
|
||||
|
||||
|
|
|
@ -181,8 +181,11 @@ public:
|
|||
|
||||
void trigger()
|
||||
{
|
||||
if (getRun() == false)
|
||||
start();
|
||||
setRun(true);
|
||||
if(IsRunning() == false)
|
||||
{
|
||||
Start();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
|
173
src/utility.h
173
src/utility.h
|
@ -972,194 +972,45 @@ inline void str_replace_char(std::string & str, char from, char to)
|
|||
}
|
||||
}
|
||||
|
||||
/*
|
||||
A simple mutex implementation
|
||||
*/
|
||||
#ifdef _WIN32
|
||||
class SimpleMutex
|
||||
{
|
||||
CRITICAL_SECTION mut;
|
||||
|
||||
public:
|
||||
|
||||
SimpleMutex()
|
||||
{
|
||||
InitializeCriticalSection(&mut);
|
||||
}
|
||||
|
||||
~SimpleMutex()
|
||||
{
|
||||
unlock();
|
||||
DeleteCriticalSection(&mut);
|
||||
}
|
||||
|
||||
void lock()
|
||||
{
|
||||
EnterCriticalSection(&mut);
|
||||
}
|
||||
|
||||
bool trylock()
|
||||
{
|
||||
if (!TryEnterCriticalSection(&mut))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
void unlock()
|
||||
{
|
||||
LeaveCriticalSection(&mut);
|
||||
}
|
||||
};
|
||||
#else
|
||||
class SimpleMutex
|
||||
{
|
||||
pthread_mutexattr_t attr;
|
||||
pthread_mutex_t mut;
|
||||
|
||||
public:
|
||||
|
||||
SimpleMutex()
|
||||
{
|
||||
pthread_mutexattr_init(&attr);
|
||||
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
|
||||
pthread_mutex_init(&mut, &attr);
|
||||
}
|
||||
|
||||
~SimpleMutex()
|
||||
{
|
||||
unlock();
|
||||
pthread_mutex_destroy(&mut);
|
||||
pthread_mutexattr_destroy(&attr);
|
||||
}
|
||||
|
||||
void lock()
|
||||
{
|
||||
pthread_mutex_lock(&mut);
|
||||
}
|
||||
|
||||
int trylock()
|
||||
{
|
||||
if (pthread_mutex_trylock(&mut))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
void unlock()
|
||||
{
|
||||
pthread_mutex_unlock(&mut);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
/*
|
||||
A base class for simple background thread implementation
|
||||
*/
|
||||
class SimpleThread
|
||||
|
||||
class SimpleThread : public JThread
|
||||
{
|
||||
bool run;
|
||||
SimpleMutex run_mutex;
|
||||
#ifdef _WIN32
|
||||
HANDLE thread;
|
||||
#else
|
||||
pthread_t thread;
|
||||
pthread_attr_t attr;
|
||||
#endif
|
||||
JMutex run_mutex;
|
||||
|
||||
public:
|
||||
|
||||
SimpleThread():
|
||||
run(false),
|
||||
run_mutex()
|
||||
JThread(),
|
||||
run(true)
|
||||
{
|
||||
#ifndef _WIN32
|
||||
pthread_attr_init(&attr);
|
||||
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
|
||||
#endif
|
||||
run_mutex.Init();
|
||||
}
|
||||
|
||||
virtual ~SimpleThread()
|
||||
{
|
||||
kill();
|
||||
}
|
||||
{}
|
||||
|
||||
virtual void * Thread() = 0;
|
||||
|
||||
bool getRun()
|
||||
{
|
||||
run_mutex.lock();
|
||||
bool r = run;
|
||||
run_mutex.unlock();
|
||||
return r;
|
||||
JMutexAutoLock lock(run_mutex);
|
||||
return run;
|
||||
}
|
||||
void setRun(bool a_run)
|
||||
{
|
||||
run_mutex.lock();
|
||||
JMutexAutoLock lock(run_mutex);
|
||||
run = a_run;
|
||||
run_mutex.unlock();
|
||||
}
|
||||
|
||||
void start()
|
||||
{
|
||||
if (getRun()) {
|
||||
#ifdef _WIN32
|
||||
ResumeThread(thread);
|
||||
#else
|
||||
pthread_kill(thread,SIGCONT);
|
||||
#endif
|
||||
}else{
|
||||
setRun(true);
|
||||
#ifdef _WIN32
|
||||
thread = CreateThread(NULL, 0, &runThread, this, 0, NULL);
|
||||
#else
|
||||
pthread_create(&thread, &attr, &runThread, this);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
void wait()
|
||||
{
|
||||
if (getRun()) {
|
||||
#ifdef _WIN32
|
||||
WaitForSingleObject(thread, 2000);
|
||||
CloseHandle(thread);
|
||||
#else
|
||||
pthread_join(thread,NULL);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
void stop()
|
||||
{
|
||||
setRun(false);
|
||||
wait();
|
||||
}
|
||||
|
||||
void kill()
|
||||
{
|
||||
if (getRun()) {
|
||||
setRun(false);
|
||||
#ifdef _WIN32
|
||||
TerminateThread(thread,0);
|
||||
CloseHandle(thread);
|
||||
#else
|
||||
pthread_kill(thread,SIGKILL);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
static void *runThread(void* data)
|
||||
{
|
||||
SimpleThread *t = (SimpleThread*)data;
|
||||
void *r = t->Thread();
|
||||
t->setRun(false);
|
||||
#ifdef _WIN32
|
||||
ExitThread(0);
|
||||
CloseHandle(t->thread);
|
||||
#else
|
||||
pthread_exit(r);
|
||||
#endif
|
||||
return r;
|
||||
while(IsRunning())
|
||||
sleep_ms(100);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue