Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear pending signal when configuring timer #336

Merged
merged 1 commit into from
Jan 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/os/posix/ostimer.c
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ int32 OS_TimeBaseCreate_Impl(uint32 timer_id)
int status;
int i;
struct sigevent evp;
struct timespec ts;
OS_impl_timebase_internal_record_t *local;
OS_common_record_t *global;
OS_U32ValueWrapper_t arg;
Expand Down Expand Up @@ -403,6 +404,34 @@ int32 OS_TimeBaseCreate_Impl(uint32 timer_id)
sigemptyset(&local->sigset);
sigaddset(&local->sigset, local->assigned_signal);

/*
* Ensure that the chosen signal is NOT already pending.
*
* Perform a "sigtimedwait" with a zero timeout to poll the
* status of the selected signal. RT signals are also queued,
* so this needs to be called in a loop to until sigtimedwait()
* returns an error.
*
* The max number of signals that can be queued is available
* via sysconf() as the _SC_SIGQUEUE_MAX value.
*
* The output is irrelevant here; the objective is to just ensure
* that the signal is not already pending.
*/
i = sysconf( _SC_SIGQUEUE_MAX);
do
{
ts.tv_sec = 0;
ts.tv_nsec = 0;
if (sigtimedwait(&local->sigset, NULL, &ts) < 0)
{
/* signal is NOT pending */
break;
}
--i;
}
while(i > 0);

/*
** Initialize the sigevent structures for the handler.
*/
Expand Down
18 changes: 5 additions & 13 deletions src/unit-tests/ostimer-test/ut_ostimer_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,8 @@ void UT_os_timercallback(uint32 timerId)

OS_GetLocalTime(&endTime);

if (endTime.seconds == currTime.seconds)
currIntervalTime = endTime.microsecs - currTime.microsecs;
else
currIntervalTime = endTime.microsecs + (1000000 - currTime.microsecs);
currIntervalTime = 1000000 * (endTime.seconds - currTime.seconds) +
endTime.microsecs - currTime.microsecs;

if (currIntervalTime >= prevIntervalTime)
deltaTime = currIntervalTime - prevIntervalTime;
Expand All @@ -102,18 +100,12 @@ void UT_os_timercallback(uint32 timerId)
res = -1;

loopCnt++;
currTime = endTime;
prevIntervalTime = currIntervalTime;

if (loopCnt < g_cbLoopCntMax)
{
currTime = endTime;
prevIntervalTime = currIntervalTime;
}
else
if (loopCnt == g_cbLoopCntMax)
{
g_status = (res == 0) ? 1 : -1;

/* slow the timer down so the main test thread can continue */
res = OS_TimerSet(g_timerId, 1000, 500000);
}
}
}
Expand Down