PatchworkOS
Loading...
Searching...
No Matches
thread.c
Go to the documentation of this file.
1#include "thread.h"
2#include "syscalls.h"
3
4#include <stdlib.h>
5
7
9static mtx_t mutex;
10
11static void _thread_init(_thread_t* thread)
12{
13 list_entry_init(&thread->entry);
15 thread->id = 0;
16 thread->result = 0;
17 thread->err = EOK;
18 thread->private = NULL;
19}
20
22{
25
26 // We cant yet use the heap yet
29
31}
32
34{
35 _thread_t* thread = malloc(sizeof(_thread_t));
36 if (thread == NULL)
37 {
38 return NULL;
39 }
40
41 _thread_init(thread);
42 thread->private = private;
43
45
46 thread->id = _syscall_thread_create(entry, thread);
47 if (thread->id == ERR)
48 {
51 free(thread);
52 return NULL;
53 }
54
55 list_push(&threads, &thread->entry);
57
58 return thread;
59}
60
62{
64 list_remove(&threads, &thread->entry);
66 if (thread != &thread0)
67 {
68 free(thread);
69 }
70}
71
73{
75 _thread_t* thread;
76 LIST_FOR_EACH(thread, &threads, entry)
77 {
78 if (thread->id == id)
79 {
81 return thread;
82 }
83 }
84
86 return NULL;
87}
#define errno
Error number variable.
Definition errno.h:27
#define EOK
No error.
Definition errno.h:32
#define LIST_FOR_EACH(elem, list, member)
Iterates over a list.
Definition list.h:65
static void list_remove(list_t *list, list_entry_t *entry)
Removes a list entry from its current list.
Definition list.h:317
static void list_push(list_t *list, list_entry_t *entry)
Pushes an entry to the end of the list.
Definition list.h:345
static void list_entry_init(list_entry_t *entry)
Initializes a list entry.
Definition list.h:184
static void list_init(list_t *list)
Initializes a list.
Definition list.h:198
#define NULL
Pointer error value.
Definition NULL.h:23
#define ERR
Integer error value.
Definition ERR.h:17
__UINT64_TYPE__ tid_t
Thread Identifier.
Definition tid_t.h:12
void _threading_init(void)
Definition thread.c:21
static mtx_t mutex
Definition thread.c:9
static _thread_t thread0
Definition thread.c:6
static list_t threads
Definition thread.c:8
static void _thread_init(_thread_t *thread)
Definition thread.c:11
_thread_t * _thread_get(tid_t id)
Definition thread.c:72
_thread_t * _thread_new(_thread_entry_t entry, void *private)
Definition thread.c:33
void _thread_free(_thread_t *thread)
Definition thread.c:61
static tid_t _syscall_thread_create(void *entry, void *arg)
Definition syscalls.h:213
static tid_t _syscall_gettid(void)
Definition syscalls.h:128
static errno_t _syscall_errno(void)
Definition syscalls.h:118
#define _THREAD_ATTACHED
Definition thread.h:18
void(* _thread_entry_t)(_thread_t *)
Definition thread.h:16
#define atomic_init(obj, value)
Definition stdatomic.h:75
_PUBLIC void * malloc(size_t size)
Definition malloc.c:5
_PUBLIC void free(void *ptr)
Definition free.c:11
tid_t id
Definition thread.h:27
int result
Definition thread.h:28
list_entry_t entry
Definition thread.h:25
errno_t err
Definition thread.h:29
atomic_uint64_t state
Definition thread.h:26
void * private
Definition thread.h:30
A doubly linked list.
Definition list.h:51
_PUBLIC int mtx_lock(mtx_t *mtx)
Definition mtx_lock.c:11
_PUBLIC int mtx_init(mtx_t *mtx, int type)
Definition mtx_init.c:10
_PUBLIC int mtx_unlock(mtx_t *mtx)
Definition mtx_unlock.c:10
@ mtx_recursive
Definition threads.h:67