Skip to content

Commit

Permalink
Merge pull request #1352 from p12tic/cleanup-callbacks
Browse files Browse the repository at this point in the history
Cleanup internal callback APIs
  • Loading branch information
p12tic authored Nov 1, 2021
2 parents 4486830 + d7de571 commit fc6d4e4
Show file tree
Hide file tree
Showing 50 changed files with 159 additions and 531 deletions.
14 changes: 3 additions & 11 deletions src/lib/arch/IArchMultithread.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#pragma once

#include "common/IInterface.h"
#include <functional>

/*!
\class ArchCondImpl
Expand Down Expand Up @@ -71,7 +72,7 @@ barrier. Each architecture must implement this interface.
class IArchMultithread : public IInterface {
public:
//! Type of thread entry point
typedef void* (*ThreadFunc)(void*);
typedef void (*ThreadFunc)(void*);
//! Type of thread identifier
typedef unsigned int ThreadID;
//! Types of signals
Expand Down Expand Up @@ -160,7 +161,7 @@ class IArchMultithread : public IInterface {
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
/*!
Expand Down Expand Up @@ -235,15 +236,6 @@ class IArchMultithread : public IInterface {
*/
virtual bool isExitedThread(ArchThread thread) = 0;

//! Returns the exit code of a thread
/*!
Waits indefinitely for \c thread to exit (if it hasn't yet) then
returns the thread's exit code.
(Cancellation point)
*/
virtual void* getResultOfThread(ArchThread thread) = 0;

