Skip to content

Commit 2933eb1

Browse files
committed
thread: wrap the user supplied start function
This will allow us to mangle its return value and otherwise sanity check the state (e.g., verifying that no locks are held). Signed-off-by: Josef 'Jeff' Sipek <[email protected]>
1 parent 93528c3 commit 2933eb1

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

thread.c

+34-1
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,44 @@
2121
*/
2222

2323
#include <jeffpc/thread.h>
24+
#include <jeffpc/error.h>
25+
#include <jeffpc/mem.h>
26+
27+
struct xthr_info {
28+
void *(*f)(void *);
29+
void *arg;
30+
};
31+
32+
static void *xthr_setup(void *_info)
33+
{
34+
struct xthr_info info = *((struct xthr_info *) _info);
35+
36+
/* free early since the function may run for a very long time */
37+
free(_info);
38+
39+
return info.f(info.arg);
40+
}
2441

2542
int xthr_create(pthread_t *restrict thread, void *(*start)(void*),
2643
void *restrict arg)
2744
{
45+
struct xthr_info *info;
2846
pthread_t tmp;
47+
int ret;
48+
49+
if (!start)
50+
return -EINVAL;
51+
52+
info = malloc(sizeof(struct xthr_info));
53+
if (!info)
54+
return -ENOMEM;
55+
56+
info->f = start;
57+
info->arg = arg;
58+
59+
ret = -pthread_create(thread ? thread : &tmp, NULL, xthr_setup, info);
60+
if (ret)
61+
free(info);
2962

30-
return -pthread_create(thread ? thread : &tmp, NULL, start, arg);
63+
return ret;
3164
}

0 commit comments

Comments
 (0)