51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
-#ifndef UTIL_THREAD_HEADER
-#define UTIL_THREAD_HEADER
+#pragma once
-#include "../irrlichttypes.h"
-#include "../jthread/jthread.h"
-#include "../jthread/jmutex.h"
-#include "../jthread/jmutexautolock.h"
+#include "irrlichttypes.h"
+#include "threading/thread.h"
+#include "threading/mutex_auto_lock.h"
#include "porting.h"
#include "log.h"
+#include "container.h"
template<typename T>
-class MutexedVariable {
+class MutexedVariable
+{
public:
- MutexedVariable(T value):
+ MutexedVariable(const T &value):
m_value(value)
{}
T get()
{
- JMutexAutoLock lock(m_mutex);
+ MutexAutoLock lock(m_mutex);
return m_value;
}
- void set(T value)
+ void set(const T &value)
{
- JMutexAutoLock lock(m_mutex);
+ MutexAutoLock lock(m_mutex);
m_value = value;
}
- // You'll want to grab this in a SharedPtr
- JMutexAutoLock *getLock()
- {
- return new JMutexAutoLock(m_mutex);
- }
-
// You pretty surely want to grab the lock when accessing this
T m_value;
-
private:
- JMutex m_mutex;
+ std::mutex m_mutex;
};
/*
template<typename Key, typename T, typename Caller, typename CallerData>
class GetRequest {
public:
- GetRequest() {}
- ~GetRequest() {}
+ GetRequest() = default;
+ ~GetRequest() = default;
- GetRequest(Key a_key) {
- key = a_key;
+ GetRequest(const Key &a_key): key(a_key)
+ {
}
Key key;
return m_queue.empty();
}
- void add(Key key, Caller caller, CallerData callerdata,
+ void add(const Key &key, Caller caller, CallerData callerdata,
ResultQueue<Key, T, Caller, CallerData> *dest)
{
typename std::deque<GetRequest<Key, T, Caller, CallerData> >::iterator i;
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
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() = default;
- 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();
}
- END_DEBUG_EXCEPTION_HANDLER(errorstream)
+ END_DEBUG_EXCEPTION_HANDLER
return NULL;
}
protected:
virtual void doUpdate() = 0;
- virtual const char *getName() = 0;
private:
- JSemaphore m_update_sem;
+ Semaphore m_update_sem;
};
-
-#endif
-