Replace SimpleThread by JThread now implementing same features
authorsapier <Sapier at GMX dot net>
Tue, 3 Dec 2013 22:32:03 +0000 (23:32 +0100)
committersapier <Sapier at GMX dot net>
Sun, 15 Dec 2013 12:39:42 +0000 (13:39 +0100)
src/client.cpp
src/client.h
src/connection.cpp
src/connection.h
src/emerge.cpp
src/emerge.h
src/httpfetch.cpp
src/server.cpp
src/util/thread.h

index ee63cf7c81e395a5089f2e72582f80fc2fdffc83..b3030991b826f0f3a15053a1c43e6c0047697b36 100644 (file)
@@ -177,7 +177,7 @@ void * MeshUpdateThread::Thread()
        
        BEGIN_DEBUG_EXCEPTION_HANDLER
 
-       while(getRun())
+       while(!StopRequested())
        {
                /*// Wait for output queue to flush.
                // Allow 2 in queue, this makes less frametime jitter.
@@ -302,9 +302,8 @@ Client::~Client()
                m_con.Disconnect();
        }
 
-       m_mesh_update_thread.setRun(false);
-       while(m_mesh_update_thread.IsRunning())
-               sleep_ms(100);
+       m_mesh_update_thread.Stop();
+       m_mesh_update_thread.Wait();
        while(!m_mesh_update_thread.m_queue_out.empty()) {
                MeshUpdateResult r = m_mesh_update_thread.m_queue_out.pop_front();
                delete r.mesh;
index 5969adc8e017347104664d0f4285176c549619de..a74668d5b8c1de2c561edd573baaee56f3a7530f 100644 (file)
@@ -103,7 +103,7 @@ struct MeshUpdateResult
        }
 };
 
-class MeshUpdateThread : public SimpleThread
+class MeshUpdateThread : public JThread
 {
 public:
 
index 42262846fbb789806071adba7ff4194c1d1c7f4c..8f83f62192c097f696469c4162b46d073fb3a07f 100644 (file)
@@ -556,7 +556,7 @@ Connection::Connection(u32 protocol_id, u32 max_packet_size, float timeout,
 
 Connection::~Connection()
 {
-       stop();
+       Stop();
        // Delete peers
        for(std::map<u16, Peer*>::iterator
                        j = m_peers.begin();
@@ -578,7 +578,7 @@ void * Connection::Thread()
        u32 curtime = porting::getTimeMs();
        u32 lasttime = curtime;
 
-       while(getRun())
+       while(!StopRequested())
        {
                BEGIN_DEBUG_EXCEPTION_HANDLER
                
index a1d564849810b1afc1d995ed858c56d2cf03db72..56badc904e5226c39a7feb16dcee687461df47b1 100644 (file)
@@ -450,11 +450,11 @@ struct ConnectionEvent
                        return "CONNEVENT_NONE";
                case CONNEVENT_DATA_RECEIVED:
                        return "CONNEVENT_DATA_RECEIVED";
-               case CONNEVENT_PEER_ADDED: 
+               case CONNEVENT_PEER_ADDED:
                        return "CONNEVENT_PEER_ADDED";
-               case CONNEVENT_PEER_REMOVED: 
+               case CONNEVENT_PEER_REMOVED:
                        return "CONNEVENT_PEER_REMOVED";
-               case CONNEVENT_BIND_FAILED: 
+               case CONNEVENT_BIND_FAILED:
                        return "CONNEVENT_BIND_FAILED";
                }
                return "Invalid ConnectionEvent";
@@ -544,7 +544,7 @@ struct ConnectionCommand
        }
 };
 
-class Connection: public SimpleThread
+class Connection: public JThread
 {
 public:
        Connection(u32 protocol_id, u32 max_packet_size, float timeout, bool ipv6);
index 6635a6518b26fc2bb70efac60a0df0bf6109dd68..ff00a0b62bf862aa8c616cf4d6c0f4b3e0e4d6b7 100644 (file)
@@ -47,7 +47,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "mapgen_math.h"
 
 
-class EmergeThread : public SimpleThread
+class EmergeThread : public JThread
 {
 public:
        Server *m_server;
@@ -61,26 +61,17 @@ public:
        std::queue<v3s16> blockqueue;
 
        EmergeThread(Server *server, int ethreadid):
-               SimpleThread(),
+               JThread(),
                m_server(server),
                map(NULL),
                emerge(NULL),
                mapgen(NULL),
+               enable_mapgen_debug_info(false),
                id(ethreadid)
        {
        }
 
        void *Thread();
-
-       void trigger()
-       {
-               setRun(true);
-               if(IsRunning() == false)
-               {
-                       Start();
-               }
-       }
-
        bool popBlockEmerge(v3s16 *pos, u8 *flags);
        bool getBlockOrStartGen(v3s16 p, MapBlock **b,
                        BlockMakeData *data, bool allow_generate);
@@ -137,9 +128,9 @@ EmergeManager::EmergeManager(IGameDef *gamedef) {
 
 EmergeManager::~EmergeManager() {
        for (unsigned int i = 0; i != emergethread.size(); i++) {
-               emergethread[i]->setRun(false);
+               emergethread[i]->Stop();
                emergethread[i]->qevent.signal();
-               emergethread[i]->stop();
+               emergethread[i]->Wait();
                delete emergethread[i];
                delete mapgen[i];
        }
@@ -261,9 +252,9 @@ Mapgen *EmergeManager::getCurrentMapgen() {
 }
 
 
-void EmergeManager::triggerAllThreads() {
+void EmergeManager::startAllThreads() {
        for (unsigned int i = 0; i != emergethread.size(); i++)
-               emergethread[i]->trigger();
+               emergethread[i]->Start();
 }
 
 
@@ -499,7 +490,7 @@ void *EmergeThread::Thread() {
        mapgen = emerge->mapgen[id];
        enable_mapgen_debug_info = emerge->mapgen_debug_info;
 
-       while (getRun())
+       while (!StopRequested())
        try {
                if (!popBlockEmerge(&p, &flags)) {
                        qevent.wait();
index d5bb72f85d0303f2fc4282522f18530e6ec09de9..b2b00adc9a531625632011056aa56e48d4a163ce 100644 (file)
@@ -119,7 +119,7 @@ public:
        Mapgen *createMapgen(std::string mgname, int mgid,
                                                MapgenParams *mgparams);
        MapgenParams *createMapgenParams(std::string mgname);
-       void triggerAllThreads();
+       void startAllThreads();
        bool enqueueBlockEmerge(u16 peer_id, v3s16 p, bool allow_generate);
 
        void registerMapgen(std::string name, MapgenFactory *mgfactory);
index 64e77bc543a11e59679b1aa557c3647519859992..12e203ef20a5a1f4d9f4a5135a2abf78ccf66249 100644 (file)
@@ -319,7 +319,7 @@ struct HTTPFetchOngoing
        }
 };
 
-class CurlFetchThread : public SimpleThread
+class CurlFetchThread : public JThread
 {
 protected:
        enum RequestType {
@@ -539,7 +539,6 @@ protected:
 
        void * Thread()
        {
-               ThreadStarted();
                log_register_thread("CurlFetchThread");
                DSTACK(__FUNCTION_NAME);
 
@@ -553,7 +552,7 @@ protected:
 
                assert(m_all_ongoing.empty());
 
-               while (getRun()) {
+               while (!StopRequested()) {
                        BEGIN_DEBUG_EXCEPTION_HANDLER
 
                        /*
@@ -641,9 +640,9 @@ void httpfetch_cleanup()
 {
        verbosestream<<"httpfetch_cleanup: cleaning up"<<std::endl;
 
-       g_httpfetch_thread->setRun(false);
+       g_httpfetch_thread->Stop();
        g_httpfetch_thread->requestWakeUp();
-       g_httpfetch_thread->stop();
+       g_httpfetch_thread->Wait();
        delete g_httpfetch_thread;
 
        curl_global_cleanup();
@@ -652,8 +651,6 @@ void httpfetch_cleanup()
 void httpfetch_async(const HTTPFetchRequest &fetchrequest)
 {
        g_httpfetch_thread->requestFetch(fetchrequest);
-       if (!g_httpfetch_thread->IsRunning())
-               g_httpfetch_thread->Start();
 }
 
 static void httpfetch_request_clear(unsigned long caller)
index 1455de721767fd461a566d475a94759d8c476bbb..c1cedf0f39297f41dc0114916a7f5eec08d36fc6 100644 (file)
@@ -73,14 +73,14 @@ public:
        {}
 };
 
-class ServerThread : public SimpleThread
+class ServerThread : public JThread
 {
        Server *m_server;
 
 public:
 
        ServerThread(Server *server):
-               SimpleThread(),
+               JThread(),
                m_server(server)
        {
        }
@@ -98,7 +98,7 @@ void * ServerThread::Thread()
 
        BEGIN_DEBUG_EXCEPTION_HANDLER
 
-       while(getRun())
+       while(!StopRequested())
        {
                try{
                        //TimeTaker timer("AsyncRunStep() + Receive()");
@@ -963,14 +963,13 @@ void Server::start(unsigned short port)
        infostream<<"Starting server on port "<<port<<"..."<<std::endl;
 
        // Stop thread if already running
-       m_thread->stop();
+       m_thread->Stop();
 
        // Initialize connection
        m_con.SetTimeoutMs(30);
        m_con.Serve(port);
 
        // Start thread
-       m_thread->setRun(true);
        m_thread->Start();
 
        // ASCII art for the win!
@@ -993,9 +992,9 @@ void Server::stop()
        infostream<<"Server: Stopping and waiting threads"<<std::endl;
 
        // Stop threads (set run=false first so both start stopping)
-       m_thread->setRun(false);
+       m_thread->Stop();
        //m_emergethread.setRun(false);
-       m_thread->stop();
+       m_thread->Wait();
        //m_emergethread.stop();
 
        infostream<<"Server: Threads stopped"<<std::endl;
@@ -1682,7 +1681,7 @@ void Server::AsyncRunStep()
                {
                        counter = 0.0;
 
-                       m_emerge->triggerAllThreads();
+                       m_emerge->startAllThreads();
 
                        // Update m_enable_rollback_recording here too
                        m_enable_rollback_recording =
index 3f5ef1199b7f6e4acd2317212f7eee601cefa7e6..bb8e03317b8a685a2c3834b72903d85ee1fee175 100644 (file)
@@ -59,53 +59,9 @@ private:
        JMutex m_mutex;
 };
 
-/*
-       A base class for simple background thread implementation
-*/
-
-class SimpleThread : public JThread
-{
-       bool run;
-       JMutex run_mutex;
-
-public:
-
-       SimpleThread():
-               JThread(),
-               run(true)
-       {
-       }
-
-       virtual ~SimpleThread()
-       {}
-
-       virtual void * Thread() = 0;
-
-       bool getRun()
-       {
-               JMutexAutoLock lock(run_mutex);
-               return run;
-       }
-       void setRun(bool a_run)
-       {
-               JMutexAutoLock lock(run_mutex);
-               run = a_run;
-       }
-
-       void stop()
-       {
-               setRun(false);
-               while(IsRunning())
-                       sleep_ms(100);
-       }
-};
-
 /*
        A single worker thread - multiple client threads queue framework.
 */
-
-
-
 template<typename Key, typename T, typename Caller, typename CallerData>
 class GetResult
 {