1 /*
2  * Copyright (c) 2023 Ali Mashtizadeh
3  * All rights reserved.
4  */
5 
6 #include <stdbool.h>
7 #include <stdint.h>
8 #include <string.h>
9 
10 #include <sys/cdefs.h>
11 #include <sys/kassert.h>
12 #include <sys/kconfig.h>
13 #include <sys/kdebug.h>
14 #include <sys/kmem.h>
15 #include <sys/mp.h>
16 #include <sys/queue.h>
17 #include <sys/thread.h>
18 #include <sys/spinlock.h>
19 #include <sys/waitchannel.h>
20 #include <sys/mutex.h>
21 #include <errno.h>
22 
23 /*
24  * For debugging so we can assert the owner without holding a reference to the
25  * thread.  You can access the current thread through curProc[CPU()].
26  */
27 extern Thread *curProc[MAX_CPUS];
28 
29 void
Mutex_Init(Mutex * mtx,const char * name)30 Mutex_Init(Mutex *mtx, const char *name)
31 {
32     Spinlock_Init(&mtx->lock, name, SPINLOCK_TYPE_NORMAL);
33     WaitChannel_Init(&mtx->chan, name);
34 
35     return;
36 }
37 
38 void
Mutex_Destroy(Mutex * mtx)39 Mutex_Destroy(Mutex *mtx)
40 {
41     WaitChannel_Destroy(&mtx->chan);
42     Spinlock_Destroy(&mtx->lock);
43     return;
44 }
45 
46 /**
47  * Mutex_Lock --
48  *
49  * Acquires the mutex.
50  */
51 void
Mutex_Lock(Mutex * mtx)52 Mutex_Lock(Mutex *mtx)
53 {
54     /*
55      * You cannot hold a spinlock while trying to acquire a Mutex that may
56      * sleep!
57      */
58     ASSERT(Critical_Level() == 0);
59 
60     /* XXXFILLMEIN */
61 }
62 
63 /**
64  * Mutex_TryLock --
65  *
66  * Attempts to acquire the user mutex.  Returns EBUSY if the lock is already
67  * taken, otherwise 0 on success.
68  */
69 int
Mutex_TryLock(Mutex * mtx)70 Mutex_TryLock(Mutex *mtx)
71 {
72     /* XXXFILLMEIN */
73 
74     return 0;
75 }
76 
77 /**
78  * Mutex_Unlock --
79  *
80  * Releases the user mutex.
81  */
82 void
Mutex_Unlock(Mutex * mtx)83 Mutex_Unlock(Mutex *mtx)
84 {
85     /* XXXFILLMEIN */
86 
87     return;
88 }
89 
90