diff options
author | Cyrille Bagard <nocbos@gmail.com> | 2009-08-30 17:23:51 (GMT) |
---|---|---|
committer | Cyrille Bagard <nocbos@gmail.com> | 2009-08-30 17:23:51 (GMT) |
commit | ef3b996ad359e0da5e93184dab9200fad9105faf (patch) | |
tree | 7ae9252e856a64dfbcb77c9a0a7a4965e0452cbd /src/glibext | |
parent | cc7ec539c4bd0e55cf9dc156c769e306b93b419e (diff) |
Provided a clean way to run delayed tasks.
git-svn-id: svn://svn.gna.org/svn/chrysalide/trunk@109 abbe820e-26c8-41b2-8c08-b7b2b41f8b0a
Diffstat (limited to 'src/glibext')
-rw-r--r-- | src/glibext/Makefile.am | 17 | ||||
-rw-r--r-- | src/glibext/delayed-int.h | 82 | ||||
-rw-r--r-- | src/glibext/delayed.c | 226 | ||||
-rw-r--r-- | src/glibext/delayed.h | 81 |
4 files changed, 406 insertions, 0 deletions
diff --git a/src/glibext/Makefile.am b/src/glibext/Makefile.am new file mode 100644 index 0000000..334b49a --- /dev/null +++ b/src/glibext/Makefile.am @@ -0,0 +1,17 @@ + +noinst_LTLIBRARIES = libglibext.la + +libglibext_la_SOURCES = \ + delayed-int.h \ + delayed.h delayed.c + +libglibext_la_LDFLAGS = + + +INCLUDES = $(LIBGTK_CFLAGS) $(LIBXML_CFLAGS) + +AM_CPPFLAGS = + +AM_CFLAGS = $(DEBUG_CFLAGS) $(WARNING_FLAGS) $(COMPLIANCE_FLAGS) + +SUBDIRS = diff --git a/src/glibext/delayed-int.h b/src/glibext/delayed-int.h new file mode 100644 index 0000000..7fb8f3a --- /dev/null +++ b/src/glibext/delayed-int.h @@ -0,0 +1,82 @@ + +/* OpenIDA - Outil d'analyse de fichiers binaires + * delayed-int.h - définitions internes pour la gestion des travaux différés + * + * Copyright (C) 2009 Cyrille Bagard + * + * This file is part of OpenIDA. + * + * OpenIDA 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. + * + * OpenIDA 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 Foobar. If not, see <http://www.gnu.org/licenses/>. + */ + + +#include "delayed.h" + + +#include "../common/dllist.h" + + + +/* -------------------------- TACHE DIFFEREE DANS LE TEMPS -------------------------- */ + + +/* Travail différé (instance) */ +struct _GDelayedWork +{ + GObject parent; /* A laisser en premier */ + + DL_LIST_ITEM(link); /* Lien vers les maillons */ + +}; + +/* Travail différé (classe) */ +struct _GDelayedWorkClass +{ + GObjectClass parent; /* A laisser en premier */ + +}; + + +#define delayed_work_list_add_tail(new, head) dl_list_add_tail(new, head, GDelayedWork, link) +#define delayed_work_list_del(item, head) dl_list_del(item, head, GDelayedWork, link) + + + +/* ------------------------- TRAITEMENT DE TACHES DIFFEREES ------------------------- */ + + +/* Effectue le traitement d'une tâche donnée. */ +typedef void (* process_work_fc) (GWorkQueue *, GDelayedWork *); + + +/* Gestionnaire des travaux différés (instance) */ +struct _GWorkQueue +{ + GObject parent; /* A laisser en premier */ + + GDelayedWork *works; /* Tâches à mener à bien */ + GMutex *mutex; /* Verrou pour l'accès */ + GCond *cond; /* Réveil pour un traitement */ + + GThread *thread; /* Procédure de traitement */ + process_work_fc process; /* Coeur du traitement */ + +}; + +/* Gestionnaire des travaux différés (classe) */ +struct _GWorkQueueClass +{ + GObjectClass parent; /* A laisser en premier */ + +}; diff --git a/src/glibext/delayed.c b/src/glibext/delayed.c new file mode 100644 index 0000000..4388b2c --- /dev/null +++ b/src/glibext/delayed.c @@ -0,0 +1,226 @@ + +/* OpenIDA - Outil d'analyse de fichiers binaires + * delayed.c - gestion des travaux différés + * + * Copyright (C) 2009 Cyrille Bagard + * + * This file is part of OpenIDA. + * + * OpenIDA 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. + * + * OpenIDA 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 Foobar. If not, see <http://www.gnu.org/licenses/>. + */ + + +#include "delayed.h" + + +#include "delayed-int.h" + + + +/* -------------------------- TACHE DIFFEREE DANS LE TEMPS -------------------------- */ + + +/* Initialise la classe des travaux différés. */ +static void g_delayed_work_class_init(GDelayedWorkClass *); + +/* Initialise une instance de travail différé. */ +static void g_delayed_work_init(GDelayedWork *); + + + +/* ------------------------- TRAITEMENT DE TACHES DIFFEREES ------------------------- */ + + +/* Initialise la classe des travaux différés. */ +static void g_work_queue_class_init(GWorkQueueClass *); + +/* Initialise une instance de gestionnaire de travaux différés. */ +static void g_work_queue_init(GWorkQueue *); + +/* Assure le traitement en différé. */ +static void *g_work_queue_process(GWorkQueue *); + + + +/* ---------------------------------------------------------------------------------- */ +/* TACHE DIFFEREE DANS LE TEMPS */ +/* ---------------------------------------------------------------------------------- */ + + +/* Indique le type défini pour les travaux différés. */ +G_DEFINE_TYPE(GDelayedWork, g_delayed_work, G_TYPE_OBJECT); + + +/****************************************************************************** +* * +* Paramètres : klass = classe à initialiser. * +* * +* Description : Initialise la classe des travaux différés. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_delayed_work_class_init(GDelayedWorkClass *klass) +{ + +} + + +/****************************************************************************** +* * +* Paramètres : work = instance à initialiser. * +* * +* Description : Initialise une instance de travail différé. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_delayed_work_init(GDelayedWork *work) +{ + +} + + + +/* ---------------------------------------------------------------------------------- */ +/* TRAITEMENT DE TACHES DIFFEREES */ +/* ---------------------------------------------------------------------------------- */ + + +/* Indique le type défini pour le gestionnaire des travaux différés. */ +G_DEFINE_TYPE(GWorkQueue, g_work_queue, G_TYPE_OBJECT); + + +/****************************************************************************** +* * +* Paramètres : klass = classe à initialiser. * +* * +* Description : Initialise la classe des travaux différés. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_work_queue_class_init(GWorkQueueClass *klass) +{ + +} + + +/****************************************************************************** +* * +* Paramètres : queue = instance à initialiser. * +* * +* Description : Initialise une instance de gestionnaire de travaux différés. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_work_queue_init(GWorkQueue *queue) +{ + GError *error; /* Bilan de création de thread */ + + queue->mutex = g_mutex_new(); + if (queue->mutex == NULL) + goto gwqi_error; + + queue->cond = g_cond_new(); + if (queue->cond == NULL) + goto gwqi_error; + + queue->thread = g_thread_create((GThreadFunc)g_work_queue_process, queue, FALSE, &error); + if (!queue->thread) + goto gwqi_error; + + gwqi_error: + + /* TODO */ return; + +} + + +/****************************************************************************** +* * +* Paramètres : queue = gestionnaire des actions à mener. * +* * +* Description : Assure le traitement en différé. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void *g_work_queue_process(GWorkQueue *queue) +{ + GDelayedWork *work; /* Traitement à mener */ + + while (1) + { + g_mutex_lock(queue->mutex); + + if (dl_list_empty(queue->works)) + g_cond_wait(queue->cond, queue->mutex); + + work = queue->works; + delayed_work_list_del(work, &queue->works); + + g_mutex_unlock(queue->mutex); + + queue->process(queue, work); + + /* TODO : delete work */ + + } + + return NULL; + +} + + +/****************************************************************************** +* * +* Paramètres : queu = gestionnaire des actions à mener. * +* work = nouvelle tâche à programmer, puis effectuer. * +* * +* Description : Place une nouvelle tâche en attente. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +void g_delayed_queue_schedule_work(GWorkQueue *queue, GDelayedWork *work) +{ + g_mutex_lock(queue->mutex); + + delayed_work_list_add_tail(work, &queue->works); + + g_cond_signal(queue->cond); + + g_mutex_unlock(queue->mutex); + +} diff --git a/src/glibext/delayed.h b/src/glibext/delayed.h new file mode 100644 index 0000000..d1e3967 --- /dev/null +++ b/src/glibext/delayed.h @@ -0,0 +1,81 @@ + +/* OpenIDA - Outil d'analyse de fichiers binaires + * delayed.h - prototypes pour la gestion des travaux différés + * + * Copyright (C) 2009 Cyrille Bagard + * + * This file is part of OpenIDA. + * + * OpenIDA 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. + * + * OpenIDA 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 Foobar. If not, see <http://www.gnu.org/licenses/>. + */ + + +#ifndef _GLIBEXT_DELAYED_H +#define _GLIBEXT_DELAYED_H + + +#include <glib-object.h> + + + +/* -------------------------- TACHE DIFFEREE DANS LE TEMPS -------------------------- */ + + +#define G_TYPE_DELAYED_WORK g_delayed_work_get_type() +#define G_DELAYED_WORK(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), g_delayed_work_get_type(), GDelayedWork)) +#define G_IS_DELAYED_WORK(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), g_delayed_work_get_type())) +#define G_DELAYED_WORK_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), G_TYPE_DELAYED_WORK, GDelayedWorkClass)) +#define G_IS_DELAYED_WORK_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), G_TYPE_DELAYED_WORK)) +#define G_DELAYED_WORK_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), G_TYPE_DELAYED_WORK, GDelayedWorkClass)) + + +/* Travail différé (instance) */ +typedef struct _GDelayedWork GDelayedWork; + +/* Travail différé (classe) */ +typedef struct _GDelayedWorkClass GDelayedWorkClass; + + +/* Indique le type défini pour les travaux différés. */ +GType g_delayed_work_get_type(void); + + + +/* ------------------------- TRAITEMENT DE TACHES DIFFEREES ------------------------- */ + + +#define G_TYPE_WORK_QUEUE g_work_queue_get_type() +#define G_WORK_QUEUE(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), g_work_queue_get_type(), GWorkQueue)) +#define G_IS_WORK_QUEUE(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), g_work_queue_get_type())) +#define G_WORK_QUEUE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), G_TYPE_WORK_QUEUE, GWorkQueueClass)) +#define G_IS_WORK_QUEUE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), G_TYPE_WORK_QUEUE)) +#define G_WORK_QUEUE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), G_TYPE_WORK_QUEUE, GWorkQueueClass)) + + +/* Gestionnaire des travaux différés (instance) */ +typedef struct _GWorkQueue GWorkQueue; + +/* Gestionnaire des travaux différés (classe) */ +typedef struct _GWorkQueueClass GWorkQueueClass; + + +/* Indique le type défini pour le gestionnaire des travaux différés. */ +GType g_work_queue_get_type(void); + +/* Place une nouvelle tâche en attente. */ +void g_delayed_queue_schedule_work(GWorkQueue *, GDelayedWork *); + + + +#endif /* _GLIBEXT_DELAYED_H */ |