diff --git a/include/sys/threads.h b/include/sys/threads.h index 04d47197..7eac584a 100644 --- a/include/sys/threads.h +++ b/include/sys/threads.h @@ -77,10 +77,7 @@ __attribute__((noreturn)) extern void endthread(void); -static inline int beginthread(void (*start)(void *), unsigned int priority, void *stack, unsigned int stacksz, void *arg) -{ - return beginthreadex(start, priority, stack, stacksz, arg, NULL); -} +extern int beginthread(void (*start)(void *), unsigned int priority, void *stack, unsigned int stacksz, void *arg); extern int threadsinfo(int n, threadinfo_t *info); diff --git a/sys/threads.c b/sys/threads.c index b3545325..df7e3e07 100644 --- a/sys/threads.c +++ b/sys/threads.c @@ -15,6 +15,7 @@ #include #include +#include int mutexCreate(handle_t *h) @@ -79,3 +80,41 @@ int mutexLock2(handle_t m1, handle_t m2) return EOK; } + + +typedef struct { + void *arg; + void (*start)(void *); +} wrapperArgs_t; + + +static void wrapper(void *arg) +{ + wrapperArgs_t *ctx = (wrapperArgs_t *)arg; + + ctx->start(ctx->arg); + + free(ctx); + endthread(); +} + + +int beginthread(void (*start)(void *), unsigned int priority, void *stack, unsigned int stacksz, void *arg) +{ + wrapperArgs_t *ctx; + int ret; + + ctx = malloc(sizeof(wrapperArgs_t)); + if (ctx == NULL) { + return -ENOMEM; + } + ctx->start = start; + ctx->arg = arg; + + ret = beginthreadex(wrapper, priority, stack, stacksz, ctx, NULL); + if (ret < 0) { + free(ctx); + } + + return ret; +}