10a7ce71fSopenharmony_ci/*
20a7ce71fSopenharmony_ci * Copyright (C) 2022 HiHope Open Source Organization .
30a7ce71fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
40a7ce71fSopenharmony_ci * you may not use this file except in compliance with the License.
50a7ce71fSopenharmony_ci * You may obtain a copy of the License at
60a7ce71fSopenharmony_ci *
70a7ce71fSopenharmony_ci *     http:// www.apache.org/licenses/LICENSE-2.0
80a7ce71fSopenharmony_ci *
90a7ce71fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
100a7ce71fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
110a7ce71fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
120a7ce71fSopenharmony_ci * See the License for the specific language governing permissions and
130a7ce71fSopenharmony_ci *
140a7ce71fSopenharmony_ci * limitations under the License.
150a7ce71fSopenharmony_ci */
160a7ce71fSopenharmony_ci
170a7ce71fSopenharmony_ci#include <stdio.h>
180a7ce71fSopenharmony_ci#include <unistd.h>
190a7ce71fSopenharmony_ci
200a7ce71fSopenharmony_ci#include "ohos_init.h"
210a7ce71fSopenharmony_ci#include "cmsis_os2.h"
220a7ce71fSopenharmony_ci
230a7ce71fSopenharmony_ci#define ATTR.STACK_SIZE 1024
240a7ce71fSopenharmony_ci#define OS_DELAY 5
250a7ce71fSopenharmony_ci#define OS_DELAY_F 13
260a7ce71fSopenharmony_ci#define OS_DELAY_S 17
270a7ce71fSopenharmony_ci#define NUM 100
280a7ce71fSopenharmony_ci#define NUMS 2
290a7ce71fSopenharmony_ci
300a7ce71fSopenharmony_cistatic int g_test_value = 0;
310a7ce71fSopenharmony_ci
320a7ce71fSopenharmony_civoid number_thread(int *arg)
330a7ce71fSopenharmony_ci{
340a7ce71fSopenharmony_ci    osMutexId_t *mid = (osMutexId_t *)arg;
350a7ce71fSopenharmony_ci    while (1) {
360a7ce71fSopenharmony_ci        if (osMutexAcquire(*mid, NUM) == osOK) {
370a7ce71fSopenharmony_ci            g_test_value++;
380a7ce71fSopenharmony_ci            if (g_test_value % NUMS == 0) {
390a7ce71fSopenharmony_ci                printf("[Mutex Test]%s gets an even value %d.\r\n", osThreadGetName(osThreadGetId()), g_test_value);
400a7ce71fSopenharmony_ci            } else {
410a7ce71fSopenharmony_ci                printf("[Mutex Test]%s gets an odd value %d.\r\n",  osThreadGetName(osThreadGetId()), g_test_value);
420a7ce71fSopenharmony_ci            }
430a7ce71fSopenharmony_ci            osMutexRelease(*mid);
440a7ce71fSopenharmony_ci            osDelay(OS_DELAY);
450a7ce71fSopenharmony_ci        }
460a7ce71fSopenharmony_ci    }
470a7ce71fSopenharmony_ci}
480a7ce71fSopenharmony_ci
490a7ce71fSopenharmony_ciosThreadId_t newThread(char *name, osThreadFunc_t func, int *arg)
500a7ce71fSopenharmony_ci{
510a7ce71fSopenharmony_ci    osThreadAttr_t attr = {
520a7ce71fSopenharmony_ci        name, 0, NULL, 0, NULL, 1024*2, osPriorityNormal, 0, 0
530a7ce71fSopenharmony_ci    };
540a7ce71fSopenharmony_ci    osThreadId_t tid = osThreadNew(func, arg, &attr);
550a7ce71fSopenharmony_ci    if (tid == NULL) {
560a7ce71fSopenharmony_ci        printf("[Mutex Test]osThreadNew(%s) failed.\r\n", name);
570a7ce71fSopenharmony_ci    } else {
580a7ce71fSopenharmony_ci        printf("[Mutex Test]osThreadNew(%s) success, thread id: %d.\r\n", name, tid);
590a7ce71fSopenharmony_ci    }
600a7ce71fSopenharmony_ci    return tid;
610a7ce71fSopenharmony_ci}
620a7ce71fSopenharmony_ci
630a7ce71fSopenharmony_civoid rtosv2_mutex_main(int *arg)
640a7ce71fSopenharmony_ci{
650a7ce71fSopenharmony_ci    (int)arg;
660a7ce71fSopenharmony_ci    osMutexAttr_t attr = {0};
670a7ce71fSopenharmony_ci
680a7ce71fSopenharmony_ci    osMutexId_t mid = osMutexNew(&attr);
690a7ce71fSopenharmony_ci    if (mid == NULL) {
700a7ce71fSopenharmony_ci        printf("[Mutex Test]osMutexNew, create mutex failed.\r\n");
710a7ce71fSopenharmony_ci    } else {
720a7ce71fSopenharmony_ci        printf("[Mutex Test]osMutexNew, create mutex success.\r\n");
730a7ce71fSopenharmony_ci    }
740a7ce71fSopenharmony_ci
750a7ce71fSopenharmony_ci    osThreadId_t tid1 = newThread("Thread_1", number_thread, &mid);
760a7ce71fSopenharmony_ci    osThreadId_t tid2 = newThread("Thread_2", number_thread, &mid);
770a7ce71fSopenharmony_ci    osThreadId_t tid3 = newThread("Thread_3", number_thread, &mid);
780a7ce71fSopenharmony_ci
790a7ce71fSopenharmony_ci    osDelay(OS_DELAY_F);
800a7ce71fSopenharmony_ci    osThreadId_t tid = osMutexGetOwner(mid);
810a7ce71fSopenharmony_ci    printf("[Mutex Test]osMutexGetOwner, thread id: %p, thread name: %s.\r\n", tid, osThreadGetName(tid));
820a7ce71fSopenharmony_ci    osDelay(OS_DELAY_S);
830a7ce71fSopenharmony_ci
840a7ce71fSopenharmony_ci    osThreadTerminate(tid1);
850a7ce71fSopenharmony_ci    osThreadTerminate(tid2);
860a7ce71fSopenharmony_ci    osThreadTerminate(tid3);
870a7ce71fSopenharmony_ci    osMutexDelete(mid);
880a7ce71fSopenharmony_ci}
890a7ce71fSopenharmony_ci
900a7ce71fSopenharmony_cistatic void MutexTestTask(void)
910a7ce71fSopenharmony_ci{
920a7ce71fSopenharmony_ci    osThreadAttr_t attr;
930a7ce71fSopenharmony_ci
940a7ce71fSopenharmony_ci    attr.name = "rtosv2_mutex_main";
950a7ce71fSopenharmony_ci    attr.attr_bits = 0U;
960a7ce71fSopenharmony_ci    attr.cb_mem = NULL;
970a7ce71fSopenharmony_ci    attr.cb_size = 0U;
980a7ce71fSopenharmony_ci    attr.stack_mem = NULL;
990a7ce71fSopenharmony_ci    attr.stack_size = ATTR.STACK_SIZE;
1000a7ce71fSopenharmony_ci    attr.priority = osPriorityNormal;
1010a7ce71fSopenharmony_ci
1020a7ce71fSopenharmony_ci    if (osThreadNew((osThreadFunc_t)rtosv2_mutex_main, NULL, &attr) == NULL) {
1030a7ce71fSopenharmony_ci        printf("[MutexTestTask] Failed to create rtosv2_mutex_main!\n");
1040a7ce71fSopenharmony_ci    }
1050a7ce71fSopenharmony_ci}
1060a7ce71fSopenharmony_ci
1070a7ce71fSopenharmony_ciAPP_FEATURE_INIT(MutexTestTask);