X-Git-Url: https://scm.lunaixsky.com/lunaix-os.git/blobdiff_plain/8fce4520de1f257819b16f9253fa28dcdae743f4..bffa3430fbbaaad29bec0b5bee9c1f0bfc7fd068:/lunaix-os/kernel/ds/mutex.c diff --git a/lunaix-os/kernel/ds/mutex.c b/lunaix-os/kernel/ds/mutex.c index 6af1087..41e131d 100644 --- a/lunaix-os/kernel/ds/mutex.c +++ b/lunaix-os/kernel/ds/mutex.c @@ -1,26 +1,41 @@ #include #include +#include -void -mutex_lock(mutex_t* mutex) +static inline bool must_inline +__mutex_check_owner(mutex_t* mutex) { - if (atomic_load(&mutex->lk) && mutex->owner == __current->pid) { - atomic_fetch_add(&mutex->lk, 1); - return; - } + return mutex->owner == __current->pid; +} +static inline void must_inline +__mutext_lock(mutex_t* mutex) +{ while (atomic_load(&mutex->lk)) { - sched_yieldk(); + preempt_current(); } atomic_fetch_add(&mutex->lk, 1); mutex->owner = __current->pid; } +static inline void must_inline +__mutext_unlock(mutex_t* mutex) +{ + if (__mutex_check_owner(mutex)) + atomic_fetch_sub(&mutex->lk, 1); +} + +void +mutex_lock(mutex_t* mutex) +{ + __mutext_lock(mutex); +} + void mutex_unlock(mutex_t* mutex) { - mutex_unlock_for(mutex, __current->pid); + __mutext_unlock(mutex); } void @@ -30,4 +45,21 @@ mutex_unlock_for(mutex_t* mutex, pid_t pid) return; } atomic_fetch_sub(&mutex->lk, 1); +} + +void +mutex_lock_nested(mutex_t* mutex) +{ + if (atomic_load(&mutex->lk) && __mutex_check_owner(mutex)) { + atomic_fetch_add(&mutex->lk, 1); + return; + } + + __mutext_lock(mutex); +} + +void +mutex_unlock_nested(mutex_t* mutex) +{ + mutex_unlock_for(mutex, __current->pid); } \ No newline at end of file