Skip to content

Commit

Permalink
Fix nasa#104, Remove OS_Tick2Micros and internalize OS_Milli2Ticks
Browse files Browse the repository at this point in the history
- Removed OS_Tick2Micros implementation, tests, stubs, references
- Moved prototype from API to internal for OS_Milli2Ticks
- Updated OS_Milli2Ticks to return status
- Added check for rollover in OS_Milli2Ticks
- OS_Milli2Ticks now sets and limits ticks as int
- Updated all internal use of OS_Milli2Ticks to check for error
  and returns immediately on error (won't wait maximum amount)
- Coverage tests updated to check for new error cases
- OS_Milli2Ticks stub updated (default implementation)
  • Loading branch information
skliper committed Sep 22, 2020
1 parent 4f7e201 commit 4b82abe
Show file tree
Hide file tree
Showing 20 changed files with 121 additions and 220 deletions.
30 changes: 1 addition & 29 deletions src/os/inc/osapi-os-core.h
Original file line number Diff line number Diff line change
Expand Up @@ -1222,38 +1222,10 @@ int32 OS_MutSemGetIdByName (osal_id_t *sem_id, const char *sem_name);
int32 OS_MutSemGetInfo (osal_id_t sem_id, OS_mut_sem_prop_t *mut_prop);
/**@}*/

/** @defgroup OSAPITime OSAL Time/Tick APIs
/** @defgroup OSAPITime OSAL Time APIs
* @{
*/

/*-------------------------------------------------------------------------------------*/
/**
* @brief Convert time units from milliseconds to system ticks
*
* This function accepts a time interval in milliseconds and
* returns the tick equivalent. If the result is not an exact
* number of system ticks, the result will be rounded up to
* the nearest tick.
*
* @param[in] milli_seconds the number of milliseconds
*
* @return The number of ticks
*/
int32 OS_Milli2Ticks (uint32 milli_seconds);

/*-------------------------------------------------------------------------------------*/
/**
* @brief Get the system tick size, in microseconds
*
* This function returns the duration of a system tick in micro seconds
*
* @note care is taken to ensure this does not return "0" since it is often used
* as the divisor in mathematical operations
*
* @return Duration of a system tick in microseconds
*/
int32 OS_Tick2Micros (void);

/*-------------------------------------------------------------------------------------*/
/**
* @brief Get the local time
Expand Down
1 change: 0 additions & 1 deletion src/os/posix/src/os-impl-timebase.c
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,6 @@ int32 OS_Posix_TimeBaseAPI_Impl_Init(void)

/*
* Pre-calculate the clock tick to microsecond conversion factor.
* This is used by OS_Tick2Micros(), OS_Milli2Ticks(), etc.
*/
OS_SharedGlobalVars.TicksPerSecond = sysconf(_SC_CLK_TCK);
if (OS_SharedGlobalVars.TicksPerSecond <= 0)
Expand Down
7 changes: 5 additions & 2 deletions src/os/rtems/src/os-impl-binsem.c
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,12 @@ int32 OS_BinSemTake_Impl (uint32 sem_id)
int32 OS_BinSemTimedWait_Impl (uint32 sem_id, uint32 msecs)
{
rtems_status_code status;
uint32 TimeInTicks;
int TimeInTicks;

TimeInTicks = OS_Milli2Ticks(msecs);
if (OS_Milli2Ticks(msecs, &TimInTicks) != OS_SUCCESS)
{
return OS_ERROR;
}

status = rtems_semaphore_obtain(OS_impl_bin_sem_table[sem_id].id, RTEMS_WAIT, TimeInTicks) ;

Expand Down
2 changes: 1 addition & 1 deletion src/os/rtems/src/os-impl-timebase.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ int32 OS_Rtems_TimeBaseAPI_Impl_Init ( void )


/*
* Finally compute the Microseconds per tick that is used for OS_Tick2Micros() call
* Finally compute the Microseconds per tick
* This must further round again to the nearest microsecond, so it is undesirable to use
* this for time computations if the result is not exact.
*/
Expand Down
6 changes: 6 additions & 0 deletions src/os/shared/inc/os-shared-timebase.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ int32 OS_TimeBaseGetInfo_Impl (uint32 timer_id, OS_timebase_prop_t *timer_
------------------------------------------------------------------*/
void OS_TimeBase_CallbackThread (osal_id_t timebase_id);

/*----------------------------------------------------------------
Function: OS_Milli2Ticks
Purpose: Convert milliseconds to ticks
------------------------------------------------------------------*/
int32 OS_Milli2Ticks(uint32 milli_seconds, int *ticks);

#endif /* INCLUDE_OS_SHARED_TIMEBASE_H_ */

55 changes: 19 additions & 36 deletions src/os/shared/src/osapi-timebase.c
Original file line number Diff line number Diff line change
Expand Up @@ -545,49 +545,32 @@ void OS_TimeBase_CallbackThread(osal_id_t timebase_id)
}
} /* end OS_TimeBase_CallbackThread */

/****************************************************************************************
Other Time-Related API Implementation
***************************************************************************************/

/*
* This is the OSAL-defined interface to the OS Timer tick -
* Not sure what it is really useful for since none of the user API timer calls deal with
* OS ticks directly.
*/


/*----------------------------------------------------------------
*
* Function: OS_Tick2Micros
*
* Purpose: Implemented per public OSAL API
* See description in API and header file for detail
*
*-----------------------------------------------------------------*/
int32 OS_Tick2Micros (void)
{
return (OS_SharedGlobalVars.MicroSecPerTick);
} /* end OS_Tick2Micros */


/*----------------------------------------------------------------
*
* Function: OS_Milli2Ticks
*
* Purpose: Implemented per public OSAL API
* See description in API and header file for detail
* Purpose: Internal helper to convert milliseconds to ticks
*
* Returns: OS_SUCCESS on success, OS_ERROR on failure (rollover)
*
*-----------------------------------------------------------------*/
int32 OS_Milli2Ticks(uint32 milli_seconds)
int32 OS_Milli2Ticks(uint32 milli_seconds, int *ticks)
{
unsigned long num_of_ticks;

num_of_ticks = (unsigned long)milli_seconds;
num_of_ticks *= OS_SharedGlobalVars.TicksPerSecond;
num_of_ticks = (num_of_ticks + 999) / 1000;

return((uint32)num_of_ticks);
} /* end OS_Milli2Ticks */
uint64 num_of_ticks;
int32 return_code = OS_SUCCESS;

num_of_ticks = (((uint64)milli_seconds * OS_SharedGlobalVars.TicksPerSecond) + 999) / 1000;

/* Check against maximum int32 (limit from some OS's) */
if (num_of_ticks <= INT_MAX)
{
*ticks = (int)num_of_ticks;
}
else
{
return_code = OS_ERROR;
*ticks = 0;
}

return return_code;
} /* end OS_Milli2Ticks */
13 changes: 12 additions & 1 deletion src/os/vxworks/src/os-impl-binsem.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include "os-impl-binsem.h"
#include "os-shared-binsem.h"
#include "os-shared-timebase.h"

