Forráskód Böngészése

change(thread): add class thdlist

greatbridf 2 éve
szülő
commit
9caca574db
3 módosított fájl, 113 hozzáadás és 65 törlés
  1. 69 12
      include/kernel/process.hpp
  2. 40 50
      src/kernel/process.cpp
  3. 4 3
      src/kernel/syscall.cpp

+ 69 - 12
include/kernel/process.hpp

@@ -17,6 +17,14 @@ typedef size_t pid_t;
 class process;
 struct thread;
 
+class proclist;
+class readyqueue;
+
+inline process* volatile current_process;
+inline thread* volatile current_thread;
+inline proclist* procs;
+inline readyqueue* readythds;
+
 struct process_attr {
     uint16_t system : 1;
     uint16_t zombie : 1 = 0;
@@ -74,6 +82,12 @@ public:
         alloc_kstack();
     }
 
+    inline thread(const thread& thd, process* new_parent)
+        : thread { thd }
+    {
+        owner = new_parent;
+    }
+
     constexpr ~thread()
     {
         if (kstack)
@@ -81,10 +95,47 @@ public:
     }
 };
 
+class thdlist {
+private:
+    types::list<thread> thds;
+
+public:
+    constexpr thdlist(const thdlist& obj) = delete;
+    constexpr thdlist(thdlist&& obj) = delete;
+
+    constexpr thdlist& operator=(const thdlist& obj) = delete;
+    constexpr thdlist& operator=(thdlist&& obj) = delete;
+
+    constexpr thdlist(thdlist&& obj, process* new_parent)
+        : thds { types::move(obj.thds) }
+    {
+        for (auto& thd : thds)
+            thd.owner = new_parent;
+    }
+
+    explicit constexpr thdlist(void)
+    {
+    }
+
+    // implementation is below
+    constexpr ~thdlist();
+
+    template <typename... Args>
+    constexpr thread& Emplace(Args&&... args)
+    {
+        return *thds.emplace_back(types::forward<Args>(args)...);
+    }
+
+    constexpr size_t size(void) const
+    {
+        return thds.size();
+    }
+};
+
 class process {
 public:
     mutable kernel::mm_list mms;
-    types::list<thread> thds;
+    thdlist thds;
     kernel::evtqueue wait_lst;
     process_attr attr;
     pid_t pid;
@@ -92,14 +143,18 @@ public:
 
 public:
     process(process&& val);
-    process(const process&) = delete;
-    process(const process& proc, const thread& main_thread);
+    process(const process&);
 
-    // only used for system initialization
-    explicit process(pid_t ppid);
-    explicit process(void (*func_in_kernel_space)(void), pid_t ppid);
+    explicit process(pid_t ppid, bool system = true);
 
-    ~process();
+    constexpr bool is_system(void) const
+    {
+        return attr.system;
+    }
+    constexpr bool is_zombie(void) const
+    {
+        return attr.zombie;
+    }
 
 private:
     static inline pid_t max_pid;
@@ -233,11 +288,6 @@ public:
     }
 };
 
-inline process* volatile current_process;
-inline thread* volatile current_thread;
-inline proclist* procs;
-inline readyqueue* readythds;
-
 extern "C" void NORETURN init_scheduler();
 void schedule(void);
 
@@ -248,4 +298,11 @@ constexpr uint32_t push_stack(uint32_t** stack, uint32_t val)
     return val;
 }
 
+// class thdlist
+constexpr thdlist::~thdlist()
+{
+    for (auto iter = thds.begin(); iter != thds.end(); ++iter)
+        readythds->remove_all(&iter);
+}
+
 void k_new_thread(void (*func)(void*), void* data);

+ 40 - 50
src/kernel/process.cpp

@@ -12,6 +12,7 @@
 #include <kernel_main.h>
 #include <types/allocator.hpp>
 #include <types/assert.h>
+#include <types/cplusplus.hpp>
 #include <types/elf.hpp>
 #include <types/hash_map.hpp>
 #include <types/list.hpp>
