1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-07-17 17:08:39 +00:00

Clean up threading

* Rename everything.
    * Strip J prefix.
    * Change UpperCamelCase functions to lowerCamelCase.
  * Remove global (!) semaphore count mutex on OSX.
  * Remove semaphore count getter (unused, unsafe, depended on internal
    API functions on Windows, and used a hack on OSX).
  * Add `Atomic<type>`.
  * Make `Thread` handle thread names.
  * Add support for C++11 multi-threading.
  * Combine pthread and win32 sources.
  * Remove `ThreadStarted` (unused, unneeded).
  * Move some includes from the headers to the sources.
  * Move all of `Event` into its header (allows inlining with no new includes).
  * Make `Event` use `Semaphore` (except on Windows).
  * Move some porting functions into `Thread`.
  * Integrate logging with `Thread`.
  * Add threading test.
This commit is contained in:
ShadowNinja 2015-04-07 06:13:12 -04:00
parent 6a1047d8c1
commit e4bff8be94
77 changed files with 1594 additions and 2046 deletions

View file

@ -21,9 +21,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#define UTIL_THREAD_HEADER
#include "../irrlichttypes.h"
#include "../jthread/jthread.h"
#include "../jthread/jmutex.h"
#include "../jthread/jmutexautolock.h"
#include "../threading/thread.h"
#include "../threading/mutex.h"
#include "../threading/mutex_auto_lock.h"
#include "porting.h"
#include "log.h"
@ -36,27 +36,27 @@ public:
T get()
{
JMutexAutoLock lock(m_mutex);
MutexAutoLock lock(m_mutex);
return m_value;
}
void set(T value)
{
JMutexAutoLock lock(m_mutex);
MutexAutoLock lock(m_mutex);
m_value = value;
}
// You'll want to grab this in a SharedPtr
JMutexAutoLock *getLock()
MutexAutoLock *getLock()
{
return new JMutexAutoLock(m_mutex);
return new MutexAutoLock(m_mutex);
}
// You pretty surely want to grab the lock when accessing this
T m_value;
private:
JMutex m_mutex;
Mutex m_mutex;
};
/*
@ -118,7 +118,7 @@ public:
typename std::list<CallerInfo<Caller, CallerData, Key, T> >::iterator j;
{
JMutexAutoLock lock(m_queue.getMutex());
MutexAutoLock lock(m_queue.getMutex());
/*
If the caller is already on the list, only update CallerData
@ -192,44 +192,33 @@ private:
MutexedQueue<GetRequest<Key, T, Caller, CallerData> > m_queue;
};
class UpdateThread : public JThread {
class UpdateThread : public Thread
{
public:
UpdateThread() {}
virtual ~UpdateThread() {}
UpdateThread(const std::string &name) : Thread(name + "Update") {}
~UpdateThread() {}
void deferUpdate()
{
m_update_sem.Post();
}
void deferUpdate() { m_update_sem.post(); }
void Stop()
void stop()
{
JThread::Stop();
Thread::stop();
// give us a nudge
m_update_sem.Post();
m_update_sem.post();
}
void *Thread()
void *run()
{
ThreadStarted();
const char *thread_name = getName();
log_register_thread(thread_name);
porting::setThreadName(thread_name);
DSTACK(__FUNCTION_NAME);
BEGIN_DEBUG_EXCEPTION_HANDLER
while (!StopRequested()) {
m_update_sem.Wait();
while (!stopRequested()) {
m_update_sem.wait();
// Set semaphore to 0
while (m_update_sem.wait(0));
// Empty the queue, just in case doUpdate() is expensive
while (m_update_sem.GetValue())
m_update_sem.Wait();
if (StopRequested())
break;
if (stopRequested()) break;
doUpdate();
}
@ -241,10 +230,9 @@ public:
protected:
virtual void doUpdate() = 0;
virtual const char *getName() = 0;
private:
JSemaphore m_update_sem;
Semaphore m_update_sem;
};
#endif