/****************************************************************************************
DEFINES
Expand Down Expand Up @@ -175,7 +176,17 @@ int32 OS_BinSemTake_Impl (uint32 sem_id)
*-----------------------------------------------------------------*/
int32 OS_BinSemTimedWait_Impl (uint32 sem_id, uint32 msecs)
{
return OS_VxWorks_GenericSemTake(OS_impl_bin_sem_table[sem_id].vxid, OS_Milli2Ticks(msecs));
int ticks;
int32 status;

status = OS_Milli2Ticks(msecs, &ticks);

if (status == OS_SUCCESS)
{
status = OS_VxWorks_GenericSemTake(OS_impl_bin_sem_table[sem_id].vxid, ticks);
}

return status;
} /* end OS_BinSemTimedWait_Impl */


Expand Down
14 changes: 12 additions & 2 deletions src/os/vxworks/src/os-impl-countsem.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "os-vxworks.h"
#include "os-impl-countsem.h"
#include "os-shared-countsem.h"
#include "os-shared-timebase.h"

/****************************************************************************************
DEFINES
Expand Down Expand Up @@ -153,8 +154,17 @@ int32 OS_CountSemTake_Impl (uint32 sem_id)
*-----------------------------------------------------------------*/
int32 OS_CountSemTimedWait_Impl (uint32 sem_id, uint32 msecs)
{
return OS_VxWorks_GenericSemTake(OS_impl_count_sem_table[sem_id].vxid,
OS_Milli2Ticks(msecs));
int ticks;
int32 status;

status = OS_Milli2Ticks(msecs, &ticks);

if (status == OS_SUCCESS)
{
status = OS_VxWorks_GenericSemTake(OS_impl_count_sem_table[sem_id].vxid, ticks);
}

return status;
} /* end OS_CountSemTimedWait_Impl */


