util: thread: Let thread use self-allocated memory 65/270465/2
authorDongwoo Lee <dwoo08.lee@samsung.com>
Thu, 3 Feb 2022 06:30:50 +0000 (15:30 +0900)
committerDongwoo Lee <dwoo08.lee@samsung.com>
Thu, 3 Feb 2022 06:30:50 +0000 (15:30 +0900)
Make easy to use thread without concerning about memory using thread

Change-Id: I170fd0afe772f47554dca95f3f61afd6b38e2921
Signed-off-by: Dongwoo Lee <dwoo08.lee@samsung.com>
include/util/thread.h
src/util/thread.c

index 6d63fb1840363a8ed44b9a7d3d0691d8b3dc8222..a806ead2159accebe8c3c6936656db0e5a18e3b6 100644 (file)
@@ -55,10 +55,10 @@ struct thread {
        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);
index 787c8e98754a5848e7f8a7f68473957b899bc82d..8bcef562325d1160bf2878c20cd4f22d027e3f7b 100644 (file)
@@ -74,13 +74,16 @@ void destroy_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;
@@ -88,10 +91,16 @@ static int do_create_thread(struct thread *thread,
        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;
@@ -122,8 +131,10 @@ static int do_create_thread(struct thread *thread,
                goto err;
        }
 
-       thread->id = tid;
-       thread->ctx = ctx;
+       new_thread->id = tid;
+       new_thread->ctx = ctx;
+
+       *thread = new_thread;
 
        return 0;
 
@@ -131,17 +142,19 @@ err:
        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)
 {
@@ -149,7 +162,7 @@ int create_timer_thread(struct thread *thread,
                                  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);
@@ -191,5 +204,7 @@ int wait_for_completion(struct thread *thread, void **result)
        free(thread->ctx);
        thread->ctx = NULL;
 
+       free(thread);
+
        return 0;
 }