Skip to content
Snippets Groups Projects
Commit 1a80ca0f authored by Dor Laor's avatar Dor Laor
Browse files

Initialize class mutex and its spinlock correctly

parent b7fcf036
No related branches found
No related tags found
No related merge requests found
...@@ -7,6 +7,8 @@ ...@@ -7,6 +7,8 @@
class mutex { class mutex {
public: public:
mutex() { mutex_init(&_mutex); }
~mutex() { mutex_destroy(&_mutex); }
void lock(); void lock();
bool try_lock(); bool try_lock();
void unlock(); void unlock();
......
...@@ -14,6 +14,11 @@ struct cspinlock { ...@@ -14,6 +14,11 @@ struct cspinlock {
typedef struct cspinlock spinlock_t; typedef struct cspinlock spinlock_t;
static inline void spinlock_init(spinlock_t *sl)
{
sl->lock = false;
}
struct cmutex { struct cmutex {
bool _locked; bool _locked;
struct wait_list { struct wait_list {
...@@ -33,7 +38,10 @@ void mutex_unlock(mutex_t* m); ...@@ -33,7 +38,10 @@ void mutex_unlock(mutex_t* m);
static __always_inline void mutex_init(mutex_t* m) static __always_inline void mutex_init(mutex_t* m)
{ {
memset(m, 0, sizeof(*m)); m->_locked = false;
m->_wait_list.first = 0;
m->_wait_list.last = 0;
spinlock_init(&m->_wait_lock);
} }
static __always_inline void mutex_destroy(mutex_t* m) static __always_inline void mutex_destroy(mutex_t* m)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment