- docs updated
[strongswan.git] / Source / charon / queues / job_queue.c
1 /**
2 * @file job_queue.c
3 *
4 * @brief Implementation of job_queue_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
26 #include "job_queue.h"
27
28 #include <utils/allocator.h>
29 #include <utils/linked_list.h>
30
31
32 typedef struct private_job_queue_t private_job_queue_t;
33
34 /**
35 * @brief Private Variables and Functions of job_queue class
36 *
37 */
38 struct private_job_queue_t {
39
40 /**
41 * public members
42 */
43 job_queue_t public;
44
45 /**
46 * The jobs are stored in a linked list
47 */
48 linked_list_t *list;
49
50 /**
51 * access to linked_list is locked through this mutex
52 */
53 pthread_mutex_t mutex;
54
55 /**
56 * If the queue is empty a thread has to wait
57 * This condvar is used to wake up such a thread
58 */
59 pthread_cond_t condvar;
60 };
61
62
63 /**
64 * implements job_queue_t.get_count
65 */
66 static int get_count(private_job_queue_t *this)
67 {
68 int count;
69 pthread_mutex_lock(&(this->mutex));
70 count = this->list->get_count(this->list);
71 pthread_mutex_unlock(&(this->mutex));
72 return count;
73 }
74
75 /**
76 * implements job_queue_t.get
77 */
78 static job_t *get(private_job_queue_t *this)
79 {
80 int oldstate;
81 job_t *job;
82 pthread_mutex_lock(&(this->mutex));
83 /* go to wait while no jobs available */
84 while(this->list->get_count(this->list) == 0)
85 {
86 /* add mutex unlock handler for cancellation, enable cancellation */
87 pthread_cleanup_push((void(*)(void*))pthread_mutex_unlock, (void*)&(this->mutex));
88 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
89
90 pthread_cond_wait( &(this->condvar), &(this->mutex));
91
92 /* reset cancellation, remove mutex-unlock handler (without executing) */
93 pthread_setcancelstate(oldstate, NULL);
94 pthread_cleanup_pop(0);
95 }
96 this->list->remove_first(this->list,(void **) &job);
97 pthread_mutex_unlock(&(this->mutex));
98 return job;
99 }
100
101 /**
102 * implements function job_queue_t.add
103 */
104 static void add(private_job_queue_t *this, job_t *job)
105 {
106 pthread_mutex_lock(&(this->mutex));
107 this->list->insert_last(this->list,job);
108 pthread_cond_signal( &(this->condvar));
109 pthread_mutex_unlock(&(this->mutex));
110 }
111
112 /**
113 * implements job_queue_t.destroy
114 */
115 static void job_queue_destroy (private_job_queue_t *this)
116 {
117 while (this->list->get_count(this->list) > 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_all(job);
126 }
127 this->list->destroy(this->list);
128
129 pthread_mutex_destroy(&(this->mutex));
130
131 pthread_cond_destroy(&(this->condvar));
132
133 allocator_free(this);
134 }
135
136 /*
137 *
138 * Documented in header
139 */
140 job_queue_t *job_queue_create()
141 {
142 private_job_queue_t *this = allocator_alloc_thing(private_job_queue_t);
143
144 this->public.get_count = (int(*)(job_queue_t*))get_count;
145 this->public.get = (job_t*(*)(job_queue_t*))get;
146 this->public.add = (void(*)(job_queue_t*, job_t*))add;
147 this->public.destroy = (void(*)(job_queue_t*))job_queue_destroy;
148
149 this->list = linked_list_create();
150 pthread_mutex_init(&(this->mutex), NULL);
151 pthread_cond_init(&(this->condvar), NULL);
152
153 return (&this->public);
154 }