Linux Audio

Check our new training course

Loading...
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
/*
 * Copyright (c) 2016 Intel Corporation
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <ztest.h>
#include <irq_offload.h>

/* Macro declarations */
#define SEM_INIT_VAL (0U)
#define SEM_MAX_VAL  (10U)

#define sem_give_from_isr(sema) irq_offload(isr_sem_give, sema)
#define sem_take_from_isr(sema) irq_offload(isr_sem_take, sema)

#define SEM_TIMEOUT (K_MSEC(100))
#define STACK_SIZE (1024 + CONFIG_TEST_EXTRA_STACKSIZE)
#define TOTAL_THREADS_WAITING (5)

struct timeout_info {
	u32_t timeout;
	struct k_sem *sema;
};

/******************************************************************************/
/* Kobject declaration */
K_SEM_DEFINE(simple_sem, SEM_INIT_VAL, SEM_MAX_VAL);
K_SEM_DEFINE(low_prio_sem, SEM_INIT_VAL, SEM_MAX_VAL);
K_SEM_DEFINE(mid_prio_sem, SEM_INIT_VAL, SEM_MAX_VAL);
K_SEM_DEFINE(high_prio_sem, SEM_INIT_VAL, SEM_MAX_VAL);
K_SEM_DEFINE(multiple_thread_sem, SEM_INIT_VAL, SEM_MAX_VAL);
K_THREAD_STACK_DEFINE(stack_1, STACK_SIZE);
K_THREAD_STACK_DEFINE(stack_2, STACK_SIZE);
K_THREAD_STACK_DEFINE(stack_3, STACK_SIZE);
K_THREAD_STACK_ARRAY_DEFINE(multiple_stack, TOTAL_THREADS_WAITING, STACK_SIZE);
K_PIPE_DEFINE(timeout_info_pipe,
	      sizeof(struct timeout_info) * TOTAL_THREADS_WAITING, 4);


struct k_thread sem_tid, sem_tid_1, sem_tid_2;
struct k_thread multiple_tid[TOTAL_THREADS_WAITING];

/******************************************************************************/
/* Helper functions */
void isr_sem_give(void *semaphore)
{
	k_sem_give((struct k_sem *)semaphore);
}

void isr_sem_take(void *semaphore)
{
	k_sem_take((struct k_sem *)semaphore, K_NO_WAIT);
}

void sem_give_task(void *p1, void *p2, void *p3)
{
	k_sem_give(&simple_sem);
}

void sem_take_timeout_forever_helper(void *p1, void *p2, void *p3)
{
	k_sleep(K_MSEC(100));
	k_sem_give(&simple_sem);
}

void sem_take_timeout_isr_helper(void *p1, void *p2, void *p3)
{
	sem_give_from_isr(&simple_sem);
}

void sem_take_multiple_low_prio_helper(void *p1, void *p2, void *p3)
{
	s32_t ret_value;

	ret_value = k_sem_take(&low_prio_sem, K_FOREVER);
	zassert_true(ret_value == 0,
		     "k_sem_take failed when its shouldn't have\n");

	ret_value = k_sem_take(&multiple_thread_sem, K_FOREVER);
	zassert_true(ret_value == 0,
		     "k_sem_take failed when its shouldn't have\n");

	k_sem_give(&low_prio_sem);
}

void sem_take_multiple_mid_prio_helper(void *p1, void *p2, void *p3)
{
	s32_t ret_value;

	ret_value = k_sem_take(&mid_prio_sem, K_FOREVER);
	zassert_true(ret_value == 0,
		     "k_sem_take failed when its shouldn't have\n");

	ret_value = k_sem_take(&multiple_thread_sem, K_FOREVER);
	zassert_true(ret_value == 0,
		     "k_sem_take failed when its shouldn't have\n");

	k_sem_give(&mid_prio_sem);
}

void sem_take_multiple_high_prio_helper(void *p1, void *p2, void *p3)
{
	s32_t ret_value;

	ret_value = k_sem_take(&high_prio_sem, K_FOREVER);
	zassert_true(ret_value == 0,
		     "k_sem_take failed when its shouldn't have\n");

	ret_value = k_sem_take(&multiple_thread_sem, K_FOREVER);
	zassert_true(ret_value == 0,
		     "k_sem_take failed when its shouldn't have\n");

	k_sem_give(&high_prio_sem);
}


