• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 HiHope Open Source Organization .
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  *
14  * limitations under the License.
15  */
16 
17 #include <stdio.h>
18 #include <unistd.h>
19 
20 #include "ohos_init.h"
21 #include "cmsis_os2.h"
22 
23 #define OS_DELAY 4
24 #define OS_DELAY_F 50
25 #define OS_DELAY_S 3
26 #define NUM 1
27 #define ATTR.STACK_SIZE 1024
28 #define BUFFER_SIZE 5U
29 static int product_number = 0;
30 osSemaphoreId_t empty_id;
31 osSemaphoreId_t filled_id;
32 
producer_thread(int * arg)33 void producer_thread(int *arg)
34 {
35     (int)arg;
36     empty_id = osSemaphoreNew(BUFFER_SIZE, BUFFER_SIZE, NULL);
37     filled_id = osSemaphoreNew(BUFFER_SIZE, 0U, NULL);
38     while (1) {
39         osSemaphoreAcquire(empty_id, osWaitForever);
40         product_number++;
41         printf("[Semp Test]%s produces a product, now product number: %d.\r\n",
42             osThreadGetName(osThreadGetId()), product_number);
43         osDelay(OS_DELAY);
44         osSemaphoreRelease(filled_id);
45     }
46 }
47 
consumer_thread(int * arg)48 void consumer_thread(int *arg)
49 {
50     (int)arg;
51     while (NUM) {
52         osSemaphoreAcquire(filled_id, osWaitForever);
53         product_number--;
54         printf("[Semp Test]%s consumes a product, now product number: %d.\r\n",
55             osThreadGetName(osThreadGetId()), product_number);
56         osDelay(OS_DELAY_S);
57         osSemaphoreRelease(empty_id);
58     }
59 }
60 
newThread(char * name,osThreadFunc_t func,int * arg)61 osThreadId_t newThread(char *name, osThreadFunc_t func, int *arg)
62 {
63     osThreadAttr_t attr = {
64         name, 0, NULL, 0, NULL, 1024*2, osPriorityNormal, 0, 0
65     };
66     osThreadId_t tid = osThreadNew(func, arg, &attr);
67     if (tid == NULL) {
68         printf("[Semp Test]osThreadNew(%s) failed.\r\n", name);
69     } else {
70         printf("[Semp Test]osThreadNew(%s) success, thread id: %d.\r\n", name, tid);
71     }
72     return tid;
73 }
74 
rtosv2_semp_main(int * arg)75 void rtosv2_semp_main(int *arg)
76 {
77     (int)arg;
78     empty_id = osSemaphoreNew(BUFFER_SIZE, BUFFER_SIZE, NULL);
79     filled_id = osSemaphoreNew(BUFFER_SIZE, 0U, NULL);
80 
81     osThreadId_t ptid1 = newThread("producer1", producer_thread, NULL);
82     osThreadId_t ptid2 = newThread("producer2", producer_thread, NULL);
83     osThreadId_t ptid3 = newThread("producer3", producer_thread, NULL);
84     osThreadId_t ctid1 = newThread("consumer1", consumer_thread, NULL);
85     osThreadId_t ctid2 = newThread("consumer2", consumer_thread, NULL);
86 
87     osDelay(OS_DELAY_F);
88 
89     osThreadTerminate(ptid1);
90     osThreadTerminate(ptid2);
91     osThreadTerminate(ptid3);
92     osThreadTerminate(ctid1);
93     osThreadTerminate(ctid2);
94 
95     osSemaphoreDelete(empty_id);
96     osSemaphoreDelete(filled_id);
97 }
98 
SempTestTask(void)99 static void SempTestTask(void)
100 {
101     osThreadAttr_t attr;
102 
103     attr.name = "rtosv2_semp_main";
104     attr.attr_bits = 0U;
105     attr.cb_mem = NULL;
106     attr.cb_size = 0U;
107     attr.stack_mem = NULL;
108     attr.stack_size = ATTR.STACK_SIZE;
109     attr.priority = osPriorityNormal;
110 
111     if (osThreadNew((osThreadFunc_t)rtosv2_semp_main, NULL, &attr) == NULL) {
112         printf("[SempTestTask] Failed to create rtosv2_semp_main!\n");
113     }
114 }
115 
116 APP_FEATURE_INIT(SempTestTask);