1 /*
2 * Copyright (c) 2022 Talkweb 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 "ohos_run.h"
21 #include "cmsis_os2.h"
22
23 osSemaphoreId_t sem1;
24
Thread_Semp1(void)25 void Thread_Semp1(void)
26 {
27 while (1) {
28 osSemaphoreRelease(sem1);
29 printf("Thread_Semp1 Release Semap \n");
30 osDelay(200U);
31 }
32 }
33
Thread_Semp2(void)34 void Thread_Semp2(void)
35 {
36 while (1) {
37 osSemaphoreAcquire(sem1, osWaitForever);
38 printf("Thread_Semp2 get Semap \n");
39 osDelay(100U);
40 }
41 }
42
semp_example(void)43 void semp_example(void)
44 {
45 osThreadAttr_t attr;
46
47 attr.attr_bits = 0U;
48 attr.cb_mem = NULL;
49 attr.cb_size = 0U;
50 attr.stack_mem = NULL;
51 attr.stack_size = 1024 * 4;
52 attr.priority = 10;
53
54 attr.name = "Thread_Semp1";
55 if (osThreadNew((osThreadFunc_t)Thread_Semp1, NULL, &attr) == NULL) {
56 printf("create Thread_Semp1 failed!\n");
57 }
58
59 attr.name = "Thread_Semp2";
60 if (osThreadNew((osThreadFunc_t)Thread_Semp2, NULL, &attr) == NULL) {
61 printf("create Thread_Semp2 failed!\n");
62 }
63
64 sem1 = osSemaphoreNew(3, 0, NULL);
65 if (sem1 == NULL) {
66 printf("Semp1 create failed!\n");
67 }
68 }
69
70 OHOS_APP_RUN(semp_example);