/**
 * @ingroup kernel_semaphore_tests
 * @{
 */

/**
 * @brief Test semaphore count when given by an ISR
 * @see k_sem_give()
 */
void test_simple_sem_from_isr(void)
{
	u32_t signal_count;

	/*
	 * Signal the semaphore several times from an ISR.  After each signal,
	 * check the signal count.
	 */

	for (int i = 0; i < 5; i++) {
		sem_give_from_isr(&simple_sem);

		signal_count = k_sem_count_get(&simple_sem);
		zassert_true(signal_count == (i + 1),
			     "signal count missmatch Expected %d, got %d\n",
			     (i + 1), signal_count);
	}

}

/**
 * @brief Test semaphore count when given by thread
 * @see k_sem_give()
 */
void test_simple_sem_from_task(void)
{
	u32_t signal_count;

	/*
	 * Signal the semaphore several times from a task.  After each signal,
	 * check the signal count.
	 */

	k_sem_reset(&simple_sem);

	for (int i = 0; i < 5; i++) {
		k_sem_give(&simple_sem);

		signal_count = k_sem_count_get(&simple_sem);
		zassert_true(signal_count == (i + 1),
			     "signal count missmatch Expected %d, got %d\n",
			     (i + 1), signal_count);
	}

}

/**
 * @brief Test if k_sem_take() decreases semaphore count
 * @see k_sem_take()
 */
void test_sem_take_no_wait(void)
{
	u32_t signal_count;
	s32_t ret_value;

	/*
	 * Test the semaphore without wait.  Check the signal count after each
	 * attempt (it should be decrementing by 1 each time).
	 */


	for (int i = 4; i >= 0; i--) {
		ret_value = k_sem_take(&simple_sem, K_NO_WAIT);
		zassert_true(ret_value == 0,
			     "unable to do k_sem_take which returned %d\n",
			     ret_value);

		signal_count = k_sem_count_get(&simple_sem);
		zassert_true(signal_count == i,
			     "signal count missmatch Expected %d, got %d\n",
			     i, signal_count);
	}

}

/**
 * @brief Test k_sem_take() when there is no semaphore to take
 * @see k_sem_take()
 */
void test_sem_take_no_wait_fails(void)
{
	u32_t signal_count;
	s32_t ret_value;

	/*
	 * Test the semaphore without wait.  Check the signal count after each
	 * attempt (it should be decrementing by 1 each time).
	 */

	k_sem_reset(&simple_sem);

	for (int i = 4; i >= 0; i--) {
		ret_value = k_sem_take(&simple_sem, K_NO_WAIT);
		zassert_true(ret_value == -EBUSY,
			     "k_sem_take returned when not possible");

		signal_count = k_sem_count_get(&simple_sem);
		zassert_true(signal_count == 0U,
			     "signal count missmatch Expected 0, got %d\n",
			     signal_count);
	}

}

/**
 * @brief Test k_sem_take() with timeout expiry
 * @see k_sem_take()
 */
void test_sem_take_timeout_fails(void)
{
	s32_t ret_value;

	/*
	 * Test the semaphore with timeout without a k_sem_give.
	 */

	k_sem_reset(&simple_sem);

	for (int i = 4; i >= 0; i--) {
		ret_value = k_sem_take(&simple_sem, SEM_TIMEOUT);
		zassert_true(ret_value == -EAGAIN,
			     "k_sem_take succeeded when its not possible");
	}

}

/**
 * @brief Test k_sem_take() with timeout
 * @see k_sem_take()
 */
void test_sem_take_timeout(void)
{
	s32_t ret_value;

	/*
	 * Signal the semaphore upon which the another thread is waiting.  The
	 * alternate task (which is at a lower priority) will cause simple_sem
	 * to be signalled, thus waking this task.
	 */
	k_thread_create(&sem_tid, stack_1, STACK_SIZE,
			sem_give_task, NULL, NULL, NULL,
			K_PRIO_PREEMPT(0), K_USER | K_INHERIT_PERMS,
			K_NO_WAIT);

	k_sem_reset(&simple_sem);

	ret_value = k_sem_take(&simple_sem, SEM_TIMEOUT);
	zassert_true(ret_value == 0,
		     "k_sem_take failed when its shouldn't have");
	k_thread_abort(&sem_tid);

}

/**
 * @brief Test k_sem_take() with forever timeout
 * @see k_sem_take()
 */
