- job_t is now defined in its own files job.h and job.c
[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 Private Variables and Functions of job_queue class
33 *
34 */
35 typedef struct private_job_queue_s private_job_queue_t;
36
37
38 struct private_job_queue_s {
39 job_queue_t public;
40
41 /**
42 * The jobs are stored in a linked list
43 */
44 linked_list_t *list;
45 /**
46 * access to linked_list is locked through this mutex
47 */
48 pthread_mutex_t mutex;
49
50 /**
51 * If the queue is empty a thread has to wait
52 * This condvar is used to wake up such a thread
53 */
54 pthread_cond_t condvar;
55 };
56
57
58 /**
59 * @brief implements function get_count of job_queue_t
60 */
61 status_t get_count(private_job_queue_t *this, int *count)
62 {
63 pthread_mutex_lock(&(this->mutex));
64 this->list->get_count(this->list,count);
65 pthread_mutex_unlock(&(this->mutex));
66 return SUCCESS;
67 }
68
69 /**
70 * @brief implements function get of job_queue_t
71 */
72 status_t get(private_job_queue_t *this, job_t **job)
73 {
74 int count;
75 int oldstate;
76 pthread_mutex_lock(&(this->mutex));
77 /* go to wait while no jobs available */
78 this->list->get_count(this->list,&count);
79 while(count == 0)
80 {
81 /* add mutex unlock handler for cancellation, enable cancellation */
82 pthread_cleanup_push((void(*)(void*))pthread_mutex_unlock, (void*)&(this->mutex));
83 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
84 pthread_cond_wait( &(this->condvar), &(this->mutex));
85
86 /* reset cancellation, remove mutex-unlock handler (without executing) */
87 pthread_setcancelstate(oldstate, NULL);
88 pthread_cleanup_pop(0);
89 this->list->get_count(this->list,&count);
90 }
91 this->list->remove_first(this->list,(void **) job);
92 pthread_mutex_unlock(&(this->mutex));
93 return SUCCESS;
94 }
95
96 /**
97 * @brief implements function add of job_queue_t
98 */
99 status_t add(private_job_queue_t *this, job_t *job)
100 {
101 pthread_mutex_lock(&(this->mutex));
102 this->list->insert_last(this->list,job);
103 pthread_cond_signal( &(this->condvar));
104 pthread_mutex_unlock(&(this->mutex));
105 return SUCCESS;
106 }
107
108 /**
109 * @brief implements function destroy of job_queue_t
110 *
111 */
112 status_t job_queue_destroy (private_job_queue_t *this)
113 {
114 int count;
115 this->list->get_count(this->list,&count);
116
117 while (count > 0)
118 {
119 job_t *job;
120 if (this->list->remove_first(this->list,(void *) &job) != SUCCESS)
121 {
122 this->list->destroy(this->list);
123 break;
124 }
125 job->destroy(job);
126 this->list->get_count(this->list,&count);
127 }
128 this->list->destroy(this->list);
129
130 pthread_mutex_destroy(&(this->mutex));
131
132 pthread_cond_destroy(&(this->condvar));
133
134 pfree(this);
135 return SUCCESS;
136 }
137
138 /*
139 *
140 * Documented in header
141 */
142 job_queue_t *job_queue_create()
143 {
144 linked_list_t *linked_list = linked_list_create();
145 if (linked_list == NULL)
146 {
147 return NULL;
148 }
149
150 private_job_queue_t *this = alloc_thing(private_job_queue_t, "private_job_queue_t");
151 if (this == NULL)
152 {
153 linked_list->destroy(linked_list);
154 return NULL;
155 }
156
157 this->public.get_count = (status_t(*)(job_queue_t*, int*))get_count;
158 this->public.get = (status_t(*)(job_queue_t*, job_t**))get;
159 this->public.add = (status_t(*)(job_queue_t*, job_t*))add;
160 this->public.destroy = (status_t(*)(job_queue_t*))job_queue_destroy;
161
162 this->list = linked_list;
163 pthread_mutex_init(&(this->mutex), NULL);
164 pthread_cond_init(&(this->condvar), NULL);
165
166 return (&this->public);
167 }