1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2009 Index Data
4 Pazpar2 is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "sel_thread.h"
34 struct work_item *next;
37 static struct work_item *queue_remove_last(struct work_item **q)
39 struct work_item **work_p = q, *work_this = 0;
41 while (*work_p && (*work_p)->next)
42 work_p = &(*work_p)->next;
51 static void queue_trav(struct work_item *q, void (*f)(void *data))
53 for (; q; q = q->next)
61 pthread_mutex_t mutex;
62 pthread_cond_t input_data;
65 struct work_item *input_queue;
66 struct work_item *output_queue;
67 struct work_item *free_queue;
68 void (*work_handler)(void *work_data);
69 void (*work_destroy)(void *work_data);
72 static void *sel_thread_handler(void *vp)
74 sel_thread_t p = (sel_thread_t) vp;
78 struct work_item *work_this = 0;
79 /* wait for some work */
80 pthread_mutex_lock(&p->mutex);
81 while (!p->stop_flag && !p->input_queue)
82 pthread_cond_wait(&p->input_data, &p->mutex);
83 /* see if we were waken up because we're shutting down */
86 /* got something. Take the last one out of input_queue */
88 assert(p->input_queue);
89 work_this = queue_remove_last(&p->input_queue);
92 pthread_mutex_unlock(&p->mutex);
94 /* work on this item */
95 p->work_handler(work_this->data);
97 /* put it back into output queue */
98 pthread_mutex_lock(&p->mutex);
99 work_this->next = p->output_queue;
100 p->output_queue = work_this;
101 pthread_mutex_unlock(&p->mutex);
103 /* wake up select/poll with a single byte */
104 write(p->fd[1], "", 1);
106 pthread_mutex_unlock(&p->mutex);
110 sel_thread_t sel_thread_create(void (*work_handler)(void *work_data),
111 void (*work_destroy)(void *work_data),
112 int *read_fd, int no_of_threads)
115 NMEM nmem = nmem_create();
116 sel_thread_t p = nmem_malloc(nmem, sizeof(*p));
118 assert(work_handler);
119 /* work_destroy may be NULL */
121 assert(no_of_threads >= 1);
133 p->work_handler = work_handler;
134 p->work_destroy = work_destroy;
137 p->no_threads = no_of_threads;
138 pthread_mutex_init(&p->mutex, 0);
139 pthread_cond_init(&p->input_data, 0);
141 p->thread_id = nmem_malloc(nmem, sizeof(*p->thread_id) * p->no_threads);
142 for (i = 0; i < p->no_threads; i++)
143 pthread_create (p->thread_id + i, 0, sel_thread_handler, p);
147 void sel_thread_destroy(sel_thread_t p)
150 pthread_mutex_lock(&p->mutex);
152 pthread_cond_broadcast(&p->input_data);
153 pthread_mutex_unlock(&p->mutex);
155 for (i = 0; i< p->no_threads; i++)
156 pthread_join(p->thread_id[i], 0);
160 queue_trav(p->input_queue, p->work_destroy);
161 queue_trav(p->output_queue, p->work_destroy);
166 pthread_cond_destroy(&p->input_data);
167 pthread_mutex_destroy(&p->mutex);
168 nmem_destroy(p->nmem);
171 void sel_thread_add(sel_thread_t p, void *data)
173 struct work_item *work_p;
175 pthread_mutex_lock(&p->mutex);
179 work_p = p->free_queue;
180 p->free_queue = p->free_queue->next;
183 work_p = nmem_malloc(p->nmem, sizeof(*work_p));
186 work_p->next = p->input_queue;
187 p->input_queue = work_p;
189 pthread_cond_signal(&p->input_data);
190 pthread_mutex_unlock(&p->mutex);
193 void *sel_thread_result(sel_thread_t p)
195 struct work_item *work_this = 0;
199 pthread_mutex_lock(&p->mutex);
201 /* got something. Take the last one out of output_queue */
202 work_this = queue_remove_last(&p->output_queue);
205 /* put freed item in free list */
206 work_this->next = p->free_queue;
207 p->free_queue = work_this;
209 data = work_this->data;
210 read(p->fd[0], read_buf, 1);
212 pthread_mutex_unlock(&p->mutex);
219 * indent-tabs-mode: nil
221 * vim: shiftwidth=4 tabstop=8 expandtab