Endpoint syscalls implemented

This commit is contained in:
Drew Galbraith 2023-06-21 23:14:42 -07:00
parent 69501bfe01
commit c064af5fa7
27 changed files with 391 additions and 42 deletions

43
zion/object/endpoint.cpp Normal file
View file

@ -0,0 +1,43 @@
#include "object/endpoint.h"
#include "scheduler/scheduler.h"
glcr::RefPtr<Endpoint> Endpoint::Create() {
return glcr::AdoptPtr(new Endpoint);
}
glcr::ErrorCode Endpoint::Write(uint64_t num_bytes, const void* data,
z_cap_t reply_port_cap) {
MutexHolder h(mutex_);
RET_ERR(message_queue_.PushBack(num_bytes, data, 1, &reply_port_cap));
if (blocked_threads_.size() > 0) {
auto thread = blocked_threads_.PopFront();
thread->SetState(Thread::RUNNABLE);
gScheduler->Enqueue(thread);
}
return glcr::OK;
}
glcr::ErrorCode Endpoint::Read(uint64_t* num_bytes, void* data,
z_cap_t* reply_port_cap) {
mutex_.Lock();
while (message_queue_.empty()) {
auto thread = gScheduler->CurrentThread();
thread->SetState(Thread::BLOCKED);
mutex_.Unlock();
gScheduler->Yield();
mutex_.Lock();
}
mutex_.Unlock();
MutexHolder h(mutex_);
uint64_t num_caps = 1;
RET_ERR(message_queue_.PopFront(num_bytes, data, &num_caps, reply_port_cap));
if (num_caps != 1) {
return glcr::INTERNAL;
}
return glcr::OK;
}

37
zion/object/endpoint.h Normal file
View file

@ -0,0 +1,37 @@
#pragma once
#include <glacier/container/intrusive_list.h>
#include <glacier/memory/ref_ptr.h>
#include <glacier/status/error.h>
#include "lib/message_queue.h"
#include "lib/mutex.h"
#include "object/kernel_object.h"
class Endpoint;
class ReplyPort;
template <>
struct KernelObjectTag<Endpoint> {
static const uint64_t type = KernelObject::ENDPOINT;
};
class Endpoint : public KernelObject {
public:
uint64_t TypeTag() override { return KernelObject::ENDPOINT; }
static glcr::RefPtr<Endpoint> Create();
glcr::ErrorCode Write(uint64_t num_bytes, const void* data,
z_cap_t reply_port_cap);
glcr::ErrorCode Read(uint64_t* num_bytes, void* data,
z_cap_t* reply_port_cap);
private:
Mutex mutex_{"endpoint"};
UnboundedMessageQueue message_queue_;
glcr::IntrusiveList<Thread> blocked_threads_;
Endpoint() {}
};

View file

@ -12,6 +12,8 @@ class KernelObject : public glcr::RefCounted<KernelObject> {
MEMORY_OBJECT = 0x4,
CHANNEL = 0x5,
PORT = 0x6,
ENDPOINT = 0x7,
REPLY_PORT = 0x8,
};
virtual uint64_t TypeTag() = 0;

View file

@ -0,0 +1,41 @@
#include "object/reply_port.h"
#include "scheduler/scheduler.h"
glcr::RefPtr<ReplyPort> ReplyPort::Create() {
return glcr::AdoptPtr(new ReplyPort);
}
uint64_t ReplyPort::Write(uint64_t num_bytes, const void* data,
uint64_t num_caps, uint64_t* caps) {
MutexHolder h(mutex_);
RET_ERR(message_holder_.PushBack(num_bytes, data, num_caps, caps));
if (blocked_thread_) {
// FIXME: We need to handle the case where the blocked thread has died I
// think.
blocked_thread_->SetState(Thread::RUNNABLE);
gScheduler->Enqueue(blocked_thread_);
blocked_thread_ = nullptr;
}
return glcr::OK;
}
uint64_t ReplyPort::Read(uint64_t* num_bytes, void* data, uint64_t* num_caps,
uint64_t* caps) {
mutex_.Lock();
if (message_holder_.empty()) {
// Multiple threads can't block on a reply port.
if (blocked_thread_) {
mutex_.Unlock();
return glcr::FAILED_PRECONDITION;
}
blocked_thread_ = gScheduler->CurrentThread();
blocked_thread_->SetState(Thread::BLOCKED);
mutex_.Unlock();
gScheduler->Yield();
mutex_.Lock();
}
return message_holder_.PopFront(num_bytes, data, num_caps, caps);
}

33
zion/object/reply_port.h Normal file
View file

@ -0,0 +1,33 @@
#pragma once
#include <glacier/memory/ref_ptr.h>
#include "lib/message_queue.h"
#include "lib/mutex.h"
#include "object/kernel_object.h"
class ReplyPort;
template <>
struct KernelObjectTag<ReplyPort> {
static const uint64_t type = KernelObject::REPLY_PORT;
};
class ReplyPort : public KernelObject {
public:
uint64_t TypeTag() override { return KernelObject::REPLY_PORT; }
static glcr::RefPtr<ReplyPort> Create();
uint64_t Write(uint64_t num_bytes, const void* data, uint64_t num_caps,
uint64_t* caps);
uint64_t Read(uint64_t* num_bytes, void* data, uint64_t* num_caps,
uint64_t* caps);
private:
Mutex mutex_{"reply_port"};
SingleMessageQueue message_holder_;
glcr::RefPtr<Thread> blocked_thread_;
ReplyPort() {}
};