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/delayed.c | |
| 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/delayed.c')
| -rw-r--r-- | src/glibext/delayed.c | 226 | 
1 files changed, 226 insertions, 0 deletions
| 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); + +} | 