void test_sem_take_timeout_forever(void)
{
	s32_t ret_value;

	/*
	 * Signal the semaphore upon which the another thread is waiting.  The
	 * alternate task (which is at a lower priority) will cause simple_sem
	 * to be signalled, thus waking this task.
	 */
	k_thread_create(&sem_tid, stack_1, STACK_SIZE,
			sem_take_timeout_forever_helper, NULL, NULL, NULL,
			K_PRIO_PREEMPT(0), K_USER | K_INHERIT_PERMS,
			K_NO_WAIT);

	k_sem_reset(&simple_sem);

	ret_value = k_sem_take(&simple_sem, K_FOREVER);
	zassert_true(ret_value == 0,
		     "k_sem_take failed when its shouldn't have");
	k_thread_abort(&sem_tid);

}

/**
 * @brief Test k_sem_take() with timeout in ISR context
 * @see k_sem_take()
 */
void test_sem_take_timeout_isr(void)
{
	s32_t ret_value;

	/*
	 * Signal the semaphore upon which the another thread is waiting.  The
	 * alternate task (which is at a lower priority) will cause simple_sem
	 * to be signalled, thus waking this task.
	 */
	k_thread_create(&sem_tid, stack_1, STACK_SIZE,
			sem_take_timeout_isr_helper, NULL, NULL, NULL,
			K_PRIO_PREEMPT(0), 0, K_NO_WAIT);

	k_sem_reset(&simple_sem);

	ret_value = k_sem_take(&simple_sem, SEM_TIMEOUT);
	zassert_true(ret_value == 0,
		     "k_sem_take failed when its shouldn't have");
	k_thread_abort(&sem_tid);

}

/**
 * @brief Test multiple semaphore take
 * @see k_sem_take()
 */
void test_sem_take_multiple(void)
{
	u32_t signal_count;

	/*
	 * Signal the semaphore upon which the another thread is waiting.  The
	 * alternate task (which is at a lower priority) will cause simple_sem
	 * to be signalled, thus waking this task.
	 */
	k_thread_create(&sem_tid, stack_1, STACK_SIZE,
			sem_take_multiple_low_prio_helper,
			NULL, NULL, NULL,
			K_PRIO_PREEMPT(3), K_USER | K_INHERIT_PERMS,
			K_NO_WAIT);

	k_thread_create(&sem_tid_1, stack_2, STACK_SIZE,
			sem_take_multiple_mid_prio_helper,
			NULL, NULL, NULL,
			K_PRIO_PREEMPT(2), K_USER | K_INHERIT_PERMS,
			K_NO_WAIT);

	k_thread_create(&sem_tid_2, stack_3, STACK_SIZE,
			sem_take_multiple_high_prio_helper,
			NULL, NULL, NULL,
			K_PRIO_PREEMPT(1), K_USER | K_INHERIT_PERMS,
			K_NO_WAIT);


	/* time for those 3 threads to complete */
	k_sleep(K_MSEC(20));

	/* Let these threads proceed to take the multiple_sem */
	k_sem_give(&high_prio_sem);
	k_sem_give(&mid_prio_sem);
	k_sem_give(&low_prio_sem);
	k_sleep(K_MSEC(200));

	/* enable the higher priority thread to run. */
	k_sem_give(&multiple_thread_sem);
	k_sleep(K_MSEC(200));
	/* check which threads completed. */
	signal_count = k_sem_count_get(&high_prio_sem);
	zassert_true(signal_count == 1U,
		     "Higher priority threads didn't execute");

	signal_count = k_sem_count_get(&mid_prio_sem);
	zassert_true(signal_count == 0U,
		     "Medium priority threads shouldn't have executed");

	signal_count = k_sem_count_get(&low_prio_sem);
	zassert_true(signal_count == 0U,
		     "low priority threads shouldn't have executed");

	/* enable the Medium priority thread to run. */
	k_sem_give(&multiple_thread_sem);
	k_sleep(K_MSEC(200));
	/* check which threads completed. */
	signal_count = k_sem_count_get(&high_prio_sem);
	zassert_true(signal_count == 1U,
		     "Higher priority thread executed again");

	signal_count = k_sem_count_get(&mid_prio_sem);
	zassert_true(signal_count == 1U,
		     "Medium priority thread didn't get executed");

	signal_count = k_sem_count_get(&low_prio_sem);
	zassert_true(signal_count == 0U,
		     "low priority thread shouldn't have executed");

	/* enable the low priority thread to run. */
	k_sem_give(&multiple_thread_sem);
	k_sleep(K_MSEC(200));
	/* check which threads completed. */
	signal_count = k_sem_count_get(&high_prio_sem);
	zassert_true(signal_count == 1U,
		     "Higher priority thread executed again");

	signal_count = k_sem_count_get(&mid_prio_sem);
	zassert_true(signal_count == 1U,
		     "Medium priority thread executed again");

	signal_count = k_sem_count_get(&low_prio_sem);
	zassert_true(signal_count == 1U,
		     "low priority thread didn't get executed");

}

