2 Copyright (C) 2008 Mathias Gottschlag
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in the
6 Software without restriction, including without limitation the rights to use,
7 copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
8 Software, and to permit persons to whom the Software is furnished to do so,
9 subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16 PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
18 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
19 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 * Thread management functions
26 #ifndef KE_THREAD_H_INCLUDED
27 #define KE_THREAD_H_INCLUDED
29 #include "ke/process.h"
31 #define KE_THREAD_RUNNING 0
32 #define KE_THREAD_SLEEP 1
33 #define KE_THREAD_FSREQUEST 2
34 #define KE_THREAD_KILLED 3
35 #define KE_THREAD_WAIT 4
36 #define KE_THREAD_INUSE 5
39 * Structure containing the information about a thread.
41 typedef struct KeThread
44 * Bottom address of the kernel stack
45 * \todo This is not yet freed when the kernel is destroyed.
47 uintptr_t kernelstack_bottom
;
49 * Current kernel stack top
51 uintptr_t kernelstack
;
53 * Bottom address of the user stack (if allocated).
57 * Size of the user stack.
59 uintptr_t userstack_size
;
62 * User stack frame on the kernel stack, 0, if this is a kernel thread.
67 * Current status of the thread.
71 * Timeout variable used for sleeping/waiting etc.
76 * Process which this thread belongs to.
81 * If this lock is locked, the thread is currently being executed on a CPU.
87 * Creates a user thread in the given process.
88 * \param process Process into which the thread will be inserted.
89 * \param entry Entry point for the thread
90 * \param paramcount Number of parameters passed to the entry function. The
91 * parameters are passed after this number.
93 KeThread
*keCreateThread(KeProcess
*process
, uintptr_t entry
, uint32_t paramcount
, ...);
95 * Stops and destroys a thread.
97 int keDestroyThread(KeThread
*thread
);
100 * Clones a thread in another process. No memory is copied by this, only the raw
101 * process is copied using an existing user stack.
102 * \param process Process for the new thread.
103 * \param thread Thread in another process which is cloned.
104 * \param stack kernel stack of the thread being cloned.
106 KeThread
*keCloneThread(KeProcess
*process
, KeThread
*thread
, uintptr_t stack
);
109 * Creates a new kernel thread.
110 * \param entry Pointer to the entry function.
111 * \param paramcount Number of parameters passed to the function.
113 KeThread
*keCreateKernelThread(uintptr_t entry
, uint32_t paramcount
, ...);
115 * Creates a new idle thread. The idle thread just executes
116 * "while (1) asm("hlt");".
118 KeThread
*keCreateIdleThread(void);
121 * Sets a syscall parameter in the given thread.
122 * \param thread Thread to be modified.
123 * \param index Index of the syscall parameter.
124 * \param value New value of the parameter.
126 void keThreadSetParam(KeThread
*thread
, uint8_t index
, uint32_t value
);
128 * Returns a syscall parameter in the given thread.
129 * \param thread Thread to be modified.
130 * \param index Index of the syscall parameter.
132 uint32_t keThreadGetParam(KeThread
*thread
, uint8_t index
);
135 * Returns a thread which can be currently executed and locks it.
136 * \param oldthread If set to 1, this function returns the current thread if
137 * there is no other thread available.
139 KeThread
*keGetSchedulableThread(int oldthread
);
142 * Sleeps for a certain amount of time.
143 * \param ms Time in milliseconds
145 void keSleep(uint32_t ms
);
147 * Stops the currently running thread.
149 void keExitThread(void);