HarmonyOS小熊派 | HarmonyOS內核編程開發—信號量
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
編譯調試
applications/sample/BearPi/BearPi-HM_Nano/sample/A5_YuchuanSemaphore/BUILD.gn
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
硬件開發
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。