mirror of
https://github.com/clearlinux/micro-config-drive.git
synced 2026-08-28 09:27:04 +00:00
2c7ba9a934
The project has been renamed from clr-cloud-init to clearly communicate the scope and target of the project. This change simply ensures all the source files are now consistent in their agreement of the project name. The (C) usage was also swapped for ©, simply because it is the proper symbol and looks better. Signed-off-by: Ikey Doherty <michael.i.doherty@intel.com>
164 lines
4.1 KiB
C
164 lines
4.1 KiB
C
/***
|
|
Copyright © 2015 Intel Corporation
|
|
|
|
Author: Julio Montes <julio.montes@intel.com>
|
|
|
|
This file is part of micro-config-drive.
|
|
|
|
micro-config-drive is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
micro-config-drive is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with micro-config-drive. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
In addition, as a special exception, the copyright holders give
|
|
permission to link the code of portions of this program with the
|
|
OpenSSL library under certain conditions as described in each
|
|
individual source file, and distribute linked combinations
|
|
including the two.
|
|
You must obey the GNU General Public License in all respects
|
|
for all of the code used other than OpenSSL. If you modify
|
|
file(s) with this exception, you may extend this exception to your
|
|
version of the file(s), but you are not obligated to do so. If you
|
|
do not wish to do so, delete this exception statement from your
|
|
version. If you delete this exception statement from all source
|
|
files in the program, then also delete it here.
|
|
***/
|
|
|
|
#include <stdlib.h>
|
|
#include <sys/sysinfo.h>
|
|
|
|
#include "async_task.h"
|
|
#include "lib.h"
|
|
|
|
#define MOD "async_task: "
|
|
|
|
static GThreadPool* thread_pool = NULL;
|
|
static GMainLoop* main_loop = NULL;
|
|
static guint tasks = 0;
|
|
|
|
G_LOCK_DEFINE(thread_pool);
|
|
G_LOCK_DEFINE(tasks);
|
|
|
|
struct async_task_data {
|
|
GThreadFunc func;
|
|
gpointer data;
|
|
};
|
|
|
|
static void async_task_callback(GPid pid, gint status, __unused__ gpointer null) {
|
|
LOG("PID %d ends, exit status %d\n", pid, status);
|
|
|
|
g_spawn_close_pid(pid);
|
|
|
|
--tasks;
|
|
if (0 == tasks) {
|
|
LOG(MOD "Quit main loop\n");
|
|
g_main_loop_quit(main_loop);
|
|
}
|
|
}
|
|
|
|
static void async_task_run_task(struct async_task_data* data, __unused__ gpointer null) {
|
|
if (data && data->func) {
|
|
data->func(data->data);
|
|
}
|
|
|
|
g_free(data);
|
|
}
|
|
|
|
bool async_task_init(void) {
|
|
main_loop = g_main_loop_new(NULL, 0);
|
|
if (!main_loop) {
|
|
LOG(MOD "Cannot create a new main loop\n");
|
|
return false;
|
|
}
|
|
|
|
thread_pool = g_thread_pool_new((GFunc)async_task_run_task, NULL, get_nprocs(), true, NULL);
|
|
if (!thread_pool) {
|
|
LOG(MOD "Cannot create a new thread pool\n");
|
|
g_main_loop_unref(main_loop);
|
|
main_loop = NULL;
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
bool async_task_run(GThreadFunc func, gpointer data) {
|
|
GError *error = NULL;
|
|
struct async_task_data* task_data = g_malloc(sizeof(struct async_task_data));
|
|
task_data->func = func;
|
|
task_data->data = data;
|
|
|
|
G_LOCK(thread_pool);
|
|
if (!thread_pool) {
|
|
LOG(MOD "Error finish task was called and thread pool is null \n");
|
|
G_UNLOCK(thread_pool);
|
|
return false;
|
|
}
|
|
g_thread_pool_push(thread_pool, task_data, &error);
|
|
G_UNLOCK(thread_pool);
|
|
|
|
if (error) {
|
|
LOG(MOD "Error pushing a new thread: %s\n", error->message);
|
|
g_error_free(error);
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
bool async_task_exec(const gchar* command) {
|
|
GPid pid = 0;
|
|
GError *error = NULL;
|
|
gchar* command_line = g_strescape(command, NULL);
|
|
gchar* argvp[] = {SHELL_PATH, "-c", command_line, NULL };
|
|
|
|
g_spawn_async(NULL, argvp, NULL,
|
|
G_SPAWN_DO_NOT_REAP_CHILD,
|
|
NULL, NULL, &pid, &error);
|
|
|
|
if (error) {
|
|
LOG(MOD "Error running async command: %s\n", error->message);
|
|
g_error_free(error);
|
|
g_free(command_line);
|
|
return false;
|
|
}
|
|
|
|
g_child_watch_add(pid, async_task_callback, NULL);
|
|
|
|
LOG(MOD "Executing [%d]: %s -c \"%s\"\n", pid, SHELL_PATH, command_line);
|
|
|
|
g_free(command_line);
|
|
|
|
G_LOCK(tasks);
|
|
++tasks;
|
|
G_UNLOCK(tasks);
|
|
|
|
return true;
|
|
}
|
|
|
|
void async_task_finish(void) {
|
|
G_LOCK(thread_pool);
|
|
g_thread_pool_free(thread_pool, false, true);
|
|
thread_pool = NULL;
|
|
G_UNLOCK(thread_pool);
|
|
|
|
G_LOCK(tasks);
|
|
if (tasks) {
|
|
LOG(MOD "Running main loop\n");
|
|
g_main_loop_run(main_loop);
|
|
}
|
|
tasks = 0;
|
|
G_UNLOCK(tasks);
|
|
|
|
g_main_loop_unref(main_loop);
|
|
main_loop = NULL;
|
|
}
|