source: vendor/current/source3/lib/pthreadpool.c@ 414

Last change on this file since 414 was 414, checked in by Herwig Bauernfeind, 16 years ago

Samba 3.5.0: Initial import

File size: 9.5 KB
Line 
1/*
2 * Unix SMB/CIFS implementation.
3 * thread pool implementation
4 * Copyright (C) Volker Lendecke 2009
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <errno.h>
21#include <stdio.h>
22#include <unistd.h>
23#include <stdlib.h>
24#include <string.h>
25#include <pthread.h>
26#include <signal.h>
27#include <assert.h>
28#include <fcntl.h>
29
30#include "pthreadpool.h"
31
32struct pthreadpool_job {
33 struct pthreadpool_job *next;
34 int id;
35 void (*fn)(void *private_data);
36 void *private_data;
37};
38
39struct pthreadpool {
40 /*
41 * Control access to this struct
42 */
43 pthread_mutex_t mutex;
44
45 /*
46 * Threads waiting for work do so here
47 */
48 pthread_cond_t condvar;
49
50 /*
51 * List of work jobs
52 */
53 struct pthreadpool_job *jobs, *last_job;
54
55 /*
56 * pipe for signalling
57 */
58 int sig_pipe[2];
59
60 /*
61 * indicator to worker threads that they should shut down
62 */
63 int shutdown;
64
65 /*
66 * maximum number of threads
67 */
68 int max_threads;
69
70 /*
71 * Number of threads
72 */
73 int num_threads;
74
75 /*
76 * Number of idle threads
77 */
78 int num_idle;
79
80 /*
81 * An array of threads that require joining, the array has
82 * "max_threads" elements. It contains "num_exited" ids.
83 */
84 int num_exited;
85 pthread_t exited[1]; /* We alloc more */
86};
87
88/*
89 * Initialize a thread pool
90 */
91
92int pthreadpool_init(unsigned max_threads, struct pthreadpool **presult)
93{
94 struct pthreadpool *pool;
95 size_t size;
96 int ret;
97
98 size = sizeof(struct pthreadpool) + max_threads * sizeof(pthread_t);
99
100 pool = (struct pthreadpool *)malloc(size);
101 if (pool == NULL) {
102 return ENOMEM;
103 }
104
105 ret = pthread_mutex_init(&pool->mutex, NULL);
106 if (ret != 0) {
107 free(pool);
108 return ret;
109 }
110
111 ret = pthread_cond_init(&pool->condvar, NULL);
112 if (ret != 0) {
113 pthread_mutex_destroy(&pool->mutex);
114 free(pool);
115 return ret;
116 }
117
118 pool->shutdown = 0;
119 pool->jobs = pool->last_job = NULL;
120 pool->num_threads = 0;
121 pool->num_exited = 0;
122 pool->max_threads = max_threads;
123 pool->num_idle = 0;
124 pool->sig_pipe[0] = -1;
125 pool->sig_pipe[1] = -1;
126
127 *presult = pool;
128 return 0;
129}
130
131/*
132 * Create and return a file descriptor which becomes readable when a job has
133 * finished
134 */
135
136int pthreadpool_sig_fd(struct pthreadpool *pool)
137{
138 int result, ret;
139
140 ret = pthread_mutex_lock(&pool->mutex);
141 if (ret != 0) {
142 errno = ret;
143 return -1;
144 }
145
146 if (pool->sig_pipe[0] != -1) {
147 result = pool->sig_pipe[0];
148 goto done;
149 }
150
151 ret = pipe(pool->sig_pipe);
152 if (ret == -1) {
153 result = -1;
154 goto done;
155 }
156
157 result = pool->sig_pipe[0];
158done:
159 ret = pthread_mutex_unlock(&pool->mutex);
160 assert(ret == 0);
161 return result;
162}
163
164/*
165 * Do a pthread_join() on all children that have exited, pool->mutex must be
166 * locked
167 */
168static void pthreadpool_join_children(struct pthreadpool *pool)
169{
170 int i;
171
172 for (i=0; i<pool->num_exited; i++) {
173 pthread_join(pool->exited[i], NULL);
174 }
175 pool->num_exited = 0;
176}
177
178/*
179 * Fetch a finished job number from the signal pipe
180 */
181
182int pthreadpool_finished_job(struct pthreadpool *pool)
183{
184 int result, ret, fd;
185 ssize_t nread;
186
187 ret = pthread_mutex_lock(&pool->mutex);
188 if (ret != 0) {
189 errno = ret;
190 return -1;
191 }
192
193 /*
194 * Just some cleanup under the mutex
195 */
196 pthreadpool_join_children(pool);
197
198 fd = pool->sig_pipe[0];
199
200 ret = pthread_mutex_unlock(&pool->mutex);
201 assert(ret == 0);
202
203 if (fd == -1) {
204 errno = EINVAL;
205 return -1;
206 }
207
208 nread = -1;
209 errno = EINTR;
210
211 while ((nread == -1) && (errno == EINTR)) {
212 nread = read(fd, &result, sizeof(int));
213 }
214
215 /*
216 * TODO: handle nread > 0 && nread < sizeof(int)
217 */
218
219 /*
220 * Lock the mutex to provide a memory barrier for data from the worker
221 * thread to the main thread. The pipe access itself does not have to
222 * be locked, for sizeof(int) the write to a pipe is atomic, and only
223 * one thread reads from it. But we need to lock the mutex briefly
224 * even if we don't do anything under the lock, to make sure we can
225 * see all memory the helper thread has written.
226 */
227
228 ret = pthread_mutex_lock(&pool->mutex);
229 if (ret == -1) {
230 errno = ret;
231 return -1;
232 }
233
234 ret = pthread_mutex_unlock(&pool->mutex);
235 assert(ret == 0);
236
237 return result;
238}
239
240/*
241 * Destroy a thread pool, finishing all threads working for it
242 */
243
244int pthreadpool_destroy(struct pthreadpool *pool)
245{
246 int ret, ret1;
247
248 ret = pthread_mutex_lock(&pool->mutex);
249 if (ret != 0) {
250 return ret;
251 }
252
253 if (pool->num_threads > 0) {
254 /*
255 * We have active threads, tell them to finish, wait for that.
256 */
257
258 pool->shutdown = 1;
259
260 if (pool->num_idle > 0) {
261 /*
262 * Wake the idle threads. They will find pool->quit to
263 * be set and exit themselves
264 */
265 ret = pthread_cond_broadcast(&pool->condvar);
266 if (ret != 0) {
267 pthread_mutex_unlock(&pool->mutex);
268 return ret;
269 }
270 }