os/kernel/tasking.h

123 lines
4.2 KiB
C
Raw Normal View History

2020-07-25 16:54:37 -05:00
/**
* \file
*/
2020-07-22 19:26:55 -05:00
#ifndef KERN_TASKING_H
#define KERN_TASKING_H
2019-02-11 09:30:28 -06:00
#include <stdint.h>
2020-07-22 19:35:23 -05:00
#include <sys/types.h>
2019-02-11 09:30:28 -06:00
2020-07-12 14:28:58 -05:00
#ifndef TASKING_H
2020-07-22 19:26:55 -05:00
2020-07-25 16:54:37 -05:00
/**
* Represents the state of a thread
*/
2020-07-25 18:00:53 -05:00
typedef enum thread_state {
2020-07-25 16:54:37 -05:00
THREAD_RUNNING, //!< The state of a running thread
THREAD_READY, //!< The state of a ready to run thread
THREAD_EXITED, //!< The state of an exited thread
THREAD_BLOCKED //!< The state of a generically blocked thread
2020-07-25 18:00:53 -05:00
} thread_state;
2020-07-22 19:26:55 -05:00
2020-07-12 14:28:58 -05:00
#endif
2020-07-20 09:51:30 -05:00
struct Thread;
2020-07-25 16:54:37 -05:00
/**
* Represents a process
*/
2020-07-20 09:51:30 -05:00
typedef struct Process {
2020-07-25 16:54:37 -05:00
char priv; //!< Whether the process is privileged (can execute syscalls to acesss all of memory/has acess to IO ports).
pid_t pid; //!< The PID of this process
pid_t next_tid; //!< The TID that the next created thread will use.
2020-07-28 06:50:26 -05:00
int num_threads; //!< The number of threads in this process
int num_threads_blocked; //!< The number of blocked threads in this process
struct Thread* first_thread; //!< A pointer to the head of the linked list of threads for this process.
2020-07-20 09:51:30 -05:00
} Process;
2020-07-25 16:54:37 -05:00
/**
* Represents a thread of a process
*/
2020-07-20 09:51:30 -05:00
typedef struct Thread {
2020-07-25 16:54:37 -05:00
void* kernel_esp; //!< The thread's kernel stack.
void* kernel_esp_top; //!< The top of the thread's kernel stack.
void* address_space; //!< The address space of this thread. (it is in here and not in the process to simplify the task switch asembly)
2020-07-25 16:54:37 -05:00
pid_t tid; //!< The TID of this thread.
2020-07-25 18:00:53 -05:00
thread_state state; //!< The state of this thread. (running,ready to run,blocked,etc.)
2020-07-25 16:54:37 -05:00
int errno; //!< The errno value for this thread.
2020-07-28 06:50:26 -05:00
struct Thread* next_thread_in_process; //!< The next thread in the process.
struct Thread* prev_thread_in_process; //!< The previous thread in the process.
struct Thread* next_ready_to_run; //!< If the thread is in the ready to run list, this is the next ready to run thread. (potentially in a different process)
struct Thread* prev_ready_to_run; //!< If the thread is in the ready to run list, this is the previous ready to run thread. (potentially in a different process)
2020-07-25 16:54:37 -05:00
Process* process; //!< The thread's process.
2020-07-20 09:51:30 -05:00
} Thread;
2019-02-11 09:30:28 -06:00
2020-07-25 18:00:53 -05:00
extern Thread* current_thread;
2020-07-22 19:26:55 -05:00
2020-07-25 16:54:37 -05:00
/**
* Create a task
* \param eip The start address of the task
* \param address_space The address space of the task
2020-07-25 16:54:37 -05:00
* \param kmode Whether the task is a kernel mode task
* \param param1_exists Whether param1_arg is a valid value
* \param param1_arg The thread's start function first parameter
* \param param2_exists Whether param2_arg is a valid value
* \param param2_arg The thread's start function second parameter/
* \param isThread Whether we are creating a new process or a thread in a process. If we are creating a theead, param2_arg becomes the PID for the newly created thread, and param2_exists must be 0.
*/
void tasking_create_task(void* eip,void* address_space,char kmode,char param1_exists,void* param1_arg,char param2_exists,void* param2_arg,char isThread);
2020-07-25 16:54:37 -05:00
/**
* Initialize tasking
*/
2020-07-22 19:26:55 -05:00
void tasking_init();
2020-07-25 16:54:37 -05:00
/**
* Check whether the current process is privleged
2020-07-25 18:01:05 -05:00
* \return whether the current process is privleged
2020-07-25 16:54:37 -05:00
*/
2020-07-25 18:00:53 -05:00
char tasking_is_privleged();
2020-07-25 16:54:37 -05:00
/**
* Get the PID of the current thread.
2020-07-25 18:01:05 -05:00
* \return The current thread's PID
2020-07-25 16:54:37 -05:00
*/
2020-07-25 18:00:53 -05:00
pid_t tasking_get_PID();
2020-07-25 16:54:37 -05:00
/**
* Get the adddress of errno for the current thread
2020-07-25 18:01:05 -05:00
* \return The address of errno
2020-07-25 16:54:37 -05:00
*/
2020-07-22 19:26:55 -05:00
int* tasking_get_errno_address();
2020-07-25 16:54:37 -05:00
/**
* Create a new thread
* \param start The start address of the task
* \param pid The PID that gets the new thread
* \param param_exists Whether param_arg is a valid value
* \param param_arg The thread's start function parameter
* \return the TID of the thread
*/
2020-07-23 11:50:23 -05:00
pid_t tasking_new_thread(void* start,pid_t pid,char param_exists,void* param_arg);
2020-07-22 19:26:55 -05:00
2020-07-25 16:54:37 -05:00
/**
* Terminate the current thread
* If the main thread terminates, the whole process terminates.
* \note Currently, calling tasking_exit from any thread terminates the whole process.
2020-07-25 18:01:05 -05:00
* \param code The exit code of the thread
2020-07-25 16:54:37 -05:00
*/
2020-07-23 11:50:23 -05:00
void tasking_exit(int code);
2020-07-25 16:54:37 -05:00
/**
* Block the current thread & yield
* \param newstate The state to block it in
*/
2020-07-25 18:00:53 -05:00
void tasking_block(thread_state newstate);
2020-07-25 16:54:37 -05:00
/**
* Unblock a thread
* \param pid The PID that contains the thread to unblock
* \param tid The TID in the process to unblock.
*/
2020-07-23 11:50:23 -05:00
void tasking_unblock(pid_t pid,pid_t tid);
2020-07-25 16:54:37 -05:00
/**
* Yield to the next ready thread in any process
*/
2020-07-22 19:26:55 -05:00
void tasking_yield();
2019-02-11 09:30:28 -06:00
#endif