Add a method for blocking threads on ports.

Additionally add the first lock class since we are becoming more
concurrent.
This commit is contained in:
Drew Galbraith 2023-06-12 20:56:25 -07:00
parent b6735d3175
commit 6986f534f8
13 changed files with 88 additions and 14 deletions

11
zion/lib/mutex.cpp Normal file
View file

@ -0,0 +1,11 @@
#include "lib/mutex.h"
#include "debug/debug.h"
#include "scheduler/scheduler.h"
void Mutex::Lock() {
while (__atomic_fetch_or(&lock_, 0x1, __ATOMIC_SEQ_CST) == 0x1) {
dbgln("Lock sleep: %s", name_);
gScheduler->Preempt();
}
}

29
zion/lib/mutex.h Normal file
View file

@ -0,0 +1,29 @@
#pragma once
#include <stdint.h>
class Mutex {
public:
Mutex(const char* name) : name_(name) {}
void Lock();
void Unlock() { lock_ = false; }
private:
const char* name_;
uint8_t lock_ = 0;
};
class MutexHolder {
public:
MutexHolder(Mutex& mutex) : mutex_(mutex) { mutex_.Lock(); }
~MutexHolder() { mutex_.Unlock(); }
MutexHolder(MutexHolder&) = delete;
MutexHolder(MutexHolder&&) = delete;
private:
Mutex& mutex_;
};