//! Returns an ID for a thread
/*!
Returns some ID number for \c thread. This is for logging purposes.
Expand Down
29 changes: 5 additions & 24 deletions src/lib/arch/unix/ArchMultithreadPosix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,24 +59,19 @@ class ArchThreadImpl {
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;
void* m_result;
void* m_networkData;
};

ArchThreadImpl::ArchThreadImpl() :
m_refCount(1),
m_id(0),
m_func(NULL),
m_userData(NULL),
m_cancel(false),
m_cancelling(false),
m_exited(false),
m_result(NULL),
m_networkData(NULL)
{
// do nothing
Expand Down Expand Up @@ -319,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
Expand All @@ -341,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.
Expand Down Expand Up @@ -389,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);
}

Expand Down Expand Up @@ -526,13 +517,6 @@ ArchMultithreadPosix::isExitedThread(ArchThread thread)
return thread->m_exited;
}

void*
ArchMultithreadPosix::getResultOfThread(ArchThread thread)
{
std::lock_guard<std::mutex> lock(m_threadMutex);
return thread->m_result;
}

IArchMultithread::ThreadID
ArchMultithreadPosix::getIDOfThread(ArchThread thread)
{
Expand Down Expand Up @@ -699,10 +683,8 @@ ArchMultithreadPosix::doThreadFunc(ArchThread thread)
std::lock_guard<std::mutex> lock(m_threadMutex);
}

void* result = NULL;
try {
// go
result = (*thread->m_func)(thread->m_userData);
thread->func_();
}

catch (XThreadCancel&) {
Expand All @@ -721,7 +703,6 @@ ArchMultithreadPosix::doThreadFunc(ArchThread thread)
// thread has exited
{
std::lock_guard<std::mutex> lock(m_threadMutex);
thread->m_result = result;
thread->m_exited = true;
}

Expand Down
3 changes: 1 addition & 2 deletions src/lib/arch/unix/ArchMultithreadPosix.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class ArchMultithreadPosix : public IArchMultithread {
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);
Expand All @@ -77,7 +77,6 @@ class ArchMultithreadPosix : public IArchMultithread {
virtual bool wait(ArchThread, double timeout);
virtual bool isSameThread(ArchThread, ArchThread);
virtual bool isExitedThread(ArchThread);
virtual void* getResultOfThread(ArchThread);
virtual ThreadID getIDOfThread(ArchThread);
virtual void setSignalHandler(ESignal, SignalFunc, void*);
virtual void raiseSignal(ESignal);
Expand Down
29 changes: 4 additions & 25 deletions src/lib/arch/win32/ArchMultithreadWindows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,18 @@ class ArchThreadImpl {
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;
void* m_result;
void* m_networkData;
};

ArchThreadImpl::ArchThreadImpl() :
m_refCount(1),
m_thread(NULL),
m_id(0),
m_func(NULL),
m_userData(NULL),
m_cancelling(false),
m_result(NULL),
m_networkData(NULL)
{
m_exit = CreateEvent(NULL, TRUE, FALSE, NULL);
Expand Down Expand Up @@ -292,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;
Expand Down Expand Up @@ -523,15 +516,6 @@ ArchMultithreadWindows::isExitedThread(ArchThread thread)
return (WaitForSingleObject(thread->m_exit, 0) == WAIT_OBJECT_0);
}

void*
ArchMultithreadWindows::getResultOfThread(ArchThread thread)
{
lockMutex(m_threadMutex);
void* result = thread->m_result;
unlockMutex(m_threadMutex);
return result;
}

IArchMultithread::ThreadID
ArchMultithreadWindows::getIDOfThread(ArchThread thread)
{
Expand Down Expand Up @@ -678,10 +662,8 @@ ArchMultithreadWindows::doThreadFunc(ArchThread thread)
lockMutex(m_threadMutex);
unlockMutex(m_threadMutex);

void* result = NULL;
try {
// go
result = (*thread->m_func)(thread->m_userData);
thread->func_();
}

catch (XThreadCancel&) {
Expand All @@ -695,9 +677,6 @@ ArchMultithreadWindows::doThreadFunc(ArchThread thread)
}

// thread has exited
lockMutex(m_threadMutex);
thread->m_result = result;
unlockMutex(m_threadMutex);
SetEvent(thread->m_exit);

// done with thread
Expand Down
3 changes: 1 addition & 2 deletions src/lib/arch/win32/ArchMultithreadWindows.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class ArchMultithreadWindows : public IArchMultithread {
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);
Expand All @@ -83,7 +83,6 @@ class ArchMultithreadWindows : public IArchMultithread {
virtual bool wait(ArchThread, double timeout);
virtual bool isSameThread(ArchThread, ArchThread);
virtual bool isExitedThread(ArchThread);
virtual void* getResultOfThread(ArchThread);
virtual ThreadID getIDOfThread(ArchThread);
virtual void setSignalHandler(ESignal, SignalFunc, void*);
virtual void raiseSignal(ESignal);
Expand Down
9 changes: 1 addition & 8 deletions src/lib/arch/win32/ArchTaskBarWindows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -501,13 +501,6 @@ ArchTaskBarWindows::threadMainLoop()
UnregisterClass(className, instanceWin32());
}

void*
ArchTaskBarWindows::threadEntry(void* self)
{
static_cast<ArchTaskBarWindows*>(self)->threadMainLoop();
return NULL;
}

HINSTANCE ArchTaskBarWindows::instanceWin32()
{
return ArchMiscWindows::instanceWin32();
Expand Down
1 change: 0 additions & 1 deletion src/lib/arch/win32/ArchTaskBarWindows.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ class ArchTaskBarWindows : public IArchTaskBar {
static LRESULT CALLBACK
staticWndProc(HWND, UINT, WPARAM, LPARAM);
void threadMainLoop();
static void* threadEntry(void*);

HINSTANCE instanceWin32();

Expand Down
4 changes: 1 addition & 3 deletions src/lib/barrier/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@

#if SYSAPI_WIN32
#include "base/IEventQueue.h"
#include "base/TMethodJob.h"
#endif

#include <iostream>
Expand Down Expand Up @@ -229,8 +228,7 @@ App::handleIpcMessage(const Event& e, void*)
}
}

void
App::runEventsLoop(void*)
void App::run_events_loop()
{
m_events->loop();

Expand Down
2 changes: 1 addition & 1 deletion src/lib/barrier/App.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class App : public IApp {
protected:
void initIpcClient();
void cleanupIpcClient();
void runEventsLoop(void*);
void run_events_loop();

IArchTaskBarReceiver* m_taskBarReceiver;
bool m_suspended;
Expand Down
6 changes: 1 addition & 5 deletions src/lib/barrier/ClientApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
#include "base/TMethodEventJob.h"
#include "base/log_outputters.h"
#include "base/EventQueue.h"
#include "base/TMethodJob.h"
#include "base/Log.h"
#include "common/Version.h"

Expand Down Expand Up @@ -465,10 +464,7 @@ ClientApp::mainLoop()

#if defined(MAC_OS_X_VERSION_10_7)

Thread thread(
new TMethodJob<ClientApp>(
this, &ClientApp::runEventsLoop,
NULL));
Thread thread([this](){ run_events_loop(); });

// wait until carbon loop is ready
OSXScreen* screen = dynamic_cast<OSXScreen*>(
Expand Down
1 change: 0 additions & 1 deletion src/lib/barrier/ClientApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ namespace barrier { class Screen; }
class Event;
class Client;
class NetworkAddress;
class Thread;
class ClientArgs;

class ClientApp : public App {
Expand Down
6 changes: 1 addition & 5 deletions src/lib/barrier/ServerApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
#include "base/EventQueue.h"
#include "base/log_outputters.h"
#include "base/FunctionEventJob.h"
#include "base/TMethodJob.h"
#include "base/IEventQueue.h"
#include "base/Log.h"
#include "base/TMethodEventJob.h"
Expand Down Expand Up @@ -797,10 +796,7 @@ ServerApp::mainLoop()

#if defined(MAC_OS_X_VERSION_10_7)

Thread thread(
new TMethodJob<ServerApp>(
this, &ServerApp::runEventsLoop,
NULL));
Thread thread([this](){ run_events_loop(); });

// wait until carbon loop is ready
OSXScreen* screen = dynamic_cast<OSXScreen*>(
Expand Down
5 changes: 2 additions & 3 deletions src/lib/barrier/StreamChunker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,13 @@ bool StreamChunker::s_interruptFile = false;
Mutex* StreamChunker::s_interruptMutex = NULL;

void
StreamChunker::sendFile(
char* filename,
StreamChunker::sendFile(const char* filename,
IEventQueue* events,
void* eventTarget)
{
s_isChunkingFile = true;

std::fstream file(static_cast<char*>(filename), std::ios::in | std::ios::binary);
std::fstream file(filename, std::ios::in | std::ios::binary);

if (!file.is_open()) {
throw runtime_error("failed to open file");
Expand Down
5 changes: 1 addition & 4 deletions src/lib/barrier/StreamChunker.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ class Mutex;

class StreamChunker {
public:
static void sendFile(
char* filename,
IEventQueue* events,
void* eventTarget);
static void sendFile(const char* filename, IEventQueue* events, void* eventTarget);
static void sendClipboard(
String& data,
size_t size,
Expand Down
1 change: 0 additions & 1 deletion src/lib/barrier/win32/DaemonApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#include "net/SocketMultiplexer.h"
#include "arch/XArch.h"
#include "base/Log.h"
#include "base/TMethodJob.h"
#include "base/TMethodEventJob.h"
#include "base/EventQueue.h"
#include "base/log_outputters.h"
Expand Down
Loading

0 comments on commit fc6d4e4

Please sign in to comment.