aboutsummaryrefslogtreecommitdiffstats
path: root/src/charon/processing/jobs/callback_job.c
blob: ad51dc55dd3ec06faa0cf10de0926039ce2cb7a0 (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
 * Copyright (C) 2007 Martin Willi
 * Hochschule fuer Technik Rapperswil
 *
 * 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.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * 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.
 */

#include "callback_job.h"

#include <pthread.h>

#include <daemon.h>
#include <utils/mutex.h>

typedef struct private_callback_job_t private_callback_job_t;

/**
 * Private data of an callback_job_t Object.
 */
struct private_callback_job_t {
	/**
	 * Public callback_job_t interface.
	 */
	callback_job_t public;

	/**
	 * Callback to call on execution
	 */
	callback_job_cb_t callback;

	/**
	 * parameter to supply to callback
	 */
	void *data;

	/**
	 * cleanup function for data
	 */
	callback_job_cleanup_t cleanup;

	/**
	 * thread ID of the job, if running
	 */
	pthread_t thread;

	/**
	 * mutex to access jobs interna
	 */
	mutex_t *mutex;

	/**
	 * list of asociated child jobs
	 */
	linked_list_t *children;

	/**
	 * parent of this job, or NULL
	 */
	private_callback_job_t *parent;
};

/**
 * Implements job_t.destroy.
 */
static void destroy(private_callback_job_t *this)
{
	if (this->cleanup)
	{
		this->cleanup(this->data);
	}
	this->children->destroy(this->children);
	this->mutex->destroy(this->mutex);
	free(this);
}

/**
 * unregister a child from its parent, if any.
 */
static void unregister(private_callback_job_t *this)
{
	if (this->parent)
	{
		iterator_t *iterator;
		private_callback_job_t *child;

		this->parent->mutex->lock(this->parent->mutex);
		iterator = this->parent->children->create_iterator(this->parent->children, TRUE);
		while (iterator->iterate(iterator, (void**)&child))
		{
			if (child == this)
			{
				iterator->remove(iterator);
				break;
			}
		}
		iterator->destroy(iterator);
		this->parent->mutex->unlock(this->parent->mutex);
	}
}

/**
 * Implementation of callback_job_t.cancel.
 */
static void cancel(private_callback_job_t *this)
{
	pthread_t thread;

	this->mutex->lock(this->mutex);
	thread = this->thread;

	/* terminate its children */
	this->children->invoke_offset(this->children, offsetof(callback_job_t, cancel));
	this->mutex->unlock(this->mutex);

	/* terminate thread */
	if (thread)
	{
		pthread_cancel(thread);
		pthread_join(thread, NULL);
	}
	/* avoid later execution of a cancelled job */
	this->callback = NULL;
}

/**
 * Implementation of job_t.execute.
 */
static void execute(private_callback_job_t *this)
{
	bool cleanup = FALSE;

	this->mutex->lock(this->mutex);
	this->thread = pthread_self();
	this->mutex->unlock(this->mutex);

	pthread_cleanup_push((void*)destroy, this);
	while (TRUE)
	{
		if (this->callback == NULL)
		{
			cleanup = TRUE;
			break;
		}
		switch (this->callback(this->data))
		{
			case JOB_REQUEUE_DIRECT:
				continue;
			case JOB_REQUEUE_FAIR:
			{
				charon->processor->queue_job(charon->processor,
											 &this->public.job_interface);
				break;
			}
			case JOB_REQUEUE_NONE:
			default:
			{
				cleanup = TRUE;
				break;
			}
		}
		break;
	}
	this->thread = 0;
	unregister(this);
	pthread_cleanup_pop(cleanup);
}

/*
 * Described in header.
 */
callback_job_t *callback_job_create(callback_job_cb_t cb, void *data,
									callback_job_cleanup_t cleanup,
									callback_job_t *parent)
{
	private_callback_job_t *this = malloc_thing(private_callback_job_t);

	/* interface functions */
	this->public.job_interface.execute = (void (*) (job_t *)) execute;
	this->public.job_interface.destroy = (void (*) (job_t *)) destroy;
	this->public.cancel = (void(*)(callback_job_t*))cancel;

	/* private variables */
	this->mutex = mutex_create(MUTEX_TYPE_DEFAULT);
	this->callback = cb;
	this->data = data;
	this->cleanup = cleanup;
	this->thread = 0;
	this->children = linked_list_create();
	this->parent = (private_callback_job_t*)parent;

	/* register us at parent */
	if (parent)
	{
		this->parent->mutex->lock(this->parent->mutex);
		this->parent->children->insert_last(this->parent->children, this);
		this->parent->mutex->unlock(this->parent->mutex);
	}

	return &this->public;
}