HarmonyOS小熊派 | HarmonyOS內核編程開發—信號量

      網友投稿 741 2022-05-30

      BearPi-HM_Nano開發板HarmonyOS內核編程開發——信號量

      本示例將演示如何在BearPi-HM_Nano開發板上使用cmsis 2.0 接口通過信號量同時從不同的線程訪問共享資源

      Semaphore API分析

      osSemaphoreNew()

      osSemaphoreId_t osSemaphoreNew(uint32_t max_count,uint32_t initial_count,const osSemaphoreAttr_t *attr)

      描述:

      函數osMessageQueueNew創建并初始化一個消息隊列對象。該函數返回消息隊列對象標識符,如果出現錯誤則返回NULL,可以在RTOS啟動(調用 osKernelStart)之前安全地調用該函數,也可以在內核初始化 (調用 osKernelInitialize)之前調用該函數。

      注意?:不能在中斷服務調用該函數

      參數:

      osSemaphoreRelease()

      osStatus_t osSemaphoreRelease(osSemaphoreId_t semaphore_id)

      描述:?函數osSemaphoreRelease釋放由參數semaphore_id指定的信號量對象的標記

      注意?:該函數可以在中斷服務例程調用

      參數:

      osSemaphoreAcquire()

      osStatus_t osSemaphoreAcquire(osSemaphoreId_t semaphore_id,uint32_t timeout)

      描述:?阻塞函數osSemaphoreAcquire一直等待,直到由參數semaphore_id指定的信號量對象的標記可用為止。如果一個令牌可用,該函數立即返回并遞減令牌計數。

      注意?:如果參數timeout設置為0,可以從中斷服務例程調用。

      參數:

      軟件設計

      主要代碼分析

      在Semaphore_example函數中,通過osSemaphoreNew()函數創建了sem1信號量,Thread_Semaphore1()函數中通過osSemaphoreRelease()函數釋放兩個信號量,Thread_Semaphore2()和Thread_Semaphore3()函數中,先開始阻塞等待sem1信號量。只有當Thread_Semaphore1()函數中增加兩次信號量,Thread_Semaphore2()和Thread_Semaphore3()才能繼續同步運行。若Thread_Semaphore1()函數中只增加一次信號量,那Thread_Semaphore2()和Thread_Semaphore3()只能輪流執行。

      semaphore_yuchuan_example.c

      #include #include "ohos_init.h" #include "cmsis_os2.h" #include "iot_gpio.h" #include "iot_gpio_ex.h" #define GPIO_LED 2 osSemaphoreId_t semaphoreId; void yuchuanThreadSemaphore(void) { osStatus_t status; while (1) { //釋放兩次信號量,使huayingThread和meiliThread兩個任務同步 status = osSemaphoreRelease(semaphoreId); //osSemaphoreRelease(semaphoreId); if (status != osOK) { /* code */ printf("YuchuanThread Semaphore Release Falied!!!\n"); } else { printf("YuchuanThread Semaphore Release Success!!!\n"); } osDelay(100); } } void huayingThreadSemaphore(void) { osStatus_t status; while (1) { //阻塞信號量 //osSemaphoreAcquire(semaphoreId,osWaitForever); status = osSemaphoreAcquire(semaphoreId,50U); if (status != osOK) { /* code */ printf("HuayingThread get Semaphore Falied!!!\n"); } else { printf("HuayingThread get Semaphore Success!!!\n"); //設置GPIO輸出高電平點亮LED燈 IoTGpioSetOutputVal(GPIO_LED,IOT_GPIO_VALUE1); //osSemaphoreDelete(semaphoreId); } //osDelay(1); } } void meiLiThreadSeamphore(void) { osStatus_t status; while (1) { //阻塞信號量 status = osSemaphoreAcquire(semaphoreId,osWaitForever); if (status != osOK) { /* code */ printf("MeiliThread get Semaphore Falied!!!\n"); } else { printf("MeiliThread get Semaphore Success!!!\n"); //設置GPIO輸出低電平熄滅LED燈 IoTGpioSetOutputVal(GPIO_LED,IOT_GPIO_VALUE0); } osDelay(500); } } static void yuchuanSemaphoreExample(void) { //初始化GPIO IoTGpioInit(GPIO_LED); //設置GPIO為輸出方向 IoTGpioSetDir(GPIO_LED,IOT_GPIO_DIR_OUT); osThreadAttr_t attr; attr.attr_bits = 0U; attr.cb_mem = NULL; attr.cb_size = 0U; attr.stack_mem = NULL; attr.stack_size = 1024 * 4; attr.priority = 25; attr.name = "YuchuanThread"; if (osThreadNew((osThreadFunc_t)yuchuanThreadSemaphore,NULL,&attr) == NULL) { /* code */ printf("Falied to Create YuchuanThread!!!\n"); } attr.name = "HuayingThread"; if (osThreadNew((osThreadFunc_t)huayingThreadSemaphore,NULL,&attr) == NULL) { /* code */ printf("Falied to Create HuayingThread!!!\n"); } attr.name = "MeiliThread"; if (osThreadNew((osThreadFunc_t)meiLiThreadSeamphore,NULL,&attr) == NULL) { /* code */ printf("Falied to Create MeiliThread!!!\n"); } semaphoreId = osSemaphoreNew(4,0,NULL); if (semaphoreId == NULL) { /* code */ printf("Falied to Create Semaphore!!!\n"); } } APP_FEATURE_INIT(yuchuanSemaphoreExample);

      編譯調試

      applications/sample/BearPi/BearPi-HM_Nano/sample/A5_YuchuanSemaphore/BUILD.gn

      HarmonyOS小熊派 | HarmonyOS內核編程開發—信號量

      static_library("yuchuanSemaphore"){ sources = [ "semaphore_yuchuan_example.c", ] include_dirs = [ "http://base/iot_hardware/peripheral/interfaces/kits", ] }

      修改 BUILD.gn 文件

      修改?applications\BearPi\BearPi-HM_Nano\sample路徑下 BUILD.gn 文件,指定?semp_example?參與編譯。

      # Copyright (c) 2020 Nanjing Xiaoxiongpai Intelligent Technology 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("http://build/lite/config/component/lite_component.gni") lite_component("sample") { features = [ #"A1_kernal_thread:thread_example", #"A2_kernel_timer:timer_example", #"A3_kernel_event:event_example", #"A4_kernel_mutex:mutex_example", #"A5_kernel_semaphore:semaphore_example", #"A6_kernel_message:message_example", #"B1_basic_led_blink:led_example", #"B2_basic_button:button_example", #"B3_basic_pwm_led:pwm_example", #"B4_basic_adc:adc_example", #"B5_basic_i2c_nfc:i2c_example", #"B6_basic_uart:uart_example", #"C1_e53_sf1_mq2:e53_sf1_example", #"C2_e53_ia1_temp_humi_pls:e53_ia1_example", #"C3_e53_sc1_pls:e53_sc1_example", #"C4_e53_sc2_axis:e53_sc2_example", #"C5_e53_is1_infrared:e53_is1_example", #"D1_iot_wifi_ap:wifi_ap", #"D2_iot_wifi_sta_connect:wifi_sta_connect", #"D3_iot_udp_client:udp_client", #"D4_iot_tcp_server:tcp_server", #"D5_iot_mqtt:iot_mqtt", #"D6_iot_cloud_oc:oc_mqtt", #"D7_iot_cloud_onenet:onenet_mqtt", #"D8_iot_cloud_oc_smoke:cloud_oc_smoke", #"D9_iot_cloud_oc_light:cloud_oc_light", #"D10_iot_cloud_oc_manhole_cover:cloud_oc_manhole_cover", #"D11_iot_cloud_oc_infrared:cloud_oc_infrared", #"D12_iot_cloud_oc_agriculture:cloud_oc_agriculture", #"D13_iot_cloud_oc_gps:cloud_oc_gps", #"A1_YuchuanThread:yuchuanThread", #"A2_YuchuanTimer:yuchuanTimer", #"A4_YuchuanMutex:yuchuanMutex", "A5_YuchuanSemaphore:yuchuanSemaphore", ] }

      運行結果

      示例代碼編譯燒錄代碼后,按下開發板的RESET按鍵,通過串口助手查看日志,Thread_Semaphore1一次釋放兩個信號量,Thread_Semaphore2和Thread_Semaphore3同步執行。

      hiview init success.YuchuanThread Semaphore Release Success!!! HuayingThread get Semaphore Success!!! 00 00:00:00 0 132 D 0/HIVIEW: log limit init success. 00 00:00:00 0 132 I 1/SAMGR: Bootstrap core services(count:3). 00 00:00:00 0 132 I 1/SAMGR: Init service:0x4b0368 TaskPool:0xe4b3c 00 00:00:00 0 132 I 1/SAMGR: Init service:0x4b0374 TaskPool:0xe4b5c 00 00:00:00 0 132 I 1/SAMGR: Init service:0x4b0840 TaskPool:0xe4b7c 00 00:00:00 0 164 I 1/SAMGR: Init service 0x4b0374 success! 00 00:00:00 0 64 I 1/SAMGR: Init service 0x4b0368 success! 00 00:00:00 0 8 I 1/SAMGR: Init service 0x4b0840 success! 00 00:00:00 0 8 I 1/SAMGR: Initialized all core system services! 00 00:00:00 0 64 I 1/SAMGR: Bootstrap system and application services(count:0). 00 00:00:00 0 64 I 1/SAMGR: Initialized all system and application services! 00 00:00:00 0 64 I 1/SAMGR: Bootstrap dynamic registered services(count:0). HuayingThread get Semaphore Falied!!! YuchuanThread Semaphore Release Success!!! HuayingThread get Semaphore Falied!!! MeiliThread get Semaphore Success!!! HuayingThread get Semaphore Falied!!! YuchuanThread Semaphore Release Success!!! HuayingThread get Semaphore Falied!!! HuayingThread get Semaphore Success!!! HuayingThread get Semaphore Falied!!! YuchuanThread Semaphore Release Success!!! HuayingThread get Semaphore Falied!!! HuayingThread get Semaphore Success!!! HuayingThread get Semaphore Falied!!!

      硬件開發

      版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。

      上一篇:ROS機器人操作系統資料與資訊(2018年7月)
      下一篇:架構師之路 — 軟件架構 — Overview
      相關文章
      久久亚洲精精品中文字幕| 亚洲av无码不卡私人影院| 自拍偷自拍亚洲精品情侣| 亚洲av中文无码| 久久久久亚洲精品无码网址色欲 | 国产亚洲高清不卡在线观看| 亚洲av无码成人精品区在线播放| WWW亚洲色大成网络.COM| 日本系列1页亚洲系列| 国产精品亚洲专区在线播放 | 亚洲国产精品免费观看| 中文字幕在线观看亚洲日韩| 亚洲色欲色欲www在线播放| 亚洲日本VA午夜在线影院| 亚洲人成无码网站在线观看| 亚洲欧美国产欧美色欲| 欧洲亚洲国产精华液| 伊在人亚洲香蕉精品区麻豆| vvvv99日韩精品亚洲| 亚洲午夜福利精品无码| 伊人久久大香线蕉亚洲| 久久久久久久久亚洲| 亚洲日韩国产精品无码av| 亚洲av无码片在线观看| 中文字幕无码精品亚洲资源网久久 | 亚洲午夜一区二区电影院| 国产亚洲玖玖玖在线观看| 亚洲熟妇av午夜无码不卡| 久久久久亚洲AV无码去区首| 亚洲国产高清精品线久久| 伊人久久大香线蕉亚洲五月天| 亚洲动漫精品无码av天堂| 伊人久久综在合线亚洲2019| 亚洲另类自拍丝袜第1页| 亚洲色偷偷色噜噜狠狠99网| 大桥未久亚洲无av码在线| 中文字幕在线亚洲精品| 亚洲av无码成人黄网站在线观看 | 校园亚洲春色另类小说合集| 亚洲中文无韩国r级电影| 久久亚洲精品中文字幕三区|