[+] Initial Linux exit signal handler

[*] Linux Semaphore bug (apparently I can't write loops)
[*] Linux sleep bug
This commit is contained in:
Reece Wilson 2022-04-07 05:35:17 +01:00
parent d3428f4cd9
commit d81d4564e9
9 changed files with 91 additions and 11 deletions

50
Source/Exit/Exit.Unix.cpp Normal file
View File

@ -0,0 +1,50 @@
/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: Exit.Unix.cpp
Date: 2022-4-7
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "Exit.hpp"
#include "Exit.Unix.hpp"
#include <Source/Grug/Grug.hpp>
namespace Aurora::Exit
{
static void SendExitSignal(Grug::Arrow *)
{
PostLevel(AuThreads::GetThread(), Exit::ETriggerLevel::eSigTerminate);
}
static void HandleSigTerminate(int sig)
{
Grug::Arrow arrow;
Grug::HurlArrow(&arrow, SendExitSignal, {});
Grug::ArrowWait(&arrow);
}
void InitUnix()
{
struct sigaction action =
{
.sa_handler = HandleSigTerminate,
.sa_flags = SA_ONSTACK
};
sigemptyset(&action.sa_mask);
sigaction(SIGINT, &action, nullptr);
}
void DeinitUnix()
{
struct sigaction action =
{
.sa_handler = SIG_DFL,
.sa_flags = SA_ONSTACK
};
sigemptyset(&action.sa_mask);
sigaction(SIGINT, &action, nullptr);
}
}

13
Source/Exit/Exit.Unix.hpp Normal file
View File

@ -0,0 +1,13 @@
/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: Exit.Unix.hpp
Date: 2022-4-7
Author: Reece
***/
#pragma once
namespace Aurora::Exit
{
void InitUnix();
void DeinitUnix();
}

View File

@ -9,6 +9,7 @@
#include "Exit.hpp" #include "Exit.hpp"
#include <Source/Grug/Grug.hpp> #include <Source/Grug/Grug.hpp>
#include "MTWatchDog.hpp" #include "MTWatchDog.hpp"
#include "Exit.Unix.hpp"
namespace Aurora::Exit namespace Aurora::Exit
{ {
@ -126,10 +127,18 @@ namespace Aurora::Exit
{ {
gMutex = AuThreadPrimitives::MutexUnique(); gMutex = AuThreadPrimitives::MutexUnique();
InitWatchdog(); InitWatchdog();
#if defined(AURORA_IS_LINUX_DERIVED)
InitUnix();
#endif
} }
void DeinitExit() void DeinitExit()
{ {
#if defined(AURORA_IS_LINUX_DERIVED)
DeinitUnix();
#endif
gMutex.reset(); gMutex.reset();
} }
} }

View File

@ -154,6 +154,7 @@ namespace Aurora::Grug
return; return;
} }
//AU_LOCK_GUARD(gMutex);
gCondVar->Signal(); gCondVar->Signal();
} }

View File

@ -42,11 +42,10 @@ namespace Aurora::Threading::Primitives
} }
else else
{ {
struct timespec tspec; struct timespec tspec, start;
Time::ms2tsabs(&tspec, timeout); Time::ms2tsabs(&tspec, timeout);
auto ret = pthread_cond_timedwait(&pthreadCv_, mutex, &tspec); auto ret = pthread_cond_timedwait(&pthreadCv_, mutex, &tspec);
if (ret != 0) if (ret != 0)
{ {
SysAssert(errno == ETIMEDOUT, "conditional timed wait failed"); SysAssert(errno == ETIMEDOUT, "conditional timed wait failed");

View File

@ -74,7 +74,7 @@ namespace Aurora::Threading::Primitives
void Semaphore::Unlock(long count) void Semaphore::Unlock(long count)
{ {
for (int i = 0; i++; i < count) for (int i = 0; i < count; i++)
{ {
Unlock(); Unlock();
} }

View File

@ -18,7 +18,7 @@ namespace Aurora::Threading
AUKN_SYM void Sleep(AuUInt64 timeout) AUKN_SYM void Sleep(AuUInt64 timeout)
{ {
#if defined(AURORA_IS_LINUX_DERIVED) #if defined(AURORA_IS_LINUX_DERIVED)
usleep(timeout * 1000000); usleep(timeout * 1000);
#elif defined(AURORA_IS_MODERNNT_DERIVED) #elif defined(AURORA_IS_MODERNNT_DERIVED)
::Sleep(timeout); ::Sleep(timeout);
#else #else
@ -31,7 +31,7 @@ namespace Aurora::Threading
AUKN_SYM void SleepNs(AuUInt64 timeout) AUKN_SYM void SleepNs(AuUInt64 timeout)
{ {
#if defined(AURORA_IS_LINUX_DERIVED) || defined(AURORA_IS_BSD_DERIVED) #if defined(AURORA_IS_LINUX_DERIVED) || defined(AURORA_IS_BSD_DERIVED)
usleep(timeout); usleep(timeout / 1000);
#else #else
auto status = YieldPollNs(true, timeout + Time::CurrentInternalClockNS(), [=]() auto status = YieldPollNs(true, timeout + Time::CurrentInternalClockNS(), [=]()
{ {

View File

@ -7,6 +7,10 @@
***/ ***/
#pragma once #pragma once
#if defined(AURORA_IS_POSIX_DERIVED)
#include <time.h>
#endif
namespace Aurora::Time namespace Aurora::Time
{ {
#if defined(AURORA_PLATFORM_WIN32) #if defined(AURORA_PLATFORM_WIN32)
@ -35,9 +39,13 @@ namespace Aurora::Time
static void ms2tsabs(struct timespec *ts, unsigned long ms) static void ms2tsabs(struct timespec *ts, unsigned long ms)
{ {
ms += ConvertAuroraToUnixMS(CurrentClockMS()); clock_gettime(CLOCK_REALTIME, ts);
ts->tv_sec = ms / 1000;
ts->tv_nsec = (ms % 1000) * 1000000; auto baseNS = ((AuUInt64)ms * (AuUInt64)1'000'000) + (AuUInt64)ts->tv_nsec;
auto remainderNS = (AuUInt64)baseNS % (AuUInt64)1'000'000'000;
ts->tv_sec += baseNS / 1'000'000'000ull;
ts->tv_nsec = remainderNS;
} }
#endif #endif