aboutsummaryrefslogtreecommitdiffstats
path: root/calendar/pcs/job.c
blob: f8223f0bab23d0c365ce2954c6875d96409bb28a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/* GNOME personal calendar server - job manager
 *
 * Copyright (C) 2000 Helix Code, Inc.
 * Copyright (C) 2000 Ximian, Inc.
 *
 * Author: Federico Mena-Quintero <federico@ximian.com>
 *
 * This program 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 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 */

#include <config.h>
#include "job.h"



/* The job list */

typedef struct {
    JobFunc func;
    gpointer data;
} Job;

static GSList *jobs_head;
static GSList *jobs_tail;

static guint jobs_idle_id;



/* Runs a job and dequeues it */
static gboolean
run_job (gpointer data)
{
    Job *job;
    GSList *l;

    g_assert (jobs_head != NULL);

    job = jobs_head->data;
    (* job->func) (job->data);
    g_free (job);

    l = jobs_head;
    jobs_head = g_slist_remove_link (jobs_head, jobs_head);
    g_slist_free_1 (l);

    if (!jobs_head) {
        jobs_tail = NULL;
        jobs_idle_id = 0;
        return FALSE;
    } else
        return TRUE;
}

/**
 * job_add:
 * @func: Function to run the job.
 * @data: Data to pass to @function.
 * 
 * Adds a job to the queue.  The job will automatically be run asynchronously.
 **/
void
job_add (JobFunc func, gpointer data)
{
    Job *job;

    g_return_if_fail (func != NULL);

    job = g_new (Job, 1);
    job->func = func;
    job->data = data;

    if (!jobs_head) {
        g_assert (jobs_tail == NULL);
        g_assert (jobs_idle_id == 0);

        jobs_head = g_slist_append (NULL, job);
        jobs_tail = jobs_head;

        jobs_idle_id = g_idle_add (run_job, NULL);
    } else {
        g_assert (jobs_tail != NULL);
        g_assert (jobs_idle_id != 0);

        jobs_tail = g_slist_append (jobs_tail, job)->next;
    }
}