Skip to content

Commit

Permalink
thread: add mtx_alloc (#396)
Browse files Browse the repository at this point in the history
  • Loading branch information
sreimers authored Jun 10, 2022
1 parent c2b9985 commit 79a6168
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
12 changes: 11 additions & 1 deletion include/re_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
34 changes: 34 additions & 0 deletions src/thread/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,40 @@
#include <re_thread.h>


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)
{
Expand Down

0 comments on commit 79a6168

Please sign in to comment.