/**
 * @brief Test semaphore give and take and its count from ISR
 * @see k_sem_give()
 */
void test_sem_give_take_from_isr(void)
{
	u32_t signal_count;

	k_sem_reset(&simple_sem);

	/* Give semaphore from an isr and do a check for the count */
	for (int i = 0; i < SEM_MAX_VAL; i++) {
		sem_give_from_isr(&simple_sem);

		signal_count = k_sem_count_get(&simple_sem);
		zassert_true(signal_count == i + 1,
			     "signal count missmatch Expected %d, got %d\n",
			     i + 1, signal_count);
	}

	/* Take semaphore from an isr and do a check for the count */
	for (int i = SEM_MAX_VAL; i > 0; i--) {
		sem_take_from_isr(&simple_sem);

		signal_count = k_sem_count_get(&simple_sem);
		zassert_true(signal_count == (i - 1),
			     "signal count missmatch Expected %d, got %d\n",
			     (i - 1), signal_count);
	}
}

/**
 * @}
 */

void test_sem_multiple_threads_wait_helper(void *p1, void *p2, void *p3)
{
	/* get blocked until the test thread gives the semaphore */
	k_sem_take(&multiple_thread_sem, K_FOREVER);

	/* Inform the test thread that this thread has got multiple_thread_sem*/
	k_sem_give(&simple_sem);
}


/**
 * @brief Test multiple semaphore take and give with wait
 * @ingroup kernel_semaphore_tests
 * @see k_sem_take(), k_sem_give()
 */
void test_sem_multiple_threads_wait(void)
{
	u32_t signal_count;
	s32_t ret_value;
	u32_t repeat_count = 0U;

	k_sem_reset(&simple_sem);
	k_sem_reset(&multiple_thread_sem);


	do {
		for (int i = 0; i < TOTAL_THREADS_WAITING; i++) {
			k_thread_create(&multiple_tid[i],
					multiple_stack[i], STACK_SIZE,
					test_sem_multiple_threads_wait_helper,
					NULL, NULL, NULL,
					K_PRIO_PREEMPT(1),
					K_USER | K_INHERIT_PERMS, K_NO_WAIT);
		}

		/* giving time for the other threads to execute  */
		k_sleep(K_MSEC(500));

		/* Give the semaphores */
		for (int i = 0; i < TOTAL_THREADS_WAITING; i++) {
			k_sem_give(&multiple_thread_sem);
		}

		/* giving time for the other threads to execute  */
		k_sleep(K_MSEC(500));

		/* check if all the threads are done. */
		for (int i = 0; i < TOTAL_THREADS_WAITING; i++) {
			ret_value = k_sem_take(&simple_sem, K_FOREVER);
			zassert_true(ret_value == 0,
				     "Some of the threads didn't get multiple_thread_sem\n"
				     );
		}

		signal_count = k_sem_count_get(&simple_sem);
		zassert_true(signal_count == 0U,
			     "signal count missmatch Expected 0, got %d\n",
			     signal_count);

		signal_count = k_sem_count_get(&multiple_thread_sem);
		zassert_true(signal_count == 0U,
			     "signal count missmatch Expected 0, got %d\n",
			     signal_count);

		/* Verify a wait q that has been emptied / reset
		 * correctly by running again.
		 */
		repeat_count++;
	} while (repeat_count < 2);
}

/**
 * @brief Test semaphore timeout period
 * @ingroup kernel_semaphore_tests
 * @see k_sem_take(), k_sem_give(), k_sem_reset()
 */
