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