AquilaOS documentation
How it works, and its code
Toggle main menu visibility
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
16
MALLOC_DEFINE
(M_THREAD,
"thread"
,
"thread structure"
);
17
27
int
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
)) {
41
kfree
(
thread
);
42
return
-ENOMEM;
43
}
44
45
if
(ref)
46
*ref =
thread
;
47
48
return
0;
49
}
50
59
int
thread_kill
(
struct
thread
*
thread
)
60
{
61
if
(!
thread
)
62
return
-EINVAL;
63
64
/* release its resources */
65
poll_unregister
(
thread
);
66
arch_thread_kill
(
thread
);
67
thread
->
state
=
ZOMBIE
;
68
return
0;
69
}
70
86
int
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
}
arch_thread_create
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
arch_thread_kill
void arch_thread_kill(struct thread *thread)
Frees a thread's CPU state and kernel stack.
Definition
thread.c:133
arch.h
panic.h
system.h
MALLOC_DEFINE
#define MALLOC_DEFINE(type, _name, _desc)
Definition
kvmem.h:30
kmalloc
void * kmalloc(size_t size, struct malloc_type *type, int flags)
Allocates memory for one of the kernel's structures.
Definition
kvmem.c:123
M_ZERO
#define M_ZERO
Definition
kvmem.h:26
kfree
void kfree(void *)
Frees an object that kmalloc() gave.
Definition
kvmem.c:146
proc.h
queue.h
sched.h
signal.h
NULL
#define NULL
Definition
stddef.h:27
uintptr_t
__SIZE_TYPE__ uintptr_t
Definition
stddef.h:12
proc
A process.
Definition
proc.h:75
proc::tid_last
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
proc::threads
struct queue threads
its threads
Definition
proc.h:136
thread
A thread.
Definition
thread.h:23
thread::state
state_t state
its state
Definition
thread.h:28
thread::tid
tid_t tid
the thread's ID, in its process
Definition
thread.h:25
thread::stack
uintptr_t stack
its user stack
Definition
thread.h:39
thread::owner
struct proc * owner
its process
Definition
thread.h:31
thread_kill
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
thread_create
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
thread_new
int thread_new(struct proc *proc, struct thread **rthread)
Makes a new thread in a process.
Definition
thread.c:27
ZOMBIE
@ ZOMBIE
Definition
thread.h:19
poll_unregister
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
kernel
sys
thread.c
Generated by
1.18.0