Skip to content

Commit 2650802

Browse files
SgrrZhfstephanosio
authored andcommitted
tests: kernel: semaphore: fix mutual exclusion test issue
Mutual exclusion test assume that the excution order of two threads like this: mutual_exclusion1 -> mutual_exclusion2 -> mutual_exclusion1 ... but some times the excution order of two threads would be this: mutual_exclusion1 -> mutual_exclusion2 -> mutual_exclusion2 ... This patch increase the loop cycle, add a variable 'tmp' to store the value of 'critical_var' before operating it. Signed-off-by: Huifeng Zhang <[email protected]>
1 parent 3ca07c0 commit 2650802

File tree

1 file changed

+6
-4
lines changed
  • tests/kernel/semaphore/semaphore/src

1 file changed

+6
-4
lines changed

tests/kernel/semaphore/semaphore/src/main.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,35 +181,37 @@ void sem_take_multiple_high_prio_helper(void *p1, void *p2, void *p3)
181181
/* First function for mutual exclusion test */
182182
void sem_queue_mutual_exclusion1(void *p1, void *p2, void *p3)
183183
{
184-
for (int i = 0; i < 5; i++) {
184+
for (int i = 0; i < 10000; i++) {
185185
expect_k_sem_take_nomsg(&mut_sem, K_FOREVER, 0);
186186

187187
/* in that function critical section makes critical var +1 */
188+
uint32_t tmp = critical_var;
188189
critical_var += 1;
189190

190191
/* Check that common value was not changed by another thread,
191192
* when semaphore is taken by current thread, and no other
192193
* thread can enter the critical section
193194
*/
194-
zassert_true(critical_var == 1, NULL);
195+
zassert_true(critical_var == tmp + 1, NULL);
195196
k_sem_give(&mut_sem);
196197
}
197198
}
198199

199200
/* Second function for mutual exclusion test */
200201
void sem_queue_mutual_exclusion2(void *p1, void *p2, void *p3)
201202
{
202-
for (int i = 0; i < 5; i++) {
203+
for (int i = 0; i < 10000; i++) {
203204
expect_k_sem_take_nomsg(&mut_sem, K_FOREVER, 0);
204205

205206
/* in that function critical section makes critical var 0 */
207+
uint32_t tmp = critical_var;
206208
critical_var -= 1;
207209

208210
/* Check that common value was not changed by another thread,
209211
* when semaphore is taken by current thread, and no other
210212
* thread can enter the critical section
211213
*/
212-
zassert_true(critical_var == 0, NULL);
214+
zassert_true(critical_var == tmp - 1, NULL);
213215
k_sem_give(&mut_sem);
214216
}
215217
}

0 commit comments

Comments
 (0)