69 lines
1.7 KiB
C
69 lines
1.7 KiB
C
#include "tos_k.h"
|
|
|
|
#define STK_SIZE_TASK_1 512
|
|
#define STK_SIZE_TASK_2 512
|
|
#define MAX_COUNT 5
|
|
|
|
k_stack_t stack_task_1[STK_SIZE_TASK_1];
|
|
k_stack_t stack_task_2[STK_SIZE_TASK_2];
|
|
|
|
k_task_t task_1;
|
|
k_task_t task_2;
|
|
|
|
extern void entry_task_1(void *arg);
|
|
extern void entry_task_2(void *arg);
|
|
|
|
k_mutex_t counter_lock;
|
|
|
|
static uint32_t counter = 0;
|
|
|
|
void entry_task_1(void *arg)
|
|
{
|
|
k_err_t err;
|
|
|
|
while (K_TRUE) {
|
|
if (counter >= MAX_COUNT) {
|
|
tos_task_destroy(K_NULL);
|
|
}
|
|
err = tos_mutex_pend(&counter_lock);
|
|
if (err == K_ERR_NONE) {
|
|
counter++;
|
|
printf("Task %s: Counter is %d\n", tos_task_curr_task_get() -> name, counter);
|
|
tos_mutex_post(&counter_lock);
|
|
}
|
|
if (counter > 50) {
|
|
tos_task_destroy(K_NULL);
|
|
}
|
|
tos_task_delay(300);
|
|
}
|
|
}
|
|
|
|
void entry_task_2(void *arg)
|
|
{
|
|
k_err_t err;
|
|
|
|
while (K_TRUE) {
|
|
if (counter >= MAX_COUNT) {
|
|
tos_task_destroy(K_NULL);
|
|
}
|
|
err = tos_mutex_pend(&counter_lock);
|
|
if (err == K_ERR_NONE) {
|
|
counter++;
|
|
printf("Task %s: Counter is %d\n", tos_task_curr_task_get() -> name, counter);
|
|
tos_mutex_post(&counter_lock);
|
|
}
|
|
tos_task_delay(300);
|
|
}
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
tos_knl_init();
|
|
// 创建临界区保护互斥锁
|
|
tos_mutex_create(&counter_lock);
|
|
(void)tos_task_create(&task_1, "task1", entry_task_1, NULL,
|
|
4, stack_task_1, STK_SIZE_TASK_1, 0);
|
|
(void)tos_task_create(&task_2, "task2", entry_task_2, NULL,
|
|
4, stack_task_2, STK_SIZE_TASK_2, 0);
|
|
tos_knl_start();
|
|
} |