10a7ce71fSopenharmony_ci# HiSpark WiFi-IoT 套件样例开发--信号量(Semaphore)
20a7ce71fSopenharmony_ci
30a7ce71fSopenharmony_ci![hihope_illustration](https://gitee.com/hihopeorg/hispark-hm-pegasus/raw/master/docs/figures/hihope_illustration.png)
40a7ce71fSopenharmony_ci
50a7ce71fSopenharmony_ci[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开发者中得到广泛应用。
60a7ce71fSopenharmony_ci
70a7ce71fSopenharmony_ci![wifi_iot](https://gitee.com/hihopeorg/hispark-hm-pegasus/raw/master/docs/figures/2.png)
80a7ce71fSopenharmony_ci
90a7ce71fSopenharmony_ci## 一、Semaphore API
100a7ce71fSopenharmony_ci
110a7ce71fSopenharmony_ci| API名称             | 说明                                                         |
120a7ce71fSopenharmony_ci| ------------------- | ------------------------------------------------------------ |
130a7ce71fSopenharmony_ci| osSemaphoreNew      | 创建并初始化一个信号量                                       |
140a7ce71fSopenharmony_ci| osSemaphoreGetName  | 获取一个信号量的名字                                         |
150a7ce71fSopenharmony_ci| osSemaphoreAcquire  | 获取一个信号量的令牌,若获取不到,则会超时返回               |
160a7ce71fSopenharmony_ci| osSemaphoreRelease  | 释放一个信号量的令牌,但是令牌的数量不超过初始定义的的令牌数 |
170a7ce71fSopenharmony_ci| osSemaphoreGetCount | 获取当前的信号量令牌数                                       |
180a7ce71fSopenharmony_ci| osSemaphoreDelete   | 删除一个信号量                                               |
190a7ce71fSopenharmony_ci
200a7ce71fSopenharmony_ci## 二、代码分析
210a7ce71fSopenharmony_ci
220a7ce71fSopenharmony_ci`osSemaphoreAcquire`获取共享资源的访问权限,若获取失败,则等待;访问成功后,可以通过`osSemaphoreRelease`释放对共享资源的访问
230a7ce71fSopenharmony_ci
240a7ce71fSopenharmony_ci本样例为经典的消费者与生产者问题,需要确保仓库满时,生产者需要进入等待状态,产品消费完时,消费者需要进入等待状态
250a7ce71fSopenharmony_ci
260a7ce71fSopenharmony_ci```c
270a7ce71fSopenharmony_civoid producer_thread(void *arg) {
280a7ce71fSopenharmony_ci    (void)arg;
290a7ce71fSopenharmony_ci    empty_id = osSemaphoreNew(BUFFER_SIZE, BUFFER_SIZE, NULL);
300a7ce71fSopenharmony_ci    filled_id = osSemaphoreNew(BUFFER_SIZE, 0U, NULL);
310a7ce71fSopenharmony_ci    while(1) {
320a7ce71fSopenharmony_ci        osSemaphoreAcquire(empty_id, osWaitForever);
330a7ce71fSopenharmony_ci        product_number++;
340a7ce71fSopenharmony_ci        printf("[Semp Test]%s produces a product, now product number: %d.\r\n", osThreadGetName(osThreadGetId()), product_number);
350a7ce71fSopenharmony_ci        osDelay(4);
360a7ce71fSopenharmony_ci        osSemaphoreRelease(filled_id);
370a7ce71fSopenharmony_ci    }
380a7ce71fSopenharmony_ci}
390a7ce71fSopenharmony_ci
400a7ce71fSopenharmony_civoid consumer_thread(void *arg) {
410a7ce71fSopenharmony_ci    (void)arg;
420a7ce71fSopenharmony_ci    while(1){
430a7ce71fSopenharmony_ci        osSemaphoreAcquire(filled_id, osWaitForever);
440a7ce71fSopenharmony_ci        product_number--;
450a7ce71fSopenharmony_ci        printf("[Semp Test]%s consumes a product, now product number: %d.\r\n", osThreadGetName(osThreadGetId()), product_number);
460a7ce71fSopenharmony_ci        osDelay(3);
470a7ce71fSopenharmony_ci        osSemaphoreRelease(empty_id);
480a7ce71fSopenharmony_ci    }
490a7ce71fSopenharmony_ci}
500a7ce71fSopenharmony_ci```
510a7ce71fSopenharmony_ci
520a7ce71fSopenharmony_ci由于消费产品的速度大于生产速度,所以定义了三个生产者,两个消费者
530a7ce71fSopenharmony_ci
540a7ce71fSopenharmony_ci```c
550a7ce71fSopenharmony_civoid rtosv2_semp_main(void *arg) {
560a7ce71fSopenharmony_ci    (void)arg;
570a7ce71fSopenharmony_ci    empty_id = osSemaphoreNew(BUFFER_SIZE, BUFFER_SIZE, NULL);
580a7ce71fSopenharmony_ci    filled_id = osSemaphoreNew(BUFFER_SIZE, 0U, NULL);
590a7ce71fSopenharmony_ci 
600a7ce71fSopenharmony_ci    osThreadId_t ptid1 = newThread("producer1", producer_thread, NULL);
610a7ce71fSopenharmony_ci    osThreadId_t ptid2 = newThread("producer2", producer_thread, NULL);
620a7ce71fSopenharmony_ci    osThreadId_t ptid3 = newThread("producer3", producer_thread, NULL);
630a7ce71fSopenharmony_ci    osThreadId_t ctid1 = newThread("consumer1", consumer_thread, NULL);
640a7ce71fSopenharmony_ci    osThreadId_t ctid2 = newThread("consumer2", consumer_thread, NULL);
650a7ce71fSopenharmony_ci
660a7ce71fSopenharmony_ci    osDelay(50);
670a7ce71fSopenharmony_ci
680a7ce71fSopenharmony_ci    osThreadTerminate(ptid1);
690a7ce71fSopenharmony_ci    osThreadTerminate(ptid2);
700a7ce71fSopenharmony_ci    osThreadTerminate(ptid3);
710a7ce71fSopenharmony_ci    osThreadTerminate(ctid1);
720a7ce71fSopenharmony_ci    osThreadTerminate(ctid2);
730a7ce71fSopenharmony_ci
740a7ce71fSopenharmony_ci    osSemaphoreDelete(empty_id);
750a7ce71fSopenharmony_ci    osSemaphoreDelete(filled_id);
760a7ce71fSopenharmony_ci}
770a7ce71fSopenharmony_ci```
780a7ce71fSopenharmony_ci
790a7ce71fSopenharmony_ci
800a7ce71fSopenharmony_ci
810a7ce71fSopenharmony_ci## 三、如何编译
820a7ce71fSopenharmony_ci
830a7ce71fSopenharmony_ci1. 将此目录下的 `semp.c` 和 `BUILD.gn` 复制到openharmony源码的`applications\sample\wifi-iot\app\iothardware`目录下,
840a7ce71fSopenharmony_ci2. 修改openharmony源码的`applications\sample\wifi-iot\app\BUILD.gn`文件,将其中的 `features` 改为:
850a7ce71fSopenharmony_ci
860a7ce71fSopenharmony_ci```
870a7ce71fSopenharmony_ci    features = [
880a7ce71fSopenharmony_ci        "iothardware:semp_demo",
890a7ce71fSopenharmony_ci    ]
900a7ce71fSopenharmony_ci```
910a7ce71fSopenharmony_ci
920a7ce71fSopenharmony_ci3. 在openharmony源码顶层目录执行:`python build.py wifiiot`
930a7ce71fSopenharmony_ci
940a7ce71fSopenharmony_ci## 四、运行结果
950a7ce71fSopenharmony_ci
960a7ce71fSopenharmony_ci截取部分运行结果
970a7ce71fSopenharmony_ci
980a7ce71fSopenharmony_ci```
990a7ce71fSopenharmony_ci[Semp Test] osThreadNew(consumer1) success, thread id: 0xe8910.
1000a7ce71fSopenharmony_ci[Semp Test] osThreadNew(consumer2) success, thread id: 0xe871c.
1010a7ce71fSopenharmony_ci[Semp Test] osThreadNew(producer1) success, thread id: 0xe84c4.
1020a7ce71fSopenharmony_ci[Semp Test] [Semp Test] producer1 produces a product, now product number: 1.
1030a7ce71fSopenharmony_ci[Semp Test] producer2 produces a product, now product number: 2.
1040a7ce71fSopenharmony_ciosThreadNew(producer2) success, thread id: 0xe8974.
1050a7ce71fSopenharmony_ci[Semp Test] osThreadNew(producer3) success, thread id: 0xe89d8.
1060a7ce71fSopenharmony_ci[Semp Test] producer3 produces a product, now product number: 3.
1070a7ce71fSopenharmony_ci[Semp Test] producer1 produces a product, now product number: 4.
1080a7ce71fSopenharmony_ci[Semp Test] consumer1 consumes a product, now product number: 3.
1090a7ce71fSopenharmony_ci[Semp Test] producer2 produces a product, now product number: 4.
1100a7ce71fSopenharmony_ci[Semp Test] consumer2 consumes a product, now product number: 3.
1110a7ce71fSopenharmony_ci[Semp Test] consumer1 consumes a product, now product number: 2.
1120a7ce71fSopenharmony_ci[Semp Test] producer3 produces a product, now product number: 3.
1130a7ce71fSopenharmony_ci[Semp Test] consumer2 consumes a product, now product number: 2.
1140a7ce71fSopenharmony_ci[Semp Test] producer1 produces a product, now product number: 3.
1150a7ce71fSopenharmony_ci```
1160a7ce71fSopenharmony_ci
1170a7ce71fSopenharmony_ci### 【套件支持】
1180a7ce71fSopenharmony_ci
1190a7ce71fSopenharmony_ci##### 1. 套件介绍  http://www.hihope.org/pro/pro1.aspx?mtt=8
1200a7ce71fSopenharmony_ci
1210a7ce71fSopenharmony_ci##### 2. 套件购买  https://item.taobao.com/item.htm?id=622343426064&scene=taobao_shop
1220a7ce71fSopenharmony_ci
1230a7ce71fSopenharmony_ci##### 3. 技术资料
1240a7ce71fSopenharmony_ci
1250a7ce71fSopenharmony_ci- Gitee码云网站(OpenHarmony Sample Code等) **https://gitee.com/hihopeorg**
1260a7ce71fSopenharmony_ci
1270a7ce71fSopenharmony_ci- HiHope官网-资源中心(SDK包、技术文档下载)[**www.hihope.org**](http://www.hihope.org/)
1280a7ce71fSopenharmony_ci
1290a7ce71fSopenharmony_ci##### 4. 互动交流
1300a7ce71fSopenharmony_ci
1310a7ce71fSopenharmony_ci- 润和HiHope技术交流-微信群(加群管理员微信13605188699,发送文字#申请加入润和官方群#,予以邀请入群)
1320a7ce71fSopenharmony_ci- HiHope开发者社区-论坛 **https://bbs.elecfans.com/group_1429**
1330a7ce71fSopenharmony_ci- 润和HiHope售后服务群(QQ:980599547)
1340a7ce71fSopenharmony_ci- 售后服务电话(025-52668590)
1350a7ce71fSopenharmony_ci
136