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
#include "common/IInterface.h"
#include <functional>
/*!
\class ArchCondImpl
@ -160,7 +161,7 @@ public:
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.
*/
virtual ArchThread newThread(ThreadFunc func, void* userData) = 0;
virtual ArchThread newThread(const std::function<void()>& func) = 0;
//! Get a reference to the calling thread
/*!

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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