<div dir="ltr">bump<br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, 29 Jul 2021 at 00:28, Zacchaeus Leung <<a href="mailto:zakthertemsdev@gmail.com">zakthertemsdev@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">the timer_create() method can use CLOCK_MONOTONIC but there was no test for this<br>
<br>
Closes #3888<br>
---<br>
cpukit/include/rtems/posix/timer.h | 1 +<br>
cpukit/posix/src/psxtimercreate.c | 3 +-<br>
cpukit/posix/src/timergettime.c | 52 ++++++++++++++---------<br>
testsuites/psxtests/psxtimer02/psxtimer.c | 26 ++++++++++++<br>
4 files changed, 60 insertions(+), 22 deletions(-)<br>
<br>
diff --git a/cpukit/include/rtems/posix/timer.h b/cpukit/include/rtems/posix/timer.h<br>
index bcbf07a65a..7ae089173a 100644<br>
--- a/cpukit/include/rtems/posix/timer.h<br>
+++ b/cpukit/include/rtems/posix/timer.h<br>
@@ -48,6 +48,7 @@ typedef struct {<br>
uint32_t ticks; /* Number of ticks of the initialization */<br>
uint32_t overrun; /* Number of expirations of the timer */<br>
struct timespec time; /* Time at which the timer was started */<br>
+ clockid_t clock_type; /* The type of timer */<br>
} POSIX_Timer_Control;<br>
<br>
/**<br>
diff --git a/cpukit/posix/src/psxtimercreate.c b/cpukit/posix/src/psxtimercreate.c<br>
index a63cf1d100..2b5a10140f 100644<br>
--- a/cpukit/posix/src/psxtimercreate.c<br>
+++ b/cpukit/posix/src/psxtimercreate.c<br>
@@ -40,7 +40,7 @@ int timer_create(<br>
{<br>
POSIX_Timer_Control *ptimer;<br>
<br>
- if ( clock_id != CLOCK_REALTIME )<br>
+ if ( clock_id != CLOCK_REALTIME && clock_id != CLOCK_MONOTONIC )<br>
rtems_set_errno_and_return_minus_one( EINVAL );<br>
<br>
if ( !timerid )<br>
@@ -91,6 +91,7 @@ int timer_create(<br>
ptimer->timer_data.it_value.tv_nsec = 0;<br>
ptimer->timer_data.it_interval.tv_sec = 0;<br>
ptimer->timer_data.it_interval.tv_nsec = 0;<br>
+ ptimer->clock_type = clock_id;<br>
<br>
_Watchdog_Preinitialize( &ptimer->Timer, _Per_CPU_Get_snapshot() );<br>
_Watchdog_Initialize( &ptimer->Timer, _POSIX_Timer_TSR );<br>
diff --git a/cpukit/posix/src/timergettime.c b/cpukit/posix/src/timergettime.c<br>
index ee2a566f0e..b29d03efa1 100644<br>
--- a/cpukit/posix/src/timergettime.c<br>
+++ b/cpukit/posix/src/timergettime.c<br>
@@ -28,6 +28,7 @@<br>
#include <rtems/score/todimpl.h><br>
#include <rtems/score/watchdogimpl.h><br>
#include <rtems/seterr.h><br>
+#include <rtems/timespec.h><br>
<br>
/*<br>
* - When a timer is initialized, the value of the time in<br>
@@ -39,35 +40,44 @@<br>
int timer_gettime(<br>
timer_t timerid,<br>
struct itimerspec *value<br>
)<br>
<br>
{<br>
POSIX_Timer_Control *ptimer;<br>
- ISR_lock_Context lock_context;<br>
- uint64_t now;<br>
- uint32_t remaining;<br>
+ ISR_lock_Context lock_context;<br>
+ Per_CPU_Control *cpu;<br>
+ struct timespec now;<br>
+ struct timespec expire;<br>
+ struct timespec result;<br>
<br>
if ( !value )<br>
rtems_set_errno_and_return_minus_one( EINVAL );<br>
<br>
ptimer = _POSIX_Timer_Get( timerid, &lock_context );<br>
- if ( ptimer != NULL ) {<br>
- Per_CPU_Control *cpu;<br>
-<br>
- cpu = _POSIX_Timer_Acquire_critical( ptimer, &lock_context );<br>
- now = cpu->Watchdog.ticks;<br>
-<br>
- if ( now < ptimer->Timer.expire ) {<br>
- remaining = (uint32_t) ( ptimer->Timer.expire - now );<br>
- } else {<br>
- remaining = 0;<br>
- }<br>
+ if ( ptimer == NULL ) {<br>
+ rtems_set_errno_and_return_minus_one( EINVAL );<br>
+ }<br>
<br>
- _Timespec_From_ticks( remaining, &value->it_value );<br>
- value->it_interval = ptimer->timer_data.it_interval;<br>
+ cpu = _POSIX_Timer_Acquire_critical( ptimer, &lock_context );<br>
+ rtems_timespec_from_ticks( ptimer->Timer.expire, &expire );<br>
<br>
- _POSIX_Timer_Release( cpu, &lock_context );<br>
- return 0;<br>
+ if ( ptimer->clock_type == CLOCK_MONOTONIC ) {<br>
+ _Timecounter_Nanouptime(&now);<br>
+ }<br>
+ if ( ptimer->clock_type == CLOCK_REALTIME ) {<br>
+ _TOD_Get(&now);<br>
}<br>
<br>
- rtems_set_errno_and_return_minus_one( EINVAL );<br>
-}<br>
+<br>
+ if ( rtems_timespec_less_than( &now, &expire ) ) {<br>
+ rtems_timespec_subtract( &now, &expire, &result );<br>
+ } else {<br>
+ result.tv_nsec = 0;<br>
+ result.tv_sec = 0;<br>
+ }<br>
+ <br>
+ value->it_value=result;<br>
+ value->it_interval = ptimer->timer_data.it_interval;<br>
+ <br>
+ _POSIX_Timer_Release( cpu, &lock_context );<br>
+ return 0;<br>
+}<br>
\ No newline at end of file<br>
diff --git a/testsuites/psxtests/psxtimer02/psxtimer.c b/testsuites/psxtests/psxtimer02/psxtimer.c<br>
index 9f79d33c42..1a79369efb 100644<br>
--- a/testsuites/psxtests/psxtimer02/psxtimer.c<br>
+++ b/testsuites/psxtests/psxtimer02/psxtimer.c<br>
@@ -126,6 +126,32 @@ void *POSIX_Init (<br>
puts( "timer_delete - bad id - EINVAL" );<br>
status = timer_delete( timer );<br>
fatal_posix_service_status_errno( status, EINVAL, "bad id" );<br>
+ <br>
+ puts( "timer_create (monotonic) - bad timer id pointer - EINVAL" );<br>
+ status = timer_create( CLOCK_MONOTONIC, &event, NULL );<br>
+ fatal_posix_service_status_errno( status, EINVAL, "bad timer id" );<br>
+<br>
+ puts( "timer_create (monotonic) - OK" );<br>
+ status = timer_create( CLOCK_MONOTONIC, NULL, &timer );<br>
+ posix_service_failed( status, "timer_create OK" );<br>
+<br>
+ puts( "timer_create (monotonic) - too many - EAGAIN" );<br>
+ status = timer_create( CLOCK_MONOTONIC, NULL, &timer1 );<br>
+ fatal_posix_service_status_errno( status, EAGAIN, "too many" );<br>
+ <br>
+ clock_gettime( CLOCK_MONOTONIC, &now );<br>
+ itimer.it_value = now;<br>
+ itimer.it_value.tv_sec = itimer.it_value.tv_sec - 1;<br>
+ puts( "timer_settime (monotonic) - bad itimer value - previous time - EINVAL" );<br>
+ status = timer_settime( timer, TIMER_ABSTIME, &itimer, NULL );<br>
+ fatal_posix_service_status_errno( status, EINVAL, "bad itimer value #3" );<br>
+<br>
+ clock_gettime( CLOCK_MONOTONIC, &now );<br>
+ itimer.it_value = now;<br>
+ itimer.it_value.tv_sec = itimer.it_value.tv_sec + 1;<br>
+ puts( "timer_settime (monotonic) - bad id - EINVAL" );<br>
+ status = timer_settime( timer1, TIMER_ABSTIME, &itimer, NULL );<br>
+ fatal_posix_service_status_errno( status, EINVAL, "bad id" );<br>
<br>
TEST_END();<br>
rtems_test_exit (0);<br>
-- <br>
2.32.0<br>
<br>
</blockquote></div>