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.

83 lines
2.5 KiB

пре 4 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 4 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 4 година
пре 3 година
пре 4 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
  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. /**
  12. * @brief Represent the different execution states a process can be in
  13. */
  14. enum class process_status {
  15. inactive = 0,
  16. running = 1,
  17. waiting = 2,
  18. finished = 3,
  19. zombie = 4
  20. };
  21. /**
  22. * @brief A process ID type
  23. */
  24. using pid_t = size_t;
  25. /**
  26. * @brief Represent and manages all the process info the scheduler doesn't need to be aware of
  27. *
  28. * Anything thread local is part of that
  29. */
  30. struct base_process_info {
  31. virtual void initialize() {} //< An initializer
  32. virtual void checkpoint() {} //< Creates a checkpoint if the feature is available
  33. virtual void restore() {} //< Restores a checkpoint if the feature is available
  34. virtual void switch_in() {} //< Called as the process is resuming
  35. virtual void switch_out() {} //< Called as the process is paused
  36. virtual void cleanup() {} //< Called before the process get destroyed
  37. virtual ~base_process_info() {} //< Called as the process is destroyed
  38. };
  39. /**
  40. * @brief Represents the data used for scheduling and managing a process resources.
  41. */
  42. struct process_data{
  43. pid_t pid; //< The current pid
  44. gp::function<void()> fn; //< Whatever there is to execute
  45. void* stack; //< The stack lower bound pointer
  46. size_t stack_sz; //< The stack size in bytes
  47. gp::system::process_status state; //< The current process' state
  48. std::atomic_bool is_running; //< Whenever the process is currently using execution time
  49. [[no_unique_address]] gp::system::specifics::platform_data specifics; //< The special bits used to handle context switching
  50. gp::unique_ptr<base_process_info> info; //< Other data related to processes but not to scheduling
  51. process_data(gp::function<void()> _fn, void* _stack, size_t _stack_sz, gp::unique_ptr<base_process_info>&& _info)
  52. : fn(_fn)
  53. , stack(_stack)
  54. , stack_sz(_stack_sz)
  55. , state(gp::system::process_status::inactive)
  56. , specifics(gp::buffer<char>{(char*)stack, stack_sz})
  57. , info(gp::move(_info))
  58. {}
  59. process_data(process_data&& v)
  60. : fn(v.fn)
  61. , stack(v.stack)
  62. , stack_sz(v.stack_sz)
  63. , state(v.state)
  64. , specifics(v.specifics)
  65. , info(gp::move(v.info))
  66. {}
  67. ~process_data() {
  68. if(info) {
  69. info->cleanup();
  70. }
  71. }
  72. };
  73. }
  74. }