process.hpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. #pragma once
  2. #include "types/map.hpp"
  3. #include "types/pair.hpp"
  4. #include <kernel/event/evtqueue.hpp>
  5. #include <kernel/interrupt.h>
  6. #include <kernel/mm.hpp>
  7. #include <kernel/task.h>
  8. #include <types/cplusplus.hpp>
  9. #include <types/hash_map.hpp>
  10. #include <types/list.hpp>
  11. #include <types/stdint.h>
  12. #include <types/types.h>
  13. typedef size_t pid_t;
  14. class process;
  15. struct thread;
  16. class proclist;
  17. class readyqueue;
  18. inline process* volatile current_process;
  19. inline thread* volatile current_thread;
  20. inline proclist* procs;
  21. inline readyqueue* readythds;
  22. struct process_attr {
  23. uint16_t system : 1;
  24. uint16_t zombie : 1 = 0;
  25. };
  26. struct thread_attr {
  27. uint32_t system : 1;
  28. uint32_t ready : 1;
  29. uint32_t wait : 1;
  30. };
  31. struct thread {
  32. private:
  33. inline void alloc_kstack(void)
  34. {
  35. // TODO: alloc low mem
  36. kstack = to_pp(alloc_n_raw_pages(2));
  37. kstack += THREAD_KERNEL_STACK_SIZE;
  38. esp = reinterpret_cast<uint32_t*>(kstack);
  39. }
  40. public:
  41. uint32_t* esp;
  42. pptr_t kstack;
  43. process* owner;
  44. thread_attr attr;
  45. explicit inline thread(process* _owner, bool system)
  46. : owner { _owner }
  47. , attr {
  48. .system = system,
  49. .ready = 1,
  50. .wait = 0,
  51. }
  52. {
  53. alloc_kstack();
  54. }
  55. constexpr thread(thread&& val)
  56. : esp { val.esp }
  57. , kstack { val.kstack }
  58. , owner { val.owner }
  59. , attr { val.attr }
  60. {
  61. val.attr = {};
  62. val.esp = 0;
  63. val.kstack = 0;
  64. val.owner = nullptr;
  65. }
  66. inline thread(const thread& val)
  67. : owner { val.owner }
  68. , attr { val.attr }
  69. {
  70. alloc_kstack();
  71. }
  72. inline thread(const thread& thd, process* new_parent)
  73. : thread { thd }
  74. {
  75. owner = new_parent;
  76. }
  77. constexpr ~thread()
  78. {
  79. if (kstack)
  80. free_n_raw_pages(to_page(kstack), 2);
  81. }
  82. };
  83. class thdlist {
  84. private:
  85. types::list<thread> thds;
  86. public:
  87. constexpr thdlist(const thdlist& obj) = delete;
  88. constexpr thdlist(thdlist&& obj) = delete;
  89. constexpr thdlist& operator=(const thdlist& obj) = delete;
  90. constexpr thdlist& operator=(thdlist&& obj) = delete;
  91. constexpr thdlist(thdlist&& obj, process* new_parent)
  92. : thds { types::move(obj.thds) }
  93. {
  94. for (auto& thd : thds)
  95. thd.owner = new_parent;
  96. }
  97. explicit constexpr thdlist(void)
  98. {
  99. }
  100. // implementation is below
  101. constexpr ~thdlist();
  102. template <typename... Args>
  103. constexpr thread& Emplace(Args&&... args)
  104. {
  105. return *thds.emplace_back(types::forward<Args>(args)...);
  106. }
  107. constexpr size_t size(void) const
  108. {
  109. return thds.size();
  110. }
  111. };
  112. class process {
  113. public:
  114. mutable kernel::mm_list mms;
  115. thdlist thds;
  116. kernel::evtqueue wait_lst;
  117. process_attr attr;
  118. pid_t pid;
  119. pid_t ppid;
  120. public:
  121. process(process&& val);
  122. process(const process&);
  123. explicit process(pid_t ppid, bool system = true);
  124. constexpr bool is_system(void) const
  125. {
  126. return attr.system;
  127. }
  128. constexpr bool is_zombie(void) const
  129. {
  130. return attr.zombie;
  131. }
  132. private:
  133. static inline pid_t max_pid;
  134. static inline pid_t alloc_pid(void)
  135. {
  136. return ++max_pid;
  137. }
  138. };
  139. class proclist final {
  140. public:
  141. using list_type = types::map<pid_t, process>;
  142. using child_index_type = types::hash_map<pid_t, types::list<pid_t>, types::linux_hasher<pid_t>>;
  143. using iterator_type = list_type::iterator_type;
  144. using const_iterator_type = list_type::const_iterator_type;
  145. private:
  146. list_type m_procs;
  147. child_index_type m_child_idx;
  148. public:
  149. template <typename... Args>
  150. iterator_type emplace(Args&&... args)
  151. {
  152. process _proc(types::forward<Args>(args)...);
  153. auto pid = _proc.pid;
  154. auto ppid = _proc.ppid;
  155. auto iter = m_procs.insert(types::make_pair(pid, types::move(_proc)));
  156. auto children = m_child_idx.find(ppid);
  157. if (!children) {
  158. m_child_idx.emplace(ppid, types::list<pid_t> {});
  159. children = m_child_idx.find(ppid);
  160. }
  161. children->value.push_back(pid);
  162. return iter;
  163. }
  164. constexpr void remove(pid_t pid)
  165. {
  166. make_children_orphans(pid);
  167. auto proc_iter = m_procs.find(pid);
  168. auto ppid = proc_iter->value.ppid;
  169. auto& parent_children = m_child_idx.find(ppid)->value;
  170. auto i = parent_children.find(pid);
  171. parent_children.erase(i);
  172. m_procs.erase(proc_iter);
  173. }
  174. constexpr process* find(pid_t pid)
  175. {
  176. return &m_procs.find(pid)->value;
  177. }
  178. constexpr bool has_child(pid_t pid)
  179. {
  180. auto children = m_child_idx.find(pid);
  181. return children && !children->value.empty();
  182. }
  183. constexpr void make_children_orphans(pid_t pid)
  184. {
  185. auto children = m_child_idx.find(pid);
  186. if (children) {
  187. auto init_children = m_child_idx.find(1);
  188. for (auto iter = children->value.begin(); iter != children->value.end(); ++iter) {
  189. init_children->value.push_back(*iter);
  190. this->find(*iter)->ppid = 1;
  191. }
  192. m_child_idx.remove(children);
  193. }
  194. }
  195. };
  196. class readyqueue final {
  197. public:
  198. using list_type = types::list<thread*>;
  199. using iterator_type = list_type::iterator_type;
  200. using const_iterator_type = list_type::const_iterator_type;
  201. private:
  202. list_type m_thds;
  203. private:
  204. readyqueue(const readyqueue&) = delete;
  205. readyqueue(readyqueue&&) = delete;
  206. readyqueue& operator=(const readyqueue&) = delete;
  207. readyqueue& operator=(readyqueue&&) = delete;
  208. ~readyqueue() = delete;
  209. public:
  210. constexpr explicit readyqueue(void) = default;
  211. constexpr void push(thread* thd)
  212. {
  213. m_thds.push_back(thd);
  214. }
  215. constexpr thread* pop(void)
  216. {
  217. auto iter = m_thds.begin();
  218. while (!((*iter)->attr.ready))
  219. iter = m_thds.erase(iter);
  220. auto* ptr = *iter;
  221. m_thds.erase(iter);
  222. return ptr;
  223. }
  224. constexpr thread* query(void)
  225. {
  226. auto* thd = this->pop();
  227. this->push(thd);
  228. return thd;
  229. }
  230. constexpr void remove_all(thread* thd)
  231. {
  232. auto iter = m_thds.find(thd);
  233. while (iter != m_thds.end()) {
  234. m_thds.erase(iter);
  235. iter = m_thds.find(thd);
  236. }
  237. }
  238. };
  239. extern "C" void NORETURN init_scheduler();
  240. void schedule(void);
  241. constexpr uint32_t push_stack(uint32_t** stack, uint32_t val)
  242. {
  243. --*stack;
  244. **stack = val;
  245. return val;
  246. }
  247. // class thdlist
  248. constexpr thdlist::~thdlist()
  249. {
  250. for (auto iter = thds.begin(); iter != thds.end(); ++iter)
  251. readythds->remove_all(&iter);
  252. }
  253. void k_new_thread(void (*func)(void*), void* data);