4 * @brief Implementation of job_queue_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 "job_queue.h"
28 #include <utils/allocator.h>
29 #include <utils/linked_list.h>
32 typedef struct private_job_queue_t private_job_queue_t
;
35 * @brief Private Variables and Functions of job_queue class
38 struct private_job_queue_t
{
42 * The jobs are stored in a linked list
46 * access to linked_list is locked through this mutex
48 pthread_mutex_t mutex
;
51 * If the queue is empty a thread has to wait
52 * This condvar is used to wake up such a thread
54 pthread_cond_t condvar
;
59 * implements job_queue_t.get_count
61 static int get_count(private_job_queue_t
*this)
64 pthread_mutex_lock(&(this->mutex
));
65 count
= this->list
->get_count(this->list
);
66 pthread_mutex_unlock(&(this->mutex
));
71 * implements job_queue_t.get
73 static job_t
*get(private_job_queue_t
*this)
77 pthread_mutex_lock(&(this->mutex
));
78 /* go to wait while no jobs available */
79 while(this->list
->get_count(this->list
) == 0)
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
);
85 pthread_cond_wait( &(this->condvar
), &(this->mutex
));
87 /* reset cancellation, remove mutex-unlock handler (without executing) */
88 pthread_setcancelstate(oldstate
, NULL
);
89 pthread_cleanup_pop(0);
91 this->list
->remove_first(this->list
,(void **) &job
);
92 pthread_mutex_unlock(&(this->mutex
));
97 * implements function job_queue_t.add
99 static void add(private_job_queue_t
*this, job_t
*job
)
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
));
108 * implements job_queue_t.destroy
110 static void job_queue_destroy (private_job_queue_t
*this)
112 while (this->list
->get_count(this->list
) > 0)
115 if (this->list
->remove_first(this->list
,(void *) &job
) != SUCCESS
)
117 this->list
->destroy(this->list
);
120 job
->destroy_all(job
);
122 this->list
->destroy(this->list
);
124 pthread_mutex_destroy(&(this->mutex
));
126 pthread_cond_destroy(&(this->condvar
));
128 allocator_free(this);
133 * Documented in header
135 job_queue_t
*job_queue_create()
137 private_job_queue_t
*this = allocator_alloc_thing(private_job_queue_t
);
139 this->public.get_count
= (int(*)(job_queue_t
*))get_count
;
140 this->public.get
= (job_t
*(*)(job_queue_t
*))get
;
141 this->public.add
= (void(*)(job_queue_t
*, job_t
*))add
;
142 this->public.destroy
= (void(*)(job_queue_t
*))job_queue_destroy
;
144 this->list
= linked_list_create();
145 pthread_mutex_init(&(this->mutex
), NULL
);
146 pthread_cond_init(&(this->condvar
), NULL
);
148 return (&this->public);