diff --git a/include/re_thread.h b/include/re_thread.h index 59dda8480..cff6e4782 100644 --- a/include/re_thread.h +++ b/include/re_thread.h @@ -255,11 +255,21 @@ void tss_delete(tss_t key); /****************************************************************************** - * Extra + * Extra - non C11 helpers *****************************************************************************/ /* int thrd_prio(enum thrd_prio prio) */ /* void thrd_print(struct re_printf *pf, void *unused); */ +/** + * Allocates and initializes a new mutex + * + * @param mtx Pointer to new mutex + * + * @return 0 if success, otherwise errorcode + */ +int mtx_alloc(mtx_t **mtx); + + /** * Creates a new thread with name * diff --git a/src/thread/thread.c b/src/thread/thread.c index 6d2b4c261..41d06af25 100644 --- a/src/thread/thread.c +++ b/src/thread/thread.c @@ -3,6 +3,40 @@ #include +static void mtx_destructor(void *data) +{ + mtx_t *mtx = data; + + mtx_destroy(mtx); +} + + +int mtx_alloc(mtx_t **mtx) +{ + mtx_t *m; + int err; + + if (!mtx) + return EINVAL; + + m = mem_alloc(sizeof(mtx_t), mtx_destructor); + if (!m) + return ENOMEM; + + err = mtx_init(m, mtx_plain); + if (err) + goto out; + + *mtx = m; + +out: + if (err) + mem_deref(m); + + return err; +} + + int thrd_create_name(thrd_t *thr, const char *name, thrd_start_t func, void *arg) {