void test_sem_measure_timeouts(void)
{
	s32_t ret_value;
	u32_t start_ticks, end_ticks;

	k_sem_reset(&simple_sem);

	/* With timeout of 1 sec */
	start_ticks = k_uptime_get();

	ret_value = k_sem_take(&simple_sem, K_SECONDS(1));

	end_ticks = k_uptime_get();

	zassert_true(ret_value == -EAGAIN,
		     "k_sem_take failed when its shouldn't have");

	zassert_true((end_ticks - start_ticks >= K_SECONDS(1)),
		     "time missmatch expected %d ,got %d\n",
		     K_SECONDS(1), end_ticks - start_ticks);

	/* With 0 as the timeout */
	start_ticks = k_uptime_get();

	ret_value = k_sem_take(&simple_sem, 0);

	end_ticks = k_uptime_get();

	zassert_true(ret_value == -EBUSY,
		     "k_sem_take failed when its shouldn't have");

	zassert_true((end_ticks - start_ticks < 1),
		     "time missmatch expected %d ,got %d\n",
		     1, end_ticks - start_ticks);

}

void test_sem_measure_timeout_from_thread_helper(void *p1, void *p2, void *p3)
{
	/* first sync the 2 threads */
	k_sem_give(&simple_sem);

	/* give the semaphore */
	k_sem_give(&multiple_thread_sem);

}


/**
 * @brief Test timeout of semaphore from thread
 * @ingroup kernel_semaphore_tests
 * @see k_sem_give(), k_sem_reset(), k_sem_take()
 */
void test_sem_measure_timeout_from_thread(void)
{
	s32_t ret_value;
	u32_t start_ticks, end_ticks;

	k_sem_reset(&simple_sem);
	k_sem_reset(&multiple_thread_sem);

	/* Give a semaphore from a thread and calcualte the time taken.*/
	k_thread_create(&sem_tid, stack_1, STACK_SIZE,
			test_sem_measure_timeout_from_thread_helper,
			NULL, NULL, NULL,
			K_PRIO_PREEMPT(3), 0, K_NO_WAIT);


	/* first sync the 2 threads */
	k_sem_take(&simple_sem, K_FOREVER);

	/* With timeout of 1 sec */
	start_ticks = k_uptime_get();

	ret_value = k_sem_take(&multiple_thread_sem, K_SECONDS(1));

	end_ticks = k_uptime_get();

	zassert_true(ret_value == 0,
		     "k_sem_take failed when its shouldn't have");

	zassert_true((end_ticks - start_ticks <= K_SECONDS(1)),
		     "time missmatch. expected less than%d ,got %d\n",
		     K_SECONDS(1), end_ticks - start_ticks);

}

void test_sem_multiple_take_and_timeouts_helper(void *timeout,
						void *p2,
						void *p3)
{
	u32_t start_ticks, end_ticks;
	size_t bytes_written;

	start_ticks = k_uptime_get();

	k_sem_take(&simple_sem, (int)timeout);

	end_ticks = k_uptime_get();

	zassert_true((end_ticks - start_ticks >= (int)timeout),
		     "time missmatch. expected less than%d ,got %d\n",
		     timeout, end_ticks - start_ticks);


	k_pipe_put(&timeout_info_pipe, &timeout, sizeof(int),
		   &bytes_written, sizeof(int), K_FOREVER);

}

/**
 * @brief Test multiple semaphore take with timeouts
 * @ingroup kernel_semaphore_tests
 * @see k_sem_take(), k_sem_reset()
 */
void test_sem_multiple_take_and_timeouts(void)
{
	u32_t timeout;
	size_t bytes_read;

	k_sem_reset(&simple_sem);

	/* Multiple threads timeout and the sequence in which it times out
	 * is pushed into a pipe and checked later on.
	 */
	for (int i = 0; i < TOTAL_THREADS_WAITING; i++) {
		k_thread_create(&multiple_tid[i],
				multiple_stack[i], STACK_SIZE,
				test_sem_multiple_take_and_timeouts_helper,
				(void *)K_SECONDS(i + 1), NULL, NULL,
				K_PRIO_PREEMPT(1), 0, K_NO_WAIT);
	}

	for (int i = 0; i < TOTAL_THREADS_WAITING; i++) {
		k_pipe_get(&timeout_info_pipe, &timeout, sizeof(int),
			   &bytes_read, sizeof(int), K_FOREVER);
		zassert_true(timeout == K_SECONDS(i + 1),
			     "timeout didn't occur properly");
	}

	/* cleanup */
	for (int i = 0; i < TOTAL_THREADS_WAITING; i++) {
		k_thread_abort(&multiple_tid[i]);
	}

}

