summaryrefslogtreecommitdiff
path: root/lib/bt/esp_ble_mesh/common/mutex.c
blob: 6c3b2bf6f72ae3b5aa545904d9ac6a7365ef5701 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
 * SPDX-FileCopyrightText: 2017-2021 Espressif Systems (Shanghai) CO LTD
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include "mesh/common.h"

static bt_mesh_mutex_t alarm_lock;
static bt_mesh_mutex_t list_lock;
static bt_mesh_mutex_t buf_lock;
static bt_mesh_mutex_t atomic_lock;

void bt_mesh_mutex_create(bt_mesh_mutex_t *mutex)
{
    if (!mutex) {
        BT_ERR("Create, invalid mutex");
        return;
    }

#if CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC
#if CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_EXTERNAL
    mutex->buffer = heap_caps_calloc_prefer(1, sizeof(StaticQueue_t), 2, MALLOC_CAP_SPIRAM|MALLOC_CAP_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
#elif CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_IRAM_8BIT
    mutex->buffer = heap_caps_calloc_prefer(1, sizeof(StaticQueue_t), 2, MALLOC_CAP_INTERNAL|MALLOC_CAP_IRAM_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
#endif
    __ASSERT(mutex->buffer, "Failed to create mutex buffer");
    mutex->mutex = xSemaphoreCreateMutexStatic(mutex->buffer);
    __ASSERT(mutex->mutex, "Failed to create static mutex");
#else /* CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC */
    mutex->mutex = xSemaphoreCreateMutex();
    __ASSERT(mutex->mutex, "Failed to create mutex");
#endif /* CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC */
}

void bt_mesh_mutex_free(bt_mesh_mutex_t *mutex)
{
    if (!mutex) {
        BT_ERR("Free, invalid mutex");
        return;
    }

    if (mutex->mutex) {
        vSemaphoreDelete(mutex->mutex);
        mutex->mutex = NULL;
#if CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC
        heap_caps_free(mutex->buffer);
        mutex->buffer = NULL;
#endif
    }
}

void bt_mesh_mutex_lock(bt_mesh_mutex_t *mutex)
{
    if (!mutex) {
        BT_ERR("Lock, invalid mutex");
        return;
    }

    if (mutex->mutex) {
        xSemaphoreTake(mutex->mutex, portMAX_DELAY);
    }
}

void bt_mesh_mutex_unlock(bt_mesh_mutex_t *mutex)
{
    if (!mutex) {
        BT_ERR("Unlock, invalid mutex");
        return;
    }

    if (mutex->mutex) {
        xSemaphoreGive(mutex->mutex);
    }
}

void bt_mesh_r_mutex_create(bt_mesh_mutex_t *mutex)
{
    if (!mutex) {
        BT_ERR("Create, invalid recursive mutex");
        return;
    }

#if CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC
#if CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_EXTERNAL
    mutex->buffer = heap_caps_calloc_prefer(1, sizeof(StaticQueue_t), 2, MALLOC_CAP_SPIRAM|MALLOC_CAP_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
#elif CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_IRAM_8BIT
    mutex->buffer = heap_caps_calloc_prefer(1, sizeof(StaticQueue_t), 2, MALLOC_CAP_INTERNAL|MALLOC_CAP_IRAM_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
#endif
    __ASSERT(mutex->buffer, "Failed to create recursive mutex buffer");
    mutex->mutex = xSemaphoreCreateRecursiveMutexStatic(mutex->buffer);
    __ASSERT(mutex->mutex, "Failed to create static recursive mutex");
#else /* CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC */
    mutex->mutex = xSemaphoreCreateRecursiveMutex();
    __ASSERT(mutex->mutex, "Failed to create recursive mutex");
#endif /* CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC */
}

void bt_mesh_r_mutex_free(bt_mesh_mutex_t *mutex)
{
    bt_mesh_mutex_free(mutex);
}

void bt_mesh_r_mutex_lock(bt_mesh_mutex_t *mutex)
{
    if (!mutex) {
        BT_ERR("Lock, invalid recursive mutex");
        return;
    }

    if (mutex->mutex) {
        xSemaphoreTakeRecursive(mutex->mutex, portMAX_DELAY);
    }
}

void bt_mesh_r_mutex_unlock(bt_mesh_mutex_t *mutex)
{
    if (!mutex) {
        BT_ERR("Unlock, invalid recursive mutex");
        return;
    }

    if (mutex->mutex) {
        xSemaphoreGiveRecursive(mutex->mutex);
    }
}

void bt_mesh_alarm_lock(void)
{
    bt_mesh_mutex_lock(&alarm_lock);
}

void bt_mesh_alarm_unlock(void)
{
    bt_mesh_mutex_unlock(&alarm_lock);
}

void bt_mesh_list_lock(void)
{
    bt_mesh_mutex_lock(&list_lock);
}

void bt_mesh_list_unlock(void)
{
    bt_mesh_mutex_unlock(&list_lock);
}

void bt_mesh_buf_lock(void)
{
    bt_mesh_mutex_lock(&buf_lock);
}

void bt_mesh_buf_unlock(void)
{
    bt_mesh_mutex_unlock(&buf_lock);
}

void bt_mesh_atomic_lock(void)
{
    bt_mesh_mutex_lock(&atomic_lock);
}

void bt_mesh_atomic_unlock(void)
{
    bt_mesh_mutex_unlock(&atomic_lock);
}

void bt_mesh_mutex_init(void)
{
    bt_mesh_mutex_create(&alarm_lock);
    bt_mesh_mutex_create(&list_lock);
    bt_mesh_mutex_create(&buf_lock);
    bt_mesh_mutex_create(&atomic_lock);
}

#if CONFIG_BLE_MESH_DEINIT
void bt_mesh_mutex_deinit(void)
{
    bt_mesh_mutex_free(&alarm_lock);
    bt_mesh_mutex_free(&list_lock);
    bt_mesh_mutex_free(&buf_lock);
    bt_mesh_mutex_free(&atomic_lock);
}
#endif /* CONFIG_BLE_MESH_DEINIT */