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:
parent
b6735d3175
commit
6986f534f8
|
@ -31,6 +31,7 @@ AhciDevice::AhciDevice(AhciPort* port) : port_struct_(port) {
|
|||
command_list_->command_headers[0].command_table_base_addr & 0xFFF;
|
||||
command_table_ = reinterpret_cast<CommandTable*>(vaddr + ct_off);
|
||||
|
||||
port_struct_->interrupt_status = 0;
|
||||
port_struct_->interrupt_enable = 0xFFFFFFFF;
|
||||
}
|
||||
|
||||
|
|
|
@ -13,7 +13,6 @@ const uint64_t kGhc_InteruptEnable = 0x2;
|
|||
|
||||
void interrupt_thread(void* void_driver) {
|
||||
AhciDriver* driver = static_cast<AhciDriver*>(void_driver);
|
||||
dbgln("this %lx", driver);
|
||||
|
||||
driver->InterruptLoop();
|
||||
|
||||
|
@ -132,7 +131,6 @@ void AhciDriver::DumpPorts() {
|
|||
}
|
||||
|
||||
void AhciDriver::InterruptLoop() {
|
||||
dbgln("this %lx", this);
|
||||
while (true) {
|
||||
uint64_t type, bytes, caps;
|
||||
check(ZPortRecv(irq_port_cap_, 0, 0, 0, 0, &type, &bytes, &caps));
|
||||
|
@ -189,7 +187,6 @@ z_err_t AhciDriver::RegisterIrq() {
|
|||
}
|
||||
uint64_t irq_num = Z_IRQ_PCI_BASE + pci_device_header_->interrupt_pin - 1;
|
||||
RET_ERR(ZIrqRegister(irq_num, &irq_port_cap_));
|
||||
dbgln("this %lx", this);
|
||||
irq_thread_ = Thread(interrupt_thread, this);
|
||||
return Z_OK;
|
||||
}
|
||||
|
|
|
@ -10,6 +10,7 @@ add_executable(zion
|
|||
interrupt/interrupt.cpp
|
||||
interrupt/interrupt_enter.s
|
||||
interrupt/timer.cpp
|
||||
lib/mutex.cpp
|
||||
loader/init_loader.cpp
|
||||
memory/kernel_heap.cpp
|
||||
memory/kernel_stack_manager.cpp
|
||||
|
|
|
@ -76,8 +76,8 @@ void MemToStr(uint64_t u, char* str) {
|
|||
|
||||
void AddProcPrefix() {
|
||||
if (gScheduler != nullptr) {
|
||||
auto& t = gScheduler->CurrentThread();
|
||||
dbg("[%u.%u] ", t.pid(), t.tid());
|
||||
auto t = gScheduler->CurrentThread();
|
||||
dbg("[%u.%u] ", t->pid(), t->tid());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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_;
|
||||
};
|
|
@ -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;
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
#pragma once
|
||||
|
||||
#include "lib/linked_list.h"
|
||||
#include "lib/mutex.h"
|
||||
#include "object/kernel_object.h"
|
||||
#include "object/thread.h"
|
||||
#include "usr/zcall_internal.h"
|
||||
|
||||
class Port : public KernelObject {
|
||||
|
@ -19,4 +21,8 @@ class Port : public KernelObject {
|
|||
};
|
||||
|
||||
LinkedList<Message> pending_messages_;
|
||||
|
||||
LinkedList<RefPtr<Thread>> blocked_threads_;
|
||||
|
||||
Mutex mutex_{"Port"};
|
||||
};
|
||||
|
|
|
@ -34,12 +34,14 @@ Process::Process()
|
|||
: id_(gNextId++), vmas_(MakeRefCounted<AddressSpace>()), state_(RUNNING) {}
|
||||
|
||||
RefPtr<Thread> Process::CreateThread() {
|
||||
MutexHolder lock(mutex_);
|
||||
RefPtr<Thread> thread = MakeRefCounted<Thread>(*this, next_thread_id_++);
|
||||
threads_.PushBack(thread);
|
||||
return thread;
|
||||
}
|
||||
|
||||
RefPtr<Thread> Process::GetThread(uint64_t tid) {
|
||||
MutexHolder lock(mutex_);
|
||||
auto iter = threads_.begin();
|
||||
while (iter != threads_.end()) {
|
||||
if (iter->tid() == tid) {
|
||||
|
@ -52,6 +54,7 @@ RefPtr<Thread> Process::GetThread(uint64_t tid) {
|
|||
}
|
||||
|
||||
void Process::CheckState() {
|
||||
MutexHolder lock(mutex_);
|
||||
auto iter = threads_.begin();
|
||||
while (iter != threads_.end()) {
|
||||
if (iter->GetState() != Thread::FINISHED) {
|
||||
|
@ -63,6 +66,7 @@ void Process::CheckState() {
|
|||
}
|
||||
|
||||
RefPtr<Capability> Process::ReleaseCapability(uint64_t cid) {
|
||||
MutexHolder lock(mutex_);
|
||||
auto iter = caps_.begin();
|
||||
while (iter != caps_.end()) {
|
||||
if (*iter && iter->id() == cid) {
|
||||
|
@ -78,6 +82,7 @@ RefPtr<Capability> Process::ReleaseCapability(uint64_t cid) {
|
|||
}
|
||||
|
||||
RefPtr<Capability> Process::GetCapability(uint64_t cid) {
|
||||
MutexHolder lock(mutex_);
|
||||
auto iter = caps_.begin();
|
||||
while (iter != caps_.end()) {
|
||||
if (*iter && iter->id() == cid) {
|
||||
|
@ -91,11 +96,13 @@ RefPtr<Capability> Process::GetCapability(uint64_t cid) {
|
|||
}
|
||||
|
||||
uint64_t Process::AddCapability(const RefPtr<Capability>& cap) {
|
||||
MutexHolder lock(mutex_);
|
||||
cap->set_id(next_cap_id_++);
|
||||
caps_.PushBack(cap);
|
||||
return cap->id();
|
||||
}
|
||||
uint64_t Process::AddCapability(const RefPtr<Thread>& thread) {
|
||||
MutexHolder lock(mutex_);
|
||||
uint64_t cap_id = next_cap_id_++;
|
||||
caps_.PushBack(
|
||||
MakeRefCounted<Capability>(thread, Capability::THREAD, cap_id, ZC_WRITE));
|
||||
|
@ -103,24 +110,28 @@ uint64_t Process::AddCapability(const RefPtr<Thread>& thread) {
|
|||
}
|
||||
|
||||
uint64_t Process::AddCapability(const RefPtr<Process>& p) {
|
||||
MutexHolder lock(mutex_);
|
||||
uint64_t cap_id = next_cap_id_++;
|
||||
caps_.PushBack(MakeRefCounted<Capability>(p, Capability::PROCESS, cap_id,
|
||||
ZC_WRITE | ZC_PROC_SPAWN_THREAD));
|
||||
return cap_id;
|
||||
}
|
||||
uint64_t Process::AddCapability(const RefPtr<AddressSpace>& vmas) {
|
||||
MutexHolder lock(mutex_);
|
||||
uint64_t cap_id = next_cap_id_++;
|
||||
caps_.PushBack(MakeRefCounted<Capability>(vmas, Capability::ADDRESS_SPACE,
|
||||
cap_id, ZC_WRITE));
|
||||
return cap_id;
|
||||
}
|
||||
uint64_t Process::AddCapability(const RefPtr<MemoryObject>& vmmo) {
|
||||
MutexHolder lock(mutex_);
|
||||
uint64_t cap_id = next_cap_id_++;
|
||||
caps_.PushBack(MakeRefCounted<Capability>(vmmo, Capability::MEMORY_OBJECT,
|
||||
cap_id, ZC_WRITE));
|
||||
return cap_id;
|
||||
}
|
||||
uint64_t Process::AddCapability(const RefPtr<Channel>& chan) {
|
||||
MutexHolder lock(mutex_);
|
||||
uint64_t cap_id = next_cap_id_++;
|
||||
caps_.PushBack(MakeRefCounted<Capability>(chan, Capability::CHANNEL, cap_id,
|
||||
ZC_WRITE | ZC_READ));
|
||||
|
@ -128,6 +139,7 @@ uint64_t Process::AddCapability(const RefPtr<Channel>& chan) {
|
|||
}
|
||||
|
||||
uint64_t Process::AddCapability(const RefPtr<Port>& port) {
|
||||
MutexHolder lock(mutex_);
|
||||
uint64_t cap_id = next_cap_id_++;
|
||||
caps_.PushBack(MakeRefCounted<Capability>(port, Capability::PORT, cap_id,
|
||||
ZC_WRITE | ZC_READ));
|
||||
|
@ -135,6 +147,7 @@ uint64_t Process::AddCapability(const RefPtr<Port>& port) {
|
|||
}
|
||||
|
||||
void Process::AddCapability(uint64_t cap_id, const RefPtr<MemoryObject>& vmmo) {
|
||||
MutexHolder lock(mutex_);
|
||||
caps_.PushBack(MakeRefCounted<Capability>(vmmo, Capability::MEMORY_OBJECT,
|
||||
cap_id, ZC_WRITE));
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
|
||||
#include "capability/capability.h"
|
||||
#include "lib/linked_list.h"
|
||||
#include "lib/mutex.h"
|
||||
#include "lib/ref_ptr.h"
|
||||
#include "object/address_space.h"
|
||||
#include "object/channel.h"
|
||||
|
@ -51,6 +52,9 @@ class Process : public KernelObject {
|
|||
friend class MakeRefCountedFriend<Process>;
|
||||
Process();
|
||||
Process(uint64_t id) : id_(id), vmas_(AddressSpace::ForRoot()) {}
|
||||
|
||||
Mutex mutex_{"Process"};
|
||||
|
||||
uint64_t id_;
|
||||
RefPtr<AddressSpace> vmas_;
|
||||
State state_;
|
||||
|
|
|
@ -15,7 +15,7 @@ extern "C" void jump_user_space(uint64_t rip, uint64_t rsp, uint64_t arg1,
|
|||
|
||||
extern "C" void thread_init() {
|
||||
asm("sti");
|
||||
gScheduler->CurrentThread().Init();
|
||||
gScheduler->CurrentThread()->Init();
|
||||
panic("Reached end of thread.");
|
||||
}
|
||||
|
||||
|
|
|
@ -13,7 +13,7 @@ class Scheduler {
|
|||
void Enable() { enabled_ = true; }
|
||||
|
||||
Process& CurrentProcess() { return current_thread_->process(); }
|
||||
Thread& CurrentThread() { return *current_thread_; }
|
||||
RefPtr<Thread> CurrentThread() { return current_thread_; }
|
||||
|
||||
void Enqueue(const RefPtr<Thread>& thread) {
|
||||
runnable_threads_.PushBack(thread);
|
||||
|
|
|
@ -24,7 +24,7 @@ extern "C" void syscall_enter();
|
|||
|
||||
// Used by syscall_enter.s
|
||||
extern "C" uint64_t GetKernelRsp() {
|
||||
return gScheduler->CurrentThread().Rsp0Start();
|
||||
return gScheduler->CurrentThread()->Rsp0Start();
|
||||
}
|
||||
|
||||
void InitSyscall() {
|
||||
|
@ -210,12 +210,12 @@ z_err_t IrqRegister(ZIrqRegisterReq* req, ZIrqRegisterResp* resp) {
|
|||
}
|
||||
|
||||
extern "C" z_err_t SyscallHandler(uint64_t call_id, void* req, void* resp) {
|
||||
Thread& thread = gScheduler->CurrentThread();
|
||||
RefPtr<Thread> thread = gScheduler->CurrentThread();
|
||||
switch (call_id) {
|
||||
case Z_PROCESS_EXIT:
|
||||
// FIXME: kill process here.
|
||||
dbgln("Exit code: %u", req);
|
||||
thread.Exit();
|
||||
thread->Exit();
|
||||
panic("Returned from thread exit");
|
||||
break;
|
||||
case Z_PROCESS_SPAWN:
|
||||
|
@ -227,7 +227,7 @@ extern "C" z_err_t SyscallHandler(uint64_t call_id, void* req, void* resp) {
|
|||
case Z_THREAD_START:
|
||||
return ThreadStart(reinterpret_cast<ZThreadStartReq*>(req));
|
||||
case Z_THREAD_EXIT:
|
||||
thread.Exit();
|
||||
thread->Exit();
|
||||
panic("Returned from thread exit");
|
||||
break;
|
||||
|
||||
|
|
Loading…
Reference in New Issue