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

Fix #12, Comment update to correct for microseconds not always rounding up + a… #406

Merged
merged 1 commit into from
Apr 21, 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
7 changes: 4 additions & 3 deletions src/os/posix/ostimer.c
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,10 @@ int32 OS_Posix_TimeBaseAPI_Impl_Init(void)
}

/*
* Calculate microseconds per tick - Rounding UP
* - If the ratio is not an integer, this will choose the next higher value
* - The result is guaranteed not to be zero.
* Calculate microseconds per tick
* - If the ratio is not an integer, this will round to the nearest integer value
* - This is used internally for reporting accuracy,
* - TicksPerSecond values over 2M will return zero
*/
OS_SharedGlobalVars.MicroSecPerTick = (1000000 + (OS_SharedGlobalVars.TicksPerSecond / 2)) /
OS_SharedGlobalVars.TicksPerSecond;
Expand Down
9 changes: 9 additions & 0 deletions src/os/shared/osapi-common.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ int32 OS_API_Init(void)
{
int32 return_code = OS_SUCCESS;
uint32 idtype;
uint32 microSecPerSec;

if (OS_SharedGlobalVars.Initialized != false)
{
Expand Down Expand Up @@ -161,6 +162,14 @@ int32 OS_API_Init(void)
return_code = OS_ERROR;
}

microSecPerSec = OS_SharedGlobalVars.MicroSecPerTick * OS_SharedGlobalVars.TicksPerSecond;

if ( microSecPerSec != 1000000 )
{
OS_DEBUG("Warning: Microsecs per sec value of %lu does not equal 1000000 (MicroSecPerTick: %ld TicksPerSecond: %ld)\n",
(unsigned long) microSecPerSec, (long) OS_SharedGlobalVars.MicroSecPerTick, (long) OS_SharedGlobalVars.TicksPerSecond);
}

return(return_code);
} /* end OS_API_Init */

Expand Down
5 changes: 5 additions & 0 deletions src/unit-test-coverage/shared/src/coveragetest-common.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ void Test_OS_API_Init(void)
OS_SharedGlobalVars.Initialized = false;
OSAPI_TEST_FUNCTION_RC(OS_API_Init(), OS_SUCCESS);

Test_MicroSecPerTick = 1000;
Test_TicksPerSecond = 1001;
OS_SharedGlobalVars.Initialized = false;
OSAPI_TEST_FUNCTION_RC(OS_API_Init(), OS_SUCCESS);

/* Second call should return ERROR */
OSAPI_TEST_FUNCTION_RC(OS_API_Init(), OS_ERROR);

Expand Down