2 * Copyright (C) 2005-2007 Martin Willi
3 * Copyright (C) 2005 Jan Hutter
4 * Hochschule fuer Technik Rapperswil
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 #include "processor.h"
27 #include <utils/linked_list.h>
30 typedef struct private_processor_t private_processor_t
;
33 * Private data of processor_t class.
35 struct private_processor_t
{
37 * Public processor_t interface.
42 * Number of running threads
47 * Desired number of threads
49 u_int desired_threads
;
52 * Number of threads waiting for work
57 * The jobs are stored in a linked list
62 * access to linked_list is locked through this mutex
64 pthread_mutex_t mutex
;
67 * Condvar to wait for new jobs
69 pthread_cond_t jobadded
;
72 * Condvar to wait for terminated threads
74 pthread_cond_t threadterminated
;
77 static void process_jobs(private_processor_t
*this);
80 * restart a terminated thread
82 static void restart(private_processor_t
*this)
86 if (pthread_create(&thread
, NULL
, (void*)process_jobs
, this) != 0)
88 pthread_mutex_lock(&this->mutex
);
89 this->total_threads
--;
90 pthread_cond_broadcast(&this->threadterminated
);
91 pthread_mutex_unlock(&this->mutex
);
96 * Process queued jobs, called by the worker threads
98 static void process_jobs(private_processor_t
*this)
102 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
104 DBG2(DBG_JOB
, "started worker thread, thread_ID: %06u", (int)pthread_self());
106 pthread_mutex_lock(&this->mutex
);
107 while (this->desired_threads
>= this->total_threads
)
111 if (this->list
->get_count(this->list
) == 0)
113 this->idle_threads
++;
114 pthread_cond_wait(&this->jobadded
, &this->mutex
);
115 this->idle_threads
--;
118 this->list
->remove_first(this->list
, (void**)&job
);
119 pthread_mutex_unlock(&this->mutex
);
120 /* terminated threads are restarted, so we have a constant pool */
121 pthread_cleanup_push((void*)restart
, this);
123 pthread_cleanup_pop(0);
124 pthread_mutex_lock(&this->mutex
);
126 this->total_threads
--;
127 pthread_cond_signal(&this->threadterminated
);
128 pthread_mutex_unlock(&this->mutex
);
132 * Implementation of processor_t.get_total_threads.
134 static u_int
get_total_threads(private_processor_t
*this)
137 pthread_mutex_lock(&this->mutex
);
138 count
= this->total_threads
;
139 pthread_mutex_unlock(&this->mutex
);
144 * Implementation of processor_t.get_idle_threads.
146 static u_int
get_idle_threads(private_processor_t
*this)
149 pthread_mutex_lock(&this->mutex
);
150 count
= this->idle_threads
;
151 pthread_mutex_unlock(&this->mutex
);
156 * implements processor_t.get_job_load
158 static u_int
get_job_load(private_processor_t
*this)
161 pthread_mutex_lock(&this->mutex
);
162 load
= this->list
->get_count(this->list
);
163 pthread_mutex_unlock(&this->mutex
);
168 * implements function processor_t.queue_job
170 static void queue_job(private_processor_t
*this, job_t
*job
)
172 pthread_mutex_lock(&this->mutex
);
173 this->list
->insert_last(this->list
, job
);
174 pthread_cond_signal(&this->jobadded
);
175 pthread_mutex_unlock(&this->mutex
);
179 * Implementation of processor_t.set_threads.
181 static void set_threads(private_processor_t
*this, u_int count
)
183 pthread_mutex_lock(&this->mutex
);
184 if (count
> this->total_threads
)
185 { /* increase thread count */
189 this->desired_threads
= count
;
190 DBG1(DBG_JOB
, "spawning %d worker threads", count
- this->total_threads
);
191 for (i
= this->total_threads
; i
< count
; i
++)
193 if (pthread_create(¤t
, NULL
, (void*)process_jobs
, this) == 0)
195 this->total_threads
++;
199 else if (count
< this->total_threads
)
200 { /* decrease thread count */
201 this->desired_threads
= count
;
203 pthread_cond_broadcast(&this->jobadded
);
204 pthread_mutex_unlock(&this->mutex
);
208 * Implementation of processor_t.destroy.
210 static void destroy(private_processor_t
*this)
212 set_threads(this, 0);
213 pthread_mutex_lock(&this->mutex
);
214 while (this->total_threads
> 0)
216 pthread_cond_broadcast(&this->jobadded
);
217 pthread_cond_wait(&this->threadterminated
, &this->mutex
);
219 pthread_mutex_unlock(&this->mutex
);
220 this->list
->destroy_offset(this->list
, offsetof(job_t
, destroy
));
225 * Described in header.
227 processor_t
*processor_create(size_t pool_size
)
229 private_processor_t
*this = malloc_thing(private_processor_t
);
231 this->public.get_total_threads
= (u_int(*)(processor_t
*))get_total_threads
;
232 this->public.get_idle_threads
= (u_int(*)(processor_t
*))get_idle_threads
;
233 this->public.get_job_load
= (u_int(*)(processor_t
*))get_job_load
;
234 this->public.queue_job
= (void(*)(processor_t
*, job_t
*))queue_job
;
235 this->public.set_threads
= (void(*)(processor_t
*, u_int
))set_threads
;
236 this->public.destroy
= (void(*)(processor_t
*))destroy
;
238 this->list
= linked_list_create();
239 pthread_mutex_init(&this->mutex
, NULL
);
240 pthread_cond_init(&this->jobadded
, NULL
);
241 pthread_cond_init(&this->threadterminated
, NULL
);
242 this->total_threads
= 0;
243 this->desired_threads
= 0;
244 this->idle_threads
= 0;
246 return &this->public;