README.md
1# HiSpark WiFi-IoT 套件样例开发--信号量(Semaphore)
2
3![hihope_illustration](https://gitee.com/hihopeorg/hispark-hm-pegasus/raw/master/docs/figures/hihope_illustration.png)
4
5[HiSpark WiFi-IoT开发套件](https://item.taobao.com/item.htm?spm=a1z10.1-c-s.w5003-23341819265.1.bf644a82Da9PZK&id=622343426064&scene=taobao_shop) 首发于HDC 2020,是首批支持OpenHarmony 2.0的开发套件,亦是官方推荐套件,由润和软件HiHope量身打造,已在OpenHarmony社区和广大OpenHarmony开发者中得到广泛应用。
6
7![wifi_iot](https://gitee.com/hihopeorg/hispark-hm-pegasus/raw/master/docs/figures/2.png)
8
9## 一、Semaphore API
10
11| API名称 | 说明 |
12| ------------------- | ------------------------------------------------------------ |
13| osSemaphoreNew | 创建并初始化一个信号量 |
14| osSemaphoreGetName | 获取一个信号量的名字 |
15| osSemaphoreAcquire | 获取一个信号量的令牌,若获取不到,则会超时返回 |
16| osSemaphoreRelease | 释放一个信号量的令牌,但是令牌的数量不超过初始定义的的令牌数 |
17| osSemaphoreGetCount | 获取当前的信号量令牌数 |
18| osSemaphoreDelete | 删除一个信号量 |
19
20## 二、代码分析
21
22`osSemaphoreAcquire`获取共享资源的访问权限,若获取失败,则等待;访问成功后,可以通过`osSemaphoreRelease`释放对共享资源的访问
23
24本样例为经典的消费者与生产者问题,需要确保仓库满时,生产者需要进入等待状态,产品消费完时,消费者需要进入等待状态
25
26```c
27void producer_thread(void *arg) {
28 (void)arg;
29 empty_id = osSemaphoreNew(BUFFER_SIZE, BUFFER_SIZE, NULL);
30 filled_id = osSemaphoreNew(BUFFER_SIZE, 0U, NULL);
31 while(1) {
32 osSemaphoreAcquire(empty_id, osWaitForever);
33 product_number++;
34 printf("[Semp Test]%s produces a product, now product number: %d.\r\n", osThreadGetName(osThreadGetId()), product_number);
35 osDelay(4);
36 osSemaphoreRelease(filled_id);
37 }
38}
39
40void consumer_thread(void *arg) {
41 (void)arg;
42 while(1){
43 osSemaphoreAcquire(filled_id, osWaitForever);
44 product_number--;
45 printf("[Semp Test]%s consumes a product, now product number: %d.\r\n", osThreadGetName(osThreadGetId()), product_number);
46 osDelay(3);
47 osSemaphoreRelease(empty_id);
48 }
49}
50```
51
52由于消费产品的速度大于生产速度,所以定义了三个生产者,两个消费者
53
54```c
55void rtosv2_semp_main(void *arg) {
56 (void)arg;
57 empty_id = osSemaphoreNew(BUFFER_SIZE, BUFFER_SIZE, NULL);
58 filled_id = osSemaphoreNew(BUFFER_SIZE, 0U, NULL);
59
60 osThreadId_t ptid1 = newThread("producer1", producer_thread, NULL);
61 osThreadId_t ptid2 = newThread("producer2", producer_thread, NULL);
62 osThreadId_t ptid3 = newThread("producer3", producer_thread, NULL);
63 osThreadId_t ctid1 = newThread("consumer1", consumer_thread, NULL);
64 osThreadId_t ctid2 = newThread("consumer2", consumer_thread, NULL);
65
66 osDelay(50);
67
68 osThreadTerminate(ptid1);
69 osThreadTerminate(ptid2);
70 osThreadTerminate(ptid3);
71 osThreadTerminate(ctid1);
72 osThreadTerminate(ctid2);
73
74 osSemaphoreDelete(empty_id);
75 osSemaphoreDelete(filled_id);
76}
77```
78
79
80
81## 三、如何编译
82
831. 将此目录下的 `semp.c` 和 `BUILD.gn` 复制到openharmony源码的`applications\sample\wifi-iot\app\iothardware`目录下,
842. 修改openharmony源码的`applications\sample\wifi-iot\app\BUILD.gn`文件,将其中的 `features` 改为:
85
86```
87 features = [
88 "iothardware:semp_demo",
89 ]
90```
91
923. 在openharmony源码顶层目录执行:`python build.py wifiiot`
93
94## 四、运行结果
95
96截取部分运行结果
97
98```
99[Semp Test] osThreadNew(consumer1) success, thread id: 0xe8910.
100[Semp Test] osThreadNew(consumer2) success, thread id: 0xe871c.
101[Semp Test] osThreadNew(producer1) success, thread id: 0xe84c4.
102[Semp Test] [Semp Test] producer1 produces a product, now product number: 1.
103[Semp Test] producer2 produces a product, now product number: 2.
104osThreadNew(producer2) success, thread id: 0xe8974.
105[Semp Test] osThreadNew(producer3) success, thread id: 0xe89d8.
106[Semp Test] producer3 produces a product, now product number: 3.
107[Semp Test] producer1 produces a product, now product number: 4.
108[Semp Test] consumer1 consumes a product, now product number: 3.
109[Semp Test] producer2 produces a product, now product number: 4.
110[Semp Test] consumer2 consumes a product, now product number: 3.
111[Semp Test] consumer1 consumes a product, now product number: 2.
112[Semp Test] producer3 produces a product, now product number: 3.
113[Semp Test] consumer2 consumes a product, now product number: 2.
114[Semp Test] producer1 produces a product, now product number: 3.
115```
116
117### 【套件支持】
118
119##### 1. 套件介绍 http://www.hihope.org/pro/pro1.aspx?mtt=8
120
121##### 2. 套件购买 https://item.taobao.com/item.htm?id=622343426064&scene=taobao_shop
122
123##### 3. 技术资料
124
125- Gitee码云网站(OpenHarmony Sample Code等) **https://gitee.com/hihopeorg**
126
127- HiHope官网-资源中心(SDK包、技术文档下载)[**www.hihope.org**](http://www.hihope.org/)
128
129##### 4. 互动交流
130
131- 润和HiHope技术交流-微信群(加群管理员微信13605188699,发送文字#申请加入润和官方群#,予以邀请入群)
132- HiHope开发者社区-论坛 **https://bbs.elecfans.com/group_1429**
133- 润和HiHope售后服务群(QQ:980599547)
134- 售后服务电话(025-52668590)
135
136