Expand Down
8 changes: 6 additions & 2 deletions src/os/vxworks/src/os-impl-queues.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "os-vxworks.h"
#include "os-impl-queues.h"
#include "os-shared-queue.h"
#include "os-shared-timebase.h"


/****************************************************************************************
Expand Down Expand Up @@ -137,8 +138,11 @@ int32 OS_QueueGet_Impl (uint32 queue_id, void *data, uint32 size, uint32 *size_c
}
else
{
/* msecs rounded to the closest system tick count */
ticks = OS_Milli2Ticks(timeout);
/* msecs rounded to the closest system tick count if possible */
if (OS_Milli2Ticks(timeout, &ticks) != OS_SUCCESS)
{
return OS_ERROR;
}
}

status = msgQReceive(OS_impl_queue_table[queue_id].vxid, data, size, ticks);
Expand Down
7 changes: 6 additions & 1 deletion src/os/vxworks/src/os-impl-tasks.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

#include "os-shared-task.h"
#include "os-shared-idmap.h"
#include "os-shared-timebase.h"

#include <errnoLib.h>
#include <taskLib.h>
Expand Down Expand Up @@ -323,7 +324,11 @@ int32 OS_TaskDelay_Impl (uint32 milli_second)
/* msecs rounded to the closest system tick count */
int sys_ticks;

sys_ticks = OS_Milli2Ticks(milli_second);
/* Convert to ticks if possible */
if (OS_Milli2Ticks(milli_second, &sys_ticks) != OS_SUCCESS)
{
return OS_ERROR;
}

/* if successful, the execution of task will pend here until delay finishes */
if(taskDelay(sys_ticks) != OK)
Expand Down
2 changes: 1 addition & 1 deletion src/os/vxworks/src/os-impl-timebase.c
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ int32 OS_VxWorks_TimeBaseAPI_Impl_Init ( void )


/*
* Finally compute the Microseconds per tick that is used for OS_Tick2Micros() call
* Finally compute the Microseconds per tick
* This must further round again to the nearest microsecond, so it is undesirable to use
* this for time computations if the result is not exact.
*/
Expand Down
40 changes: 24 additions & 16 deletions src/unit-test-coverage/shared/src/coveragetest-timebase.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "os-shared-common.h"

#include <OCS_string.h>
#include <limits.h>

static uint32 TimerSyncCount = 0;
static uint32 TimerSyncRetVal = 0;
Expand Down Expand Up @@ -293,28 +294,36 @@ void Test_OS_TimeBase_CallbackThread(void)
OS_TimeBase_CallbackThread(UT_OBJID_2);
}

void Test_OS_Tick2Micros(void)
{
/*
* Test Case For:
* int32 OS_Tick2Micros (void)
*/
OS_SharedGlobalVars.MicroSecPerTick = 5555;
int32 actual = OS_Tick2Micros();

UtAssert_True(actual == 5555, "OS_Tick2Micros() (%ld) == 5555", (long)actual);
}

void Test_OS_Milli2Ticks(void)
{
/*
* Test Case For:
* int32 OS_Milli2Ticks(uint32 milli_seconds)
*/
OS_SharedGlobalVars.TicksPerSecond = 500;
int32 actual = OS_Milli2Ticks(5678);
uint32 msec;
int ticks;
int expected;

UtAssert_True(actual == 2839, "OS_Milli2Ticks() (%ld) == 2839", (long)actual);
msec = 5678;
OS_SharedGlobalVars.TicksPerSecond = 500;
UtAssert_INT32_EQ(OS_Milli2Ticks(msec, &ticks), OS_SUCCESS);
UtAssert_INT32_EQ(ticks, 2839);

/* Bigger than uint32 but valid case */
msec = UINT_MAX - 1;
expected = (((uint64)msec * OS_SharedGlobalVars.TicksPerSecond) + 999) / 1000;
UtAssert_INT32_EQ(OS_Milli2Ticks(msec, &ticks), OS_SUCCESS);
UtAssert_INT32_EQ(ticks, expected);

/* int rollover case */
msec = UINT_MAX;
UtAssert_INT32_EQ(OS_Milli2Ticks(msec, &ticks), OS_ERROR);
UtAssert_INT32_EQ(ticks, 0);

/* Max value rollover case */
OS_SharedGlobalVars.TicksPerSecond = INT_MAX;
UtAssert_INT32_EQ(OS_Milli2Ticks(msec, &ticks), OS_ERROR);
UtAssert_INT32_EQ(ticks, 0);
}


