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

View file

@ -1,5 +1,7 @@
#include "object/port.h"
#include "scheduler/scheduler.h"
Port::Port() {}
z_err_t Port::Write(const ZMessage& msg) {
@ -21,16 +23,26 @@ z_err_t Port::Write(const ZMessage& msg) {
for (uint64_t i = 0; i < msg.num_bytes; i++) {
message.bytes[i] = msg.bytes[i];
}
MutexHolder lock(mutex_);
pending_messages_.PushBack(message);
if (blocked_threads_.size() > 0) {
gScheduler->Enqueue(blocked_threads_.PopFront());
}
return Z_OK;
}
z_err_t Port::Read(ZMessage& msg) {
if (pending_messages_.size() < 1) {
dbgln("Implement blocking");
return Z_ERR_UNIMPLEMENTED;
mutex_.Lock();
while (pending_messages_.size() < 1) {
blocked_threads_.PushBack(gScheduler->CurrentThread());
mutex_.Unlock();
gScheduler->Yield();
mutex_.Lock();
}
mutex_.Unlock();
MutexHolder lock(mutex_);
Message next_msg = pending_messages_.PeekFront();
if (next_msg.num_bytes > msg.num_bytes) {
return Z_ERR_BUFF_SIZE;