fixed daemon kill before threads are spawned
[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 pthread_mutex_lock(&this->mutex);
125 thread = this->thread;
126
127 /* terminate its children */
128 this->children->invoke_offset(this->children, offsetof(callback_job_t, cancel));
129 pthread_mutex_unlock(&this->mutex);
130
131 /* terminate thread */
132 if (thread)
133 {
134 pthread_cancel(thread);
135 pthread_join(thread, NULL);
136 }
137 }
138
139 /**
140 * Implementation of job_t.execute.
141 */
142 static void execute(private_callback_job_t *this)
143 {
144 bool cleanup = FALSE;
145
146 pthread_mutex_lock(&this->mutex);
147 this->thread = pthread_self();
148 pthread_cond_signal(&this->condvar);
149 pthread_mutex_unlock(&this->mutex);
150
151 pthread_cleanup_push((void*)destroy, this);
152 while (TRUE)
153 {
154 switch (this->callback(this->data))
155 {
156 case JOB_REQUEUE_DIRECT:
157 continue;
158 case JOB_REQUEUE_FAIR:
159 {
160 charon->processor->queue_job(charon->processor,
161 &this->public.job_interface);
162 break;
163 }
164 case JOB_REQUEUE_NONE:
165 default:
166 {
167 cleanup = TRUE;
168 break;
169 }
170 }
171 break;
172 }
173 unregister(this);
174 pthread_cleanup_pop(cleanup);
175 }
176
177 /*
178 * Described in header.
179 */
180 callback_job_t *callback_job_create(callback_job_cb_t cb, void *data,
181 callback_job_cleanup_t cleanup,
182 callback_job_t *parent)
183 {
184 private_callback_job_t *this = malloc_thing(private_callback_job_t);
185
186 /* interface functions */
187 this->public.job_interface.execute = (void (*) (job_t *)) execute;
188 this->public.job_interface.destroy = (void (*) (job_t *)) destroy;
189 this->public.cancel = (void(*)(callback_job_t*))cancel;
190
191 /* private variables */
192 pthread_mutex_init(&this->mutex, NULL);
193 pthread_cond_init(&this->condvar, NULL);
194 this->callback = cb;
195 this->data = data;
196 this->cleanup = cleanup;
197 this->thread = 0;
198 this->children = linked_list_create();
199 this->parent = (private_callback_job_t*)parent;
200
201 /* register us at parent */
202 if (parent)
203 {
204 pthread_mutex_lock(&this->parent->mutex);
205 this->parent->children->insert_last(this->parent->children, this);
206 pthread_mutex_unlock(&this->parent->mutex);
207 }
208
209 return &this->public;
210 }
211