Expand Down Expand Up @@ -354,7 +363,6 @@ void UtTest_Setup(void)
ADD_TEST(OS_TimeBaseGetInfo);
ADD_TEST(OS_TimeBaseGetFreeRun);
ADD_TEST(OS_TimeBase_CallbackThread);
ADD_TEST(OS_Tick2Micros);
ADD_TEST(OS_Milli2Ticks);
}

Expand Down
4 changes: 4 additions & 0 deletions src/unit-test-coverage/vxworks/src/coveragetest-binsem.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include "os-shared-binsem.h"
#include "os-shared-idmap.h"
#include "os-shared-timebase.h"

#include <OCS_errno.h>
#include <OCS_objLib.h>
Expand Down Expand Up @@ -108,6 +109,9 @@ void Test_OS_BinSemTimedWait_Impl(void)

UT_SetForceFail(UT_StubKey_GenericSemTake, OS_SEM_FAILURE);
OSAPI_TEST_FUNCTION_RC(OS_BinSemTimedWait_Impl(0,100), OS_SEM_FAILURE);

UT_SetForceFail(UT_KEY(OS_Milli2Ticks), OS_ERROR);
OSAPI_TEST_FUNCTION_RC(OS_BinSemTimedWait_Impl(0,100), OS_ERROR);
}

void Test_OS_BinSemGetInfo_Impl(void)
Expand Down
4 changes: 4 additions & 0 deletions src/unit-test-coverage/vxworks/src/coveragetest-countsem.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "ut-adaptor-countsem.h"

#include "os-shared-countsem.h"
#include "os-shared-timebase.h"

void Test_OS_VxWorks_CountSemAPI_Impl_Init(void)
{
Expand Down Expand Up @@ -85,6 +86,9 @@ void Test_OS_CountSemTimedWait_Impl(void)
* int32 OS_CountSemTimedWait_Impl ( uint32 sem_id, uint32 msecs )
*/
OSAPI_TEST_FUNCTION_RC(OS_CountSemTimedWait_Impl(0, 100), OS_SUCCESS);

UT_SetForceFail(UT_KEY(OS_Milli2Ticks), OS_ERROR);
OSAPI_TEST_FUNCTION_RC(OS_CountSemTimedWait_Impl(0,100), OS_ERROR);
}

void Test_OS_CountSemGetInfo_Impl(void)
Expand Down
4 changes: 4 additions & 0 deletions src/unit-test-coverage/vxworks/src/coveragetest-queues.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "os-vxworks-coveragetest.h"
#include "ut-adaptor-queues.h"
#include "os-shared-queue.h"
#include "os-shared-timebase.h"

#include <OCS_msgQLib.h>
#include <OCS_errno.h>
Expand Down Expand Up @@ -77,6 +78,9 @@ void Test_OS_QueueGet_Impl(void)
OSAPI_TEST_FUNCTION_RC(OS_QueueGet_Impl(0, &Data, sizeof(Data), &ActSz, OS_CHECK), OS_SUCCESS);
OSAPI_TEST_FUNCTION_RC(OS_QueueGet_Impl(0, &Data, sizeof(Data), &ActSz, 100), OS_SUCCESS);

UT_SetForceFail(UT_KEY(OS_Milli2Ticks), OS_ERROR);
OSAPI_TEST_FUNCTION_RC(OS_QueueGet_Impl(0, &Data, sizeof(Data), &ActSz, 100), OS_ERROR);

UT_SetForceFail(UT_KEY(OCS_msgQReceive), OCS_ERROR);
OCS_errno = OCS_S_objLib_OBJ_TIMEOUT;
OSAPI_TEST_FUNCTION_RC(OS_QueueGet_Impl(0, &Data, sizeof(Data), &ActSz, OS_CHECK), OS_QUEUE_TIMEOUT);
Expand Down
Loading

0 comments on commit 4b82abe

Please sign in to comment.