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