[kernel] Use map for process handles

Replace linearly-indexed vector of handles with new kutil::map. Also
provide thread::current() and process::current() accessors so that every
syscall doesn't need to include the scheduler to deduce the current
process.
This commit is contained in:
2020-09-13 15:54:47 -07:00
parent 245f260d67
commit 9dee5e4138
6 changed files with 43 additions and 43 deletions

View File

@@ -1,5 +1,6 @@
#include "j6/signals.h" #include "j6/signals.h"
#include "kutil/assert.h" #include "kutil/assert.h"
#include "cpu.h"
#include "objects/process.h" #include "objects/process.h"
#include "objects/thread.h" #include "objects/thread.h"
#include "page_manager.h" #include "page_manager.h"
@@ -9,6 +10,7 @@ kutil::vector<process*> process::s_processes;
process::process(page_table *pml4) : process::process(page_table *pml4) :
kobject(kobject::type::process), kobject(kobject::type::process),
m_pml4(pml4), m_pml4(pml4),
m_next_handle(0),
m_state(state::running) m_state(state::running)
{ {
s_processes.append(this); s_processes.append(this);
@@ -19,6 +21,12 @@ process::~process()
s_processes.remove_swap(this); s_processes.remove_swap(this);
} }
process &
process::current()
{
return *bsp_cpu_data.p;
}
void void
process::exit(unsigned code) process::exit(unsigned code)
{ {
@@ -104,28 +112,21 @@ process::add_handle(kobject *obj)
return j6_handle_invalid; return j6_handle_invalid;
obj->handle_retain(); obj->handle_retain();
size_t len = m_handles.count(); j6_handle_t handle = m_next_handle++;
m_handles.append(obj); m_handles.insert(handle, obj);
return static_cast<j6_handle_t>(len); return handle;
} }
bool bool
process::remove_handle(j6_handle_t handle) process::remove_handle(j6_handle_t handle)
{ {
if (handle < m_handles.count()) { kobject *obj = m_handles.find(handle);
kobject *obj = m_handles[handle]; if (obj) obj->handle_release();
m_handles[handle] = nullptr; return m_handles.erase(handle);
if (obj)
obj->handle_release();
return true;
}
return false;
} }
kobject * kobject *
process::lookup_handle(j6_handle_t handle) process::lookup_handle(j6_handle_t handle)
{ {
if (handle < m_handles.count()) return m_handles.find(handle);
return m_handles[handle];
return nullptr;
} }

View File

@@ -2,6 +2,8 @@
/// \file process.h /// \file process.h
/// Definition of process kobject types /// Definition of process kobject types
#include "kutil/map.h"
#include "kutil/vector.h"
#include "objects/kobject.h" #include "objects/kobject.h"
#include "page_table.h" #include "page_table.h"
@@ -22,6 +24,9 @@ public:
/// Destructor. /// Destructor.
virtual ~process(); virtual ~process();
/// Get the currently executing process.
static process & current();
/// Terminate this process. /// Terminate this process.
/// \arg code The return code to exit with. /// \arg code The return code to exit with.
void exit(unsigned code); void exit(unsigned code);
@@ -68,7 +73,8 @@ private:
page_table *m_pml4; page_table *m_pml4;
kutil::vector<thread*> m_threads; kutil::vector<thread*> m_threads;
kutil::vector<kobject*> m_handles; kutil::map<j6_handle_t, kobject*> m_handles;
j6_handle_t m_next_handle;
enum class state : uint8_t { running, exited }; enum class state : uint8_t { running, exited };
state m_state; state m_state;

View File

