summaryrefslogtreecommitdiff
path: root/jobqueue.c
blob: 427b62e8a19af80ce6b8e6b8ed2bb29ed442817f (plain)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "jobqueue.h"

typedef struct Job Job;

struct Job
{
    JobQueue *	queue;
    ExecutorJob job;
    gpointer	data;
};

struct JobQueue
{
    Executor *	executor;
    GQueue *	jobs;
    GMutex *	mutex;
    gboolean	in_executor;;
    int		ref_count;
};

static Job *
job_new (JobQueue *	queue,
	 ExecutorJob	func,
	 gpointer	data)
{
    Job *job = g_new0 (Job, 1);
    job->job = func;
    job->queue = queue;
    job->data = data;

    return job;
}

static void
job_queue_lock (JobQueue *queue)
{
    g_mutex_lock (queue->mutex);
}

static void
job_queue_unlock (JobQueue *queue)
{
    g_mutex_unlock (queue->mutex);
}

static void
delete_queue (JobQueue *queue)
{
    g_assert (queue->ref_count == 0);

    g_queue_free (queue->jobs);
    g_mutex_free (queue->mutex);
    g_free (queue);
}    

JobQueue *
job_queue_new (Executor *executor)
{
    JobQueue *queue = g_new0 (JobQueue, 1);

    queue->executor = executor;
    queue->mutex = g_mutex_new ();
    queue->in_executor = FALSE;
    queue->ref_count = 1;
    queue->jobs = g_queue_new ();
    
    return queue;
}

static void
run_queue (gpointer data)
{
    JobQueue *queue = data;
    gboolean do_free = FALSE;
    
    job_queue_lock (queue);

    g_assert (queue->in_executor == TRUE);
    
    while (!g_queue_is_empty (queue->jobs))
    {
	Job *job = g_queue_pop_head (queue->jobs);

	job_queue_unlock (queue);

	job->job (job->data);

	g_free (job);
	
	job_queue_lock (queue);
    }

    queue->ref_count--;
    queue->in_executor = FALSE;

    if (queue->ref_count == 0)
	do_free = TRUE;
    
    job_queue_unlock (queue);

    if (do_free)
	delete_queue (queue);
}

gpointer
job_queue_add (JobQueue *queue,
	       ExecutorJob func,
	       gpointer data)
{
    Job *job;
    
    job_queue_lock (queue);

    job = job_new (queue, func, data);
    g_queue_push_tail (queue->jobs, job);
    
    if (!queue->in_executor)
    {
	queue->in_executor = TRUE;
	queue->ref_count++;
	executor_add_job (queue->executor, run_queue, queue);
    }
    
    job_queue_unlock (queue);

    return job;
}

void
job_queue_remove (JobQueue *queue,
		  gpointer  job_id)
{
    Job *job = job_id;
    GList *link;

    job_queue_lock (job->queue);

    link = g_queue_find (queue->jobs, job);
    if (link)
    {
	g_queue_delete_link (queue->jobs, link);
	g_free (job);
    }
    
    job_queue_unlock (job->queue);
}

void
job_queue_free (JobQueue *queue)
{
    gboolean do_free = FALSE;
    
    job_queue_lock (queue);

    if (queue->in_executor)
    {
	g_warning ("Trying to free a running queue");
	job_queue_unlock (queue);
	return;
    }
    
    while (!g_queue_is_empty (queue->jobs))
    {
	Job *job = g_queue_pop_head (queue->jobs);
	
	g_free (job);
    }
    
    g_queue_free (queue->jobs);

    queue->ref_count--;

    if (queue->ref_count == 0)
	do_free = TRUE;
    
    job_queue_unlock (queue);

    if (do_free)
	delete_queue (queue);
}