struct thread_context *ctx;
};
-int create_daemon_thread(struct thread *thread, int (*func)(void *, void **), void *arg);
-int create_timer_thread(struct thread *thread, u_int32_t timer_expire_msec,
+int create_daemon_thread(struct thread **thread, int (*func)(void *, void **), void *arg);
+int create_timer_thread(struct thread **thread, u_int32_t timer_expire_msec,
int (*func)(void *, void **), void *arg);
-int create_worker_thread(struct thread *thread, int (*func)(void *, void **), void *arg);
+int create_worker_thread(struct thread **thread, int (*func)(void *, void **), void *arg);
void destroy_thread(struct thread *thread);
void suspend_thread(struct thread *thread);
void resume_thread(struct thread *thread);
do_destroy_thread(thread);
free(thread->ctx);
thread->ctx = NULL;
+
+ free(thread);
}
-static int do_create_thread(struct thread *thread,
+static int do_create_thread(struct thread **thread,
enum thread_type type,
u_int32_t timer_expire_msec,
int (*func)(void *, void **), void *arg)
{
+ struct thread *new_thread;
struct thread_context *ctx;
thrd_t tid;
int ret;
if (!thread || !func)
return -EINVAL;
- ctx = calloc(1, sizeof(struct thread_context));
- if (!ctx)
+ new_thread = malloc(sizeof(struct thread));
+ if (!new_thread)
return -ENOMEM;
+ ctx = calloc(1, sizeof(struct thread_context));
+ if (!ctx) {
+ ret = -ENOMEM;
+ goto err_malloc;
+ }
+
mtx_init(&ctx->lock, mtx_plain);
cnd_init(&ctx->wait);
ctx->func = func;
goto err;
}
- thread->id = tid;
- thread->ctx = ctx;
+ new_thread->id = tid;
+ new_thread->ctx = ctx;
+
+ *thread = new_thread;
return 0;
cnd_destroy(&ctx->wait);
mtx_destroy(&ctx->lock);
free(ctx);
+err_malloc:
+ free(new_thread);
return ret;
}
-int create_daemon_thread(struct thread *thread,
+int create_daemon_thread(struct thread **thread,
int (*func)(void *, void **), void *arg)
{
return do_create_thread(thread, THREAD_TYPE_DAEMON, 0, func, arg);
}
-int create_timer_thread(struct thread *thread,
+int create_timer_thread(struct thread **thread,
u_int32_t timer_expire_msec,
int (*func)(void *, void **), void *arg)
{
timer_expire_msec, func, arg);
}
-int create_worker_thread(struct thread *thread,
+int create_worker_thread(struct thread **thread,
int (*func)(void *, void **), void *arg)
{
return do_create_thread(thread, THREAD_TYPE_WORKER, 0, func, arg);
free(thread->ctx);
thread->ctx = NULL;
+ free(thread);
+
return 0;
}