1 /* sema.c */
2
3 /*
4 * Copyright (c) 1997-2010, 2013-2014 Wind River Systems, Inc.
5 *
6 * SPDX-License-Identifier: Apache-2.0
7 */
8
9 #include "syskernel.h"
10
11 struct k_sem sem1;
12 struct k_sem sem2;
13
14 /**
15 *
16 * @brief Initialize semaphores for the test
17 *
18 */
sema_test_init(void)19 void sema_test_init(void)
20 {
21 k_sem_init(&sem1, 0, 1);
22 k_sem_init(&sem2, 0, 1);
23 }
24
25
26 /**
27 *
28 * @brief Semaphore test thread
29 *
30 * @param par1 Ignored parameter.
31 * @param par2 Number of test loops.
32 * @param par3 Unused
33 *
34 */
sema_thread1(void * par1,void * par2,void * par3)35 void sema_thread1(void *par1, void *par2, void *par3)
36 {
37 int i;
38 int num_loops = POINTER_TO_INT(par2);
39
40 ARG_UNUSED(par1);
41 ARG_UNUSED(par3);
42
43 for (i = 0; i < num_loops; i++) {
44 k_sem_take(&sem1, K_FOREVER);
45 k_sem_give(&sem2);
46 }
47 }
48
49
50 /**
51 *
52 * @brief Semaphore test thread
53 *
54 * @param par1 Address of the counter.
55 * @param par2 Number of test cycles.
56 * @param par3 Unused
57 *
58 */
sema_thread2(void * par1,void * par2,void * par3)59 void sema_thread2(void *par1, void *par2, void *par3)
60 {
61 int i;
62 int *pcounter = (int *)par1;
63 int num_loops = POINTER_TO_INT(par2);
64
65 ARG_UNUSED(par3);
66
67 for (i = 0; i < num_loops; i++) {
68 k_sem_give(&sem1);
69 k_sem_take(&sem2, K_FOREVER);
70 (*pcounter)++;
71 }
72 }
73
74 /**
75 *
76 * @brief Semaphore test thread
77 *
78 * @param par1 Address of the counter.
79 * @param par2 Number of test cycles.
80 * @param par3 Unused
81 *
82 */
sema_thread3(void * par1,void * par2,void * par3)83 void sema_thread3(void *par1, void *par2, void *par3)
84 {
85 int i;
86 int *pcounter = (int *)par1;
87 int num_loops = POINTER_TO_INT(par2);
88
89 ARG_UNUSED(par3);
90
91 for (i = 0; i < num_loops; i++) {
92 k_sem_give(&sem1);
93 while (k_sem_take(&sem2, K_NO_WAIT) != 0) {
94 k_yield();
95 }
96 (*pcounter)++;
97 }
98 }
99
100
101 /**
102 *
103 * @brief The main test entry
104 *
105 * @return 1 if success and 0 on failure
106 */
sema_test(void)107 int sema_test(void)
108 {
109 uint32_t t;
110 int i = 0;
111 int return_value = 0;
112
113 fprintf(output_file, sz_test_case_fmt,
114 "Semaphore #1");
115 fprintf(output_file, sz_description,
116 "\n\tk_sem_init"
117 "\n\tk_sem_take(K_FOREVER)"
118 "\n\tk_sem_give");
119 printf(sz_test_start_fmt);
120
121 sema_test_init();
122
123 t = BENCH_START();
124
125 k_thread_create(&thread_data1, thread_stack1, STACK_SIZE, sema_thread1,
126 NULL, INT_TO_POINTER(number_of_loops), NULL,
127 K_PRIO_COOP(3), 0, K_NO_WAIT);
128 k_thread_create(&thread_data2, thread_stack2, STACK_SIZE, sema_thread2,
129 (void *) &i, INT_TO_POINTER(number_of_loops), NULL,
130 K_PRIO_COOP(3), 0, K_NO_WAIT);
131
132 t = TIME_STAMP_DELTA_GET(t);
133
134 return_value += check_result(i, t);
135
136 fprintf(output_file, sz_test_case_fmt,
137 "Semaphore #2");
138 fprintf(output_file, sz_description,
139 "\n\tk_sem_init"
140 "\n\tk_sem_take(K_NO_WAIT)"
141 "\n\tk_yield"
142 "\n\tk_sem_give");
143 printf(sz_test_start_fmt);
144
145 sema_test_init();
146 i = 0;
147
148 t = BENCH_START();
149
150 k_thread_create(&thread_data1, thread_stack1, STACK_SIZE, sema_thread1,
151 NULL, INT_TO_POINTER(number_of_loops), NULL,
152 K_PRIO_COOP(3), 0, K_NO_WAIT);
153 k_thread_create(&thread_data2, thread_stack2, STACK_SIZE, sema_thread3,
154 (void *) &i, INT_TO_POINTER(number_of_loops), NULL,
155 K_PRIO_COOP(3), 0, K_NO_WAIT);
156
157 t = TIME_STAMP_DELTA_GET(t);
158
159 return_value += check_result(i, t);
160
161 fprintf(output_file, sz_test_case_fmt,
162 "Semaphore #3");
163 fprintf(output_file, sz_description,
164 "\n\tk_sem_init"
165 "\n\tk_sem_take(K_FOREVER)"
166 "\n\tk_sem_give"
167 "\n\tk_sem_give"
168 "\n\tk_sem_take(K_FOREVER)");
169 printf(sz_test_start_fmt);
170
171 sema_test_init();
172
173 t = BENCH_START();
174
175 k_thread_create(&thread_data1, thread_stack1, STACK_SIZE, sema_thread1,
176 NULL, INT_TO_POINTER(number_of_loops), NULL,
177 K_PRIO_COOP(3), 0, K_NO_WAIT);
178 for (i = 0; i < number_of_loops; i++) {
179 k_sem_give(&sem1);
180 k_sem_take(&sem2, K_FOREVER);
181 }
182
183 t = TIME_STAMP_DELTA_GET(t);
184
185 return_value += check_result(i, t);
186
187 return return_value;
188 }
189