@@ -1,4 +1,5 @@
#include "j6/signals.h" #include "j6/signals.h"
#include "cpu.h"
#include "log.h" #include "log.h"
#include "objects/thread.h" #include "objects/thread.h"
#include "objects/process.h" #include "objects/process.h"
@@ -40,6 +41,12 @@ thread::from_tcb(TCB *tcb)
return reinterpret_cast<thread*>(kutil::offset_pointer(tcb, offset)); return reinterpret_cast<thread*>(kutil::offset_pointer(tcb, offset));
} }
thread &
thread::current()
{
return *bsp_cpu_data.t;
}
void void
thread::wait_on_signals(kobject *obj, j6_signal_t signals) thread::wait_on_signals(kobject *obj, j6_signal_t signals)
{ {

View File

@@ -50,6 +50,9 @@ public:
/// Destructor /// Destructor
virtual ~thread(); virtual ~thread();
/// Get the currently executing thread.
static thread & current();
/// Get the `ready` state of the thread. /// Get the `ready` state of the thread.
/// \returns True if the thread is ready to execute. /// \returns True if the thread is ready to execute.
inline bool ready() const { return has_state(state::ready); } inline bool ready() const { return has_state(state::ready); }

View File

@@ -2,19 +2,14 @@
#include "j6/types.h" #include "j6/types.h"
#include "objects/channel.h" #include "objects/channel.h"
#include "objects/thread.h"
#include "objects/process.h" #include "objects/process.h"
#include "scheduler.h"
namespace syscalls { namespace syscalls {
j6_status_t j6_status_t
channel_create(j6_handle_t *handle) channel_create(j6_handle_t *handle)
{ {
scheduler &s = scheduler::get(); process &p = process::current();
TCB *tcb = s.current();
thread *parent = thread::from_tcb(tcb);
process &p = parent->parent();
channel *c = new channel; channel *c = new channel;
*handle = p.add_handle(c); *handle = p.add_handle(c);
@@ -25,10 +20,7 @@ channel_create(j6_handle_t *handle)
j6_status_t j6_status_t
channel_close(j6_handle_t handle) channel_close(j6_handle_t handle)
{ {
scheduler &s = scheduler::get(); process &p = process::current();
TCB *tcb = s.current();
thread *parent = thread::from_tcb(tcb);
process &p = parent->parent();
kobject *o = p.lookup_handle(handle); kobject *o = p.lookup_handle(handle);
if (!o || o->get_type() != kobject::type::channel) if (!o || o->get_type() != kobject::type::channel)
@@ -44,10 +36,7 @@ channel_close(j6_handle_t handle)
j6_status_t j6_status_t
channel_send(j6_handle_t handle, size_t *len, void *data) channel_send(j6_handle_t handle, size_t *len, void *data)
{ {
scheduler &s = scheduler::get(); process &p = process::current();
TCB *tcb = s.current();
thread *parent = thread::from_tcb(tcb);
process &p = parent->parent();
kobject *o = p.lookup_handle(handle); kobject *o = p.lookup_handle(handle);
if (!o || o->get_type() != kobject::type::channel) if (!o || o->get_type() != kobject::type::channel)
@@ -60,10 +49,7 @@ channel_send(j6_handle_t handle, size_t *len, void *data)
j6_status_t j6_status_t
channel_receive(j6_handle_t handle, size_t *len, void *data) channel_receive(j6_handle_t handle, size_t *len, void *data)
{ {
scheduler &s = scheduler::get(); process &p = process::current();
TCB *tcb = s.current();
thread *parent = thread::from_tcb(tcb);
process &p = parent->parent();
kobject *o = p.lookup_handle(handle); kobject *o = p.lookup_handle(handle);
if (!o || o->get_type() != kobject::type::channel) if (!o || o->get_type() != kobject::type::channel)

View File

@@ -13,8 +13,8 @@ j6_status_t
object_wait(j6_handle_t handle, j6_signal_t mask, j6_signal_t *sigs) object_wait(j6_handle_t handle, j6_signal_t mask, j6_signal_t *sigs)
{ {
scheduler &s = scheduler::get(); scheduler &s = scheduler::get();
thread *th = thread::from_tcb(s.current()); thread &th = thread::current();
process &p = th->parent(); process &p = process::current();
kobject *obj = p.lookup_handle(handle); kobject *obj = p.lookup_handle(handle);
if (!obj) if (!obj)
@@ -26,13 +26,13 @@ object_wait(j6_handle_t handle, j6_signal_t mask, j6_signal_t *sigs)
return j6_status_ok; return j6_status_ok;
} }
obj->add_blocked_thread(th); obj->add_blocked_thread(&th);
th->wait_on_signals(obj, mask); th.wait_on_signals(obj, mask);
s.schedule(); s.schedule();
j6_status_t result = th->get_wait_result(); j6_status_t result = th.get_wait_result();
if (result == j6_status_ok) { if (result == j6_status_ok) {
*sigs = th->get_wait_data(); *sigs = th.get_wait_data();
} }
return result; return result;
} }
@@ -43,10 +43,7 @@ object_signal(j6_handle_t handle, j6_signal_t signals)
if ((signals & j6_signal_user_mask) != signals) if ((signals & j6_signal_user_mask) != signals)
return j6_err_invalid_arg; return j6_err_invalid_arg;
scheduler &s = scheduler::get(); process &p = process::current();
thread *th = thread::from_tcb(s.current());
process &p = th->parent();
kobject *obj = p.lookup_handle(handle); kobject *obj = p.lookup_handle(handle);
if (!obj) if (!obj)
return j6_err_invalid_arg; return j6_err_invalid_arg;