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