First iteration of denali server
This commit is contained in:
parent
82b1a5c4db
commit
ffa2d97a64
18 changed files with 273 additions and 39 deletions
89
zion/lib/shared_ptr.h
Normal file
89
zion/lib/shared_ptr.h
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "debug/debug.h"
|
||||
|
||||
template <typename T>
|
||||
class SharedPtr {
|
||||
public:
|
||||
SharedPtr() : init_(false), ptr_(0), ref_cnt_(0) {}
|
||||
// Takes ownership.
|
||||
SharedPtr(T* ptr) {
|
||||
ptr_ = ptr;
|
||||
ref_cnt_ = new uint64_t(1);
|
||||
}
|
||||
|
||||
SharedPtr(const SharedPtr<T>& other)
|
||||
: init_(other.init_), ptr_(other.ptr_), ref_cnt_(other.ref_cnt_) {
|
||||
(*ref_cnt_)++;
|
||||
}
|
||||
|
||||
SharedPtr& operator=(const SharedPtr<T>& other) {
|
||||
Cleanup();
|
||||
init_ = other.init_;
|
||||
ptr_ = other.ptr_;
|
||||
ref_cnt_ = other.ref_cnt_;
|
||||
(*ref_cnt_)++;
|
||||
return *this;
|
||||
}
|
||||
|
||||
~SharedPtr() { Cleanup(); }
|
||||
|
||||
T& operator*() {
|
||||
CheckValid();
|
||||
return *ptr_;
|
||||
}
|
||||
const T& operator*() const {
|
||||
CheckValid();
|
||||
return *ptr_;
|
||||
}
|
||||
T* operator->() {
|
||||
CheckValid();
|
||||
return ptr_;
|
||||
}
|
||||
const T* operator->() const {
|
||||
CheckValid();
|
||||
return ptr_;
|
||||
}
|
||||
|
||||
T* ptr() {
|
||||
CheckValid();
|
||||
return ptr_;
|
||||
}
|
||||
|
||||
bool operator==(const SharedPtr<T>& other) {
|
||||
CheckValid();
|
||||
other.CheckValid();
|
||||
return ptr_ == other.ptr_;
|
||||
}
|
||||
|
||||
bool empty() { return !init_; }
|
||||
|
||||
private:
|
||||
bool init_ = true;
|
||||
T* ptr_;
|
||||
uint64_t* ref_cnt_;
|
||||
|
||||
void Cleanup() {
|
||||
if (!init_) {
|
||||
return;
|
||||
}
|
||||
if (--(*ref_cnt_) == 0) {
|
||||
dbgln("Deleting shared ptr: %m", ptr_);
|
||||
delete ptr_;
|
||||
delete ref_cnt_;
|
||||
}
|
||||
}
|
||||
|
||||
void CheckValid() const {
|
||||
if (!init_) {
|
||||
panic("Accessing invalid shared ptr");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T, class... A>
|
||||
SharedPtr<T> MakeShared(A... args) {
|
||||
return {new T(args...)};
|
||||
}
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
#include "object/channel.h"
|
||||
|
||||
#include "include/zerrors.h"
|
||||
#include "scheduler/scheduler.h"
|
||||
|
||||
Pair<RefPtr<Channel>, RefPtr<Channel>> Channel::CreateChannelPair() {
|
||||
auto c1 = MakeRefCounted<Channel>();
|
||||
|
|
@ -15,21 +16,35 @@ z_err_t Channel::Write(const ZMessage& msg) {
|
|||
}
|
||||
|
||||
z_err_t Channel::Read(ZMessage& msg) {
|
||||
if (pending_messages_.size() == 0) {
|
||||
dbgln("Unimplemented add blocking.");
|
||||
return Z_ERR_UNIMPLEMENTED;
|
||||
mutex_.Lock();
|
||||
while (pending_messages_.size() == 0) {
|
||||
blocked_threads_.PushBack(gScheduler->CurrentThread());
|
||||
mutex_.Unlock();
|
||||
gScheduler->Yield();
|
||||
mutex_.Lock();
|
||||
}
|
||||
Message next_msg = pending_messages_.PeekFront();
|
||||
if (next_msg.num_bytes > msg.num_bytes) {
|
||||
mutex_.Unlock();
|
||||
|
||||
MutexHolder lock(mutex_);
|
||||
auto next_msg = pending_messages_.PeekFront();
|
||||
if (next_msg->num_bytes > msg.num_bytes) {
|
||||
return Z_ERR_BUFF_SIZE;
|
||||
}
|
||||
if (next_msg->caps.size() > msg.num_caps) {
|
||||
return Z_ERR_BUFF_SIZE;
|
||||
}
|
||||
|
||||
msg.type = next_msg.type;
|
||||
msg.num_bytes = next_msg.num_bytes;
|
||||
msg.num_caps = 0;
|
||||
msg.type = next_msg->type;
|
||||
msg.num_bytes = next_msg->num_bytes;
|
||||
|
||||
for (uint64_t i = 0; i < msg.num_bytes; i++) {
|
||||
msg.bytes[i] = next_msg.bytes[i];
|
||||
msg.bytes[i] = next_msg->bytes[i];
|
||||
}
|
||||
|
||||
msg.num_caps = next_msg->caps.size();
|
||||
auto& proc = gScheduler->CurrentProcess();
|
||||
for (uint64_t i = 0; i < msg.num_caps; i++) {
|
||||
msg.caps[i] = proc.AddCapability(next_msg->caps.PopFront());
|
||||
}
|
||||
|
||||
pending_messages_.PopFront();
|
||||
|
|
@ -38,24 +53,36 @@ z_err_t Channel::Read(ZMessage& msg) {
|
|||
}
|
||||
|
||||
z_err_t Channel::EnqueueMessage(const ZMessage& msg) {
|
||||
if (msg.num_caps > 0) {
|
||||
dbgln("Unimplemented passing caps on channel");
|
||||
return Z_ERR_UNIMPLEMENTED;
|
||||
}
|
||||
|
||||
if (msg.num_bytes > 0x1000) {
|
||||
dbgln("Large message size unimplemented: %x", msg.num_bytes);
|
||||
return Z_ERR_INVALID;
|
||||
}
|
||||
|
||||
Message message{
|
||||
.type = msg.type,
|
||||
.num_bytes = msg.num_bytes,
|
||||
.bytes = new uint8_t[msg.num_bytes],
|
||||
};
|
||||
auto message = MakeShared<Message>();
|
||||
message->type = msg.type;
|
||||
|
||||
// Copy Message body.
|
||||
message->num_bytes = msg.num_bytes;
|
||||
message->bytes = new uint8_t[msg.num_bytes];
|
||||
for (uint64_t i = 0; i < msg.num_bytes; i++) {
|
||||
message.bytes[i] = msg.bytes[i];
|
||||
message->bytes[i] = msg.bytes[i];
|
||||
}
|
||||
|
||||
// Release and store capabilities.
|
||||
for (uint64_t i = 0; i < msg.num_caps; i++) {
|
||||
auto cap = gScheduler->CurrentProcess().ReleaseCapability(msg.caps[i]);
|
||||
if (!cap) {
|
||||
return Z_ERR_CAP_NOT_FOUND;
|
||||
}
|
||||
message->caps.PushBack(cap);
|
||||
}
|
||||
|
||||
// Enqueue.
|
||||
MutexHolder lock(mutex_);
|
||||
pending_messages_.PushBack(message);
|
||||
|
||||
if (blocked_threads_.size() > 0) {
|
||||
gScheduler->Enqueue(blocked_threads_.PopFront());
|
||||
}
|
||||
return Z_OK;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,8 +3,10 @@
|
|||
#include "capability/capability.h"
|
||||
#include "include/zerrors.h"
|
||||
#include "lib/linked_list.h"
|
||||
#include "lib/mutex.h"
|
||||
#include "lib/pair.h"
|
||||
#include "lib/ref_ptr.h"
|
||||
#include "lib/shared_ptr.h"
|
||||
#include "object/kernel_object.h"
|
||||
#include "usr/zcall_internal.h"
|
||||
|
||||
|
|
@ -22,16 +24,21 @@ class Channel : public KernelObject {
|
|||
// circular dependency.
|
||||
RefPtr<Channel> peer_{nullptr};
|
||||
|
||||
Mutex mutex_{"channel"};
|
||||
|
||||
struct Message {
|
||||
uint64_t type;
|
||||
|
||||
uint64_t num_bytes;
|
||||
uint8_t* bytes;
|
||||
|
||||
LinkedList<RefPtr<Capability>> caps;
|
||||
};
|
||||
|
||||
// FIXME: This is probably dangerous because of an
|
||||
// implicit shallow copy.
|
||||
LinkedList<Message> pending_messages_;
|
||||
LinkedList<SharedPtr<Message>> pending_messages_;
|
||||
LinkedList<RefPtr<Thread>> blocked_threads_;
|
||||
|
||||
friend class MakeRefCountedFriend<Channel>;
|
||||
Channel() {}
|
||||
|
|
|
|||
|
|
@ -76,7 +76,7 @@ RefPtr<Capability> Process::ReleaseCapability(uint64_t cid) {
|
|||
}
|
||||
++iter;
|
||||
}
|
||||
dbgln("Bad cap access");
|
||||
dbgln("Bad cap release: %u", cid);
|
||||
dbgln("Num caps: %u", caps_.size());
|
||||
return {};
|
||||
}
|
||||
|
|
@ -90,7 +90,7 @@ RefPtr<Capability> Process::GetCapability(uint64_t cid) {
|
|||
}
|
||||
++iter;
|
||||
}
|
||||
dbgln("Bad cap access");
|
||||
dbgln("Bad cap access %u", cid);
|
||||
dbgln("Num caps: %u", caps_.size());
|
||||
return {};
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue