1 /*
2  * Copyright (c) 2017 Linaro Limited
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include "test_fifo.h"
8 
9 #define STACK_SIZE (512 + CONFIG_TEST_EXTRA_STACK_SIZE)
10 #define LIST_LEN 2
11 /**TESTPOINT: init via K_FIFO_DEFINE*/
12 K_FIFO_DEFINE(kfifo_c);
13 
14 struct k_fifo fifo_c;
15 
16 static K_THREAD_STACK_DEFINE(tstack, STACK_SIZE);
17 static struct k_thread thread;
18 
t_cancel_wait_entry(void * p1,void * p2,void * p3)19 static void t_cancel_wait_entry(void *p1, void *p2, void *p3)
20 {
21 	k_sleep(K_MSEC(50));
22 	k_fifo_cancel_wait((struct k_fifo *)p1);
23 }
24 
tfifo_thread_thread(struct k_fifo * pfifo)25 static void tfifo_thread_thread(struct k_fifo *pfifo)
26 {
27 	k_tid_t tid = k_thread_create(&thread, tstack, STACK_SIZE,
28 				      t_cancel_wait_entry, pfifo, NULL, NULL,
29 				      K_PRIO_PREEMPT(0), 0, K_NO_WAIT);
30 	uint32_t start_t = k_uptime_get_32();
31 	void *ret = k_fifo_get(pfifo, K_MSEC(500));
32 	uint32_t dur = k_uptime_get_32() - start_t;
33 
34 	/* While we observed the side effect of the last statement
35 	 * ( call to k_fifo_cancel_wait) of the thread, it's not fact
36 	 * that it returned, within the thread. Then it may happen
37 	 * that the test runner below will try to create another
38 	 * thread in the same stack space, then 1st thread returns
39 	 * from the call, leading to crash.
40 	 */
41 	k_thread_abort(tid);
42 	zassert_is_null(ret,
43 			"k_fifo_get didn't get 'timeout expired' status");
44 	/* 80 includes generous fuzz factor as k_sleep() will add an extra
45 	 * tick for non-tickless systems, and we may cross another tick
46 	 * boundary while doing this. We just want to ensure we didn't
47 	 * hit the timeout anyway.
48 	 */
49 	zassert_true(dur < 80,
50 		     "k_fifo_get didn't get cancelled in expected timeframe");
51 }
52 
53 /**
54  * @addtogroup kernel_fifo_tests
55  * @{
56  */
57 
58 /**
59  * @brief Test cancel waiting on a FIFO queue.
60  * @details This routine causes first thread pending on fifo (if any),
61  * to return from k_fifo_get() with NULL value (as if timeout expired).
62  * @see k_fifo_init(),k_fifo_get(), k_fifo_cancel_wait()
63  */
ZTEST(fifo_api_1cpu,test_fifo_cancel_wait)64 ZTEST(fifo_api_1cpu, test_fifo_cancel_wait)
65 {
66 	/**TESTPOINT: init via k_fifo_init*/
67 	k_fifo_init(&fifo_c);
68 	tfifo_thread_thread(&fifo_c);
69 
70 	/**TESTPOINT: test K_FIFO_DEFINEed fifo*/
71 	tfifo_thread_thread(&kfifo_c);
72 }
73 
74 /**
75  * @}
76  */
77