/* Chrysalide - Outil d'analyse de fichiers binaires
 * work.c - gestion des travaux différés
 *
 * Copyright (C) 2009-2024 Cyrille Bagard
 *
 *  This file is part of Chrysalide.
 *
 *  Chrysalide 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.
 *
 *  Chrysalide 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 Chrysalide.  If not, see <http://www.gnu.org/licenses/>.
 */


#include "work.h"


#include "work-int.h"



/* Initialise la classe des travaux différés. */
static void g_generic_work_class_init(GGenericWorkClass *);

/* Initialise une instance de travail différé. */
static void g_generic_work_init(GGenericWork *);

/* Supprime toutes les références externes. */
static void g_generic_work_dispose(GGenericWork *);

/* Procède à la libération totale de la mémoire. */
static void g_generic_work_finalize(GGenericWork *);



/* Indique le type défini pour les travaux différés. */
G_DEFINE_TYPE(GGenericWork, g_generic_work, G_TYPE_OBJECT);


/******************************************************************************
*                                                                             *
*  Paramètres  : klass = classe à initialiser.                                *
*                                                                             *
*  Description : Initialise la classe des travaux différés.                   *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_generic_work_class_init(GGenericWorkClass *klass)
{
    GObjectClass *object;                   /* Autre version de la classe  */

    object = G_OBJECT_CLASS(klass);

    object->dispose = (GObjectFinalizeFunc/* ! */)g_generic_work_dispose;
    object->finalize = (GObjectFinalizeFunc)g_generic_work_finalize;

    g_signal_new("work-completed",
                 G_TYPE_GENERIC_WORK,
                 G_SIGNAL_RUN_LAST,
                 G_STRUCT_OFFSET(GGenericWorkClass, work_completed),
                 NULL, NULL,
                 g_cclosure_marshal_VOID__VOID,
                 G_TYPE_NONE, 0);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : work = instance à initialiser.                               *
*                                                                             *
*  Description : Initialise une instance de travail différé.                  *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_generic_work_init(GGenericWork *work)
{
    DL_LIST_ITEM_INIT(&work->link);

    work->completed = false;
    g_mutex_init(&work->mutex);
    g_cond_init(&work->cond);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : work = instance d'objet GLib à traiter.                      *
*                                                                             *
*  Description : Supprime toutes les références externes.                     *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_generic_work_dispose(GGenericWork *work)
{
    g_mutex_clear(&work->mutex);
    g_cond_clear(&work->cond);

    G_OBJECT_CLASS(g_generic_work_parent_class)->dispose(G_OBJECT(work));

}


/******************************************************************************
*                                                                             *
*  Paramètres  : work = instance d'objet GLib à traiter.                      *
*                                                                             *
*  Description : Procède à la libération totale de la mémoire.                *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_generic_work_finalize(GGenericWork *work)
{
    G_OBJECT_CLASS(g_generic_work_parent_class)->finalize(G_OBJECT(work));

}


/******************************************************************************
*                                                                             *
*  Paramètres  : work = travail à traiter.                                    *
*                list = ensemble de travaux à considérer. [OUT]               *
*                                                                             *
*  Description : Intègre un travail dans une liste de tâches à effectuer.     *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void g_generic_work_add_to_list(GGenericWork *work, GGenericWork **list)
{
    dl_list_add_tail(work, list, GGenericWork, link);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : work = travail à traiter.                                    *
*                list = ensemble de travaux à considérer. [OUT]               *
*                                                                             *
*  Description : Supprime un travail d'une liste de tâches à effectuer.       *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void g_generic_work_remove_from_list(GGenericWork *work, GGenericWork **list)
{
    dl_list_del(work, list, GGenericWork, link);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : work = travail à effectuer.                                  *
*                                                                             *
*  Description : Mène l'opération programmée.                                 *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void g_generic_work_process(GGenericWork *work)
{
    G_GENERIC_WORK_GET_CLASS(work)->run(work);

    g_mutex_lock(&work->mutex);

    work->completed = true;

    g_cond_signal(&work->cond);
    g_mutex_unlock(&work->mutex);

    g_signal_emit_by_name(work, "work-completed");

}


/******************************************************************************
*                                                                             *
*  Paramètres  : work = travail à surveiller.                                 *
*                                                                             *
*  Description : Attend la fin de l'exécution d'une tâche donnée.             *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void g_generic_work_wait_for_completion(GGenericWork *work)
{
    g_mutex_lock(&work->mutex);

    while (!work->completed)
        g_cond_wait(&work->cond, &work->mutex);

    g_mutex_unlock(&work->mutex);

}