summaryrefslogtreecommitdiff
path: root/components/cplusplus/cpp11/armclang/mutex.cpp
blob: 44a70efcf7eab1892114480cf7986eec59e73e7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-04-27     flybreak     the first version.
 */

#include <arm-tpl.h>
#include <cstdint>
#include <stdatomic.h>
#include "tpl.h"

static int check_create(volatile __ARM_TPL_mutex_t *__vm, bool recursive = false)
{
    if (__vm->data == 0)
    {
        uintptr_t mut_null = 0;
        arm_tpl_mutex_struct *mutex_p = (arm_tpl_mutex_struct *)rt_malloc(sizeof(arm_tpl_mutex_struct));
        if (mutex_p == nullptr) return -1;

        if (recursive)
            mutex_p->mutex = rt_mutex_create("mutexx", RT_IPC_FLAG_PRIO);
        else
            mutex_p->mutex = rt_mutex_create("mutexx", RT_IPC_FLAG_PRIO);

        if (mutex_p->mutex == nullptr)
        {
            rt_free(mutex_p);
            return -1;
        }
        mutex_p->type = recursive ? RECURSIVE : NORMAL;
        uintptr_t mut_new = reinterpret_cast<uintptr_t>(mutex_p);
        if (!atomic_compare_exchange_strong(&__vm->data, &mut_null, mut_new))
        {
            rt_mutex_delete(mutex_p->mutex);
            rt_free(mutex_p);
        }
    }
    return 0;
}

static int mutexLock(arm_tpl_mutex_struct *mutex_p, rt_tick_t timeOut)
{
    if (mutex_p->type == RECURSIVE)
    {
        if (rt_mutex_take(mutex_p->mutex, timeOut) == 0)
            return 0;
    }
    else
    {
        if (rt_mutex_take(mutex_p->mutex, timeOut) == 0)
            return 0;
    }
    return -1;
}

static int mutexUnlock(arm_tpl_mutex_struct *mutex_p)
{
    if (mutex_p->type == RECURSIVE)
        rt_mutex_release(mutex_p->mutex);
    else
        rt_mutex_release(mutex_p->mutex);
    return 0;
}

extern "C" int __ARM_TPL_recursive_mutex_init(__ARM_TPL_mutex_t *__m)
{
    volatile __ARM_TPL_mutex_t *__vm = __m;
    return check_create(__vm, true);
}

extern "C" int __ARM_TPL_mutex_lock(__ARM_TPL_mutex_t *__m)
{
    volatile __ARM_TPL_mutex_t *__vm = __m;
    if (check_create(__vm))
        return -1;
    while (mutexLock((arm_tpl_mutex_struct *)(__vm->data), ARM_TPL_MAX_DELAY) != 0);
    return 0;
}

extern "C" int __ARM_TPL_mutex_trylock(__ARM_TPL_mutex_t *__m)
{
    volatile __ARM_TPL_mutex_t *__vm = __m;
    if (check_create(__vm))
        return -1;
    return mutexLock((arm_tpl_mutex_struct *)(__vm->data), 0);
}

extern "C" int __ARM_TPL_mutex_unlock(__ARM_TPL_mutex_t *__m)
{
    volatile __ARM_TPL_mutex_t *__vm = __m;
    return mutexUnlock((arm_tpl_mutex_struct *)(__vm->data));
}

extern "C" int __ARM_TPL_mutex_destroy(__ARM_TPL_mutex_t *__m)
{
    volatile __ARM_TPL_mutex_t *__vm = __m;
    if (__vm->data != 0)
    {
        rt_mutex_delete(((arm_tpl_mutex_struct *)(__vm->data))->mutex);
        rt_free((void *)(__vm->data));
        __vm->data = 0;
    }
    return 0;
}