当前位置: 首页 > article >正文

ESP32移植Openharmony设备开发---(6)Mutex互斥锁

Mutex互斥锁

官方文档:OpenAtom OpenHarmony

基本概念

互斥锁又称互斥型信号量,用于实现对共享资源的独占式处理。当有任务持有时,这个任务获得该互斥锁的所有权。当该任务释放它时,任务失去该互斥锁的所有权。当一个任务持有互斥锁时,其他任务将不能再持有该互斥锁。多任务环境下往往存在多个任务竞争同一共享资源的应用场景,互斥锁可被用于对共享资源的保护从而实现独占式访问。

互斥锁属性包含3个属性:协议属性、优先级上限属性和类型属性。协议属性用于处理不同优先级的任务申请互斥锁,协议属性包含如下三种:

  • LOS_MUX_PRIO_NONE 不对申请互斥锁的任务的优先级进行继承或保护操作。
  • LOS_MUX_PRIO_INHERIT 优先级继承属性,默认设置为该属性,对申请互斥锁的任务的优先级进行继承。在互斥锁设置为本协议属性情况下,申请互斥锁时,如果高优先级任务阻塞于互斥锁,则把持有互斥锁任务的优先级备份到任务控制块的优先级位图中,然后把任务优先级设置为和高优先级任务相同的优先级;持有互斥锁的任务释放互斥锁时,从任务控制块的优先级位图恢复任务优先级。
  • LOS_MUX_PRIO_PROTECT 优先级保护属性,对申请互斥锁的任务的优先级进行保护。在互斥锁设置为本协议属性情况下,申请互斥锁时,如果任务优先级小于互斥锁优先级上限,则把任务优先级备份到任务控制块的优先级位图中,然后把任务优先级设置为互斥锁优先级上限属性值;释放互斥锁时,从任务控制块的优先级位图恢复任务优先级。

互斥锁的类型属性用于标记是否检测死锁,是否支持递归持有,类型属性包含如下三种:

  • LOS_MUX_NORMAL 普通互斥锁,不会检测死锁。如果任务试图对一个互斥锁重复持有,将会引起这个线程的死锁。如果试图释放一个由别的任务持有的互斥锁,或者如果一个任务试图重复释放互斥锁都会引发不可预料的结果。
  • LOS_MUX_RECURSIVE 递归互斥锁,默认设置为该属性。在互斥锁设置为本类型属性情况下,允许同一个任务对互斥锁进行多次持有锁,持有锁次数和释放锁次数相同,其他任务才能持有该互斥锁。如果试图持有已经被其他任务持有的互斥锁,或者如果试图释放已经被释放的互斥锁,会返回错误码。
  • LOS_MUX_ERRORCHECK 错误检测互斥锁,会自动检测死锁。在互斥锁设置为本类型属性情况下,如果任务试图对一个互斥锁重复持有,或者试图释放一个由别的任务持有的互斥锁,或者如果一个任务试图释放已经被释放的互斥锁,都会返回错误码。

运行机制

多任务环境下会存在多个任务访问同一公共资源的场景,而有些公共资源是非共享的,需要任务进行独占式处理。互斥锁怎样来避免这种冲突呢?

用互斥锁处理非共享资源的同步访问时,如果有任务访问该资源,则互斥锁为加锁状态。此时其他任务如果想访问这个公共资源则会被阻塞,直到互斥锁被持有该锁的任务释放后,其他任务才能重新访问该公共资源,此时互斥锁再次上锁,如此确保同一时刻只有一个任务正在访问这个公共资源,保证了公共资源操作的完整性。

小型系统互斥锁运作示意图

BUILD.gn

# Copyright (c) 2022 Hunan OpenValley Digital Industry Development Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import("//kernel/liteos_m/liteos.gni")

module_name = get_path_info(rebase_path("."), "name")
kernel_module(module_name){
    sources = [
        "LOS_Mutex_example.c"
    ]
    
    include_dirs = [
        "//kernel/liteos_m/kernel/include"
    ]
}

LOS_Mutex_example

