General Purpose library for Freestanding C++ and POSIX systems
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
1.5 KiB

  1. #pragma once
  2. #include "gp_config.hpp"
  3. #include "gp/functional/function.hpp"
  4. #include "gp/containers/indexed_array.hpp"
  5. #include "gp/utils/pointers.hpp"
  6. #include "gp/ipc/file_description.hpp"
  7. #include "gp/system/platforms/platform_autopicker.hpp"
  8. #include <atomic>
  9. namespace gp {
  10. namespace system {
  11. enum class process_status {
  12. inactive = 0,
  13. running = 1,
  14. waiting = 2,
  15. finished = 3,
  16. zombie = 4
  17. };
  18. using pid_t = size_t;
  19. struct base_process_info {
  20. virtual void initialize() {}
  21. virtual void checkpoint() {}
  22. virtual void restore() {}
  23. virtual void switch_in() {}
  24. virtual void switch_out() {}
  25. virtual void cleanup() {}
  26. virtual ~base_process_info() {}
  27. };
  28. struct process_data{
  29. pid_t pid;
  30. gp::function<void()> fn;
  31. void* stack;
  32. size_t stack_sz;
  33. gp::system::process_status state;
  34. std::atomic_bool is_running;
  35. [[no_unique_address]] gp::system::specifics::platform_data specifics;
  36. gp::unique_ptr<base_process_info> info;
  37. process_data(gp::function<void()> _fn, void* _stack, size_t _stack_sz, gp::unique_ptr<base_process_info>&& _info)
  38. : fn(_fn)
  39. , stack(_stack)
  40. , stack_sz(_stack_sz)
  41. , state(gp::system::process_status::inactive)
  42. , specifics(gp::buffer<char>{(char*)stack, stack_sz})
  43. , info(gp::move(_info))
  44. {}
  45. process_data(process_data&& v)
  46. : fn(v.fn)
  47. , stack(v.stack)
  48. , stack_sz(v.stack_sz)
  49. , state(v.state)
  50. , specifics(v.specifics)
  51. , info(gp::move(v.info))
  52. {}
  53. ~process_data() {
  54. if(info) {
  55. info->cleanup();
  56. }
  57. }
  58. };
  59. }
  60. }