#pragma once #include #include #ifdef __cplusplus extern "C" { #endif struct rt_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; /* The mutex recursion level, indicating the number of times the mutex has * been locked beyond the first, e.g., 0 is locked once, 1 is locked twice, * etc. A level of -1 indicates that recursive locking is disallowed. */ 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_LEVEL_NONRECURSIVE (-1) #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, RT_MUTEX_LEVEL_NONRECURSIVE) #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