1 /*
2 * Copyright (C) 2015-2017 Alibaba Group Holding Limited
3 */
4
5 #include "aos_cpp.h"
6 #include "aos/kernel.h"
7 #include <stdio.h>
8
9 using namespace AOS;
10
11 #define STACK_BUF_BYTES 2048
12 #define BUF_QUEUE_BYTES 1024
13
14 static thread *pa;
15 static thread *pb;
16 static Mutex * myMutex;
17
demo_task1(void * arg)18 static void demo_task1(void *arg)
19 {
20 int count = 0;
21 long long time_start = 0;
22 long long time_cur = 0;
23
24 while (1) {
25 printf("demo_task1 count %d\n", count++);
26
27 myMutex->lock(0xFFFFFFFF);
28 printf("demo_task1 lock\n");
29
30 time_start = aos_now_ms();
31
32 do {
33 time_cur = aos_now_ms();
34 } while (time_cur <= (time_start + 3000));
35
36 myMutex->unlock();
37 printf("demo_task1 unlock\n");
38
39 pa->sleep(1000);
40
41 if (count == 10) {
42 pa->terminate();
43 }
44 };
45 }
46
demo_task2(void * arg)47 static void demo_task2(void *arg)
48 {
49 int count = 0;
50
51 while (1) {
52 printf("demo_task2 count %d\n", count++);
53
54 myMutex->lock(0xFFFFFFFF);
55 printf("demo_task2 lock\n");
56
57 myMutex->unlock();
58 printf("demo_task2 unlock\n");
59
60 pb->sleep(1000);
61
62 if (count == 10) {
63 pb->terminate();
64 }
65 };
66 }
67
test_mutex(void)68 void test_mutex(void)
69 {
70 pa = new thread;
71 pb = new thread;
72 myMutex = new Mutex;
73
74 myMutex->create("mutex");
75
76 pa->create("task", (void *)NULL, (uint8_t)20, (tick_t)50, STACK_BUF_BYTES,
77 demo_task1, (uint8_t)1);
78
79 pb->create("task", (void *)NULL, (uint8_t)20, (tick_t)50, STACK_BUF_BYTES,
80 demo_task2, (uint8_t)1);
81 }
82