56 lines
1.5 KiB
C
56 lines
1.5 KiB
C
#pragma once
|
|
|
|
#include <rt/atomic.h>
|
|
#include <rt/list.h>
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
struct rt_mutex;
|
|
|
|
void rt_mutex_init(struct rt_mutex *mutex);
|
|
|
|
void rt_mutex_lock(struct rt_mutex *mutex);
|
|
|
|
void rt_mutex_unlock(struct rt_mutex *mutex);
|
|
|
|
bool rt_mutex_trylock(struct rt_mutex *mutex);
|
|
|
|
bool rt_mutex_timedlock(struct rt_mutex *mutex, unsigned long ticks);
|
|
|
|
struct rt_mutex
|
|
{
|
|
rt_atomic_uintptr_t holder;
|
|
struct rt_list wait_list;
|
|
struct rt_list list;
|
|
int level;
|
|
};
|
|
|
|
struct rt_task;
|
|
|
|
#define RT_MUTEX_UNLOCKED ((uintptr_t)0)
|
|
#define RT_MUTEX_WAITED_MASK ((uintptr_t)1)
|
|
#define RT_MUTEX_HOLDER_MASK (~RT_MUTEX_WAITED_MASK)
|
|
#define RT_MUTEX_HOLDER_INTERRUPT (UINTPTR_MAX & RT_MUTEX_HOLDER_MASK)
|
|
|
|
#define RT_MUTEX_INIT_COMMON(name, level_) \
|
|
{ \
|
|
.holder = RT_MUTEX_UNLOCKED, \
|
|
.wait_list = RT_LIST_INIT(name.wait_list), \
|
|
.list = RT_LIST_INIT(name.list), .level = (level_), \
|
|
}
|
|
|
|
#define RT_MUTEX_INIT(name) RT_MUTEX_INIT_COMMON(name, -1)
|
|
|
|
#define RT_MUTEX_INIT_RECURSIVE(name) RT_MUTEX_INIT_COMMON(name, 0)
|
|
|
|
#define RT_MUTEX(name) struct rt_mutex name = RT_MUTEX_INIT(name)
|
|
|
|
#define RT_MUTEX_RECURSIVE(name) \
|
|
struct rt_mutex name = RT_MUTEX_INIT_RECURSIVE(name)
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|