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