1 /*
2  * Copyright (C) 2015-2017 Alibaba Group Holding Limited
3  */
4 
5 #include "aos_cpp.h"
6 #include <stdio.h>
7 
8 using namespace AOS;
9 
10 #define STACK_BUF_BYTES 2048
11 #define BUF_QUEUE_BYTES 1024
12 
13 static thread *   pa;
14 static thread *   pb;
15 static Semaphore *mySem;
16 
demo_task1(void * arg)17 static void demo_task1(void *arg)
18 {
19     int count = 0;
20 
21     while (1) {
22         printf("demo_task1 count  %d\n", count++);
23 
24         mySem->wait(0xFFFFFFFF);
25 
26         printf("demo_task1 get Semaphore\n");
27 
28         if (count == 10) {
29             pa->terminate();
30         }
31     };
32 }
33 
demo_task2(void * arg)34 static void demo_task2(void *arg)
35 {
36     int count = 0;
37 
38     while (1) {
39         printf("demo_task2 count  %d\n", count++);
40 
41         mySem->release();
42         printf("demo_task2 release Semaphore\n");
43 
44         if (count == 10) {
45             pb->terminate();
46         }
47 
48         pb->sleep((uint32_t)1000);
49     };
50 }
51 
test_semaphore(void)52 void test_semaphore(void)
53 {
54     pa    = new thread;
55     pb    = new thread;
56     mySem = new Semaphore;
57 
58     mySem->create("sem", 0);
59 
60     pa->create("task", (void *)NULL, (uint8_t)20, (tick_t)50, STACK_BUF_BYTES,
61                demo_task1, (uint8_t)1);
62 
63     pb->create("task", (void *)NULL, (uint8_t)20, (tick_t)50, STACK_BUF_BYTES,
64                demo_task2, (uint8_t)1);
65 }
66