@@ -46,74 +47,38 @@ struct no_irq_guard {
 
 process::process(process&& val)
     : mms(types::move(val.mms))
-    , thds(types::move(val.thds))
+    , thds { types::move(val.thds), this }
     , wait_lst(types::move(val.wait_lst))
+    , attr { val.attr }
     , pid(val.pid)
     , ppid(val.ppid)
 {
     if (current_process == &val)
         current_process = this;
 
-    attr.system = val.attr.system;
-
-    for (auto& item : thds)
-        item.owner = this;
-
+    val.pid = 0;
+    val.ppid = 0;
     val.attr.system = 0;
+    val.attr.zombie = 0;
 }
 
-process::process(const process& val, const thread& main_thd)
-    : mms(*kernel_mms)
-    , attr { .system = val.attr.system }
-    , pid { process::alloc_pid() }
-    , ppid { val.pid }
+process::process(const process& parent)
+    : process { parent.pid, parent.is_system() }
 {
-    auto* thd = &thds.emplace_back(main_thd);
-    thd->owner = this;
-
-    for (auto& area : val.mms) {
+    for (auto& area : parent.mms) {
         if (area.is_ident())
             continue;
 
         mms.mirror_area(area);
     }
-
-    readythds->push(thd);
 }
 
-process::process(pid_t _ppid)
+process::process(pid_t _ppid, bool _system)
     : mms(*kernel_mms)
-    , attr { .system = 1 }
+    , attr { .system = _system }
     , pid { process::alloc_pid() }
     , ppid { _ppid }
 {
-    auto thd = thds.emplace_back(this, true);
-    readythds->push(&thd);
-}
-
-process::process(void (*func)(void), pid_t _ppid)
-    : process { _ppid }
-{
-    auto* esp = &thds.begin()->esp;
-
-    // return(start) address
-    push_stack(esp, (uint32_t)func);
-    // ebx
-    push_stack(esp, 0);
-    // edi
-    push_stack(esp, 0);
-    // esi
-    push_stack(esp, 0);
-    // ebp
-    push_stack(esp, 0);
-    // eflags
-    push_stack(esp, 0x200);
-}
-
-process::~process()
-{
-    for (auto iter = thds.begin(); iter != thds.end(); ++iter)
-        readythds->remove_all(&iter);
 }
 
 inline void NORETURN _noreturn_crash(void)
@@ -161,7 +126,30 @@ void kernel_threadd_main(void)
 
 void NORETURN _kernel_init(void)
 {
-    procs->emplace(kernel_threadd_main, 1);
+    // pid 2 is kernel thread daemon
+    auto* proc = &procs->emplace(1)->value;
+
+    // create thread
+    thread thd(proc, true);
+
+    auto* esp = &thd.esp;
+
+    // return(start) address
+    push_stack(esp, (uint32_t)kernel_threadd_main);
+    // ebx
+    push_stack(esp, 0);
+    // edi
+    push_stack(esp, 0);
+    // esi
+    push_stack(esp, 0);
+    // ebp
+    push_stack(esp, 0);
+    // eflags
+    push_stack(esp, 0x200);
+
+    readythds->push(&proc->thds.Emplace(types::move(thd)));
+
+    // ------------------------------------------
 
     asm_sti();
 
@@ -217,14 +205,16 @@ void NORETURN init_scheduler()
     procs = types::pnew<types::kernel_allocator>(procs);
     readythds = types::pnew<types::kernel_allocator>(readythds);
 
-    auto* init = &procs->emplace(1);
+    // init process has no parent
+    auto* init = &procs->emplace(0)->value;
 
     // we need interrupts enabled for cow mapping so now we disable it
     // in case timer interrupt mess things up
     asm_cli();
 
-    current_process = &init->value;
-    current_thread = &init->value.thds.begin();
+    current_process = init;
+    current_thread = &init->thds.Emplace(init, true);
+    readythds->push(current_thread);
 
     tss.ss0 = KERNEL_DATA_SEGMENT;
     tss.esp0 = current_thread->kstack;

+ 4 - 3
src/kernel/syscall.cpp

@@ -34,8 +34,9 @@ void _syscall_not_impl(interrupt_stack* data)
 extern "C" void _syscall_stub_fork_return(void);
 void _syscall_fork(interrupt_stack* data)
 {
-    auto* newproc = &procs->emplace(*current_process, *current_thread);
-    thread* newthd = &newproc->value.thds.begin();
+    auto* newproc = &procs->emplace(*current_process)->value;
+    auto* newthd = &newproc->thds.Emplace(*current_thread, newproc);
+    readythds->push(newthd);
 
     // create fake interrupt stack
     push_stack(&newthd->esp, data->ss);
@@ -69,7 +70,7 @@ void _syscall_fork(interrupt_stack* data)
     // eflags
     push_stack(&newthd->esp, 0);
 
-    SYSCALL_SET_RETURN_VAL(newproc->value.pid, 0);
+    SYSCALL_SET_RETURN_VAL(newproc->pid, 0);
 }
 
 void _syscall_write(interrupt_stack* data)