lib/arch: Pass jobs to Arch threads as std::function

This commit is contained in:
Povilas Kanapickas 2021-11-01 06:16:32 +02:00
parent 9cf590ccd7
commit 53356697d9
9 changed files with 17 additions and 38 deletions

View File

@ -19,6 +19,7 @@
#pragma once #pragma once
#include "common/IInterface.h" #include "common/IInterface.h"
#include <functional>
/*! /*!
\class ArchCondImpl \class ArchCondImpl
@ -160,7 +161,7 @@ public:
Creates and starts a new thread, using \c func as the entry point Creates and starts a new thread, using \c func as the entry point
and passing it \c userData. The thread is an opaque data type. and passing it \c userData. The thread is an opaque data type.
*/ */
virtual ArchThread newThread(ThreadFunc func, void* userData) = 0; virtual ArchThread newThread(const std::function<void()>& func) = 0;
//! Get a reference to the calling thread //! Get a reference to the calling thread
/*! /*!

View File

@ -59,8 +59,7 @@ public:
int m_refCount; int m_refCount;
IArchMultithread::ThreadID m_id; IArchMultithread::ThreadID m_id;
pthread_t m_thread; pthread_t m_thread;
IArchMultithread::ThreadFunc m_func; std::function<void()> func_;;
void* m_userData;
bool m_cancel; bool m_cancel;
bool m_cancelling; bool m_cancelling;
bool m_exited; bool m_exited;
@ -70,8 +69,6 @@ public:
ArchThreadImpl::ArchThreadImpl() : ArchThreadImpl::ArchThreadImpl() :
m_refCount(1), m_refCount(1),
m_id(0), m_id(0),
m_func(NULL),
m_userData(NULL),
m_cancel(false), m_cancel(false),
m_cancelling(false), m_cancelling(false),
m_exited(false), m_exited(false),
@ -317,11 +314,8 @@ ArchMultithreadPosix::unlockMutex(ArchMutex mutex)
} }
} }
ArchThread ArchThread ArchMultithreadPosix::newThread(const std::function<void()>& func)
ArchMultithreadPosix::newThread(ThreadFunc func, void* data)
{ {
assert(func != NULL);
// initialize signal handler. we do this here instead of the // initialize signal handler. we do this here instead of the
// constructor so we can avoid daemonizing (using fork()) // constructor so we can avoid daemonizing (using fork())
// when there are multiple threads. clients can safely // when there are multiple threads. clients can safely
@ -339,8 +333,7 @@ ArchMultithreadPosix::newThread(ThreadFunc func, void* data)
// create thread impl for new thread // create thread impl for new thread
ArchThreadImpl* thread = new ArchThreadImpl; ArchThreadImpl* thread = new ArchThreadImpl;
thread->m_func = func; thread->func_ = func;
thread->m_userData = data;
// create the thread. pthread_create() on RedHat 7.2 smp fails // create the thread. pthread_create() on RedHat 7.2 smp fails
// if passed a NULL attr so use a default attr. // if passed a NULL attr so use a default attr.
@ -387,7 +380,7 @@ ArchMultithreadPosix::closeThread(ArchThread thread)
// decrement ref count and clean up thread if no more references // decrement ref count and clean up thread if no more references
if (--thread->m_refCount == 0) { if (--thread->m_refCount == 0) {
// detach from thread (unless it's the main thread) // detach from thread (unless it's the main thread)
if (thread->m_func != NULL) { if (thread->func_) {
pthread_detach(thread->m_thread); pthread_detach(thread->m_thread);
} }
@ -691,7 +684,7 @@ ArchMultithreadPosix::doThreadFunc(ArchThread thread)
} }
try { try {
(*thread->m_func)(thread->m_userData); thread->func_();
} }
catch (XThreadCancel&) { catch (XThreadCancel&) {

View File

@ -67,7 +67,7 @@ public:
virtual void closeMutex(ArchMutex); virtual void closeMutex(ArchMutex);
virtual void lockMutex(ArchMutex); virtual void lockMutex(ArchMutex);
virtual void unlockMutex(ArchMutex); virtual void unlockMutex(ArchMutex);
virtual ArchThread newThread(ThreadFunc, void*); virtual ArchThread newThread(const std::function<void()>& func);
virtual ArchThread newCurrentThread(); virtual ArchThread newCurrentThread();
virtual ArchThread copyThread(ArchThread); virtual ArchThread copyThread(ArchThread);
virtual void closeThread(ArchThread); virtual void closeThread(ArchThread);

View File

@ -49,8 +49,7 @@ public:
int m_refCount; int m_refCount;
HANDLE m_thread; HANDLE m_thread;
DWORD m_id; DWORD m_id;
IArchMultithread::ThreadFunc m_func; std::function<void()> func_;
void* m_userData;
HANDLE m_cancel; HANDLE m_cancel;
bool m_cancelling; bool m_cancelling;
HANDLE m_exit; HANDLE m_exit;
@ -61,8 +60,6 @@ ArchThreadImpl::ArchThreadImpl() :
m_refCount(1), m_refCount(1),
m_thread(NULL), m_thread(NULL),
m_id(0), m_id(0),
m_func(NULL),
m_userData(NULL),
m_cancelling(false), m_cancelling(false),
m_networkData(NULL) m_networkData(NULL)
{ {
@ -290,15 +287,13 @@ ArchMultithreadWindows::unlockMutex(ArchMutex mutex)
LeaveCriticalSection(&mutex->m_mutex); LeaveCriticalSection(&mutex->m_mutex);
} }
ArchThread ArchThread ArchMultithreadWindows::newThread(const std::function<void()>& func)
ArchMultithreadWindows::newThread(ThreadFunc func, void* data)
{ {
lockMutex(m_threadMutex); lockMutex(m_threadMutex);
// create thread impl for new thread // create thread impl for new thread
ArchThreadImpl* thread = new ArchThreadImpl; ArchThreadImpl* thread = new ArchThreadImpl;
thread->m_func = func; thread->func_ = func;
thread->m_userData = data;
// create thread // create thread
unsigned int id = 0; unsigned int id = 0;
@ -668,8 +663,7 @@ ArchMultithreadWindows::doThreadFunc(ArchThread thread)
unlockMutex(m_threadMutex); unlockMutex(m_threadMutex);
try { try {
// go thread->func_();
(*thread->m_func)(thread->m_userData);
} }
catch (XThreadCancel&) { catch (XThreadCancel&) {

View File

@ -73,7 +73,7 @@ public:
virtual void closeMutex(ArchMutex); virtual void closeMutex(ArchMutex);
virtual void lockMutex(ArchMutex); virtual void lockMutex(ArchMutex);
virtual void unlockMutex(ArchMutex); virtual void unlockMutex(ArchMutex);
virtual ArchThread newThread(ThreadFunc, void*); virtual ArchThread newThread(const std::function<void()>& func);
virtual ArchThread newCurrentThread(); virtual ArchThread newCurrentThread();
virtual ArchThread copyThread(ArchThread); virtual ArchThread copyThread(ArchThread);
virtual void closeThread(ArchThread); virtual void closeThread(ArchThread);

View File

@ -88,7 +88,7 @@ ArchTaskBarWindows::init()
// create a window on the current desktop with the current // create a window on the current desktop with the current
// thread then the current thread won't be able to switch // thread then the current thread won't be able to switch
// desktops if it needs to. // desktops if it needs to.
m_thread = ARCH->newThread(&ArchTaskBarWindows::threadEntry, this); m_thread = ARCH->newThread([this]() { threadMainLoop(); });
// wait for child thread // wait for child thread
while (!m_ready) { while (!m_ready) {
@ -501,11 +501,6 @@ ArchTaskBarWindows::threadMainLoop()
UnregisterClass(className, instanceWin32()); UnregisterClass(className, instanceWin32());
} }
void ArchTaskBarWindows::threadEntry(void* self)
{
static_cast<ArchTaskBarWindows*>(self)->threadMainLoop();
}
HINSTANCE ArchTaskBarWindows::instanceWin32() HINSTANCE ArchTaskBarWindows::instanceWin32()
{ {
return ArchMiscWindows::instanceWin32(); return ArchMiscWindows::instanceWin32();

View File

@ -84,7 +84,6 @@ private:
static LRESULT CALLBACK static LRESULT CALLBACK
staticWndProc(HWND, UINT, WPARAM, LPARAM); staticWndProc(HWND, UINT, WPARAM, LPARAM);
void threadMainLoop(); void threadMainLoop();
static void threadEntry(void*);
HINSTANCE instanceWin32(); HINSTANCE instanceWin32();

View File

@ -30,7 +30,7 @@
Thread::Thread(IJob* job) Thread::Thread(IJob* job)
{ {
m_thread = ARCH->newThread(&Thread::threadFunc, job); m_thread = ARCH->newThread([job](){ threadFunc(job); });
if (m_thread == NULL) { if (m_thread == NULL) {
// couldn't create thread // couldn't create thread
delete job; delete job;
@ -126,7 +126,7 @@ Thread::operator!=(const Thread& thread) const
return !ARCH->isSameThread(m_thread, thread.m_thread); return !ARCH->isSameThread(m_thread, thread.m_thread);
} }
void Thread::threadFunc(void* vjob) void Thread::threadFunc(IJob* job)
{ {
// get this thread's id for logging // get this thread's id for logging
IArchMultithread::ThreadID id; IArchMultithread::ThreadID id;
@ -136,9 +136,6 @@ void Thread::threadFunc(void* vjob)
ARCH->closeThread(thread); ARCH->closeThread(thread);
} }
// get job
IJob* job = static_cast<IJob*>(vjob);
try { try {
// go // go
LOG((CLOG_DEBUG1 "thread 0x%08x entry", id)); LOG((CLOG_DEBUG1 "thread 0x%08x entry", id));

View File

@ -192,7 +192,7 @@ public:
private: private:
Thread(ArchThread); Thread(ArchThread);
static void threadFunc(void*); static void threadFunc(IJob*);
private: private:
ArchThread m_thread; ArchThread m_thread;