agileRTOS (zrtos)  Version 0.8.0 (ghostbuster)
task_pthread.h File Reference
#include <zrtos/error.h>
#include <zrtos/malloc.h>
#include <zrtos/task_scheduler.h>
#include <zrtos/task_mutex.h>
#include <zrtos/types.h>
Include dependency graph for task_pthread.h:

Go to the source code of this file.

Data Structures

struct  pthread_attr_t
 
struct  pthread_t
 
struct  pthread_mutexattr_t
 
struct  pthread_mutex_t
 
struct  zrtos_task_pthread__trampoline_cb_args_t
 

Macros

#define PTHREAD_MUTEX_INITIALIZER
 

Functions

void * zrtos_task_pthread__get_heap (void)
 
void zrtos_task_pthread__set_heap (void *heap)
 
int pthread_attr_init (pthread_attr_t *attr)
 
int pthread_attr_destroy (pthread_attr_t *attr)
 
int pthread_attr_setstacksize (pthread_attr_t *attr, size_t stacksize)
 
int pthread_mutexattr_init (pthread_mutexattr_t *attr)
 
int pthread_mutexattr_destroy (pthread_mutexattr_t *attr)
 
int pthread_mutex_init (pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr)
 
int pthread_mutex_destroy (pthread_mutex_t *mutex)
 
pthread_t pthread_self (void)
 
void zrtos_task_pthread__trampoline_cb (void *args)
 
int pthread_create (pthread_t *restrict thread, const pthread_attr_t *restrict attr, void *(*start_routine)(void *), void *restrict arg)
 
int pthread_equal (pthread_t t1, pthread_t t2)
 
static void zrtos_task_pthread__free (zrtos_task_t *task)
 
int pthread_join (pthread_t thread, void **retval)
 
int pthread_mutex_lock (pthread_mutex_t *mutex)
 
int pthread_mutex_trylock (pthread_mutex_t *mutex)
 
int pthread_mutex_unlock (pthread_mutex_t *mutex)
 

Variables

void * zrtos_task_pthread__heap
 

Macro Definition Documentation

◆ PTHREAD_MUTEX_INITIALIZER

#define PTHREAD_MUTEX_INITIALIZER
Value:
{ \
}
#define ZRTOS_TASK_MUTEX__INITIALIZER
Definition: task_mutex.h:22

Definition at line 42 of file task_pthread.h.

Function Documentation

◆ pthread_attr_destroy()

int pthread_attr_destroy ( pthread_attr_t attr)

Definition at line 65 of file task_pthread.h.

65  {
66  return 0;
67 }

◆ pthread_attr_init()

int pthread_attr_init ( pthread_attr_t attr)

Definition at line 58 of file task_pthread.h.

58  {
61  ;
62  return 0;
63 }
#define ZRTOS_ARCH__GET_CPU_STATE_BUFFER_LENGTH()
Definition: atmega2560.h:192
size_t stacksize
Definition: task_pthread.h:22
#define ZRTOS_ARCH__GET_FN_CALL_STACK_LENGTH()
Definition: atmega2560.h:193

◆ pthread_attr_setstacksize()

int pthread_attr_setstacksize ( pthread_attr_t attr,
size_t  stacksize 
)

Definition at line 69 of file task_pthread.h.

69  {
70  attr->stacksize = stacksize;
71  return 0;
72 }
size_t stacksize
Definition: task_pthread.h:22

◆ pthread_create()

int pthread_create ( pthread_t *restrict  thread,
const pthread_attr_t *restrict  attr,
void *(*)(void *)  start_routine,
void *restrict  arg 
)

Definition at line 117 of file task_pthread.h.

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 }
#define ZRTOS_TASK_SCHEDULER__DO_NOT_DISTURB(code)
uint8_t zrtos_arch_stack_t
Definition: atmega2560.h:19
#define ZRTOS_TYPES__MAX(a, b)
Definition: types.h:79
void zrtos_task_pthread__trampoline_cb(void *args)
Definition: task_pthread.h:100
#define ZRTOS_ARCH__GET_CPU_STATE_BUFFER_LENGTH()
Definition: atmega2560.h:192
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
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
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
Out of memory.
Definition: error.h:36
size_t stacksize
Definition: task_pthread.h:22
struct _zrtos_task_t zrtos_task_t
#define ZRTOS_ARCH__GET_FN_CALL_STACK_LENGTH()
Definition: atmega2560.h:193
zrtos_task_t * task
Definition: task_pthread.h:26
Here is the call graph for this function:

◆ pthread_equal()

int pthread_equal ( pthread_t  t1,
pthread_t  t2 
)

Definition at line 171 of file task_pthread.h.

171  {
172  return zrtos_types__ptr_cmp(t1.task,t1.task);
173 }
int zrtos_types__ptr_cmp(void *a, void *b)
Definition: types.h:31
zrtos_task_t * task
Definition: task_pthread.h:26
Here is the call graph for this function:

◆ pthread_join()

int pthread_join ( pthread_t  thread,
void **  retval 
)

Definition at line 191 of file task_pthread.h.