/*
 * Copyright (c) 2022 Hunan OpenValley Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "los_event.h"
#include "los_mux.h"
#include "los_task.h"
#include "ohos_run.h"

#ifndef osWaitForever
#define osWaitForever 0xFFFFFFFFU
#endif

#define EVENT_MASK_CLEAN 0x00000001
#define MAX_STR_LEN 256
#define DELAY_100_TICKS 100U
#define DELAY_400_TICKS (4 * DELAY_100_TICKS)
#define DELAY_600_TICKS (6 * DELAY_100_TICKS)
#define TASK_STACK_SIZE 4096
#define TASK_PRIO 5

UINT32 mutex1_id;
UINT32 mutex2_id;

static char g_str[MAX_STR_LEN];

void firstThread(UINT32 arg)
{
    int ret = -1;
    while (1) {
        LOS_MuxPend(mutex1_id, osWaitForever);
        ret = strcpy_s(g_str, MAX_STR_LEN, "firstThread");
        if (ret != 0) {
            printf("strcpy_s fail!\r\n");
        }
        printf("firstThread is Acquire. g_str=%s\r\n", g_str);
        LOS_MuxPost(mutex1_id);
        LOS_TaskDelay(DELAY_100_TICKS);
    }
}

void secondThread(UINT32 arg)
{
    int ret = -1;
    while (1) {
        LOS_MuxPend(mutex2_id, osWaitForever);
        char tmp[MAX_STR_LEN] = {0};
        ret = sprintf_s(tmp, MAX_STR_LEN, "%s+secondThread", g_str);
        if (ret < 0) {
            printf("sprintf_s fail!\r\n");
        }

        ret = memcpy_s(g_str, MAX_STR_LEN, tmp, sizeof(tmp));
        if (ret != 0) {
            printf("memcpy_s fail!\r\n");
        }
        printf("secondThread is Acquire. g_str=%s\r\n", g_str);
        LOS_MuxPost(mutex2_id);
        LOS_TaskDelay(DELAY_100_TICKS);
    }
}

void thirdThread(UINT32 arg)
{
    int ret;
    while (1) {
        LOS_MuxPend(mutex1_id, osWaitForever);
        LOS_MuxPend(mutex2_id, osWaitForever);
        char tmp[MAX_STR_LEN] = {0};
        ret = sprintf_s(tmp, MAX_STR_LEN, "%s+thirdThread", g_str);
        if (ret < 0) {
            printf("sprintf_s fail!\r\n");
        }

        ret = memcpy_s(g_str, MAX_STR_LEN, tmp, sizeof(tmp));
        if (ret != 0) {
            printf("memcpy_s fail!\r\n");
        }
        printf("thirdThread is Acquire. g_str=%s\r\n", g_str);
        LOS_TaskDelay(DELAY_100_TICKS);
        LOS_MuxPost(mutex1_id);
        LOS_TaskDelay(DELAY_400_TICKS);
        LOS_MuxPost(mutex2_id);
        LOS_TaskDelay(DELAY_600_TICKS);
    }
}

void os_mutex_example(void)
{
    int ret = -1;
    ret = memset_s(g_str, MAX_STR_LEN, 0, MAX_STR_LEN);
    if (ret != 0) {
        printf("memset_s fail!\r\n");
    }
    TSK_INIT_PARAM_S attr = {0};
    attr.uwStackSize = TASK_STACK_SIZE;
    attr.usTaskPrio = TASK_PRIO;

    // 创建互斥锁
    if (LOS_MuxCreate(&mutex1_id) != LOS_OK) {
        printf("create Mutex1 failed!  ret=%x\n", ret);
        return;
    }

    if (LOS_MuxCreate(&mutex2_id) != LOS_OK) {
        printf("create Mutex2 failed!\n");
        LOS_MuxDelete(mutex1_id);
        return;
    }

    // 锁任务调度
    LOS_TaskLock();
    attr.pcName = "firstThread";
    attr.pfnTaskEntry = (TSK_ENTRY_FUNC)firstThread;
    UINT32 taskIDFirst;
    if (LOS_TaskCreate(&taskIDFirst, &attr) != LOS_OK) {
        printf("create firstThread failed!\n");
        LOS_MuxDelete(mutex1_id);
        LOS_MuxDelete(mutex2_id);
        return;
    }
    attr.pcName = "secondThread";
    attr.pfnTaskEntry = (TSK_ENTRY_FUNC)secondThread;
    UINT32 taskIDTwo;
    if (LOS_TaskCreate(&taskIDTwo, &attr) != LOS_OK) {
        printf("create secondThread failed!\n");
    }
    attr.pcName = "thirdThread";
    attr.pfnTaskEntry = (TSK_ENTRY_FUNC)thirdThread;
    UINT32 taskIDThree;
    if (LOS_TaskCreate(&taskIDThree, &attr) != LOS_OK) {
        printf("create thirdThread failed!\n");
    }
    LOS_TaskUnlock(); // 解锁任务调度
}

OHOS_APP_RUN(os_mutex_example);

代码分析:

  • firstThread|secondThread|、thirdThread:循环中尝试获取mutex1_id,然后修改g_str的值,添加上自己的标识,并打印当前线程获得互斥锁后的g_str值。释放互斥锁并延时一段时间。

互斥锁同步行为分析

  • firstThreadsecondThread 分别对不同的互斥锁进行操作(互斥锁id不同),所以可以并行执行
  • thirdThread 则需要同时获得两个互斥锁,这意味着它将在其他两个任务释放各自的互斥锁后才能继续执行。这种情况下,thirdThread 的执行会依赖于firstThreadsecondThread 的进度。

API参考

LOS_MuxCreate()
UINT32 LOS_MuxCreate(UINT32 *muxHandle);

描述:

创建互斥锁

参数:

名字

描述

muxHandle

互斥锁id

LOS_MuxPend()
UINT32 LOS_MuxPend(UINT32 muxHandle, UINT32 timeout);

描述:

获取互斥锁

参数:

名字

描述

muxHandle

互斥锁id,由LOS_MuxCreate创建得到

timeout

delay时间

LOS_MuxPost()
UINT32 LOS_MuxPost(UINT32 muxHandle);

描述:

释放互斥锁

参数:

名字

描述

muxHandle

互斥锁id,由LOS_MuxCreate创建得到

LOS_MuxDelete()
UINT32 LOS_MuxDelete(UINT32 muxHandle);

描述:

删除互斥锁

参数:

名字

描述

muxHandle

互斥锁id,由LOS_MuxCreate创建得到

编译并烧录

修改工作文件夹级的BUILD.gn文件,将编译的文件夹指定为新建的005_LOS_Mutex

在源码根目录下使用hb工具对写好的代码进行编译

选择mini级系统

同理 产品选择esp公司下的esp32

选择完毕后在源码根目录下执行hb build -f 进行编译

编译完成后会有如下界面,并且编译后的代码固件位于:out\esp32\esp32

验证结果

打开串口工具->选择COM5->打开串口

按下ESP32开发板上的EN键,即可观察到实验现象:


http://www.kler.cn/news/363708.html

相关文章:

  • 香港国际金融市场的多元化投资与风险管理策略
  • 使用 CDN 后 Apache 的日志记录客户真实 IP
  • PoissonRecon学习笔记
  • 测试一个股票行情API
  • 如何用示波器检测次级点火系统(一)
  • 面试总结一
  • 「亲测好用」6款热门电脑卸载工具分享,快来看看哪个适合你?
  • YOLOv11改进策略【卷积层】| RCS-OSA 通道混洗的重参数化卷积 二次创新C3k2
  • 展会亮点回顾|HMS汽车工业通信解决方案
  • java中Set,Map,List集合的比较(不包含增删改查函数方法)
  • YoloV8改进策略:卷积篇|大感受野的小波卷积
  • 【Pip】深入理解 `requirements.txt` 文件:Python 项目依赖管理的核心工具
  • electron-vite_11各平台 Electron 镜像存到哪里了?
  • Lua字符串
  • ARM嵌入式学习--第五天
  • 倪师学习笔记-天纪-斗数星辰介绍
  • 体验先行者平台的一键生成ai神器
  • 论软件著作权
  • 数据库错误 SQLSTATE[HY000] [2002]
  • [MySQL#1] database概述 | 常见的操作指令 | MySQL架构 | 存储引擎
  • 【C#】使用vue3的axios发起get和post请求.net framework部署的API显示跨域
  • mongodb在linux下的部署
  • pyspark==堆叠
  • 【英特尔IA-32架构软件开发者开发手册第3卷:系统编程指南】2001年版翻译,2-3
  • 洛谷 P1186 玛丽卡(最短路,并查集,线段树)
  • 【LeetCode】修炼之路-0006-Zigzag Conversion (Z 字形变换)【python】