2929
3030#include <stdio.h>
3131#include <sched.h>
32+ #include <unistd.h>
33+ #include <stdlib.h>
3234
3335#include "ostest.h"
3436
3537/****************************************************************************
3638 * Pre-processor Definitions
3739 ****************************************************************************/
3840
39- #define NSEC_PER_50MS (50 * NSEC_PER_MSEC)
41+ /* Timer constants */
42+
43+ #define NSEC_PER_50MS (50 * NSEC_PER_MSEC)
44+ #define PERIOD_TEST_COUNT 15
45+ #define THREAD_LOOP_COUNT 50
46+ #define HRTIMER_TEST_THREAD_NR (CONFIG_SMP_NCPUS * 5)
4047
4148/* Set a 1ms margin to allow hrtimertest to pass in QEMU.
4249 *
@@ -165,9 +172,9 @@ test_hrtimer_callback(FAR const hrtimer_t *hrtimer, uint64_t expired)
165172
166173 test -> previous = now ;
167174
168- /* Stop the test after 15 expirations */
175+ /* Stop the test after PERIOD_TEST_COUNT expirations */
169176
170- if (test -> count < 15 )
177+ if (test -> count < PERIOD_TEST_COUNT )
171178 {
172179 return test -> period ;
173180 }
@@ -178,6 +185,67 @@ test_hrtimer_callback(FAR const hrtimer_t *hrtimer, uint64_t expired)
178185 }
179186}
180187
188+ /****************************************************************************
189+ * Name: hrtimer_test_callback
190+ *
191+ * Description:
192+ * Simple HRTimer callback for threaded tests.
193+ *
194+ ****************************************************************************/
195+
196+ static uint64_t
197+ hrtimer_test_callback (FAR const hrtimer_t * hrtimer , uint64_t expired )
198+ {
199+ return 0 ;
200+ }
201+
202+ /****************************************************************************
203+ * Name: hrtimer_test_thread
204+ *
205+ * Description:
206+ * Thread function to repeatedly test HRTimer start/cancel behavior.
207+ *
208+ ****************************************************************************/
209+
210+ static void * hrtimer_test_thread (void * arg )
211+ {
212+ hrtimer_t timer ;
213+ int ret ;
214+ int i = 0 ;
215+
216+ hrtimer_init (& timer );
217+
218+ while (i < THREAD_LOOP_COUNT )
219+ {
220+ i ++ ;
221+ uint64_t delay = rand () % NSEC_PER_MSEC ;
222+
223+ /* Cancel timer */
224+
225+ ret = hrtimer_cancel (& timer );
226+ HRTIMER_TEST (ret , OK );
227+
228+ /* Start timer with fixed period */
229+
230+ ret = hrtimer_start (& timer , hrtimer_test_callback ,
231+ 10 * NSEC_PER_USEC , HRTIMER_MODE_REL );
232+ HRTIMER_TEST (ret , OK );
233+
234+ /* Start timer with random delay */
235+
236+ ret = hrtimer_start (& timer , hrtimer_test_callback ,
237+ delay , HRTIMER_MODE_REL );
238+ HRTIMER_TEST (ret , OK );
239+ }
240+
241+ /* Cancel the timer synchronously */
242+
243+ ret = hrtimer_cancel_sync (& timer );
244+ HRTIMER_TEST (ret , OK );
245+
246+ return NULL ;
247+ }
248+
181249/****************************************************************************
182250 * Public Functions
183251 ****************************************************************************/
@@ -202,8 +270,12 @@ test_hrtimer_callback(FAR const hrtimer_t *hrtimer, uint64_t expired)
202270
203271void hrtimer_test (void )
204272{
205- int ret ;
273+ struct sched_param sparam ;
274+ unsigned int thread_id ;
275+ pthread_attr_t attr ;
276+ pthread_t pthreads [HRTIMER_TEST_THREAD_NR ];
206277 struct hrtimer_test_s hrtimer_test ;
278+ int ret ;
207279
208280 /* Initialize test structure */
209281
@@ -228,4 +300,24 @@ void hrtimer_test(void)
228300 {
229301 usleep (USEC_PER_MSEC );
230302 }
303+
304+ pthread_attr_init (& attr );
305+
306+ sparam .sched_priority = PTHREAD_DEFAULT_PRIORITY ;
307+ pthread_attr_setschedparam (& attr , & sparam );
308+
309+ for (thread_id = 0 ; thread_id < HRTIMER_TEST_THREAD_NR ; thread_id ++ )
310+ {
311+ HRTIMER_TEST (pthread_create (& pthreads [thread_id ], & attr ,
312+ hrtimer_test_thread , NULL ), 0 );
313+ }
314+
315+ /* Wait for all threads to complete */
316+
317+ for (thread_id = 0 ; thread_id < HRTIMER_TEST_THREAD_NR ; thread_id ++ )
318+ {
319+ pthread_join (pthreads [thread_id ], NULL );
320+ }
321+
322+ HRTIMER_TEST (pthread_attr_destroy (& attr ), 0 );
231323}
0 commit comments