• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020 Nanjing Xiaoxiongpai Intelligent Technology Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *    http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <stdio.h>
17 #include <string.h>
18 #include <unistd.h>
19 
20 #include "cmsis_os2.h"
21 #include "ohos_init.h"
22 
23 osSemaphoreId_t g_semaphoreId;
24 
25 #define THREAD_STACK_SIZE (1024 * 4)
26 #define THREAD_PRIO 24
27 
28 #define THREAD_DELAY_1S 100
29 #define THREAD_DELAY_10MS 1
30 
31 #define SEM_MAX_COUNT 4
32 
Semaphore1Thread(void)33 void Semaphore1Thread(void)
34 {
35     while (1) {
36         // release Semaphores twice so that Semaphore2Thread and Semaphore3Thread can execute synchronously
37         osSemaphoreRelease(g_semaphoreId);
38 
39         // if the Semaphore is released only once, Semaphore2Thread and Semaphore3Thread will run alternately.
40         osSemaphoreRelease(g_semaphoreId);
41 
42         printf("Semaphore1Thread Release  Semap \n");
43         osDelay(THREAD_DELAY_1S);
44     }
45 }
Semaphore2Thread(void)46 void Semaphore2Thread(void)
47 {
48     while (1) {
49         // wait Semaphore
50         osSemaphoreAcquire(g_semaphoreId, osWaitForever);
51 
52         printf("Semaphore2Thread get Semap \n");
53         osDelay(THREAD_DELAY_10MS);
54     }
55 }
56 
Semaphore3Thread(void)57 void Semaphore3Thread(void)
58 {
59     while (1) {
60         // wait Semaphore
61         osSemaphoreAcquire(g_semaphoreId, osWaitForever);
62 
63         printf("Semaphore3Thread get Semap \n");
64         osDelay(THREAD_DELAY_10MS);
65     }
66 }
67 
68 /**
69  * @brief Main Entry of the Semaphore Example
70  *
71  */
SemaphoreExample(void)72 void SemaphoreExample(void)
73 {
74     osThreadAttr_t attr;
75 
76     attr.attr_bits = 0U;
77     attr.cb_mem = NULL;
78     attr.cb_size = 0U;
79     attr.stack_mem = NULL;
80     attr.stack_size = THREAD_STACK_SIZE;
81     attr.priority = THREAD_PRIO;
82 
83     attr.name = "Semaphore1Thread";
84     if (osThreadNew((osThreadFunc_t)Semaphore1Thread, NULL, &attr) == NULL) {
85         printf("Failed to create Semaphore1Thread!\n");
86     }
87 
88     attr.name = "Semaphore2Thread";
89     if (osThreadNew((osThreadFunc_t)Semaphore2Thread, NULL, &attr) == NULL) {
90         printf("Failed to create Semaphore2Thread!\n");
91     }
92 
93     attr.name = "Semaphore3Thread";
94     if (osThreadNew((osThreadFunc_t)Semaphore3Thread, NULL, &attr) == NULL) {
95         printf("Failed to create Semaphore3Thread!\n");
96     }
97 
98     g_semaphoreId = osSemaphoreNew(SEM_MAX_COUNT, 0, NULL);
99     if (g_semaphoreId == NULL) {
100         printf("Failed to create Semaphore!\n");
101     }
102 }
103 APP_FEATURE_INIT(SemaphoreExample);
104