748019990ea58b9ef532dcb42c24f2108c7034dc
[monolithium.git] / kernel / include / thread.h
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
20 #ifndef _THREAD_H_
21 #define _THREAD_H_
22
23 #include <common.h>
24 #include <object.h>
25 #include <exception.h>
26 #include <sdk/list.h>
27 #include <lock.h>
28 #include <interrupt.h>
29 #include <syscalls.h>
30 #include <sdk/thread.h>
31
32 #define QUANTUM 30
33 #define MAX_THREADS 2097152
34 #define KERNEL_STACK_SIZE 262144
35
36 #define SAFE_EFLAGS_MASK 0x00000CD5
37
38 typedef enum
39 {
40     WAIT_ALWAYS,
41     WAIT_GROUP_ANY,
42     WAIT_GROUP_ALL,
43     WAIT_UNTIL_EQUAL,
44     WAIT_UNTIL_NOT_EQUAL,
45     WAIT_UNTIL_LESS,
46     WAIT_UNTIL_NOT_LESS,
47     WAIT_UNTIL_GREATER,
48     WAIT_UNTIL_NOT_GREATER
49 } wait_condition_type_t;
50
51 typedef enum
52 {
53     WAIT_TIMED_OUT,
54     WAIT_CONDITION_HIT,
55     WAIT_CANCELED
56 } wait_result_t;
57
58 typedef struct wait_condition
59 {
60     wait_condition_type_t type;
61
62     union
63     {
64         struct
65         {
66             dword_t *pointer;
67             dword_t value;
68         };
69
70         struct wait_condition *conditions[VARIABLE_SIZE];
71     };
72 } wait_condition_t;
73
74 typedef struct
75 {
76     wait_condition_t *root;
77     timeout_t timeout;
78     qword_t timestamp;
79     wait_result_t result;
80 } wait_t;
81
82 #ifndef PROCESS_TYPEDEF
83 #define PROCESS_TYPEDEF
84 typedef struct process process_t;
85 #endif
86
87 struct thread
88 {
89     object_t header;
90     list_entry_t in_queue_list;
91     list_entry_t in_process_list;
92     dword_t tid;
93     priority_t priority;
94     dword_t affinity;
95     thread_state_t state;
96     dword_t exit_code;
97     dword_t quantum;
98     qword_t running_ticks;
99     process_t *owner_process; /* weak reference */
100     dword_t terminated;
101     int32_t frozen;
102
103     void *kernel_stack;
104     uintptr_t kernel_esp;
105
106     int in_kernel;
107     registers_t *last_context;
108     bool_t terminating;
109     processor_mode_t previous_mode;
110
111     wait_t *wait;
112
113     exception_handler_t kernel_handler;
114     exception_info_t kernel_exception_info;
115     exception_handler_t user_handler;
116     exception_info_t user_exception_info;
117 };
118
119 #ifndef THREAD_TYPEDEF
120 #define THREAD_TYPEDEF
121 typedef struct thread thread_t;
122 #endif
123
124 extern bool_t scheduler_enabled;
125
126 thread_t *get_current_thread();
127 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);
128 dword_t terminate_thread_internal(thread_t *thread, dword_t return_value);
129 void scheduler(registers_t *regs);
130 wait_result_t scheduler_wait(wait_condition_t *condition, dword_t timeout);
131 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);
132 void thread_lazy_fpu(void);
133 void thread_cleanup(object_t *thread);
134 dword_t thread_pre_wait(object_t *obj, void *parameter, wait_condition_t *condition);
135 void thread_init(void);
136
137 #endif