1 /*
2  * Copyright (c) 2017 Wind River Systems, Inc.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <zephyr/sys/printk.h>
9 #include <zephyr/ztest.h>
10 
11 #define NUM_TIMEOUTS 3
12 
13 static struct k_timer timer[NUM_TIMEOUTS];
14 static struct k_sem sem[NUM_TIMEOUTS];
15 
16 static int results[NUM_TIMEOUTS], cur;
17 
thread(void * p1,void * p2,void * p3)18 static void thread(void *p1, void *p2, void *p3)
19 {
20 	ARG_UNUSED(p2);
21 	ARG_UNUSED(p3);
22 
23 	uintptr_t id = (uintptr_t)p1;
24 
25 	k_timer_status_sync(&timer[id]);
26 
27 	/* no need to protect cur, all threads have the same prio */
28 	results[cur++] = id;
29 
30 	k_sem_give(&sem[id]);
31 }
32 
33 #define STACKSIZE (512 + CONFIG_TEST_EXTRA_STACK_SIZE)
34 
35 static K_THREAD_STACK_ARRAY_DEFINE(stacks, NUM_TIMEOUTS, STACKSIZE);
36 static struct k_thread threads[NUM_TIMEOUTS];
37 
38 /**
39  * @addtogroup kernel_common_tests
40  * @{
41  */
42 
43 /**
44  * @brief Test timeout ordering
45  *
46  * @details Timeouts, when expiring on the same tick, should be handled
47  * in the same order they were queued.
48  *
49  * @see k_timer_start()
50  */
ZTEST(common_1cpu,test_timeout_order)51 ZTEST(common_1cpu, test_timeout_order)
52 {
53 	int ii, prio = k_thread_priority_get(k_current_get()) + 1;
54 
55 	for (ii = 0; ii < NUM_TIMEOUTS; ii++) {
56 		(void)k_thread_create(&threads[ii], stacks[ii], STACKSIZE,
57 				      thread, INT_TO_POINTER(ii), 0, 0,
58 				      prio, 0, K_NO_WAIT);
59 		k_timer_init(&timer[ii], 0, 0);
60 		k_sem_init(&sem[ii], 0, 1);
61 		results[ii] = -1;
62 	}
63 
64 
65 	uint32_t uptime = k_uptime_get_32();
66 
67 	/* sync on tick */
68 	while (uptime == k_uptime_get_32()) {
69 		Z_SPIN_DELAY(50);
70 	}
71 
72 	for (ii = 0; ii < NUM_TIMEOUTS; ii++) {
73 		k_timer_start(&timer[ii], K_MSEC(100), K_NO_WAIT);
74 	}
75 
76 	/* Wait for all timers to fire */
77 	k_msleep(125);
78 
79 	/* Check results */
80 	for (ii = 0; ii < NUM_TIMEOUTS; ii++) {
81 		zassert_equal(results[ii], ii, "");
82 	}
83 
84 	/* Clean up */
85 	for (ii = 0; ii < NUM_TIMEOUTS; ii++) {
86 		k_timer_stop(&timer[ii]);
87 		k_thread_join(&threads[ii], K_FOREVER);
88 	}
89 }
90 
91 extern void *common_setup(void);
92 ZTEST_SUITE(common_1cpu, NULL, common_setup,
93 		ztest_simple_1cpu_before, ztest_simple_1cpu_after, NULL);
94 
95 /**
96  * @}
97  */
98