2 * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
24 #include <sys/syscall.h>
25 #include <sys/types.h>
29 #include "launchpad_worker.h"
30 #include "log_private.h"
33 worker_job_cb callback;
44 static struct worker_s __worker;
46 int _worker_add_job(worker_job_cb callback, void *user_data)
51 _E("Invalid parameter");
55 job = malloc(sizeof(struct job_s));
61 job->callback = callback;
62 job->user_data = user_data;
64 g_mutex_lock(&__worker.mutex);
65 g_queue_push_tail(__worker.queue, job);
66 g_cond_signal(&__worker.cond);
67 g_mutex_unlock(&__worker.mutex);
72 static int __set_comm(const char *name)
75 ssize_t bytes_written;
77 pid_t tid = syscall(__NR_gettid);
79 _I("[%s] TID(%d)", name, tid);
80 snprintf(path, sizeof(path), "/proc/%d/comm", tid);
81 fd = open(path, O_WRONLY);
83 _E("Failed to open %s. error(%d)", path, errno);
87 bytes_written = write(fd, name, strlen(name) + 1);
88 if (bytes_written < 0) {
89 _E("Failed to write name(%s)", name);
98 static gpointer __worker_thread_cb(gpointer data)
100 struct worker_s *worker = (struct worker_s *)data;
104 __set_comm("worker");
106 g_mutex_lock(&worker->mutex);
107 if (g_queue_is_empty(worker->queue))
108 g_cond_wait(&worker->cond, &worker->mutex);
110 job = (struct job_s *)g_queue_pop_head(worker->queue);
111 g_mutex_unlock(&worker->mutex);
112 done = job->callback(job->user_data);
119 int _worker_init(void)
123 g_mutex_init(&__worker.mutex);
124 g_cond_init(&__worker.cond);
126 __worker.queue = g_queue_new();
127 if (!__worker.queue) {
128 _E("g_queue_new() is failed");
132 __worker.thread = g_thread_new("worker", __worker_thread_cb, &__worker);
133 if (!__worker.thread) {
134 _E("g_thread_new() is failed");
141 static bool __worker_done_cb(void *user_data)
147 void _worker_fini(void)
151 if (__worker.thread) {
152 _worker_add_job(__worker_done_cb, NULL);
153 g_thread_join(__worker.thread);
157 g_queue_free_full(__worker.queue, (GDestroyNotify)free);
159 g_cond_clear(&__worker.cond);
160 g_mutex_clear(&__worker.mutex);