]>
Commit | Line | Data |
---|---|---|
adca27eb JH |
1 | /** |
2 | * @file job_queue.c | |
79538669 | 3 | * |
d048df5c | 4 | * @brief Implementation of job_queue_t |
79538669 | 5 | * |
adca27eb JH |
6 | */ |
7 | ||
8 | /* | |
c71d53ba MW |
9 | * Copyright (C) 2005-2006 Martin Willi |
10 | * Copyright (C) 2005 Jan Hutter | |
adca27eb JH |
11 | * Hochschule fuer Technik Rapperswil |
12 | * | |
13 | * This program is free software; you can redistribute it and/or modify it | |
14 | * under the terms of the GNU General Public License as published by the | |
15 | * Free Software Foundation; either version 2 of the License, or (at your | |
16 | * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. | |
17 | * | |
18 | * This program is distributed in the hope that it will be useful, but | |
19 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | |
20 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
21 | * for more details. | |
22 | */ | |
23 | ||
954d34e3 | 24 | #include <stdlib.h> |
adca27eb | 25 | #include <pthread.h> |
79538669 | 26 | |
adca27eb | 27 | #include "job_queue.h" |
b85d20d1 | 28 | |
8080fccb | 29 | #include <utils/linked_list.h> |
adca27eb | 30 | |
95c61cb9 JH |
31 | |
32 | typedef struct private_job_queue_t private_job_queue_t; | |
33 | ||
adca27eb JH |
34 | /** |
35 | * @brief Private Variables and Functions of job_queue class | |
79538669 | 36 | * |
adca27eb | 37 | */ |
95c61cb9 | 38 | struct private_job_queue_t { |
a6cbf648 MW |
39 | |
40 | /** | |
41 | * public members | |
42 | */ | |
adca27eb | 43 | job_queue_t public; |
79538669 | 44 | |
adca27eb JH |
45 | /** |
46 | * The jobs are stored in a linked list | |
47 | */ | |
48 | linked_list_t *list; | |
a6cbf648 | 49 | |
adca27eb JH |
50 | /** |
51 | * access to linked_list is locked through this mutex | |
52 | */ | |
53 | pthread_mutex_t mutex; | |
79538669 | 54 | |
adca27eb JH |
55 | /** |
56 | * If the queue is empty a thread has to wait | |
57 | * This condvar is used to wake up such a thread | |
58 | */ | |
79538669 | 59 | pthread_cond_t condvar; |
adca27eb JH |
60 | }; |
61 | ||
62 | ||
63 | /** | |
d048df5c | 64 | * implements job_queue_t.get_count |
adca27eb | 65 | */ |
1061c878 | 66 | static int get_count(private_job_queue_t *this) |
adca27eb | 67 | { |
1061c878 | 68 | int count; |
adca27eb | 69 | pthread_mutex_lock(&(this->mutex)); |
1061c878 | 70 | count = this->list->get_count(this->list); |
adca27eb | 71 | pthread_mutex_unlock(&(this->mutex)); |
1061c878 | 72 | return count; |
adca27eb JH |
73 | } |
74 | ||
75 | /** | |
d048df5c | 76 | * implements job_queue_t.get |
adca27eb | 77 | */ |
d048df5c | 78 | static job_t *get(private_job_queue_t *this) |
adca27eb | 79 | { |
db5dc986 | 80 | int oldstate; |
d048df5c | 81 | job_t *job; |
adca27eb | 82 | pthread_mutex_lock(&(this->mutex)); |
db5dc986 | 83 | /* go to wait while no jobs available */ |
1061c878 | 84 | while(this->list->get_count(this->list) == 0) |
adca27eb | 85 | { |
db5dc986 MW |
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); | |
abba7ecb | 89 | |
adca27eb | 90 | pthread_cond_wait( &(this->condvar), &(this->mutex)); |
abba7ecb | 91 | |
db5dc986 MW |
92 | /* reset cancellation, remove mutex-unlock handler (without executing) */ |
93 | pthread_setcancelstate(oldstate, NULL); | |
94 | pthread_cleanup_pop(0); | |
adca27eb | 95 | } |
abba7ecb | 96 | this->list->remove_first(this->list, (void **)&job); |
adca27eb | 97 | pthread_mutex_unlock(&(this->mutex)); |
d048df5c | 98 | return job; |
adca27eb JH |
99 | } |
100 | ||
101 | /** | |
d048df5c | 102 | * implements function job_queue_t.add |
adca27eb | 103 | */ |
d048df5c | 104 | static void add(private_job_queue_t *this, job_t *job) |
adca27eb | 105 | { |
adca27eb JH |
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)); | |
adca27eb JH |
110 | } |
111 | ||
112 | /** | |
d048df5c | 113 | * implements job_queue_t.destroy |
adca27eb | 114 | */ |
d048df5c | 115 | static void job_queue_destroy (private_job_queue_t *this) |
79538669 | 116 | { |
55bbff11 | 117 | this->list->destroy_offset(this->list, offsetof(job_t, destroy)); |
5113680f | 118 | free(this); |
adca27eb JH |
119 | } |
120 | ||
121 | /* | |
79538669 | 122 | * |
adca27eb JH |
123 | * Documented in header |
124 | */ | |
f768bdc3 | 125 | job_queue_t *job_queue_create(void) |
adca27eb | 126 | { |
5113680f | 127 | private_job_queue_t *this = malloc_thing(private_job_queue_t); |
79538669 | 128 | |
1061c878 | 129 | this->public.get_count = (int(*)(job_queue_t*))get_count; |
d048df5c MW |
130 | this->public.get = (job_t*(*)(job_queue_t*))get; |
131 | this->public.add = (void(*)(job_queue_t*, job_t*))add; | |
132 | this->public.destroy = (void(*)(job_queue_t*))job_queue_destroy; | |
79538669 | 133 | |
d048df5c | 134 | this->list = linked_list_create(); |
adca27eb JH |
135 | pthread_mutex_init(&(this->mutex), NULL); |
136 | pthread_cond_init(&(this->condvar), NULL); | |
79538669 | 137 | |
adca27eb JH |
138 | return (&this->public); |
139 | } |