PatchworkOS
Loading...
Searching...
No Matches
thrd_create.c
Go to the documentation of this file.
1#include <stdatomic.h>
2#include <stdbool.h>
3#include <stdio.h>
4#include <stdlib.h>
5#include <sys/proc.h>
6#include <threads.h>
7
10
12
13typedef struct
14{
16 void* arg;
18
20{
21 _entry_ctx_t* ctx = thread->private;
22 thrd_start_t func = ctx->func;
23 void* arg = ctx->arg;
24
25 free(ctx);
26
27 thrd_exit(func(arg));
28}
29
30int thrd_create(thrd_t* thr, thrd_start_t func, void* arg)
31{
32 if (klog == ERR)
33 {
34 klog = openf("/dev/klog");
35 if (klog == ERR)
36 {
37 return thrd_error;
38 }
39 }
40
41 _entry_ctx_t* ctx = malloc(sizeof(_entry_ctx_t));
42 if (ctx == NULL)
43 {
44 return thrd_error;
45 }
46 ctx->func = func;
47 ctx->arg = arg;
48
49 _thread_t* thread = _thread_new(_thread_entry, ctx);
50 if (thread == NULL)
51 {
52 free(ctx);
53 return thrd_error;
54 }
55
56 thr->id = thread->id;
57 return thrd_success;
58}
fd_t openf(const char *_RESTRICT format,...)
Wrapper for opening files with a formatted path.
Definition openf.c:9
#define NULL
Pointer error value.
Definition NULL.h:23
#define ERR
Integer error value.
Definition ERR.h:17
__UINT64_TYPE__ fd_t
A file descriptor.
Definition fd_t.h:12
_thread_t * _thread_new(_thread_entry_t entry, void *private)
Definition thread.c:33
#define _THREAD_ENTRY_ATTRIBUTES
Definition thread.h:12
_PUBLIC void * malloc(size_t size)
Definition malloc.c:5
_PUBLIC void free(void *ptr)
Definition free.c:11
Definition thrd_create.c:14
void * arg
Definition thrd_create.c:16
thrd_start_t func
Definition thrd_create.c:15
tid_t id
Definition thread.h:27
void * private
Definition thread.h:30
tid_t id
Definition threads.h:38
int thrd_create(thrd_t *thr, thrd_start_t func, void *arg)
Definition thrd_create.c:30
static _THREAD_ENTRY_ATTRIBUTES void _thread_entry(_thread_t *thread)
Definition thrd_create.c:19
fd_t klog
Definition thrd_create.c:11
int(* thrd_start_t)(void *)
Definition threads.h:60
@ thrd_success
Definition threads.h:74
@ thrd_error
Definition threads.h:76
_PUBLIC _NORETURN void thrd_exit(int res)
Definition thrd_exit.c:11