1
0

thread.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * thread.h
  3. *
  4. * Copyright (C) 2018 Aleksandar Andrejevic <theflash@sdf.lonestar.org>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as
  8. * published by the Free Software Foundation, either version 3 of the
  9. * License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef _THREAD_H_
  20. #define _THREAD_H_
  21. #include <common.h>
  22. #include <object.h>
  23. #include <exception.h>
  24. #include <sdk/list.h>
  25. #include <lock.h>
  26. #include <interrupt.h>
  27. #include <syscalls.h>
  28. #include <sdk/thread.h>
  29. #define QUANTUM 30
  30. #define MAX_THREADS 2097152
  31. #define KERNEL_STACK_SIZE 0x40000
  32. #define SAFE_EFLAGS_MASK 0x00000CD5
  33. typedef enum
  34. {
  35. WAIT_ALWAYS,
  36. WAIT_GROUP_ANY,
  37. WAIT_GROUP_ALL,
  38. WAIT_UNTIL_EQUAL,
  39. WAIT_UNTIL_NOT_EQUAL,
  40. WAIT_UNTIL_LESS,
  41. WAIT_UNTIL_NOT_LESS,
  42. WAIT_UNTIL_GREATER,
  43. WAIT_UNTIL_NOT_GREATER
  44. } wait_condition_type_t;
  45. typedef enum
  46. {
  47. WAIT_TIMED_OUT,
  48. WAIT_CONDITION_HIT,
  49. WAIT_CANCELED
  50. } wait_result_t;
  51. typedef struct wait_condition
  52. {
  53. wait_condition_type_t type;
  54. union
  55. {
  56. struct
  57. {
  58. dword_t *pointer;
  59. dword_t value;
  60. };
  61. struct wait_condition *conditions[VARIABLE_SIZE];
  62. };
  63. } wait_condition_t;
  64. typedef struct
  65. {
  66. wait_condition_t *root;
  67. timeout_t timeout;
  68. qword_t timestamp;
  69. wait_result_t result;
  70. } wait_t;
  71. #ifndef PROCESS_TYPEDEF
  72. #define PROCESS_TYPEDEF
  73. typedef struct process process_t;
  74. #endif
  75. struct thread
  76. {
  77. object_t header;
  78. list_entry_t in_queue_list;
  79. list_entry_t in_process_list;
  80. dword_t tid;
  81. priority_t priority;
  82. dword_t affinity;
  83. thread_state_t state;
  84. dword_t exit_code;
  85. dword_t quantum;
  86. qword_t running_ticks;
  87. process_t *owner_process; /* weak reference */
  88. dword_t terminated;
  89. int32_t frozen;
  90. void *kernel_stack;
  91. uintptr_t kernel_esp;
  92. int in_kernel;
  93. registers_t *last_context;
  94. bool_t terminating;
  95. processor_mode_t previous_mode;
  96. wait_t *wait;
  97. exception_handler_t kernel_handler;
  98. exception_info_t kernel_exception_info;
  99. exception_handler_t user_handler;
  100. exception_info_t user_exception_info;
  101. };
  102. #ifndef THREAD_TYPEDEF
  103. #define THREAD_TYPEDEF
  104. typedef struct thread thread_t;
  105. #endif
  106. extern bool_t scheduler_enabled;
  107. thread_t *get_current_thread();
  108. dword_t create_thread_internal(process_t *proc, thread_state_t *initial_state, dword_t flags, priority_t priority, void *kernel_stack, thread_t **new_thread);
  109. dword_t terminate_thread_internal(thread_t *thread, dword_t return_value);
  110. void scheduler(registers_t *regs);
  111. wait_result_t scheduler_wait(wait_condition_t *condition, dword_t timeout);
  112. dword_t create_system_thread(thread_procedure_t routine, dword_t flags, priority_t priority, dword_t stack_size, void *param, thread_t **new_thread);
  113. void thread_lazy_fpu(void);
  114. void thread_cleanup(object_t *thread);
  115. dword_t thread_pre_wait(object_t *obj, void *parameter, wait_condition_t *condition);
  116. void thread_init(void);
  117. #endif