2 * Unix SMB/CIFS implementation.
3 * sync dummy implementation of the pthreadpool API
4 * Copyright (C) Volker Lendecke 2009
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.
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.
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/>.
30 #include "pthreadpool.h"
39 * Have we sent something into the pipe that has not been
45 * Jobids that we have not sent into the pipe yet
51 int pthreadpool_init(unsigned max_threads
, struct pthreadpool
**presult
)
53 struct pthreadpool
*pool
;
56 pool
= (struct pthreadpool
*)calloc(1, sizeof(struct pthreadpool
));
60 ret
= pipe(pool
->sig_pipe
);
70 int pthreadpool_signal_fd(struct pthreadpool
*pool
)
72 return pool
->sig_pipe
[0];
75 static int pthreadpool_write_to_pipe(struct pthreadpool
*pool
)
79 if (pool
->pipe_busy
) {
82 if (pool
->num_ids
== 0) {
89 while ((written
== -1) && (errno
== EINTR
)) {
90 written
= write(pool
->sig_pipe
[1], &pool
->ids
[0], sizeof(int));
95 if (written
!= sizeof(int)) {
97 * If a single int only partially fits into the pipe,
98 * we can assume ourselves pretty broken
100 close(pool
->sig_pipe
[1]);
101 pool
->sig_pipe
[1] = -1;
105 if (pool
->num_ids
> 1) {
106 memmove(pool
->ids
, pool
->ids
+1, sizeof(int) * (pool
->num_ids
-1));
113 int pthreadpool_add_job(struct pthreadpool
*pool
, int job_id
,
114 void (*fn
)(void *private_data
), void *private_data
)
118 if (pool
->sig_pipe
[1] == -1) {
124 tmp
= realloc(pool
->ids
, sizeof(int) * (pool
->num_ids
+1));
129 pool
->ids
[pool
->num_ids
] = job_id
;
132 return pthreadpool_write_to_pipe(pool
);
136 int pthreadpool_finished_job(struct pthreadpool
*pool
, int *jobid
)
144 while ((nread
== -1) && (errno
== EINTR
)) {
145 nread
= read(pool
->sig_pipe
[0], &ret_jobid
, sizeof(int));
150 if (nread
!= sizeof(int)) {
156 return pthreadpool_write_to_pipe(pool
);
159 int pthreadpool_destroy(struct pthreadpool
*pool
)
161 if (pool
->sig_pipe
[0] != -1) {
162 close(pool
->sig_pipe
[0]);
163 pool
->sig_pipe
[0] = -1;
166 if (pool
->sig_pipe
[1] != -1) {
167 close(pool
->sig_pipe
[1]);
168 pool
->sig_pipe
[1] = -1;