refactor: Unify ArchSleepClasses into one method
idea from https://github.com/input-leap/input-leap/pull/1462
This commit is contained in:
@ -82,12 +82,10 @@ macro(configure_unix_libs)
|
||||
include(CheckSymbolExists)
|
||||
include(CheckCSourceCompiles)
|
||||
|
||||
check_include_files(sys/select.h HAVE_SYS_SELECT_H)
|
||||
check_include_files(sys/socket.h HAVE_SYS_SOCKET_H)
|
||||
check_include_files(sys/time.h HAVE_SYS_TIME_H)
|
||||
check_include_files(unistd.h HAVE_UNISTD_H)
|
||||
|
||||
check_function_exists(nanosleep HAVE_NANOSLEEP)
|
||||
check_function_exists(sigwait HAVE_POSIX_SIGWAIT)
|
||||
check_function_exists(inet_aton HAVE_INET_ATON)
|
||||
|
||||
@ -157,9 +155,6 @@ macro(configure_unix_libs)
|
||||
# For config.h, set some static values; it may be a good idea to make these
|
||||
# values dynamic for non-standard UNIX compilers.
|
||||
set(HAVE_PTHREAD_SIGNAL 1)
|
||||
set(SELECT_TYPE_ARG1 int)
|
||||
set(SELECT_TYPE_ARG234 " (fd_set *)")
|
||||
set(SELECT_TYPE_ARG5 " (struct timeval *)")
|
||||
set(TIME_WITH_SYS_TIME 1)
|
||||
set(HAVE_SOCKLEN_T 1)
|
||||
|
||||
|
||||
@ -13,9 +13,6 @@
|
||||
/* Define if you have the `inet_aton` function. */
|
||||
#cmakedefine HAVE_INET_ATON @HAVE_INET_ATON@
|
||||
|
||||
/* Define if you have the `nanosleep` function. */
|
||||
#cmakedefine HAVE_NANOSLEEP @HAVE_NANOSLEEP@
|
||||
|
||||
/* Define if you have a POSIX `sigwait` function. */
|
||||
#cmakedefine HAVE_POSIX_SIGWAIT @HAVE_POSIX_SIGWAIT@
|
||||
|
||||
@ -28,9 +25,6 @@
|
||||
/* Define if your compiler defines socklen_t. */
|
||||
#cmakedefine HAVE_SOCKLEN_T @HAVE_SOCKLEN_T@
|
||||
|
||||
/* Define to 1 if you have the <sys/select.h> header file. */
|
||||
#cmakedefine HAVE_SYS_SELECT_H @HAVE_SYS_SELECT_H@
|
||||
|
||||
/* Define to 1 if you have the <sys/socket.h> header file. */
|
||||
#cmakedefine HAVE_SYS_SOCKET_H @HAVE_SYS_SOCKET_H@
|
||||
|
||||
@ -61,15 +55,6 @@
|
||||
/* Define this if the XKB extension is available. */
|
||||
#cmakedefine HAVE_XKB_EXTENSION @HAVE_XKB_EXTENSION@
|
||||
|
||||
/* Define to the type of arg 1 for `select`. */
|
||||
#cmakedefine SELECT_TYPE_ARG1 @SELECT_TYPE_ARG1@
|
||||
|
||||
/* Define to the type of args 2, 3 and 4 for `select`. */
|
||||
#cmakedefine SELECT_TYPE_ARG234 @SELECT_TYPE_ARG234@
|
||||
|
||||
/* Define to the type of arg 5 for `select`. */
|
||||
#cmakedefine SELECT_TYPE_ARG5 @SELECT_TYPE_ARG5@
|
||||
|
||||
/* Define to 1 if you can safely include both <sys/time.h> and <time.h>. */
|
||||
#cmakedefine TIME_WITH_SYS_TIME @TIME_WITH_SYS_TIME@
|
||||
|
||||
|
||||
@ -7,6 +7,8 @@
|
||||
|
||||
#include "arch/Arch.h"
|
||||
|
||||
#include <thread>
|
||||
|
||||
#if SYSAPI_WIN32
|
||||
#include "arch/win32/ArchMiscWindows.h"
|
||||
#endif
|
||||
@ -41,3 +43,12 @@ Arch *Arch::getInstance()
|
||||
assert(s_instance != nullptr);
|
||||
return s_instance;
|
||||
}
|
||||
|
||||
void Arch::sleep(double timeout)
|
||||
{
|
||||
ARCH->testCancelThread();
|
||||
if (timeout < 0.0)
|
||||
return;
|
||||
const auto msec = static_cast<uint64_t>(timeout * 1000);
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(msec));
|
||||
}
|
||||
|
||||
@ -33,7 +33,6 @@
|
||||
#include "arch/win32/ArchLogWindows.h"
|
||||
#include "arch/win32/ArchMultithreadWindows.h"
|
||||
#include "arch/win32/ArchNetworkWinsock.h"
|
||||
#include "arch/win32/ArchSleepWindows.h"
|
||||
#include "arch/win32/ArchTimeWindows.h"
|
||||
|
||||
#elif SYSAPI_UNIX
|
||||
@ -41,7 +40,6 @@
|
||||
#include "arch/unix/ArchDaemonUnix.h"
|
||||
#include "arch/unix/ArchLogUnix.h"
|
||||
#include "arch/unix/ArchNetworkBSD.h"
|
||||
#include "arch/unix/ArchSleepUnix.h"
|
||||
#include "arch/unix/ArchTimeUnix.h"
|
||||
|
||||
#if HAVE_PTHREAD
|
||||
@ -70,7 +68,6 @@ class Arch : public ARCH_DAEMON,
|
||||
public ARCH_LOG,
|
||||
public ARCH_MULTITHREAD,
|
||||
public ARCH_NETWORK,
|
||||
public ARCH_SLEEP,
|
||||
public ArchString,
|
||||
public ARCH_TIME
|
||||
{
|
||||
@ -102,6 +99,12 @@ public:
|
||||
s_instance = s;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief blocks calling thread for timout seconds
|
||||
* @param timeout - blocking time in seconds. if < 0 not blocked if == 0 then caller yields the CPU
|
||||
*/
|
||||
static void sleep(double timeout);
|
||||
|
||||
private:
|
||||
static Arch *s_instance;
|
||||
};
|
||||
|
||||
@ -16,8 +16,6 @@ if(WIN32)
|
||||
win32/ArchMultithreadWindows.h
|
||||
win32/ArchNetworkWinsock.cpp
|
||||
win32/ArchNetworkWinsock.h
|
||||
win32/ArchSleepWindows.cpp
|
||||
win32/ArchSleepWindows.h
|
||||
win32/ArchTimeWindows.cpp
|
||||
win32/ArchTimeWindows.h
|
||||
win32/XArchWindows.cpp
|
||||
@ -34,8 +32,6 @@ elseif(UNIX)
|
||||
unix/ArchMultithreadPosix.h
|
||||
unix/ArchNetworkBSD.cpp
|
||||
unix/ArchNetworkBSD.h
|
||||
unix/ArchSleepUnix.cpp
|
||||
unix/ArchSleepUnix.h
|
||||
unix/ArchTimeUnix.cpp
|
||||
unix/ArchTimeUnix.h
|
||||
unix/XArchUnix.cpp
|
||||
@ -52,7 +48,6 @@ add_library(arch STATIC ${PLATFORM_CODE}
|
||||
IArchLog.h
|
||||
IArchMultithread.h
|
||||
IArchNetwork.h
|
||||
IArchSleep.h
|
||||
ArchString.cpp
|
||||
ArchString.h
|
||||
IArchTime.h
|
||||
|
||||
@ -1,34 +0,0 @@
|
||||
/*
|
||||
* 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
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "common/IInterface.h"
|
||||
|
||||
//! Interface for architecture dependent sleeping
|
||||
/*!
|
||||
This interface defines the sleep operations required by
|
||||
deskflow. Each architecture must implement this interface.
|
||||
*/
|
||||
class IArchSleep : public IInterface
|
||||
{
|
||||
public:
|
||||
//! @name manipulators
|
||||
//@{
|
||||
|
||||
//! Sleep
|
||||
/*!
|
||||
Blocks the calling thread for \c timeout seconds. If
|
||||
\c timeout < 0.0 then the call returns immediately. If \c timeout
|
||||
== 0.0 then the calling thread yields the CPU.
|
||||
|
||||
(cancellation point)
|
||||
*/
|
||||
virtual void sleep(double timeout) = 0;
|
||||
|
||||
//@}
|
||||
};
|
||||
@ -1,70 +0,0 @@
|
||||
/*
|
||||
* 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
|
||||
*/
|
||||
|
||||
#include "arch/unix/ArchSleepUnix.h"
|
||||
|
||||
#include "arch/Arch.h"
|
||||
|
||||
#if TIME_WITH_SYS_TIME
|
||||
#include <sys/time.h>
|
||||
#include <time.h>
|
||||
#else
|
||||
#if HAVE_SYS_TIME_H
|
||||
#include <sys/time.h>
|
||||
#else
|
||||
#include <time.h>
|
||||
#endif
|
||||
#endif
|
||||
#if !HAVE_NANOSLEEP
|
||||
#if HAVE_SYS_SELECT_H
|
||||
#include <sys/select.h>
|
||||
#endif
|
||||
#if HAVE_SYS_TYPES_H
|
||||
#include <sys/types.h>
|
||||
#endif
|
||||
#if HAVE_UNISTD_H
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
#endif
|
||||
|
||||
//
|
||||
// ArchSleepUnix
|
||||
//
|
||||
|
||||
void ArchSleepUnix::sleep(double timeout)
|
||||
{
|
||||
ARCH->testCancelThread();
|
||||
if (timeout < 0.0) {
|
||||
return;
|
||||
}
|
||||
|
||||
#if HAVE_NANOSLEEP
|
||||
// prep timeout
|
||||
struct timespec t;
|
||||
t.tv_sec = (long)timeout;
|
||||
t.tv_nsec = (long)(1.0e+9 * (timeout - (double)t.tv_sec));
|
||||
|
||||
// wait
|
||||
while (nanosleep(&t, &t) < 0)
|
||||
ARCH->testCancelThread();
|
||||
#else
|
||||
/* emulate nanosleep() with select() */
|
||||
double startTime = ARCH->time();
|
||||
double timeLeft = timeout;
|
||||
while (timeLeft > 0.0) {
|
||||
struct timeval timeout2;
|
||||
timeout2.tv_sec = static_cast<int>(timeLeft);
|
||||
timeout2.tv_usec = static_cast<int>(1.0e+6 * (timeLeft - timeout2.tv_sec));
|
||||
select(
|
||||
(SELECT_TYPE_ARG1)0, SELECT_TYPE_ARG234 nullptr, SELECT_TYPE_ARG234 nullptr, SELECT_TYPE_ARG234 nullptr,
|
||||
SELECT_TYPE_ARG5 & timeout2
|
||||
);
|
||||
ARCH->testCancelThread();
|
||||
timeLeft = timeout - (ARCH->time() - startTime);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@ -1,23 +0,0 @@
|
||||
/*
|
||||
* 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
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "arch/IArchSleep.h"
|
||||
|
||||
#define ARCH_SLEEP ArchSleepUnix
|
||||
|
||||
//! Unix implementation of IArchSleep
|
||||
class ArchSleepUnix : public IArchSleep
|
||||
{
|
||||
public:
|
||||
ArchSleepUnix() = default;
|
||||
~ArchSleepUnix() override = default;
|
||||
|
||||
// IArchSleep overrides
|
||||
void sleep(double timeout) override;
|
||||
};
|
||||
@ -1,38 +0,0 @@
|
||||
/*
|
||||
* 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
|
||||
*/
|
||||
|
||||
#include "arch/win32/ArchSleepWindows.h"
|
||||
#include "arch/Arch.h"
|
||||
#include "arch/win32/ArchMultithreadWindows.h"
|
||||
|
||||
//
|
||||
// ArchSleepWindows
|
||||
//
|
||||
|
||||
void ArchSleepWindows::sleep(double timeout)
|
||||
{
|
||||
ARCH->testCancelThread();
|
||||
if (timeout < 0.0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// get the cancel event from the current thread. this only
|
||||
// works if we're using the windows multithread object but
|
||||
// this is windows so that's pretty certain; we'll get a
|
||||
// link error if we're not, though.
|
||||
ArchMultithreadWindows *mt = ArchMultithreadWindows::getInstance();
|
||||
if (mt != nullptr) {
|
||||
HANDLE cancelEvent = mt->getCancelEventForCurrentThread();
|
||||
WaitForSingleObject(cancelEvent, (DWORD)(1000.0 * timeout));
|
||||
if (timeout == 0.0) {
|
||||
Sleep(0);
|
||||
}
|
||||
} else {
|
||||
Sleep((DWORD)(1000.0 * timeout));
|
||||
}
|
||||
ARCH->testCancelThread();
|
||||
}
|
||||
@ -1,23 +0,0 @@
|
||||
/*
|
||||
* 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
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "arch/IArchSleep.h"
|
||||
|
||||
#define ARCH_SLEEP ArchSleepWindows
|
||||
|
||||
//! Win32 implementation of IArchSleep
|
||||
class ArchSleepWindows : public IArchSleep
|
||||
{
|
||||
public:
|
||||
ArchSleepWindows() = default;
|
||||
~ArchSleepWindows() override = default;
|
||||
|
||||
// IArchSleep overrides
|
||||
void sleep(double timeout) override;
|
||||
};
|
||||
Reference in New Issue
Block a user