4 * @brief Job-Queue based on linked_list_t
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
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>.
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
26 #include <pluto/constants.h>
27 #include <pluto/defs.h>
29 #include "job_queue.h"
32 * @brief implements function destroy of job_t
34 static status_t
job_destroy(job_t
*job
)
41 * Creates a job (documented in header-file)
43 job_t
*job_create(job_type_t type
, void *assigned_data
)
45 job_t
*this = alloc_thing(job_t
, "job_t");
47 this->destroy
= job_destroy
;
50 this->assigned_data
= assigned_data
;
56 * @brief Private Variables and Functions of job_queue class
59 typedef struct private_job_queue_s private_job_queue_t
;
62 struct private_job_queue_s
{
66 * The jobs are stored in a linked list
70 * access to linked_list is locked through this mutex
72 pthread_mutex_t mutex
;
75 * If the queue is empty a thread has to wait
76 * This condvar is used to wake up such a thread
78 pthread_cond_t condvar
;
83 * @brief implements function get_count of job_queue_t
85 status_t
get_count(private_job_queue_t
*this, int *count
)
87 pthread_mutex_lock(&(this->mutex
));
88 *count
= this->list
->count
;
89 pthread_mutex_unlock(&(this->mutex
));
94 * @brief implements function get of job_queue_t
96 status_t
get(private_job_queue_t
*this, job_t
**job
)
98 pthread_mutex_lock(&(this->mutex
));
99 // add mutex unlock handler for cancellation
100 pthread_cleanup_push((void(*)(void*))pthread_mutex_unlock
, (void*)&(this->mutex
));
101 // go to wait while no jobs available
102 while(this->list
->count
== 0)
104 pthread_cond_wait( &(this->condvar
), &(this->mutex
));
106 // remove mutex-unlock handler
107 pthread_cleanup_pop(1);
109 this->list
->remove_first(this->list
,(void **) job
);
110 pthread_mutex_unlock(&(this->mutex
));
115 * @brief implements function add of job_queue_t
117 status_t
add(private_job_queue_t
*this, job_t
*job
)
119 pthread_mutex_lock(&(this->mutex
));
120 this->list
->insert_last(this->list
,job
);
121 pthread_cond_signal( &(this->condvar
));
122 pthread_mutex_unlock(&(this->mutex
));
127 * @brief implements function destroy of job_queue_t
130 status_t
job_queue_destroy (private_job_queue_t
*this)
132 while (this->list
->count
> 0)
135 if (this->list
->remove_first(this->list
,(void *) &job
) != SUCCESS
)
137 this->list
->destroy(this->list
);
142 this->list
->destroy(this->list
);
144 pthread_mutex_destroy(&(this->mutex
));
146 pthread_cond_destroy(&(this->condvar
));
154 * Documented in header
156 job_queue_t
*job_queue_create()
158 linked_list_t
*linked_list
= linked_list_create();
159 if (linked_list
== NULL
)
164 private_job_queue_t
*this = alloc_thing(private_job_queue_t
, "private_job_queue_t");
167 linked_list
->destroy(linked_list
);
171 this->public.get_count
= (status_t(*)(job_queue_t
*, int*))get_count
;
172 this->public.get
= (status_t(*)(job_queue_t
*, job_t
**))get
;
173 this->public.add
= (status_t(*)(job_queue_t
*, job_t
*))add
;
174 this->public.destroy
= (status_t(*)(job_queue_t
*))job_queue_destroy
;
176 this->list
= linked_list
;
177 pthread_mutex_init(&(this->mutex
), NULL
);
178 pthread_cond_init(&(this->condvar
), NULL
);
180 return (&this->public);