made linked lists invoke() method consistent to clone_*() and destroy_*() methods
[strongswan.git] / src / charon / processing / jobs / callback_job.c
1 /**
2 * @file callback_job.c
3 *
4 * @brief Implementation of callback_job_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2007 Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #include "callback_job.h"
24
25 #include <daemon.h>
26
27 typedef struct private_callback_job_t private_callback_job_t;
28
29 /**
30 * Private data of an callback_job_t Object.
31 */
32 struct private_callback_job_t {
33 /**
34 * Public callback_job_t interface.
35 */
36 callback_job_t public;
37
38 /**
39 * Callback to call on execution
40 */
41 callback_job_cb_t callback;
42
43 /**
44 * parameter to supply to callback
45 */
46 void *data;
47
48 /**
49 * cleanup function for data
50 */
51 callback_job_cleanup_t cleanup;
52
53 /**
54 * thread ID of the job, if running
55 */
56 pthread_t thread;
57
58 /**
59 * mutex to access jobs interna
60 */
61 pthread_mutex_t mutex;
62
63 /**
64 * condvar to synchronize thread startup/cancellation
65 */
66 pthread_cond_t condvar;
67
68 /**
69 * list of asociated child jobs
70 */
71 linked_list_t *children;
72
73 /**
74 * parent of this job, or NULL
75 */
76 private_callback_job_t *parent;
77 };
78
79 /**
80 * Implements job_t.destroy.
81 */
82 static void destroy(private_callback_job_t *this)
83 {
84 if (this->cleanup)
85 {
86 this->cleanup(this->data);
87 }
88 this->children->destroy(this->children);
89 free(this);
90 }
91
92 /**
93 * unregister a child from its parent, if any.
94 */
95 static void unregister(private_callback_job_t *this)
96 {
97 if (this->parent)
98 {
99 iterator_t *iterator;
100 private_callback_job_t *child;
101
102 pthread_mutex_lock(&this->parent->mutex);
103 iterator = this->parent->children->create_iterator(this->parent->children, TRUE);
104 while (iterator->iterate(iterator, (void**)&child))
105 {
106 if (child == this)
107 {
108 iterator->remove(iterator);
109 break;
110 }
111 }
112 iterator->destroy(iterator);
113 pthread_mutex_unlock(&this->parent->mutex);
114 }
115 }
116
117 /**
118 * Implementation of callback_job_t.cancel.
119 */
120 static void cancel(private_callback_job_t *this)
121 {
122 pthread_t thread;
123
124 /* wait until thread has started */
125 pthread_mutex_lock(&this->mutex);
126 while (this->thread == 0)
127 {
128 pthread_cond_wait(&this->condvar, &this->mutex);
129 }
130 thread = this->thread;
131
132 /* terminate its children */
133 this->children->invoke_offset(this->children, offsetof(callback_job_t, cancel));
134 pthread_mutex_unlock(&this->mutex);
135
136 /* terminate thread */
137 pthread_cancel(thread);
138 pthread_join(thread, NULL);
139 }
140
141 /**
142 * Implementation of job_t.execute.
143 */
144 static void execute(private_callback_job_t *this)
145 {
146 bool cleanup = FALSE;
147
148 pthread_mutex_lock(&this->mutex);
149 this->thread = pthread_self();
150 pthread_cond_signal(&this->condvar);
151 pthread_mutex_unlock(&this->mutex);
152
153 pthread_cleanup_push((void*)destroy, this);
154 while (TRUE)
155 {
156 switch (this->callback(this->data))
157 {
158 case JOB_REQUEUE_DIRECT:
159 continue;
160 case JOB_REQUEUE_FAIR:
161 {
162 charon->processor->queue_job(charon->processor,
163 &this->public.job_interface);
164 break;
165 }
166 case JOB_REQUEUE_NONE:
167 default:
168 {
169 cleanup = TRUE;
170 break;
171 }
172 }
173 break;
174 }
175 unregister(this);
176 pthread_cleanup_pop(cleanup);
177 }
178
179 /*
180 * Described in header.
181 */
182 callback_job_t *callback_job_create(callback_job_cb_t cb, void *data,
183 callback_job_cleanup_t cleanup,
184 callback_job_t *parent)
185 {
186 private_callback_job_t *this = malloc_thing(private_callback_job_t);
187
188 /* interface functions */
189 this->public.job_interface.execute = (void (*) (job_t *)) execute;
190 this->public.job_interface.destroy = (void (*) (job_t *)) destroy;
191 this->public.cancel = (void(*)(callback_job_t*))cancel;
192
193 /* private variables */
194 pthread_mutex_init(&this->mutex, NULL);
195 pthread_cond_init(&this->condvar, NULL);
196 this->callback = cb;
197 this->data = data;
198 this->cleanup = cleanup;
199 this->thread = 0;
200 this->children = linked_list_create();
201 this->parent = (private_callback_job_t*)parent;
202
203 /* register us at parent */
204 if (parent)
205 {
206 pthread_mutex_lock(&this->parent->mutex);
207 this->parent->children->insert_last(this->parent->children, this);
208 pthread_mutex_unlock(&this->parent->mutex);
209 }
210
211 return &this->public;
212 }
213