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