summaryrefslogtreecommitdiff
path: root/bsp/allwinner/libraries/sunxi-hal/osal/src/hal_mutex.c
blob: 403382d9deaa65551d9410c7d9dcdbd563f57a72 (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
#include <stdlib.h>
#include <hal_mutex.h>

#define PEND_TICK_MAX (0x7FFFFFFF - 1)
#define HAL_MUTEX_OK 0

hal_mutex_t hal_mutex_create(void)
{
    return rt_mutex_create("hal_mutex", RT_IPC_FLAG_FIFO);
}

int hal_mutex_delete(hal_mutex_t mutex)
{
    if (!mutex)
    {
        return -1;
    }
    return rt_mutex_delete(mutex);
}

int hal_mutex_lock(hal_mutex_t mutex)
{
    if (!mutex)
    {
        return -1;
    }
    return rt_mutex_take(mutex, PEND_TICK_MAX);
}

int hal_mutex_unlock(hal_mutex_t mutex)
{
    if (!mutex)
    {
        return -1;
    }
    return rt_mutex_release(mutex);
}

int hal_mutex_trylock(hal_mutex_t mutex)
{
    if (!mutex)
    {
        return -1;
    }
    if (rt_mutex_take(mutex, 0) == 0)
    {
        return 0;
    }
    return -2;
}

int hal_mutex_timedwait(hal_mutex_t mutex, int ticks)
{
    if (!mutex)
    {
        return -1;
    }
    return rt_mutex_take(mutex, ticks);
}