191  {
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 }
#define ZRTOS_TASK_SCHEDULER__DO_NOT_DISTURB(code)
static void zrtos_task_pthread__free(zrtos_task_t *task)
Definition: task_pthread.h:175
Try again.
Definition: error.h:35
bool zrtos_task__is_done(zrtos_task_t *thiz)
Definition: task.h:98
bool zrtos_task_scheduler__has_task(zrtos_task_t *task)
zrtos_task_t * task
Definition: task_pthread.h:26
static void _zrtos_task_scheduler__on_tick_ex(void)
Here is the call graph for this function:

◆ pthread_mutex_destroy()

int pthread_mutex_destroy ( pthread_mutex_t mutex)

Definition at line 89 of file task_pthread.h.

89  {
91  return 0;
92 }
zrtos_task_mutex_t mutex
Definition: task_pthread.h:33
void zrtos_task_mutex__deinit(zrtos_task_mutex_t *thiz)
Definition: task_mutex.h:29
Here is the call graph for this function:

◆ pthread_mutex_init()

int pthread_mutex_init ( pthread_mutex_t *restrict  mutex,
const pthread_mutexattr_t *restrict  attr 
)

Definition at line 82 of file task_pthread.h.

85  {
86  return zrtos_task_mutex__init(&mutex->mutex) ? 0 : ZRTOS_ERROR__INVAL;
87 }
zrtos_task_mutex_t mutex
Definition: task_pthread.h:33
bool zrtos_task_mutex__init(zrtos_task_mutex_t *thiz)
Definition: task_mutex.h:24
Invalid argument.
Definition: error.h:46
Here is the call graph for this function:

◆ pthread_mutex_lock()

int pthread_mutex_lock ( pthread_mutex_t mutex)

Definition at line 223 of file task_pthread.h.

223  {
224  return zrtos_task_mutex__lock(&mutex->mutex);
225 }
zrtos_task_mutex_t mutex
Definition: task_pthread.h:33
int zrtos_task_mutex__lock(zrtos_task_mutex_t *thiz)
Definition: task_mutex.h:44
Here is the call graph for this function:

◆ pthread_mutex_trylock()

int pthread_mutex_trylock ( pthread_mutex_t mutex)

Definition at line 227 of file task_pthread.h.

227  {
228  return zrtos_task_mutex__try_lock(&mutex->mutex);
229 }
zrtos_task_mutex_t mutex
Definition: task_pthread.h:33
int zrtos_task_mutex__try_lock(zrtos_task_mutex_t *thiz)
Definition: task_mutex.h:33
Here is the call graph for this function:

◆ pthread_mutex_unlock()

int pthread_mutex_unlock ( pthread_mutex_t mutex)

Definition at line 231 of file task_pthread.h.

231  {
232  return zrtos_task_mutex__unlock(&mutex->mutex);
233 }
zrtos_task_mutex_t mutex
Definition: task_pthread.h:33
int zrtos_task_mutex__unlock(zrtos_task_mutex_t *thiz)
Definition: task_mutex.h:62
Here is the call graph for this function:

◆ pthread_mutexattr_destroy()

int pthread_mutexattr_destroy ( pthread_mutexattr_t attr)

Definition at line 78 of file task_pthread.h.

78  {
79  return 0;
80 }

◆ pthread_mutexattr_init()

int pthread_mutexattr_init ( pthread_mutexattr_t attr)

Definition at line 74 of file task_pthread.h.

74  {
75  return 0;
76 }

◆ pthread_self()

pthread_t pthread_self ( void  )

Definition at line 94 of file task_pthread.h.

94  {
95  pthread_t ret;
97  return ret;
98 }
zrtos_task_t * _zrtos_task_scheduler__get_active_task(void)
zrtos_task_t * task
Definition: task_pthread.h:26
Here is the call graph for this function:

◆ zrtos_task_pthread__free()

static void zrtos_task_pthread__free ( zrtos_task_t task)
static

Definition at line 175 of file task_pthread.h.

175  {
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 }
bool zrtos_task_scheduler__remove_task(zrtos_task_t *task)
void zrtos_malloc__free(void *ptr)
Definition: malloc.h:220
static zrtos_task_t * zrtos_task_scheduler__get_any_child(zrtos_task_t *task)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ zrtos_task_pthread__get_heap()

void* zrtos_task_pthread__get_heap ( void  )

Definition at line 50 of file task_pthread.h.

50  {
52 }
void * zrtos_task_pthread__heap
Definition: task_pthread.h:48
Here is the caller graph for this function:

◆ zrtos_task_pthread__set_heap()

void zrtos_task_pthread__set_heap ( void *  heap)

Definition at line 54 of file task_pthread.h.

54  {
56 }
void * zrtos_task_pthread__heap
Definition: task_pthread.h:48

◆ zrtos_task_pthread__trampoline_cb()

void zrtos_task_pthread__trampoline_cb ( void *  args)

Definition at line 100 of file task_pthread.h.

100  {
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 }
void * zrtos_types__ptr_subtract(void *ptr, size_t byte_len)
Definition: types.h:39
void zrtos_task__set_done(zrtos_task_t *thiz)
Definition: task.h:110
static void _zrtos_task_scheduler__on_tick_ex(void)
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ zrtos_task_pthread__heap

void* zrtos_task_pthread__heap

Definition at line 48 of file task_pthread.h.