diff options
author | ShadowNinja <shadowninja@minetest.net> | 2015-04-07 06:13:12 -0400 |
---|---|---|
committer | ShadowNinja <shadowninja@minetest.net> | 2015-08-23 22:04:06 -0400 |
commit | e4bff8be94c0db4f94e63ad448d0eeb869ccdbbd (patch) | |
tree | 7935586e79da5c8c7144e345a8c0fc1cda53beed /src/util/thread.h | |
parent | 6a1047d8c116f793890b63427d53f04ceca95d54 (diff) | |
download | hax-minetest-server-e4bff8be94c0db4f94e63ad448d0eeb869ccdbbd.tar.gz hax-minetest-server-e4bff8be94c0db4f94e63ad448d0eeb869ccdbbd.zip |
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.
Diffstat (limited to 'src/util/thread.h')
-rw-r--r-- | src/util/thread.h | 60 |
1 files changed, 24 insertions, 36 deletions
diff --git a/src/util/thread.h b/src/util/thread.h index b3a5e68a2..a32871aab 100644 --- a/src/util/thread.h +++ b/src/util/thread.h @@ -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(); - - // Empty the queue, just in case doUpdate() is expensive - while (m_update_sem.GetValue()) - m_update_sem.Wait(); + while (!stopRequested()) { + m_update_sem.wait(); + // Set semaphore to 0 + while (m_update_sem.wait(0)); - 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 |