[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:
@@ -1,5 +1,6 @@
|
||||
#include "j6/signals.h"
|
||||
#include "kutil/assert.h"
|
||||
#include "cpu.h"
|
||||
#include "objects/process.h"
|
||||
#include "objects/thread.h"
|
||||
#include "page_manager.h"
|
||||
@@ -9,6 +10,7 @@ kutil::vector<process*> process::s_processes;
|
||||
process::process(page_table *pml4) :
|
||||
kobject(kobject::type::process),
|
||||
m_pml4(pml4),
|
||||
m_next_handle(0),
|
||||
m_state(state::running)
|
||||
{
|
||||
s_processes.append(this);
|
||||
@@ -19,6 +21,12 @@ process::~process()
|
||||
s_processes.remove_swap(this);
|
||||
}
|
||||
|
||||
process &
|
||||
process::current()
|
||||
{
|
||||
return *bsp_cpu_data.p;
|
||||
}
|
||||
|
||||
void
|
||||
process::exit(unsigned code)
|
||||
{
|
||||
@@ -104,28 +112,21 @@ process::add_handle(kobject *obj)
|
||||
return j6_handle_invalid;
|
||||
|
||||
obj->handle_retain();
|
||||
size_t len = m_handles.count();
|
||||
m_handles.append(obj);
|
||||
return static_cast<j6_handle_t>(len);
|
||||
j6_handle_t handle = m_next_handle++;
|
||||
m_handles.insert(handle, obj);
|
||||
return handle;
|
||||
}
|
||||
|
||||
bool
|
||||
process::remove_handle(j6_handle_t handle)
|
||||
{
|
||||
if (handle < m_handles.count()) {
|
||||
kobject *obj = m_handles[handle];
|
||||
m_handles[handle] = nullptr;
|
||||
if (obj)
|
||||
obj->handle_release();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
kobject *obj = m_handles.find(handle);
|
||||
if (obj) obj->handle_release();
|
||||
return m_handles.erase(handle);
|
||||
}
|
||||
|
||||
kobject *
|
||||
process::lookup_handle(j6_handle_t handle)
|
||||
{
|
||||
if (handle < m_handles.count())
|
||||
return m_handles[handle];
|
||||
return nullptr;
|
||||
return m_handles.find(handle);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
/// \file process.h
|
||||
/// Definition of process kobject types
|
||||
|
||||
#include "kutil/map.h"
|
||||
#include "kutil/vector.h"
|
||||
#include "objects/kobject.h"
|
||||
#include "page_table.h"
|
||||
|
||||
@@ -22,6 +24,9 @@ public:
|
||||
/// Destructor.
|
||||
virtual ~process();
|
||||
|
||||
/// Get the currently executing process.
|
||||
static process & current();
|
||||
|
||||
/// Terminate this process.
|
||||
/// \arg code The return code to exit with.
|
||||
void exit(unsigned code);
|
||||
@@ -68,7 +73,8 @@ private:
|
||||
|
||||
page_table *m_pml4;
|
||||
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 };
|
||||
state m_state;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "j6/signals.h"
|
||||
#include "cpu.h"
|
||||
#include "log.h"
|
||||
#include "objects/thread.h"
|
||||
#include "objects/process.h"
|
||||
@@ -40,6 +41,12 @@ thread::from_tcb(TCB *tcb)
|
||||
return reinterpret_cast<thread*>(kutil::offset_pointer(tcb, offset));
|
||||
}
|
||||
|
||||
thread &
|
||||
thread::current()
|
||||
{
|
||||
return *bsp_cpu_data.t;
|
||||
}
|
||||
|
||||
void
|
||||
thread::wait_on_signals(kobject *obj, j6_signal_t signals)
|
||||
{
|
||||
|
||||
@@ -50,6 +50,9 @@ public:
|
||||
/// Destructor
|
||||
virtual ~thread();
|
||||
|
||||
/// Get the currently executing thread.
|
||||
static thread & current();
|
||||
|
||||
/// Get the `ready` state of the thread.
|
||||
/// \returns True if the thread is ready to execute.
|
||||
inline bool ready() const { return has_state(state::ready); }
|
||||
|
||||
Reference in New Issue
Block a user