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