agileRTOS (zrtos)  Version 0.8.0 (ghostbuster)
task_pthread.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2024 ykat UG (haftungsbeschraenkt) - All Rights Reserved
3  *
4  * Permission for non-commercial use is hereby granted,
5  * free of charge, without warranty of any kind.
6  */
7 #ifndef ZRTOS_VHEAP_TASK_PTHREAD_H
8 #define ZRTOS_VHEAP_TASK_PTHREAD_H
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 
13 
14 #include <zrtos/error.h>
15 #include <zrtos/malloc.h>
16 #include <zrtos/task_scheduler.h>
17 #include <zrtos/task_mutex.h>
18 #include <zrtos/types.h>
19 
20 
21 typedef struct{
22  size_t stacksize;
24 
25 typedef struct{
27 }pthread_t;
28 
29 typedef struct{
31 
32 typedef struct{
35 
36 typedef struct{
37  void *(*callback)(void*args);
38  void *args;
39  void *return_value;
41 
42 #define PTHREAD_MUTEX_INITIALIZER \
43  { \
44  .mutex = ZRTOS_TASK_MUTEX__INITIALIZER \
45  }
46 
47 
49 
52 }
53 
55  zrtos_task_pthread__heap = heap;
56 }
57 
61  ;
62  return 0;
63 }
64 
66  return 0;
67 }
68 
69 int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize){
70  attr->stacksize = stacksize;
71  return 0;
72 }
73 
75  return 0;
76 }
77 
79  return 0;
80 }
81 
83  pthread_mutex_t *restrict mutex
84  ,const pthread_mutexattr_t *restrict attr
85 ){
86  return zrtos_task_mutex__init(&mutex->mutex) ? 0 : ZRTOS_ERROR__INVAL;
87 }
88 
91  return 0;
92 }
93 
95  pthread_t ret;
97  return ret;
98 }
99 
100 __attribute__((noreturn)) void zrtos_task_pthread__trampoline_cb(void *args){
103  tmp_args
104  ,sizeof(zrtos_task_t)
105  );;
106  tmp_args->return_value = tmp_args->callback(tmp_args->args);
107  zrtos_task__set_done(task);
108 
110 
111  __builtin_unreachable();
112  while(1){
113  //should not return
114  }
115 }
116 
118  pthread_t *restrict thread
119  ,const pthread_attr_t *restrict attr
120  ,void *(*start_routine)(void *)
121  ,void *restrict arg
122 ){
123  int ret = ZRTOS_ERROR__NOMEM;
124 
126  size_t stack_size_min = ZRTOS_ARCH__GET_CPU_STATE_BUFFER_LENGTH()
128  ;
130  size_t stacksize_min = sizeof(zrtos_task_t)
132  + (
133  attr
135  stack_size_min
136  ,attr->stacksize
137  )
138  : stack_size_min
139  );
141  mem
142  ,stacksize_min
143  );
144  if(task){
145  void *mem_chunk_last_address = zrtos_types__ptr_add(
146  task
147  ,stacksize_min-1
148  );
150  task
151  ,sizeof(zrtos_task_t)
152  );
153  args->callback = start_routine;
154  args->args = arg;
156  task
157  ,(zrtos_arch_stack_t*)mem_chunk_last_address
159  ,args
160  );
162 
163  thread->task = task;
164  ret = 0;
165  }
166  });
167 
168  return ret;
169 }
170 
172  return zrtos_types__ptr_cmp(t1.task,t1.task);
173 }
174 
176  zrtos_task_t *child;
177 
178  while((child = zrtos_task_scheduler__get_any_child(task))){
179  zrtos_task_t *last = child;
180  while((child = zrtos_task_scheduler__get_any_child(child))){
181  last = child;
182  }
184  zrtos_malloc__free(last);
185  }
186 
188  zrtos_malloc__free(task);
189 }
190 
191 int pthread_join(pthread_t thread, void **retval){
192  int ret = ZRTOS_ERROR__AGAIN;
193  zrtos_task_t *task = thread.task;
194 
195  while(true){
198  if(zrtos_task__is_done(task)){
201  ;
202  if(retval){
203  *retval = args->return_value;
204  }
206  ret = 0;
207  }
208  }else{
209  ret = ESRCH;
210  }
211  });
212 
213  if(ret == ZRTOS_ERROR__AGAIN){
215  }else{
216  break;
217  }
218  }
219 
220  return ret;
221 }
222 
224  return zrtos_task_mutex__lock(&mutex->mutex);
225 }
226 
228  return zrtos_task_mutex__try_lock(&mutex->mutex);
229 }
230 
232  return zrtos_task_mutex__unlock(&mutex->mutex);
233 }
234 
235 
236 #ifdef __cplusplus
237 }
238 #endif
239 #endif
#define ZRTOS_TASK_SCHEDULER__DO_NOT_DISTURB(code)
int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr)
Definition: task_pthread.h:82
static void zrtos_task_pthread__free(zrtos_task_t *task)
Definition: task_pthread.h:175
zrtos_task_mutex_t mutex
Definition: task_pthread.h:33
uint8_t zrtos_arch_stack_t
Definition: atmega2560.h:19
void * zrtos_types__ptr_subtract(void *ptr, size_t byte_len)
Definition: types.h:39
Try again.
Definition: error.h:35
#define ZRTOS_TYPES__MAX(a, b)
Definition: types.h:79
bool zrtos_task_scheduler__remove_task(zrtos_task_t *task)
int zrtos_task_mutex__try_lock(zrtos_task_mutex_t *thiz)
Definition: task_mutex.h:33
void zrtos_task_pthread__trampoline_cb(void *args)
Definition: task_pthread.h:100
int pthread_mutexattr_init(pthread_mutexattr_t *attr)
Definition: task_pthread.h:74
int pthread_mutex_unlock(pthread_mutex_t *mutex)
Definition: task_pthread.h:231
void zrtos_task_pthread__set_heap(void *heap)
Definition: task_pthread.h:54
#define ZRTOS_ARCH__GET_CPU_STATE_BUFFER_LENGTH()
Definition: atmega2560.h:192
void * zrtos_task_pthread__heap
Definition: task_pthread.h:48
void zrtos_task_mutex__deinit(zrtos_task_mutex_t *thiz)
Definition: task_mutex.h:29
bool zrtos_task_scheduler__add_task(zrtos_task_t *task)
void * zrtos_malloc__malloc(zrtos_malloc_t *thiz, size_t length)
Definition: malloc.h:142
void zrtos_malloc__free(void *ptr)
Definition: malloc.h:220
int zrtos_task_mutex__unlock(zrtos_task_mutex_t *thiz)
Definition: task_mutex.h:62
pthread_t pthread_self(void)
Definition: task_pthread.h:94
bool zrtos_task__init_ex(zrtos_task_t *thiz, zrtos_arch_stack_t *heap, zrtos_arch_callback_t callback, void *args)
Definition: task.h:58
bool zrtos_task__is_done(zrtos_task_t *thiz)
Definition: task.h:98
int pthread_attr_init(pthread_attr_t *attr)
Definition: task_pthread.h:58
static zrtos_task_t * zrtos_task_scheduler__get_any_child(zrtos_task_t *task)
int zrtos_task_mutex__lock(zrtos_task_mutex_t *thiz)
Definition: task_mutex.h:44
void * zrtos_types__ptr_add(void *ptr, size_t byte_len)
Definition: types.h:35
void * zrtos_task_pthread__get_heap(void)
Definition: task_pthread.h:50
int pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
Definition: task_pthread.h:78
int zrtos_types__ptr_cmp(void *a, void *b)
Definition: types.h:31
int pthread_mutex_lock(pthread_mutex_t *mutex)
Definition: task_pthread.h:223
int pthread_equal(pthread_t t1, pthread_t t2)
Definition: task_pthread.h:171
bool zrtos_task_mutex__init(zrtos_task_mutex_t *thiz)
Definition: task_mutex.h:24
int pthread_attr_destroy(pthread_attr_t *attr)
Definition: task_pthread.h:65
int pthread_create(pthread_t *restrict thread, const pthread_attr_t *restrict attr, void *(*start_routine)(void *), void *restrict arg)
Definition: task_pthread.h:117
Invalid argument.
Definition: error.h:46
Out of memory.
Definition: error.h:36
void zrtos_task__set_done(zrtos_task_t *thiz)
Definition: task.h:110
int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize)
Definition: task_pthread.h:69
size_t stacksize
Definition: task_pthread.h:22
int pthread_mutex_trylock(pthread_mutex_t *mutex)
Definition: task_pthread.h:227
zrtos_task_t * _zrtos_task_scheduler__get_active_task(void)
bool zrtos_task_scheduler__has_task(zrtos_task_t *task)
struct _zrtos_task_t zrtos_task_t
int pthread_mutex_destroy(pthread_mutex_t *mutex)
Definition: task_pthread.h:89
#define ZRTOS_ARCH__GET_FN_CALL_STACK_LENGTH()
Definition: atmega2560.h:193
int pthread_join(pthread_t thread, void **retval)
Definition: task_pthread.h:191
zrtos_task_t * task
Definition: task_pthread.h:26
static void _zrtos_task_scheduler__on_tick_ex(void)