84a72992f22d68c1a734fa316e3f3defa64e0129
[strongswan.git] / Source / charon / job_queue.c
1 /**
2 * @file job_queue.c
3 *
4 * @brief Job-Queue based on linked_list_t
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, 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 <stdlib.h>
24 #include <pthread.h>
25 #include <freeswan.h>
26 #include <pluto/constants.h>
27 #include <pluto/defs.h>
28
29 #include "job_queue.h"
30
31 /**
32 * @brief implements function destroy of job_t
33 */
34 static status_t job_destroy(job_t *job)
35 {
36 pfree(job);
37 return SUCCESS;
38 }
39
40 /*
41 * Creates a job (documented in header-file)
42 */
43 job_t *job_create(job_type_t type, void *assigned_data)
44 {
45 job_t *this = alloc_thing(job_t, "job_t");
46
47 this->destroy = job_destroy;
48
49 this->type = type;
50 this->assigned_data = assigned_data;
51
52 return this;
53 }
54
55 /**
56 * @brief Private Variables and Functions of job_queue class
57 *
58 */
59 typedef struct private_job_queue_s private_job_queue_t;
60
61
62 struct private_job_queue_s {
63 job_queue_t public;
64
65 /**
66 * The jobs are stored in a linked list
67 */
68 linked_list_t *list;
69 /**
70 * access to linked_list is locked through this mutex
71 */
72 pthread_mutex_t mutex;
73
74 /**
75 * If the queue is empty a thread has to wait
76 * This condvar is used to wake up such a thread
77 */
78 pthread_cond_t condvar;
79 };
80
81
82 /**
83 * @brief implements function get_count of job_queue_t
84 */
85 status_t get_count(private_job_queue_t *this, int *count)
86 {
87 pthread_mutex_lock(&(this->mutex));
88 this->list->get_count(this->list,count);
89 pthread_mutex_unlock(&(this->mutex));
90 return SUCCESS;
91 }
92
93 /**
94 * @brief implements function get of job_queue_t
95 */
96 status_t get(private_job_queue_t *this, job_t **job)
97 {
98 int count;
99 int oldstate;
100 pthread_mutex_lock(&(this->mutex));
101 /* go to wait while no jobs available */
102 this->list->get_count(this->list,&count);
103 while(count == 0)
104 {
105 /* add mutex unlock handler for cancellation, enable cancellation */
106 pthread_cleanup_push((void(*)(void*))pthread_mutex_unlock, (void*)&(this->mutex));
107 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
108 pthread_cond_wait( &(this->condvar), &(this->mutex));
109
110 /* reset cancellation, remove mutex-unlock handler (without executing) */
111 pthread_setcancelstate(oldstate, NULL);
112 pthread_cleanup_pop(0);
113 this->list->get_count(this->list,&count);
114 }
115 this->list->remove_first(this->list,(void **) job);
116 pthread_mutex_unlock(&(this->mutex));
117 return SUCCESS;
118 }
119
120 /**
121 * @brief implements function add of job_queue_t
122 */
123 status_t add(private_job_queue_t *this, job_t *job)
124 {
125 pthread_mutex_lock(&(this->mutex));
126 this->list->insert_last(this->list,job);
127 pthread_cond_signal( &(this->condvar));
128 pthread_mutex_unlock(&(this->mutex));
129 return SUCCESS;
130 }
131
132 /**
133 * @brief implements function destroy of job_queue_t
134 *
135 */
136 status_t job_queue_destroy (private_job_queue_t *this)
137 {
138 int count;
139 this->list->get_count(this->list,&count);
140
141 while (count > 0)
142 {
143 job_t *job;
144 if (this->list->remove_first(this->list,(void *) &job) != SUCCESS)
145 {
146 this->list->destroy(this->list);
147 break;
148 }
149 job->destroy(job);
150 this->list->get_count(this->list,&count);
151 }
152 this->list->destroy(this->list);
153
154 pthread_mutex_destroy(&(this->mutex));
155
156 pthread_cond_destroy(&(this->condvar));
157
158 pfree(this);
159 return SUCCESS;
160 }
161
162 /*
163 *
164 * Documented in header
165 */
166 job_queue_t *job_queue_create()
167 {
168 linked_list_t *linked_list = linked_list_create();
169 if (linked_list == NULL)
170 {
171 return NULL;
172 }
173
174 private_job_queue_t *this = alloc_thing(private_job_queue_t, "private_job_queue_t");
175 if (this == NULL)
176 {
177 linked_list->destroy(linked_list);
178 return NULL;
179 }
180
181 this->public.get_count = (status_t(*)(job_queue_t*, int*))get_count;
182 this->public.get = (status_t(*)(job_queue_t*, job_t**))get;
183 this->public.add = (status_t(*)(job_queue_t*, job_t*))add;
184 this->public.destroy = (status_t(*)(job_queue_t*))job_queue_destroy;
185
186 this->list = linked_list;
187 pthread_mutex_init(&(this->mutex), NULL);
188 pthread_cond_init(&(this->condvar), NULL);
189
190 return (&this->public);
191 }