• Home
Name Date Size #Lines LOC

..--

BUILD.gnD22-Oct-2025764 2219

README.mdD22-Oct-20255.4 KiB137102

mutex.cD22-Oct-20253.1 KiB10375

README.md

1# 润和星闪派物联网开发套件--互斥锁(Mutex)
2
3![hihope_illustration](https://gitee.com/hihopeorg/hispark-hm-pegasus/raw/master/docs/figures/hihope_illustration.png)
4
5[润和星闪派物联网开发套件](https://item.taobao.com/item.htm?abbucket=16&id=816685710481&ns=1&priceTId=214783b117346662457694855ed644&skuId=5533042544092&spm=a21n57.sem.item.49.46a639031zWytE&utparam=%7B%22aplus_abtest%22%3A%22b28048df8f009463834be6bdac2a3713%22%7D&xxc=taobaoSearch) 基于海思WS63E解决方案的一套软硬件组合的综合性开发套件。
6
7![wifi_iot](https://img.alicdn.com/imgextra/i4/3583112207/O1CN01SvRG981SAr7bdEg3i_!!3583112207.png)
8
9## 一、Mutex API
10
11| API名称         | 说明                                                     |
12| --------------- | -------------------------------------------------------- |
13| osMutexNew      | 创建并初始化一个互斥锁                                   |
14| osMutexGetName  | 获得指定互斥锁的名字                                     |
15| osMutexAcquire  | 获得指定的互斥锁的访问权限,若互斥锁已经被锁,则返回超时 |
16| osMutexRelease  | 释放指定的互斥锁                                         |
17| osMutexGetOwner | 获得指定互斥锁的所有者线程                               |
18| osMutexDelete   | 删除指定的互斥锁                                         |
19
20## 二、代码分析
21
22全局变量`g_test_value`若同时被多个线程访问,会将其加1,然后判断其奇偶性,并输出日志,如果没有互斥锁保护,线程会被中断导致错误,所以需要创建互斥锁来保护多线程共享区域
23
24```c
25void number_thread(void *arg) {
26    osMutexId_t *mid = (osMutexId_t *)arg;
27    while(1) {
28        if (osMutexAcquire(*mid, 100) == osOK) {
29            g_test_value++;
30            if (g_test_value % 2 == 0) {
31                printf("[Mutex Test] %s gets an even value %d.\r\n", osThreadGetName(osThreadGetId()), g_test_value);
32            } else {
33                printf("[Mutex Test] %s gets an odd value %d.\r\n",  osThreadGetName(osThreadGetId()), g_test_value);
34            }
35            osMutexRelease(*mid);
36            osDelay(5);
37        }
38    }
39}
40```
41
42创建三个线程访问全局变量`g_test_value` ,同时创建一个互斥锁共所有线程使用
43
44```c
45void rtosv2_mutex_main(void *arg) {
46    (void)arg;
47    osMutexAttr_t attr = {0};
48
49    osMutexId_t mid = osMutexNew(&attr);
50    if (mid == NULL) {
51        printf("[Mutex Test] osMutexNew, create mutex failed.\r\n");
52    } else {
53        printf("[Mutex Test] osMutexNew, create mutex success.\r\n");
54    }
55
56    osThreadId_t tid1 = newThread("Thread_1", number_thread, &mid);
57    osThreadId_t tid2 = newThread("Thread_2", number_thread, &mid);
58    osThreadId_t tid3 = newThread("Thread_3", number_thread, &mid);
59
60    osDelay(13);
61    osThreadId_t tid = osMutexGetOwner(mid);
62    printf("[Mutex Test] osMutexGetOwner, thread id: %p, thread name: %s.\r\n", tid, osThreadGetName(tid));
63    osDelay(17);
64
65    osThreadTerminate(tid1);
66    osThreadTerminate(tid2);
67    osThreadTerminate(tid3);
68    osMutexDelete(mid);
69}
70```
71
72
73
74## 三、如何编译
75
761. 将00_thread目录复制到openharmony源码的`applications\sample\wifi-iot\app`目录下,
772. 修改openharmony源码的`applications\sample\wifi-iot\app\BUILD.gn`文件,将其中的 `features` 改为:
78
79```
80    features = [
81        ...
82        "03_mutex:mutex_demo",
83        ...
84    ]
85```
863. 在`device\soc\hisilicon\ws63v100\sdk\build\config\target_config\ws63\config.py`文件中,找到`'ws63-liteos-app'`部分,在其`'ram_component'`中,添加以下代码:
87```
88"mutex_demo"
89```
90
914. 在`device\soc\hisilicon\ws63v100\sdk\libs_url\ws63\cmake\ohos.cmake`文件中,找到`"ws63-liteos-app"`部分,在其`set(COMPONENT_LIST`部分,添加以下代码:
92```
93"mutex_demo"
94```
955. 在openharmony sdk根目录目录执行:`rm -rf out && hb set -p nearlink_dk_3863 && hb build -f`
96
97## 四、运行结果
98
99设置串口工具波特率为115200,复位开发板,查看打印结果
100
101```
102[Mutex Test]Thread_1 gets an even value 4.
103[Mutex Test]Thread_2 gets an odd value 5.
104[Mutex Test]Thread_3 gets an even value 6.
105
106[17:45:54.066]收←◆[Mutex Test]Thread_1 gets an odd value 7.
107[Mutex Test]Thread_2 gets an even value 8.
108[Mutex Test]Thread_3 gets an odd value 9.
109[Mutex Test]osMutexGetOwner, thread id: (nil), thread name: (null).
110
111[17:45:54.115]收←◆[Mutex Test]Thread_1 gets an even value 10.
112[Mutex Test]Thread_2 gets an odd value 11.
113[Mutex Test]Thread_3 gets an even value 12.
114
115[17:45:54.165]收←◆[Mutex Test]Thread_1 gets an odd value 13.
116[Mutex Test]Thread_2 gets an even value 14.
117[Mutex Test]Thread_3 gets an odd value 15.
118
119[17:45:54.215]收←◆[Mutex Test]Thread_1 gets an even value 16.
120[Mutex Test]Thread_2 gets an odd value 17.
121[Mutex Test]Thread_3 gets an even value 18.
122
123```
124
125### 【套件支持】
126
127##### 1. 套件购买  https://item.taobao.com/item.htm?abbucket=16&id=816685710481&ns=1&priceTId=214783b117346662457694855ed644&skuId=5533042544092&spm=a21n57.sem.item.49.46a639031zWytE&utparam=%7B%22aplus_abtest%22%3A%22b28048df8f009463834be6bdac2a3713%22%7D&xxc=taobaoSearch
128
129##### 2. 技术资料
130
131- Gitee码云网站(使用说明书、规格说明书、OpenHarmony开发案例等) **https://gitee.com/hihopeorg_group/near-link**
132- fbb_ws63代码仓(SDK包、技术文档下载)**https://gitee.com/HiSpark/fbb_ws63**
133
134##### 3. 互动交流
135- 海思社区星闪专区-论坛 **https://developer.hisilicon.com/forum/0133146886267870001**
136
137