AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
thread.c
Go to the documentation of this file.
1
7
8#include <core/system.h>
9#include <core/arch.h>
10#include <core/panic.h>
11#include <sys/proc.h>
12#include <sys/sched.h>
13#include <sys/signal.h>
14#include <ds/queue.h>
15
16MALLOC_DEFINE(M_THREAD, "thread", "thread structure");
17
27int thread_new(struct proc *proc, struct thread **ref)
28{
29 struct thread *thread = NULL;
30
31 if (!proc)
32 return -EINVAL;
33
34 thread = kmalloc(sizeof(struct thread), &M_THREAD, M_ZERO);
35 if (!thread) return -ENOMEM;
36
37 thread->owner = proc;
38 thread->tid = ++proc->tid_last;
39
40 if (!enqueue(&proc->threads, thread)) {
42 return -ENOMEM;
43 }
44
45 if (ref)
46 *ref = thread;
47
48 return 0;
49}
50
60{
61 if (!thread)
62 return -EINVAL;
63
64 /* release its resources */
68 return 0;
69}
70
86int thread_create(struct thread *thread, uintptr_t stack, uintptr_t entry, uintptr_t uentry, uintptr_t arg, uintptr_t attr __unused, struct thread **new_thread)
87{
88 int err = 0;
89 struct thread *t = NULL;
90
91 if ((err = thread_new(thread->owner, &t)))
92 return err;
93
94 if ((err = arch_thread_create(t, stack, entry, uentry, arg))) {
95 queue_remove(&thread->owner->threads, t);
96 kfree(t);
97 return err;
98 }
99
100 if (new_thread)
101 *new_thread = t;
102
103 return 0;
104}
int arch_thread_create(struct thread *thread, uintptr_t stack, uintptr_t entry, uintptr_t uentry, uintptr_t arg)
Sets up a new user thread's CPU state and kernel stack, for pthread_create().
Definition thread.c:92
void arch_thread_kill(struct thread *thread)
Frees a thread's CPU state and kernel stack.
Definition thread.c:133
#define MALLOC_DEFINE(type, _name, _desc)
Definition kvmem.h:30
void * kmalloc(size_t size, struct malloc_type *type, int flags)
Allocates memory for one of the kernel's structures.
Definition kvmem.c:123
#define M_ZERO
Definition kvmem.h:26
void kfree(void *)
Frees an object that kmalloc() gave.
Definition kvmem.c:146
#define NULL
Definition stddef.h:27
__SIZE_TYPE__ uintptr_t
Definition stddef.h:12
A process.
Definition proc.h:75
int tid_last
the last thread ID given out (threads leave in any order, so their count can't be the next one's ID)
Definition proc.h:140
struct queue threads
its threads
Definition proc.h:136
A thread.
Definition thread.h:23
state_t state
its state
Definition thread.h:28
tid_t tid
the thread's ID, in its process
Definition thread.h:25
uintptr_t stack
its user stack
Definition thread.h:39
struct proc * owner
its process
Definition thread.h:31
int thread_kill(struct thread *thread)
Releases what a thread holds (its poll() registrations, its CPU state and kernel stack) and makes it ...
Definition thread.c:59
int thread_create(struct thread *thread, uintptr_t stack, uintptr_t entry, uintptr_t uentry, uintptr_t arg, uintptr_t attr, struct thread **new_thread)
Makes a new thread in the process of an existing one, for pthread_create().
Definition thread.c:86
int thread_new(struct proc *proc, struct thread **rthread)
Makes a new thread in a process.
Definition thread.c:27
@ ZOMBIE
Definition thread.h:19
void poll_unregister(struct thread *thread)
Removes every registration of a thread, when it stops waiting (it was woken up, or it is going away).
Definition waitq.c:144