[posix] use real time signal for microsecond timer (#5122)

This commit is contained in:
Yakun Xu
2020-06-19 16:12:55 -07:00
committed by GitHub
parent b66ac5d907
commit 8248c3d9df
11 changed files with 160 additions and 35 deletions
+13 -17
View File
@@ -36,13 +36,7 @@
#include "utils/code_utils.h"
#ifndef __linux__
#define __linux__ 0
#endif
// linux microsecond timer
#if __linux__
#ifdef __linux__
#include <signal.h>
#include <time.h>
@@ -67,6 +61,12 @@ timer_t sMicroTimer;
#define DEFAULT_TIMEOUT 10 // seconds
#ifdef CLOCK_MONOTONIC_RAW
#define OT_SIMULATION_CLOCK_ID CLOCK_MONOTONIC_RAW
#else
#define OT_SIMULATION_CLOCK_ID CLOCK_MONOTONIC
#endif
static bool sIsMsRunning = false;
static uint32_t sMsAlarm = 0;
@@ -75,7 +75,7 @@ static uint32_t sUsAlarm = 0;
static uint32_t sSpeedUpFactor = 1;
#if __linux__
#ifdef __linux__
static void microTimerHandler(int aSignal, siginfo_t *aSignalInfo, void *aUserContext)
{
assert(aSignal == OPENTHREAD_CONFIG_MICRO_TIMER_SIGNAL);
@@ -90,7 +90,7 @@ void platformAlarmInit(uint32_t aSpeedUpFactor)
{
sSpeedUpFactor = aSpeedUpFactor;
#if __linux__
#ifdef __linux__
{
struct sigaction sa;
@@ -110,7 +110,7 @@ void platformAlarmInit(uint32_t aSpeedUpFactor)
sev.sigev_signo = OPENTHREAD_CONFIG_MICRO_TIMER_SIGNAL;
sev.sigev_value.sival_ptr = &sMicroTimer;
if (-1 == timer_create(CLOCK_REALTIME, &sev, &sMicroTimer))
if (-1 == timer_create(CLOCK_MONOTONIC, &sev, &sMicroTimer))
{
perror("timer_create");
exit(EXIT_FAILURE);
@@ -125,11 +125,7 @@ uint64_t platformGetNow(void)
struct timespec now;
int err;
#ifdef CLOCK_MONOTONIC_RAW
err = clock_gettime(CLOCK_MONOTONIC_RAW, &now);
#else
err = clock_gettime(CLOCK_MONOTONIC, &now);
#endif
err = clock_gettime(OT_SIMULATION_CLOCK_ID, &now);
VerifyOrDie(err == 0, OT_EXIT_ERROR_ERRNO);
@@ -181,7 +177,7 @@ void otPlatAlarmMicroStartAt(otInstance *aInstance, uint32_t aT0, uint32_t aDt)
sUsAlarm = aT0 + aDt;
sIsUsRunning = true;
#if __linux__
#ifdef __linux__
{
struct itimerspec its;
uint32_t diff = sUsAlarm - otPlatAlarmMicroGetNow();
@@ -207,7 +203,7 @@ void otPlatAlarmMicroStop(otInstance *aInstance)
sIsUsRunning = false;
#if __linux__
#ifdef __linux__
{
struct itimerspec its = {{0, 0}, {0, 0}};