void test_sem_multi_take_timeout_diff_sem_helper(void *timeout,
						 void *sema,
						 void *p3)
{
	u32_t start_ticks, end_ticks;
	s32_t ret_value;
	size_t bytes_written;
	struct timeout_info info = {
		.timeout = (u32_t) timeout,
		.sema    = (struct k_sem *)sema
	};

	start_ticks = k_uptime_get();

	ret_value = k_sem_take((struct k_sem *)sema, (int)timeout);

	end_ticks = k_uptime_get();

	zassert_true((end_ticks - start_ticks >= (int)timeout),
		     "time missmatch. expected less than%d ,got %d\n",
		     timeout, end_ticks - start_ticks);


	k_pipe_put(&timeout_info_pipe, &info, sizeof(struct timeout_info),
		   &bytes_written, sizeof(struct timeout_info), K_FOREVER);

}

/**
 * @brief Test sequence of multiple semaphore timeouts
 * @ingroup kernel_semaphore_tests
 * @see k_sem_take(), k_sem_reset()
 */
void test_sem_multi_take_timeout_diff_sem(void)
{
	size_t bytes_read;
	struct timeout_info seq_info[] = {
		{ K_SECONDS(2), &simple_sem },
		{ K_SECONDS(1), &multiple_thread_sem },
		{ K_SECONDS(3), &simple_sem },
		{ K_SECONDS(5), &multiple_thread_sem },
		{ K_SECONDS(4), &simple_sem },
	};

	struct timeout_info retrieved_info;

	k_sem_reset(&simple_sem);
	k_sem_reset(&multiple_thread_sem);

	/* Multiple threads timeout on different semaphores and the sequence
	 * in which it times out is pushed into a pipe and checked later on.
	 */
	for (int i = 0; i < TOTAL_THREADS_WAITING; i++) {

		k_thread_create(&multiple_tid[i],
				multiple_stack[i], STACK_SIZE,
				test_sem_multi_take_timeout_diff_sem_helper,
				(void *)seq_info[i].timeout,
				(void *)seq_info[i].sema,
				NULL,
				K_PRIO_PREEMPT(1), 0, K_NO_WAIT);
	}

	for (int i = 0; i < TOTAL_THREADS_WAITING; i++) {
		k_pipe_get(&timeout_info_pipe,
			   &retrieved_info,
			   sizeof(struct timeout_info),
			   &bytes_read,
			   sizeof(struct timeout_info),
			   K_FOREVER);


		zassert_true(retrieved_info.timeout == K_SECONDS(i + 1),
			     "timeout didn't occur properly");
	}

}

/* ztest main entry*/
void test_main(void)
{
	k_thread_access_grant(k_current_get(),
			      &simple_sem, &multiple_thread_sem,
			      &low_prio_sem, &mid_prio_sem, &high_prio_sem,
			      &stack_1, &stack_2, &stack_3, &timeout_info_pipe,
			      &sem_tid, &sem_tid_1, &sem_tid_2);

	ztest_test_suite(test_semaphore,
			 ztest_unit_test(test_simple_sem_from_isr),
			 ztest_user_unit_test(test_simple_sem_from_task),
			 ztest_user_unit_test(test_sem_take_no_wait),
			 ztest_user_unit_test(test_sem_take_no_wait_fails),
			 ztest_user_unit_test(test_sem_take_timeout_fails),
			 ztest_user_unit_test(test_sem_take_timeout),
			 ztest_user_unit_test(test_sem_take_timeout_forever),
			 ztest_unit_test(test_sem_take_timeout_isr),
			 ztest_user_unit_test(test_sem_take_multiple),
			 ztest_unit_test(test_sem_give_take_from_isr),
			 ztest_unit_test(test_sem_multiple_threads_wait),
			 ztest_unit_test(test_sem_measure_timeouts),
			 ztest_unit_test(test_sem_measure_timeout_from_thread),
			 ztest_unit_test(test_sem_multiple_take_and_timeouts),
			 ztest_unit_test(test_sem_multi_take_timeout_diff_sem));
	ztest_run_test_suite(test_semaphore);
}
/******************************************************************************/