• 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 static int g_test_value = 0;
22 
number_thread(void * arg)23 void number_thread(void *arg)
24 {
25     osMutexId_t *mid = (osMutexId_t *)arg;
26     const uint32_t DelayTimeMs = 100;
27     const uint32_t remainder = 2;
28     const uint32_t DelayTimeMs2 = 5;
29 
30     while (1) {
31         if (osMutexAcquire(*mid, DelayTimeMs) == osOK) {
32             g_test_value++;
33             if (g_test_value % remainder == 0) {
34                 printf("[Mutex Test]%s gets an even value %d.\r\n", osThreadGetName(osThreadGetId()), g_test_value);
35             } else {
36                 printf("[Mutex Test]%s gets an odd value %d.\r\n",  osThreadGetName(osThreadGetId()), g_test_value);
37             }
38             osMutexRelease(*mid);
39             osDelay(DelayTimeMs2);
40         }
41     }
42 }
43 
newThread(char * name,osThreadFunc_t func,void * arg)44 osThreadId_t newThread(char *name, osThreadFunc_t func, void *arg)
45 {
46     osThreadAttr_t attr = {
47         name, 0, NULL, 0, NULL, 1024*2, osPriorityNormal, 0, 0
48     };
49     osThreadId_t tid = osThreadNew(func, arg, &attr);
50     if (tid == NULL) {
51         printf("[Mutex Test]osThreadNew(%s) failed.\r\n", name);
52     } else {
53         printf("[Mutex Test]osThreadNew(%s) success, thread id: %d.\r\n", name, tid);
54     }
55     return tid;
56 }
57 
rtosv2_mutex_main(void * arg)58 void rtosv2_mutex_main(void *arg)
59 {
60     (void)arg;
61     osMutexAttr_t attr = {0};
62     const uint32_t DelayTimeMs = 17;
63 
64     osMutexId_t mid = osMutexNew(&attr);
65     if (mid == NULL) {
66         printf("[Mutex Test]osMutexNew, create mutex failed.\r\n");
67     } else {
68         printf("[Mutex Test]osMutexNew, create mutex success.\r\n");
69     }
70 
71     osThreadId_t tid1 = newThread("Thread_1", number_thread, &mid);
72     osThreadId_t tid2 = newThread("Thread_2", number_thread, &mid);
73     osThreadId_t tid3 = newThread("Thread_3", number_thread, &mid);
74 
75     osDelay(DelayTimeMs);
76     osThreadId_t tid = osMutexGetOwner(mid);
77     printf("[Mutex Test]osMutexGetOwner, thread id: %p, thread name: %s.\r\n", tid, osThreadGetName(tid));
78     osDelay(DelayTimeMs);
79 
80     osThreadTerminate(tid1);
81     osThreadTerminate(tid2);
82     osThreadTerminate(tid3);
83     osMutexDelete(mid);
84 }
85 
MutexTestTask(void)86 static void MutexTestTask(void)
87 {
88     osThreadAttr_t attr;
89 
90     attr.name = "rtosv2_mutex_main";
91     attr.attr_bits = 0U;
92     attr.cb_mem = NULL;
93     attr.cb_size = 0U;
94     attr.stack_mem = NULL;
95     attr.stack_size = 0x1000;
96     attr.priority = osPriorityNormal;
97 
98     if (osThreadNew((osThreadFunc_t)rtosv2_mutex_main, NULL, &attr) == NULL) {
99         printf("[MutexTestTask] Falied to create rtosv2_mutex_main!\n");
100     }
101 }
102 
103 APP_FEATURE_INIT(MutexTestTask);