1 /*
2  * Copyright (c) 2020 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #include <zephyr/kernel.h>
7 #include <zephyr/arch/cpu.h>
8 
9 #define NUM_THREADS 3
10 #define TCOUNT 10
11 #define COUNT_LIMIT 12
12 
13 static int count;
14 
15 K_MUTEX_DEFINE(count_mutex);
16 K_CONDVAR_DEFINE(count_threshold_cv);
17 
18 #define STACK_SIZE (1024 + CONFIG_TEST_EXTRA_STACK_SIZE)
19 
20 K_THREAD_STACK_ARRAY_DEFINE(tstacks, NUM_THREADS, STACK_SIZE);
21 
22 static struct k_thread t[NUM_THREADS];
23 
inc_count(void * p1,void * p2,void * p3)24 void inc_count(void *p1, void *p2, void *p3)
25 {
26 	int i;
27 	long my_id = (long)p1;
28 
29 	for (i = 0; i < TCOUNT; i++) {
30 		k_mutex_lock(&count_mutex, K_FOREVER);
31 		count++;
32 
33 		/*
34 		 * Check the value of count and signal waiting thread when
35 		 * condition is reached.  Note that this occurs while mutex is
36 		 * locked.
37 		 */
38 
39 		if (count == COUNT_LIMIT) {
40 			printk("%s: thread %ld, count = %d  Threshold reached.",
41 			       __func__, my_id, count);
42 			k_condvar_signal(&count_threshold_cv);
43 			printk("Just sent signal.\n");
44 		}
45 		printk("%s: thread %ld, count = %d, unlocking mutex\n",
46 		       __func__, my_id, count);
47 		k_mutex_unlock(&count_mutex);
48 
49 		/* Sleep so threads can alternate on mutex lock */
50 		k_sleep(K_MSEC(500));
51 	}
52 }
53 
watch_count(void * p1,void * p2,void * p3)54 void watch_count(void *p1, void *p2, void *p3)
55 {
56 	long my_id = (long)p1;
57 
58 	printk("Starting %s: thread %ld\n", __func__, my_id);
59 
60 	k_mutex_lock(&count_mutex, K_FOREVER);
61 	while (count < COUNT_LIMIT) {
62 		printk("%s: thread %ld Count= %d. Going into wait...\n",
63 		       __func__, my_id, count);
64 		k_condvar_wait(&count_threshold_cv, &count_mutex, K_FOREVER);
65 
66 		printk("%s: thread %ld Condition signal received. Count= %d\n",
67 		       __func__, my_id, count);
68 	}
69 	printk("%s: thread %ld Updating the value of count...\n",
70 	       __func__, my_id);
71 	count += 125;
72 	printk("%s: thread %ld count now = %d.\n", __func__, my_id, count);
73 	printk("%s: thread %ld Unlocking mutex.\n", __func__, my_id);
74 	k_mutex_unlock(&count_mutex);
75 }
76 
main(void)77 int main(void)
78 {
79 	long t1 = 1, t2 = 2, t3 = 3;
80 	int i;
81 
82 	count = 0;
83 
84 	k_thread_create(&t[0], tstacks[0], STACK_SIZE, watch_count,
85 			INT_TO_POINTER(t1), NULL, NULL, K_PRIO_PREEMPT(10), 0,
86 			K_NO_WAIT);
87 
88 	k_thread_create(&t[1], tstacks[1], STACK_SIZE, inc_count,
89 			INT_TO_POINTER(t2), NULL, NULL, K_PRIO_PREEMPT(10), 0,
90 			K_NO_WAIT);
91 
92 	k_thread_create(&t[2], tstacks[2], STACK_SIZE, inc_count,
93 			INT_TO_POINTER(t3), NULL, NULL, K_PRIO_PREEMPT(10), 0,
94 			K_NO_WAIT);
95 
96 	/* Wait for all threads to complete */
97 	for (i = 0; i < NUM_THREADS; i++) {
98 		k_thread_join(&t[i], K_FOREVER);
99 	}
100 
101 	printk("Main(): Waited and joined with %d threads. Final value of count = %d. Done.\n",
102 	       NUM_THREADS, count);
103 	return 0;
104 }
105