deskflow/src/lib/arch/win32/ArchMultithreadWindows.h

114 lines
2.7 KiB
C
Raw Normal View History

2012-06-10 16:50:54 +00:00
/*
* Deskflow -- mouse and keyboard sharing utility
* SPDX-FileCopyrightText: (C) 2012 - 2016 Symless Ltd.
* SPDX-FileCopyrightText: (C) 2002 Chris Schoeneman
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
2012-06-10 16:50:54 +00:00
*/
#pragma once
2012-06-10 16:50:54 +00:00
#include "arch/IArchMultithread.h"
#include <list>
2012-06-10 16:50:54 +00:00
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
2012-06-10 16:50:54 +00:00
2014-11-11 13:51:47 +00:00
#define ARCH_MULTITHREAD ArchMultithreadWindows
2012-06-10 16:50:54 +00:00
class ArchCondImpl
{
2012-06-10 16:50:54 +00:00
public:
enum
{
kSignal = 0,
kBroadcast
};
2012-06-10 16:50:54 +00:00
HANDLE m_events[2];
mutable int m_waitCount;
ArchMutex m_waitCountMutex;
2012-06-10 16:50:54 +00:00
};
class ArchMutexImpl
{
2012-06-10 16:50:54 +00:00
public:
CRITICAL_SECTION m_mutex;
2012-06-10 16:50:54 +00:00
};
//! Win32 implementation of IArchMultithread
class ArchMultithreadWindows : public IArchMultithread
{
2012-06-10 16:50:54 +00:00
public:
ArchMultithreadWindows();
~ArchMultithreadWindows() override;
//! @name manipulators
//@{
void setNetworkDataForCurrentThread(void *);
//@}
//! @name accessors
//@{
HANDLE getCancelEventForCurrentThread();
void *getNetworkDataForThread(ArchThread);
static ArchMultithreadWindows *getInstance();
//@}
// IArchMultithread overrides
ArchCond newCondVar() override;
void closeCondVar(ArchCond) override;
void signalCondVar(ArchCond) override;
void broadcastCondVar(ArchCond) override;
bool waitCondVar(ArchCond, ArchMutex, double timeout) override;
ArchMutex newMutex() override;
void closeMutex(ArchMutex) override;
void lockMutex(ArchMutex) override;
void unlockMutex(ArchMutex) override;
ArchThread newThread(ThreadFunc, void *) override;
ArchThread newCurrentThread() override;
ArchThread copyThread(ArchThread) override;
void closeThread(ArchThread) override;
void cancelThread(ArchThread) override;
void setPriorityOfThread(ArchThread, int n) override;
void testCancelThread() override;
bool wait(ArchThread, double timeout) override;
bool isSameThread(ArchThread, ArchThread) override;
bool isExitedThread(ArchThread) override;
void *getResultOfThread(ArchThread) override;
ThreadID getIDOfThread(ArchThread) override;
void setSignalHandler(ESignal, SignalFunc, void *) override;
void raiseSignal(ESignal) override;
2012-06-10 16:50:54 +00:00
private:
ArchThreadImpl *find(DWORD id);
ArchThreadImpl *findNoRef(DWORD id);
ArchThreadImpl *findNoRefOrCreate(DWORD id);
void insert(ArchThreadImpl *thread);
void erase(ArchThreadImpl *thread);
2012-06-10 16:50:54 +00:00
void refThread(ArchThreadImpl *rep);
void testCancelThreadImpl(ArchThreadImpl *rep);
2012-06-10 16:50:54 +00:00
void doThreadFunc(ArchThread thread);
static unsigned int __stdcall threadFunc(void *vrep);
2012-06-10 16:50:54 +00:00
private:
using ThreadList = std::list<ArchThread>;
2012-06-10 16:50:54 +00:00
static ArchMultithreadWindows *s_instance;
2012-06-10 16:50:54 +00:00
ArchMutex m_threadMutex;
2012-06-10 16:50:54 +00:00
ThreadList m_threadList;
ArchThread m_mainThread;
2012-06-10 16:50:54 +00:00
SignalFunc m_signalFunc[kNUM_SIGNALS];
void *m_signalUserData[kNUM_SIGNALS];
2012-06-10 16:50:54 +00:00
};