7bfbf10620fe0b8bd6c453d67ddbff00b23b7052
[lunaix-os.git] / lunaix-os / kernel / ds / mutex.c
1 #include <lunaix/ds/mutex.h>
2 #include <lunaix/process.h>
3
4 void
5 mutex_lock(mutex_t* mutex)
6 {
7     sem_wait(&mutex->sem);
8     mutex->owner = __current->pid;
9 }
10
11 void
12 mutex_unlock(mutex_t* mutex)
13 {
14     mutex_unlock_for(mutex, __current->pid);
15 }
16
17 void
18 mutex_unlock_for(mutex_t* mutex, pid_t pid)
19 {
20     if (mutex->owner != pid) {
21         return;
22     }
23     sem_post(&mutex->sem);
24 }