|
|
1 | /* |
|
|
2 | * libeio implementation |
|
|
3 | * |
|
|
4 | * Copyright (c) 2007,2008,2009 Marc Alexander Lehmann <libeio@schmorp.de> |
|
|
5 | * All rights reserved. |
|
|
6 | * |
|
|
7 | * Redistribution and use in source and binary forms, with or without modifica- |
|
|
8 | * tion, are permitted provided that the following conditions are met: |
|
|
9 | * |
|
|
10 | * 1. Redistributions of source code must retain the above copyright notice, |
|
|
11 | * this list of conditions and the following disclaimer. |
|
|
12 | * |
|
|
13 | * 2. Redistributions in binary form must reproduce the above copyright |
|
|
14 | * notice, this list of conditions and the following disclaimer in the |
|
|
15 | * documentation and/or other materials provided with the distribution. |
|
|
16 | * |
|
|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
|
|
18 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- |
|
|
19 | * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
|
|
20 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- |
|
|
21 | * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
|
22 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
|
|
23 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|
|
24 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- |
|
|
25 | * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
|
|
26 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
|
|
27 | * |
|
|
28 | * Alternatively, the contents of this file may be used under the terms of |
|
|
29 | * the GNU General Public License ("GPL") version 2 or any later version, |
|
|
30 | * in which case the provisions of the GPL are applicable instead of |
|
|
31 | * the above. If you wish to allow the use of your version of this file |
|
|
32 | * only under the terms of the GPL and not to allow others to use your |
|
|
33 | * version of this file under the BSD license, indicate your decision |
|
|
34 | * by deleting the provisions above and replace them with the notice |
|
|
35 | * and other provisions required by the GPL. If you do not delete the |
|
|
36 | * provisions above, a recipient may use your version of this file under |
|
|
37 | * either the BSD or the GPL. |
|
|
38 | */ |
|
|
39 | |
1 | #include "eio.h" |
40 | #include "eio.h" |
|
|
41 | |
|
|
42 | #ifdef EIO_STACKSIZE |
|
|
43 | # define XTHREAD_STACKSIZE EIO_STACKSIZE |
|
|
44 | #endif |
2 | #include "xthread.h" |
45 | #include "xthread.h" |
3 | |
46 | |
4 | #include <errno.h> |
47 | #include <errno.h> |
5 | #include <stddef.h> |
48 | #include <stddef.h> |
6 | #include <stdlib.h> |
49 | #include <stdlib.h> |
… | |
… | |
25 | #endif |
68 | #endif |
26 | |
69 | |
27 | #ifdef _WIN32 |
70 | #ifdef _WIN32 |
28 | |
71 | |
29 | /*doh*/ |
72 | /*doh*/ |
30 | |
|
|
31 | #else |
73 | #else |
32 | |
74 | |
33 | # include "config.h" |
75 | # include "config.h" |
34 | # include <sys/time.h> |
76 | # include <sys/time.h> |
35 | # include <sys/select.h> |
77 | # include <sys/select.h> |
36 | # include <unistd.h> |
78 | # include <unistd.h> |
37 | # include <utime.h> |
79 | # include <utime.h> |
38 | # include <signal.h> |
80 | # include <signal.h> |
39 | # include <dirent.h> |
81 | # include <dirent.h> |
|
|
82 | |
|
|
83 | /* POSIX_SOURCE is useless on bsd's, and XOPEN_SOURCE is unreliable there, too */ |
|
|
84 | # if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) |
|
|
85 | # define D_INO(de) (de)->d_fileno |
|
|
86 | # define _DIRENT_HAVE_D_TYPE /* sigh */ |
|
|
87 | # elif defined(__linux) || defined(d_ino) || _XOPEN_SOURCE >= 600 |
|
|
88 | # define D_INO(de) (de)->d_ino |
|
|
89 | # endif |
|
|
90 | |
|
|
91 | # ifdef _DIRENT_HAVE_D_TYPE |
|
|
92 | # define D_TYPE(de) (de)->d_type |
|
|
93 | # endif |
40 | |
94 | |
41 | # ifndef EIO_STRUCT_DIRENT |
95 | # ifndef EIO_STRUCT_DIRENT |
42 | # define EIO_STRUCT_DIRENT struct dirent |
96 | # define EIO_STRUCT_DIRENT struct dirent |
43 | # endif |
97 | # endif |
44 | |
98 | |
… | |
… | |
57 | # else |
111 | # else |
58 | # error sendfile support requested but not available |
112 | # error sendfile support requested but not available |
59 | # endif |
113 | # endif |
60 | #endif |
114 | #endif |
61 | |
115 | |
|
|
116 | #ifndef D_TYPE |
|
|
117 | # define D_TYPE(de) 0 |
|
|
118 | #endif |
|
|
119 | |
|
|
120 | #ifndef D_INO |
|
|
121 | # define D_INO(de) 0 |
|
|
122 | #endif |
|
|
123 | |
62 | /* number of seconds after which an idle threads exit */ |
124 | /* number of seconds after which an idle threads exit */ |
63 | #define IDLE_TIMEOUT 10 |
125 | #define IDLE_TIMEOUT 10 |
64 | |
126 | |
65 | /* used for struct dirent, AIX doesn't provide it */ |
127 | /* used for struct dirent, AIX doesn't provide it */ |
66 | #ifndef NAME_MAX |
128 | #ifndef NAME_MAX |
… | |
… | |
70 | /* buffer size for various temporary buffers */ |
132 | /* buffer size for various temporary buffers */ |
71 | #define EIO_BUFSIZE 65536 |
133 | #define EIO_BUFSIZE 65536 |
72 | |
134 | |
73 | #define dBUF \ |
135 | #define dBUF \ |
74 | char *eio_buf; \ |
136 | char *eio_buf; \ |
75 | X_LOCK (etplock); \ |
137 | ETP_WORKER_LOCK (self); \ |
76 | self->dbuf = eio_buf = malloc (EIO_BUFSIZE); \ |
138 | self->dbuf = eio_buf = malloc (EIO_BUFSIZE); \ |
77 | X_UNLOCK (etplock); \ |
139 | ETP_WORKER_UNLOCK (self); \ |
78 | errno = ENOMEM; \ |
140 | errno = ENOMEM; \ |
79 | if (!eio_buf) \ |
141 | if (!eio_buf) \ |
80 | return -1; |
142 | return -1; |
81 | |
143 | |
82 | #define EIO_TICKS ((1000000 + 1023) >> 10) |
144 | #define EIO_TICKS ((1000000 + 1023) >> 10) |
83 | |
145 | |
84 | /*****************************************************************************/ |
146 | /*****************************************************************************/ |
85 | |
147 | |
|
|
148 | #if __GNUC__ >= 3 |
|
|
149 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
150 | #else |
|
|
151 | # define expect(expr,value) (expr) |
|
|
152 | #endif |
|
|
153 | |
|
|
154 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
155 | #define expect_true(expr) expect ((expr) != 0, 1) |
|
|
156 | |
|
|
157 | /*****************************************************************************/ |
|
|
158 | |
|
|
159 | #define ETP_PRI_MIN EIO_PRI_MIN |
|
|
160 | #define ETP_PRI_MAX EIO_PRI_MAX |
|
|
161 | |
|
|
162 | struct etp_worker; |
|
|
163 | |
|
|
164 | #define ETP_REQ eio_req |
|
|
165 | #define ETP_DESTROY(req) eio_destroy (req) |
|
|
166 | static int eio_finish (eio_req *req); |
|
|
167 | #define ETP_FINISH(req) eio_finish (req) |
|
|
168 | static void eio_execute (struct etp_worker *self, eio_req *req); |
|
|
169 | #define ETP_EXECUTE(wrk,req) eio_execute (wrk,req) |
|
|
170 | |
|
|
171 | #define ETP_WORKER_CLEAR(req) \ |
|
|
172 | if (wrk->dbuf) \ |
|
|
173 | { \ |
|
|
174 | free (wrk->dbuf); \ |
|
|
175 | wrk->dbuf = 0; \ |
|
|
176 | } \ |
|
|
177 | \ |
|
|
178 | if (wrk->dirp) \ |
|
|
179 | { \ |
|
|
180 | closedir (wrk->dirp); \ |
|
|
181 | wrk->dirp = 0; \ |
|
|
182 | } |
|
|
183 | #define ETP_WORKER_COMMON \ |
|
|
184 | void *dbuf; \ |
|
|
185 | DIR *dirp; |
|
|
186 | |
|
|
187 | /*****************************************************************************/ |
|
|
188 | |
|
|
189 | #define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1) |
|
|
190 | |
86 | /* calculcate time difference in ~1/EIO_TICKS of a second */ |
191 | /* calculcate time difference in ~1/EIO_TICKS of a second */ |
87 | static int tvdiff (struct timeval *tv1, struct timeval *tv2) |
192 | static int tvdiff (struct timeval *tv1, struct timeval *tv2) |
88 | { |
193 | { |
89 | return (tv2->tv_sec - tv1->tv_sec ) * EIO_TICKS |
194 | return (tv2->tv_sec - tv1->tv_sec ) * EIO_TICKS |
90 | + ((tv2->tv_usec - tv1->tv_usec) >> 10); |
195 | + ((tv2->tv_usec - tv1->tv_usec) >> 10); |
91 | } |
196 | } |
92 | |
197 | |
93 | static unsigned int started, idle, wanted = 4; |
198 | static unsigned int started, idle, wanted = 4; |
94 | |
199 | |
95 | typedef struct etp_pool |
|
|
96 | { |
|
|
97 | void (*want_poll_cb) (void); |
200 | static void (*want_poll_cb) (void); |
98 | void (*done_poll_cb) (void); |
201 | static void (*done_poll_cb) (void); |
99 | |
202 | |
100 | unsigned int max_poll_time; |
203 | static unsigned int max_poll_time; /* reslock */ |
101 | unsigned int max_poll_reqs; |
204 | static unsigned int max_poll_reqs; /* reslock */ |
102 | } etp_pool; |
|
|
103 | |
205 | |
104 | static volatile unsigned int nreqs, nready, npending; |
206 | static volatile unsigned int nreqs; /* reqlock */ |
|
|
207 | static volatile unsigned int nready; /* reqlock */ |
|
|
208 | static volatile unsigned int npending; /* reqlock */ |
105 | static volatile unsigned int max_idle = 4; |
209 | static volatile unsigned int max_idle = 4; |
106 | |
210 | |
107 | static mutex_t etplock = X_MUTEX_INIT; |
211 | static mutex_t wrklock = X_MUTEX_INIT; |
108 | static mutex_t reslock = X_MUTEX_INIT; |
212 | static mutex_t reslock = X_MUTEX_INIT; |
109 | static mutex_t reqlock = X_MUTEX_INIT; |
213 | static mutex_t reqlock = X_MUTEX_INIT; |
110 | static cond_t reqwait = X_COND_INIT; |
214 | static cond_t reqwait = X_COND_INIT; |
111 | |
215 | |
|
|
216 | #if !HAVE_PREADWRITE |
|
|
217 | /* |
|
|
218 | * make our pread/pwrite emulation safe against themselves, but not against |
|
|
219 | * normal read/write by using a mutex. slows down execution a lot, |
|
|
220 | * but that's your problem, not mine. |
|
|
221 | */ |
|
|
222 | static mutex_t preadwritelock = X_MUTEX_INIT; |
|
|
223 | #endif |
|
|
224 | |
112 | typedef struct worker |
225 | typedef struct etp_worker |
113 | { |
226 | { |
114 | /* locked by etplock */ |
227 | /* locked by wrklock */ |
115 | struct worker *prev, *next; |
228 | struct etp_worker *prev, *next; |
116 | |
229 | |
117 | thread_t tid; |
230 | thread_t tid; |
118 | |
231 | |
119 | /* locked by reslock, reqlock or etplock */ |
232 | /* locked by reslock, reqlock or wrklock */ |
120 | eio_req *req; /* currently processed request */ |
233 | ETP_REQ *req; /* currently processed request */ |
121 | void *dbuf; |
234 | |
122 | DIR *dirp; |
235 | ETP_WORKER_COMMON |
123 | } worker; |
236 | } etp_worker; |
124 | |
237 | |
125 | static worker wrk_first = { &wrk_first, &wrk_first, 0 }; /* NOT etp */ |
238 | static etp_worker wrk_first = { &wrk_first, &wrk_first, 0 }; /* NOT etp */ |
|
|
239 | |
|
|
240 | #define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock) |
|
|
241 | #define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock) |
126 | |
242 | |
127 | /* worker threads management */ |
243 | /* worker threads management */ |
128 | |
244 | |
129 | static void worker_clear (worker *wrk) |
245 | static void etp_worker_clear (etp_worker *wrk) |
130 | { |
246 | { |
131 | if (wrk->dirp) |
247 | ETP_WORKER_CLEAR (wrk); |
132 | { |
|
|
133 | closedir (wrk->dirp); |
|
|
134 | wrk->dirp = 0; |
|
|
135 | } |
|
|
136 | |
|
|
137 | if (wrk->dbuf) |
|
|
138 | { |
|
|
139 | free (wrk->dbuf); |
|
|
140 | wrk->dbuf = 0; |
|
|
141 | } |
|
|
142 | } |
248 | } |
143 | |
249 | |
144 | static void worker_free (worker *wrk) |
250 | static void etp_worker_free (etp_worker *wrk) |
145 | { |
251 | { |
146 | wrk->next->prev = wrk->prev; |
252 | wrk->next->prev = wrk->prev; |
147 | wrk->prev->next = wrk->next; |
253 | wrk->prev->next = wrk->next; |
148 | |
254 | |
149 | free (wrk); |
255 | free (wrk); |
150 | } |
256 | } |
151 | |
257 | |
152 | unsigned int eio_nreqs (void) |
258 | static unsigned int etp_nreqs (void) |
153 | { |
259 | { |
|
|
260 | int retval; |
|
|
261 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
|
|
262 | retval = nreqs; |
|
|
263 | if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); |
154 | return nreqs; |
264 | return retval; |
155 | } |
265 | } |
156 | |
266 | |
157 | unsigned int eio_nready (void) |
267 | static unsigned int etp_nready (void) |
158 | { |
268 | { |
159 | unsigned int retval; |
269 | unsigned int retval; |
160 | |
270 | |
161 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
271 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
162 | retval = nready; |
272 | retval = nready; |
163 | if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); |
273 | if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); |
164 | |
274 | |
165 | return retval; |
275 | return retval; |
166 | } |
276 | } |
167 | |
277 | |
168 | unsigned int eio_npending (void) |
278 | static unsigned int etp_npending (void) |
169 | { |
279 | { |
170 | unsigned int retval; |
280 | unsigned int retval; |
171 | |
281 | |
172 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
282 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
173 | retval = npending; |
283 | retval = npending; |
174 | if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); |
284 | if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); |
175 | |
285 | |
176 | return retval; |
286 | return retval; |
177 | } |
287 | } |
178 | |
288 | |
179 | unsigned int eio_nthreads (void) |
289 | static unsigned int etp_nthreads (void) |
180 | { |
290 | { |
181 | unsigned int retval; |
291 | unsigned int retval; |
182 | |
292 | |
183 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
293 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
184 | retval = started; |
294 | retval = started; |
… | |
… | |
191 | * a somewhat faster data structure might be nice, but |
301 | * a somewhat faster data structure might be nice, but |
192 | * with 8 priorities this actually needs <20 insns |
302 | * with 8 priorities this actually needs <20 insns |
193 | * per shift, the most expensive operation. |
303 | * per shift, the most expensive operation. |
194 | */ |
304 | */ |
195 | typedef struct { |
305 | typedef struct { |
196 | eio_req *qs[EIO_NUM_PRI], *qe[EIO_NUM_PRI]; /* qstart, qend */ |
306 | ETP_REQ *qs[ETP_NUM_PRI], *qe[ETP_NUM_PRI]; /* qstart, qend */ |
197 | int size; |
307 | int size; |
198 | } reqq; |
308 | } etp_reqq; |
199 | |
309 | |
200 | static reqq req_queue; |
310 | static etp_reqq req_queue; |
201 | static reqq res_queue; |
311 | static etp_reqq res_queue; |
202 | |
312 | |
203 | static int reqq_push (reqq *q, eio_req *req) |
313 | static int reqq_push (etp_reqq *q, ETP_REQ *req) |
204 | { |
314 | { |
205 | int pri = req->pri; |
315 | int pri = req->pri; |
206 | req->next = 0; |
316 | req->next = 0; |
207 | |
317 | |
208 | if (q->qe[pri]) |
318 | if (q->qe[pri]) |
… | |
… | |
214 | q->qe[pri] = q->qs[pri] = req; |
324 | q->qe[pri] = q->qs[pri] = req; |
215 | |
325 | |
216 | return q->size++; |
326 | return q->size++; |
217 | } |
327 | } |
218 | |
328 | |
219 | static eio_req *reqq_shift (reqq *q) |
329 | static ETP_REQ *reqq_shift (etp_reqq *q) |
220 | { |
330 | { |
221 | int pri; |
331 | int pri; |
222 | |
332 | |
223 | if (!q->size) |
333 | if (!q->size) |
224 | return 0; |
334 | return 0; |
225 | |
335 | |
226 | --q->size; |
336 | --q->size; |
227 | |
337 | |
228 | for (pri = EIO_NUM_PRI; pri--; ) |
338 | for (pri = ETP_NUM_PRI; pri--; ) |
229 | { |
339 | { |
230 | eio_req *req = q->qs[pri]; |
340 | eio_req *req = q->qs[pri]; |
231 | |
341 | |
232 | if (req) |
342 | if (req) |
233 | { |
343 | { |
… | |
… | |
241 | abort (); |
351 | abort (); |
242 | } |
352 | } |
243 | |
353 | |
244 | static void etp_atfork_prepare (void) |
354 | static void etp_atfork_prepare (void) |
245 | { |
355 | { |
246 | X_LOCK (etplock); |
356 | X_LOCK (wrklock); |
247 | X_LOCK (reqlock); |
357 | X_LOCK (reqlock); |
248 | X_LOCK (reslock); |
358 | X_LOCK (reslock); |
249 | #if !HAVE_PREADWRITE |
359 | #if !HAVE_PREADWRITE |
250 | X_LOCK (preadwritelock); |
360 | X_LOCK (preadwritelock); |
251 | #endif |
361 | #endif |
252 | #if !HAVE_READDIR_R |
|
|
253 | X_LOCK (readdirlock); |
|
|
254 | #endif |
|
|
255 | } |
362 | } |
256 | |
363 | |
257 | static void etp_atfork_parent (void) |
364 | static void etp_atfork_parent (void) |
258 | { |
365 | { |
259 | #if !HAVE_READDIR_R |
|
|
260 | X_UNLOCK (readdirlock); |
|
|
261 | #endif |
|
|
262 | #if !HAVE_PREADWRITE |
366 | #if !HAVE_PREADWRITE |
263 | X_UNLOCK (preadwritelock); |
367 | X_UNLOCK (preadwritelock); |
264 | #endif |
368 | #endif |
265 | X_UNLOCK (reslock); |
369 | X_UNLOCK (reslock); |
266 | X_UNLOCK (reqlock); |
370 | X_UNLOCK (reqlock); |
267 | X_UNLOCK (etplock); |
371 | X_UNLOCK (wrklock); |
268 | } |
372 | } |
269 | |
373 | |
270 | static void etp_atfork_child (void) |
374 | static void etp_atfork_child (void) |
271 | { |
375 | { |
272 | eio_req *prv; |
376 | ETP_REQ *prv; |
273 | |
377 | |
274 | while (prv = reqq_shift (&req_queue)) |
378 | while ((prv = reqq_shift (&req_queue))) |
275 | eio_destroy (prv); |
379 | ETP_DESTROY (prv); |
276 | |
380 | |
277 | while (prv = reqq_shift (&res_queue)) |
381 | while ((prv = reqq_shift (&res_queue))) |
278 | eio_destroy (prv); |
382 | ETP_DESTROY (prv); |
279 | |
383 | |
280 | while (wrk_first.next != &wrk_first) |
384 | while (wrk_first.next != &wrk_first) |
281 | { |
385 | { |
282 | worker *wrk = wrk_first.next; |
386 | etp_worker *wrk = wrk_first.next; |
283 | |
387 | |
284 | if (wrk->req) |
388 | if (wrk->req) |
285 | eio_destroy (wrk->req); |
389 | ETP_DESTROY (wrk->req); |
286 | |
390 | |
287 | worker_clear (wrk); |
391 | etp_worker_clear (wrk); |
288 | worker_free (wrk); |
392 | etp_worker_free (wrk); |
289 | } |
393 | } |
290 | |
394 | |
291 | started = 0; |
395 | started = 0; |
292 | idle = 0; |
396 | idle = 0; |
293 | nreqs = 0; |
397 | nreqs = 0; |
… | |
… | |
302 | { |
406 | { |
303 | X_THREAD_ATFORK (etp_atfork_prepare, etp_atfork_parent, etp_atfork_child); |
407 | X_THREAD_ATFORK (etp_atfork_prepare, etp_atfork_parent, etp_atfork_child); |
304 | } |
408 | } |
305 | |
409 | |
306 | static int |
410 | static int |
307 | etp_init (etp_pool *etp, void (*want_poll)(void), void (*done_poll)(void)) |
411 | etp_init (void (*want_poll)(void), void (*done_poll)(void)) |
308 | { |
412 | { |
309 | static pthread_once_t doinit = PTHREAD_ONCE_INIT; |
413 | static pthread_once_t doinit = PTHREAD_ONCE_INIT; |
310 | |
414 | |
311 | pthread_once (&doinit, etp_once_init); |
415 | pthread_once (&doinit, etp_once_init); |
312 | |
416 | |
313 | memset (etp, 0, sizeof *etp); |
|
|
314 | |
|
|
315 | etp->want_poll_cb = want_poll; |
417 | want_poll_cb = want_poll; |
316 | etp->done_poll_cb = done_poll; |
418 | done_poll_cb = done_poll; |
317 | } |
|
|
318 | |
419 | |
319 | static etp_pool etp; |
|
|
320 | |
|
|
321 | /*****************************************************************************/ |
|
|
322 | |
|
|
323 | static void grp_try_feed (eio_req *grp) |
|
|
324 | { |
|
|
325 | while (grp->size < grp->int2 && !EIO_CANCELLED (grp)) |
|
|
326 | { |
|
|
327 | int old_len = grp->size; |
|
|
328 | |
|
|
329 | EIO_FEED (grp); |
|
|
330 | |
|
|
331 | /* stop if no progress has been made */ |
|
|
332 | if (old_len == grp->size) |
|
|
333 | { |
|
|
334 | grp->feed = 0; |
|
|
335 | break; |
|
|
336 | } |
|
|
337 | } |
|
|
338 | } |
|
|
339 | |
|
|
340 | static int eio_finish (eio_req *req); |
|
|
341 | |
|
|
342 | static int grp_dec (eio_req *grp) |
|
|
343 | { |
|
|
344 | --grp->size; |
|
|
345 | |
|
|
346 | /* call feeder, if applicable */ |
|
|
347 | grp_try_feed (grp); |
|
|
348 | |
|
|
349 | /* finish, if done */ |
|
|
350 | if (!grp->size && grp->int1) |
|
|
351 | return eio_finish (grp); |
|
|
352 | else |
|
|
353 | return 0; |
420 | return 0; |
354 | } |
421 | } |
355 | |
422 | |
356 | void eio_destroy (eio_req *req) |
|
|
357 | { |
|
|
358 | if ((req)->flags & EIO_FLAG_PTR1_FREE) free (req->ptr1); |
|
|
359 | if ((req)->flags & EIO_FLAG_PTR2_FREE) free (req->ptr2); |
|
|
360 | |
|
|
361 | EIO_DESTROY (req); |
|
|
362 | } |
|
|
363 | |
|
|
364 | static int eio_finish (eio_req *req) |
|
|
365 | { |
|
|
366 | int res = EIO_FINISH (req); |
|
|
367 | |
|
|
368 | if (req->grp) |
|
|
369 | { |
|
|
370 | int res2; |
|
|
371 | eio_req *grp = req->grp; |
|
|
372 | |
|
|
373 | /* unlink request */ |
|
|
374 | if (req->grp_next) req->grp_next->grp_prev = req->grp_prev; |
|
|
375 | if (req->grp_prev) req->grp_prev->grp_next = req->grp_next; |
|
|
376 | |
|
|
377 | if (grp->grp_first == req) |
|
|
378 | grp->grp_first = req->grp_next; |
|
|
379 | |
|
|
380 | res2 = grp_dec (grp); |
|
|
381 | |
|
|
382 | if (!res && res2) |
|
|
383 | res = res2; |
|
|
384 | } |
|
|
385 | |
|
|
386 | eio_destroy (req); |
|
|
387 | |
|
|
388 | return res; |
|
|
389 | } |
|
|
390 | |
|
|
391 | void eio_grp_cancel (eio_req *grp) |
|
|
392 | { |
|
|
393 | for (grp = grp->grp_first; grp; grp = grp->grp_next) |
|
|
394 | eio_cancel (grp); |
|
|
395 | } |
|
|
396 | |
|
|
397 | void eio_cancel (eio_req *req) |
|
|
398 | { |
|
|
399 | X_LOCK (etplock); |
|
|
400 | req->flags |= EIO_FLAG_CANCELLED; |
|
|
401 | X_UNLOCK (etplock); |
|
|
402 | |
|
|
403 | eio_grp_cancel (req); |
|
|
404 | } |
|
|
405 | |
|
|
406 | X_THREAD_PROC (eio_proc); |
423 | X_THREAD_PROC (etp_proc); |
407 | |
424 | |
408 | static void start_thread (void) |
425 | static void etp_start_thread (void) |
409 | { |
426 | { |
410 | worker *wrk = calloc (1, sizeof (worker)); |
427 | etp_worker *wrk = calloc (1, sizeof (etp_worker)); |
411 | |
428 | |
412 | /*TODO*/ |
429 | /*TODO*/ |
413 | assert (("unable to allocate worker thread data", wrk)); |
430 | assert (("unable to allocate worker thread data", wrk)); |
414 | |
431 | |
415 | X_LOCK (etplock); |
432 | X_LOCK (wrklock); |
416 | |
433 | |
417 | if (thread_create (&wrk->tid, eio_proc, (void *)wrk)) |
434 | if (thread_create (&wrk->tid, etp_proc, (void *)wrk)) |
418 | { |
435 | { |
419 | wrk->prev = &wrk_first; |
436 | wrk->prev = &wrk_first; |
420 | wrk->next = wrk_first.next; |
437 | wrk->next = wrk_first.next; |
421 | wrk_first.next->prev = wrk; |
438 | wrk_first.next->prev = wrk; |
422 | wrk_first.next = wrk; |
439 | wrk_first.next = wrk; |
423 | ++started; |
440 | ++started; |
424 | } |
441 | } |
425 | else |
442 | else |
426 | free (wrk); |
443 | free (wrk); |
427 | |
444 | |
428 | X_UNLOCK (etplock); |
445 | X_UNLOCK (wrklock); |
429 | } |
446 | } |
430 | |
447 | |
431 | static void maybe_start_thread (void) |
448 | static void etp_maybe_start_thread (void) |
432 | { |
449 | { |
433 | if (eio_nthreads () >= wanted) |
450 | if (expect_true (etp_nthreads () >= wanted)) |
434 | return; |
451 | return; |
435 | |
452 | |
436 | /* todo: maybe use idle here, but might be less exact */ |
453 | /* todo: maybe use idle here, but might be less exact */ |
437 | if (0 <= (int)eio_nthreads () + (int)eio_npending () - (int)eio_nreqs ()) |
454 | if (expect_true (0 <= (int)etp_nthreads () + (int)etp_npending () - (int)etp_nreqs ())) |
438 | return; |
455 | return; |
439 | |
456 | |
440 | start_thread (); |
457 | etp_start_thread (); |
441 | } |
458 | } |
442 | |
459 | |
443 | void eio_submit (eio_req *req) |
|
|
444 | { |
|
|
445 | req->pri += EIO_PRI_BIAS; |
|
|
446 | |
|
|
447 | if (req->pri < EIO_PRI_MIN + EIO_PRI_BIAS) req->pri = EIO_PRI_MIN + EIO_PRI_BIAS; |
|
|
448 | if (req->pri > EIO_PRI_MAX + EIO_PRI_BIAS) req->pri = EIO_PRI_MAX + EIO_PRI_BIAS; |
|
|
449 | |
|
|
450 | ++nreqs; |
|
|
451 | |
|
|
452 | X_LOCK (reqlock); |
|
|
453 | ++nready; |
|
|
454 | reqq_push (&req_queue, req); |
|
|
455 | X_COND_SIGNAL (reqwait); |
|
|
456 | X_UNLOCK (reqlock); |
|
|
457 | |
|
|
458 | maybe_start_thread (); |
|
|
459 | } |
|
|
460 | |
|
|
461 | static void end_thread (void) |
460 | static void etp_end_thread (void) |
462 | { |
461 | { |
463 | eio_req *req = calloc (1, sizeof (eio_req)); |
462 | eio_req *req = calloc (1, sizeof (eio_req)); |
464 | |
463 | |
465 | req->type = EIO_QUIT; |
464 | req->type = -1; |
466 | req->pri = EIO_PRI_MAX + EIO_PRI_BIAS; |
465 | req->pri = ETP_PRI_MAX - ETP_PRI_MIN; |
467 | |
466 | |
468 | X_LOCK (reqlock); |
467 | X_LOCK (reqlock); |
469 | reqq_push (&req_queue, req); |
468 | reqq_push (&req_queue, req); |
470 | X_COND_SIGNAL (reqwait); |
469 | X_COND_SIGNAL (reqwait); |
471 | X_UNLOCK (reqlock); |
470 | X_UNLOCK (reqlock); |
472 | |
471 | |
473 | X_LOCK (etplock); |
472 | X_LOCK (wrklock); |
474 | --started; |
473 | --started; |
475 | X_UNLOCK (etplock); |
474 | X_UNLOCK (wrklock); |
476 | } |
475 | } |
477 | |
476 | |
478 | void eio_set_max_poll_time (double nseconds) |
|
|
479 | { |
|
|
480 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
|
|
481 | etp.max_poll_time = nseconds; |
|
|
482 | if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); |
|
|
483 | } |
|
|
484 | |
|
|
485 | void eio_set_max_poll_reqs (unsigned int maxreqs) |
|
|
486 | { |
|
|
487 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
|
|
488 | etp.max_poll_reqs = maxreqs; |
|
|
489 | if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); |
|
|
490 | } |
|
|
491 | |
|
|
492 | void eio_set_max_idle (unsigned int nthreads) |
|
|
493 | { |
|
|
494 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
|
|
495 | max_idle = nthreads <= 0 ? 1 : nthreads; |
|
|
496 | if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); |
|
|
497 | } |
|
|
498 | |
|
|
499 | void eio_set_min_parallel (unsigned int nthreads) |
|
|
500 | { |
|
|
501 | if (wanted < nthreads) |
|
|
502 | wanted = nthreads; |
|
|
503 | } |
|
|
504 | |
|
|
505 | void eio_set_max_parallel (unsigned int nthreads) |
|
|
506 | { |
|
|
507 | if (wanted > nthreads) |
|
|
508 | wanted = nthreads; |
|
|
509 | |
|
|
510 | while (started > wanted) |
|
|
511 | end_thread (); |
|
|
512 | } |
|
|
513 | |
|
|
514 | int eio_poll (void) |
477 | static int etp_poll (void) |
515 | { |
478 | { |
516 | int maxreqs = etp.max_poll_reqs; |
479 | unsigned int maxreqs; |
|
|
480 | unsigned int maxtime; |
517 | struct timeval tv_start, tv_now; |
481 | struct timeval tv_start, tv_now; |
518 | eio_req *req; |
|
|
519 | |
482 | |
520 | if (etp.max_poll_time) |
483 | X_LOCK (reslock); |
|
|
484 | maxreqs = max_poll_reqs; |
|
|
485 | maxtime = max_poll_time; |
|
|
486 | X_UNLOCK (reslock); |
|
|
487 | |
|
|
488 | if (maxtime) |
521 | gettimeofday (&tv_start, 0); |
489 | gettimeofday (&tv_start, 0); |
522 | |
490 | |
523 | for (;;) |
491 | for (;;) |
524 | { |
492 | { |
|
|
493 | ETP_REQ *req; |
|
|
494 | |
525 | maybe_start_thread (); |
495 | etp_maybe_start_thread (); |
526 | |
496 | |
527 | X_LOCK (reslock); |
497 | X_LOCK (reslock); |
528 | req = reqq_shift (&res_queue); |
498 | req = reqq_shift (&res_queue); |
529 | |
499 | |
530 | if (req) |
500 | if (req) |
531 | { |
501 | { |
532 | --npending; |
502 | --npending; |
533 | |
503 | |
534 | if (!res_queue.size && etp.done_poll_cb) |
504 | if (!res_queue.size && done_poll_cb) |
535 | etp.done_poll_cb (); |
505 | done_poll_cb (); |
536 | } |
506 | } |
537 | |
507 | |
538 | X_UNLOCK (reslock); |
508 | X_UNLOCK (reslock); |
539 | |
509 | |
540 | if (!req) |
510 | if (!req) |
541 | return 0; |
511 | return 0; |
542 | |
512 | |
|
|
513 | X_LOCK (reqlock); |
543 | --nreqs; |
514 | --nreqs; |
|
|
515 | X_UNLOCK (reqlock); |
544 | |
516 | |
545 | if (req->type == EIO_GROUP && req->size) |
517 | if (expect_false (req->type == EIO_GROUP && req->size)) |
546 | { |
518 | { |
547 | req->int1 = 1; /* mark request as delayed */ |
519 | req->int1 = 1; /* mark request as delayed */ |
548 | continue; |
520 | continue; |
549 | } |
521 | } |
550 | else |
522 | else |
551 | { |
523 | { |
552 | int res = eio_finish (req); |
524 | int res = ETP_FINISH (req); |
553 | if (res) |
525 | if (expect_false (res)) |
554 | return res; |
526 | return res; |
555 | } |
527 | } |
556 | |
528 | |
557 | if (maxreqs && !--maxreqs) |
529 | if (expect_false (maxreqs && !--maxreqs)) |
558 | break; |
530 | break; |
559 | |
531 | |
560 | if (etp.max_poll_time) |
532 | if (maxtime) |
561 | { |
533 | { |
562 | gettimeofday (&tv_now, 0); |
534 | gettimeofday (&tv_now, 0); |
563 | |
535 | |
564 | if (tvdiff (&tv_start, &tv_now) >= etp.max_poll_time) |
536 | if (tvdiff (&tv_start, &tv_now) >= maxtime) |
565 | break; |
537 | break; |
566 | } |
538 | } |
567 | } |
539 | } |
568 | |
540 | |
569 | errno = EAGAIN; |
541 | errno = EAGAIN; |
570 | return -1; |
542 | return -1; |
571 | } |
543 | } |
572 | |
544 | |
|
|
545 | static void etp_cancel (ETP_REQ *req) |
|
|
546 | { |
|
|
547 | X_LOCK (wrklock); |
|
|
548 | req->flags |= EIO_FLAG_CANCELLED; |
|
|
549 | X_UNLOCK (wrklock); |
|
|
550 | |
|
|
551 | eio_grp_cancel (req); |
|
|
552 | } |
|
|
553 | |
|
|
554 | static void etp_submit (ETP_REQ *req) |
|
|
555 | { |
|
|
556 | req->pri -= ETP_PRI_MIN; |
|
|
557 | |
|
|
558 | if (expect_false (req->pri < ETP_PRI_MIN - ETP_PRI_MIN)) req->pri = ETP_PRI_MIN - ETP_PRI_MIN; |
|
|
559 | if (expect_false (req->pri > ETP_PRI_MAX - ETP_PRI_MIN)) req->pri = ETP_PRI_MAX - ETP_PRI_MIN; |
|
|
560 | |
|
|
561 | if (expect_false (req->type == EIO_GROUP)) |
|
|
562 | { |
|
|
563 | /* I hope this is worth it :/ */ |
|
|
564 | X_LOCK (reqlock); |
|
|
565 | ++nreqs; |
|
|
566 | X_UNLOCK (reqlock); |
|
|
567 | |
|
|
568 | X_LOCK (reslock); |
|
|
569 | |
|
|
570 | ++npending; |
|
|
571 | |
|
|
572 | if (!reqq_push (&res_queue, req) && want_poll_cb) |
|
|
573 | want_poll_cb (); |
|
|
574 | |
|
|
575 | X_UNLOCK (reslock); |
|
|
576 | } |
|
|
577 | else |
|
|
578 | { |
|
|
579 | X_LOCK (reqlock); |
|
|
580 | ++nreqs; |
|
|
581 | ++nready; |
|
|
582 | reqq_push (&req_queue, req); |
|
|
583 | X_COND_SIGNAL (reqwait); |
|
|
584 | X_UNLOCK (reqlock); |
|
|
585 | |
|
|
586 | etp_maybe_start_thread (); |
|
|
587 | } |
|
|
588 | } |
|
|
589 | |
|
|
590 | static void etp_set_max_poll_time (double nseconds) |
|
|
591 | { |
|
|
592 | if (WORDACCESS_UNSAFE) X_LOCK (reslock); |
|
|
593 | max_poll_time = nseconds; |
|
|
594 | if (WORDACCESS_UNSAFE) X_UNLOCK (reslock); |
|
|
595 | } |
|
|
596 | |
|
|
597 | static void etp_set_max_poll_reqs (unsigned int maxreqs) |
|
|
598 | { |
|
|
599 | if (WORDACCESS_UNSAFE) X_LOCK (reslock); |
|
|
600 | max_poll_reqs = maxreqs; |
|
|
601 | if (WORDACCESS_UNSAFE) X_UNLOCK (reslock); |
|
|
602 | } |
|
|
603 | |
|
|
604 | static void etp_set_max_idle (unsigned int nthreads) |
|
|
605 | { |
|
|
606 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
|
|
607 | max_idle = nthreads <= 0 ? 1 : nthreads; |
|
|
608 | if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); |
|
|
609 | } |
|
|
610 | |
|
|
611 | static void etp_set_min_parallel (unsigned int nthreads) |
|
|
612 | { |
|
|
613 | if (wanted < nthreads) |
|
|
614 | wanted = nthreads; |
|
|
615 | } |
|
|
616 | |
|
|
617 | static void etp_set_max_parallel (unsigned int nthreads) |
|
|
618 | { |
|
|
619 | if (wanted > nthreads) |
|
|
620 | wanted = nthreads; |
|
|
621 | |
|
|
622 | while (started > wanted) |
|
|
623 | etp_end_thread (); |
|
|
624 | } |
|
|
625 | |
|
|
626 | /*****************************************************************************/ |
|
|
627 | |
|
|
628 | static void grp_try_feed (eio_req *grp) |
|
|
629 | { |
|
|
630 | while (grp->size < grp->int2 && !EIO_CANCELLED (grp)) |
|
|
631 | { |
|
|
632 | grp->flags &= ~EIO_FLAG_GROUPADD; |
|
|
633 | |
|
|
634 | EIO_FEED (grp); |
|
|
635 | |
|
|
636 | /* stop if no progress has been made */ |
|
|
637 | if (!(grp->flags & EIO_FLAG_GROUPADD)) |
|
|
638 | { |
|
|
639 | grp->feed = 0; |
|
|
640 | break; |
|
|
641 | } |
|
|
642 | } |
|
|
643 | } |
|
|
644 | |
|
|
645 | static int grp_dec (eio_req *grp) |
|
|
646 | { |
|
|
647 | --grp->size; |
|
|
648 | |
|
|
649 | /* call feeder, if applicable */ |
|
|
650 | grp_try_feed (grp); |
|
|
651 | |
|
|
652 | /* finish, if done */ |
|
|
653 | if (!grp->size && grp->int1) |
|
|
654 | return eio_finish (grp); |
|
|
655 | else |
|
|
656 | return 0; |
|
|
657 | } |
|
|
658 | |
|
|
659 | void eio_destroy (eio_req *req) |
|
|
660 | { |
|
|
661 | if ((req)->flags & EIO_FLAG_PTR1_FREE) free (req->ptr1); |
|
|
662 | if ((req)->flags & EIO_FLAG_PTR2_FREE) free (req->ptr2); |
|
|
663 | |
|
|
664 | EIO_DESTROY (req); |
|
|
665 | } |
|
|
666 | |
|
|
667 | static int eio_finish (eio_req *req) |
|
|
668 | { |
|
|
669 | int res = EIO_FINISH (req); |
|
|
670 | |
|
|
671 | if (req->grp) |
|
|
672 | { |
|
|
673 | int res2; |
|
|
674 | eio_req *grp = req->grp; |
|
|
675 | |
|
|
676 | /* unlink request */ |
|
|
677 | if (req->grp_next) req->grp_next->grp_prev = req->grp_prev; |
|
|
678 | if (req->grp_prev) req->grp_prev->grp_next = req->grp_next; |
|
|
679 | |
|
|
680 | if (grp->grp_first == req) |
|
|
681 | grp->grp_first = req->grp_next; |
|
|
682 | |
|
|
683 | res2 = grp_dec (grp); |
|
|
684 | |
|
|
685 | if (!res && res2) |
|
|
686 | res = res2; |
|
|
687 | } |
|
|
688 | |
|
|
689 | eio_destroy (req); |
|
|
690 | |
|
|
691 | return res; |
|
|
692 | } |
|
|
693 | |
|
|
694 | void eio_grp_cancel (eio_req *grp) |
|
|
695 | { |
|
|
696 | for (grp = grp->grp_first; grp; grp = grp->grp_next) |
|
|
697 | eio_cancel (grp); |
|
|
698 | } |
|
|
699 | |
|
|
700 | void eio_cancel (eio_req *req) |
|
|
701 | { |
|
|
702 | etp_cancel (req); |
|
|
703 | } |
|
|
704 | |
|
|
705 | void eio_submit (eio_req *req) |
|
|
706 | { |
|
|
707 | etp_submit (req); |
|
|
708 | } |
|
|
709 | |
|
|
710 | unsigned int eio_nreqs (void) |
|
|
711 | { |
|
|
712 | return etp_nreqs (); |
|
|
713 | } |
|
|
714 | |
|
|
715 | unsigned int eio_nready (void) |
|
|
716 | { |
|
|
717 | return etp_nready (); |
|
|
718 | } |
|
|
719 | |
|
|
720 | unsigned int eio_npending (void) |
|
|
721 | { |
|
|
722 | return etp_npending (); |
|
|
723 | } |
|
|
724 | |
|
|
725 | unsigned int eio_nthreads (void) |
|
|
726 | { |
|
|
727 | return etp_nthreads (); |
|
|
728 | } |
|
|
729 | |
|
|
730 | void eio_set_max_poll_time (double nseconds) |
|
|
731 | { |
|
|
732 | etp_set_max_poll_time (nseconds); |
|
|
733 | } |
|
|
734 | |
|
|
735 | void eio_set_max_poll_reqs (unsigned int maxreqs) |
|
|
736 | { |
|
|
737 | etp_set_max_poll_reqs (maxreqs); |
|
|
738 | } |
|
|
739 | |
|
|
740 | void eio_set_max_idle (unsigned int nthreads) |
|
|
741 | { |
|
|
742 | etp_set_max_idle (nthreads); |
|
|
743 | } |
|
|
744 | |
|
|
745 | void eio_set_min_parallel (unsigned int nthreads) |
|
|
746 | { |
|
|
747 | etp_set_min_parallel (nthreads); |
|
|
748 | } |
|
|
749 | |
|
|
750 | void eio_set_max_parallel (unsigned int nthreads) |
|
|
751 | { |
|
|
752 | etp_set_max_parallel (nthreads); |
|
|
753 | } |
|
|
754 | |
|
|
755 | int eio_poll (void) |
|
|
756 | { |
|
|
757 | return etp_poll (); |
|
|
758 | } |
|
|
759 | |
573 | /*****************************************************************************/ |
760 | /*****************************************************************************/ |
574 | /* work around various missing functions */ |
761 | /* work around various missing functions */ |
575 | |
762 | |
576 | #if !HAVE_PREADWRITE |
763 | #if !HAVE_PREADWRITE |
|
|
764 | # undef pread |
|
|
765 | # undef pwrite |
577 | # define pread eio__pread |
766 | # define pread eio__pread |
578 | # define pwrite eio__pwrite |
767 | # define pwrite eio__pwrite |
579 | |
|
|
580 | /* |
|
|
581 | * make our pread/pwrite safe against themselves, but not against |
|
|
582 | * normal read/write by using a mutex. slows down execution a lot, |
|
|
583 | * but that's your problem, not mine. |
|
|
584 | */ |
|
|
585 | static mutex_t preadwritelock = X_MUTEX_INIT; |
|
|
586 | |
768 | |
587 | static ssize_t |
769 | static ssize_t |
588 | eio__pread (int fd, void *buf, size_t count, off_t offset) |
770 | eio__pread (int fd, void *buf, size_t count, off_t offset) |
589 | { |
771 | { |
590 | ssize_t res; |
772 | ssize_t res; |
… | |
… | |
608 | |
790 | |
609 | X_LOCK (preadwritelock); |
791 | X_LOCK (preadwritelock); |
610 | ooffset = lseek (fd, 0, SEEK_CUR); |
792 | ooffset = lseek (fd, 0, SEEK_CUR); |
611 | lseek (fd, offset, SEEK_SET); |
793 | lseek (fd, offset, SEEK_SET); |
612 | res = write (fd, buf, count); |
794 | res = write (fd, buf, count); |
613 | lseek (fd, offset, SEEK_SET); |
795 | lseek (fd, ooffset, SEEK_SET); |
614 | X_UNLOCK (preadwritelock); |
796 | X_UNLOCK (preadwritelock); |
615 | |
797 | |
616 | return res; |
798 | return res; |
617 | } |
799 | } |
618 | #endif |
800 | #endif |
619 | |
801 | |
620 | #ifndef HAVE_FUTIMES |
802 | #ifndef HAVE_FUTIMES |
621 | |
803 | |
|
|
804 | # undef utimes |
|
|
805 | # undef futimes |
622 | # define utimes(path,times) eio__utimes (path, times) |
806 | # define utimes(path,times) eio__utimes (path, times) |
623 | # define futimes(fd,times) eio__futimes (fd, times) |
807 | # define futimes(fd,times) eio__futimes (fd, times) |
624 | |
808 | |
625 | static int |
809 | static int |
626 | eio__utimes (const char *filename, const struct timeval times[2]) |
810 | eio__utimes (const char *filename, const struct timeval times[2]) |
… | |
… | |
645 | } |
829 | } |
646 | |
830 | |
647 | #endif |
831 | #endif |
648 | |
832 | |
649 | #if !HAVE_FDATASYNC |
833 | #if !HAVE_FDATASYNC |
|
|
834 | # undef fdatasync |
650 | # define fdatasync fsync |
835 | # define fdatasync(fd) fsync (fd) |
651 | #endif |
836 | #endif |
|
|
837 | |
|
|
838 | /* sync_file_range always needs emulation */ |
|
|
839 | int |
|
|
840 | eio__sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags) |
|
|
841 | { |
|
|
842 | #if HAVE_SYNC_FILE_RANGE |
|
|
843 | int res; |
|
|
844 | |
|
|
845 | if (EIO_SYNC_FILE_RANGE_WAIT_BEFORE != SYNC_FILE_RANGE_WAIT_BEFORE |
|
|
846 | || EIO_SYNC_FILE_RANGE_WRITE != SYNC_FILE_RANGE_WRITE |
|
|
847 | || EIO_SYNC_FILE_RANGE_WAIT_AFTER != SYNC_FILE_RANGE_WAIT_AFTER) |
|
|
848 | { |
|
|
849 | flags = 0 |
|
|
850 | | (flags & EIO_SYNC_FILE_RANGE_WAIT_BEFORE ? SYNC_FILE_RANGE_WAIT_BEFORE : 0) |
|
|
851 | | (flags & EIO_SYNC_FILE_RANGE_WRITE ? SYNC_FILE_RANGE_WRITE : 0) |
|
|
852 | | (flags & EIO_SYNC_FILE_RANGE_WAIT_AFTER ? SYNC_FILE_RANGE_WAIT_AFTER : 0); |
|
|
853 | } |
|
|
854 | |
|
|
855 | res = sync_file_range (fd, offset, nbytes, flags); |
|
|
856 | |
|
|
857 | if (!res || errno != ENOSYS) |
|
|
858 | return res; |
|
|
859 | #endif |
|
|
860 | |
|
|
861 | /* even though we could play tricks with the flags, it's better to always |
|
|
862 | * call fdatasync, as thta matches the expectation of it's users best */ |
|
|
863 | return fdatasync (fd); |
|
|
864 | } |
652 | |
865 | |
653 | #if !HAVE_READAHEAD |
866 | #if !HAVE_READAHEAD |
|
|
867 | # undef readahead |
654 | # define readahead(fd,offset,count) eio__readahead (fd, offset, count, self) |
868 | # define readahead(fd,offset,count) eio__readahead (fd, offset, count, self) |
655 | |
869 | |
656 | static ssize_t |
870 | static ssize_t |
657 | eio__readahead (int fd, off_t offset, size_t count, worker *self) |
871 | eio__readahead (int fd, off_t offset, size_t count, etp_worker *self) |
658 | { |
872 | { |
659 | size_t todo = count; |
873 | size_t todo = count; |
660 | dBUF; |
874 | dBUF; |
661 | |
875 | |
662 | while (todo > 0) |
876 | while (todo > 0) |
… | |
… | |
672 | return count; |
886 | return count; |
673 | } |
887 | } |
674 | |
888 | |
675 | #endif |
889 | #endif |
676 | |
890 | |
677 | #if !HAVE_READDIR_R |
|
|
678 | # define readdir_r eio__readdir_r |
|
|
679 | |
|
|
680 | static mutex_t readdirlock = X_MUTEX_INIT; |
|
|
681 | |
|
|
682 | static int |
|
|
683 | eio__readdir_r (DIR *dirp, EIO_STRUCT_DIRENT *ent, EIO_STRUCT_DIRENT **res) |
|
|
684 | { |
|
|
685 | EIO_STRUCT_DIRENT *e; |
|
|
686 | int errorno; |
|
|
687 | |
|
|
688 | X_LOCK (readdirlock); |
|
|
689 | |
|
|
690 | e = readdir (dirp); |
|
|
691 | errorno = errno; |
|
|
692 | |
|
|
693 | if (e) |
|
|
694 | { |
|
|
695 | *res = ent; |
|
|
696 | strcpy (ent->d_name, e->d_name); |
|
|
697 | } |
|
|
698 | else |
|
|
699 | *res = 0; |
|
|
700 | |
|
|
701 | X_UNLOCK (readdirlock); |
|
|
702 | |
|
|
703 | errno = errorno; |
|
|
704 | return e ? 0 : -1; |
|
|
705 | } |
|
|
706 | #endif |
|
|
707 | |
|
|
708 | /* sendfile always needs emulation */ |
891 | /* sendfile always needs emulation */ |
709 | static ssize_t |
892 | static ssize_t |
710 | eio__sendfile (int ofd, int ifd, off_t offset, size_t count, worker *self) |
893 | eio__sendfile (int ofd, int ifd, off_t offset, size_t count, etp_worker *self) |
711 | { |
894 | { |
712 | ssize_t res; |
895 | ssize_t res; |
713 | |
896 | |
714 | if (!count) |
897 | if (!count) |
715 | return 0; |
898 | return 0; |
… | |
… | |
797 | } |
980 | } |
798 | |
981 | |
799 | return res; |
982 | return res; |
800 | } |
983 | } |
801 | |
984 | |
|
|
985 | static int |
|
|
986 | eio_dent_cmp (const void *a_, const void *b_) |
|
|
987 | { |
|
|
988 | const eio_dirent *a = (const eio_dirent *)a_; |
|
|
989 | const eio_dirent *b = (const eio_dirent *)b_; |
|
|
990 | |
|
|
991 | return (int)b->score - (int)a->score ? (int)b->score - (int)a->score |
|
|
992 | : a->inode < b->inode ? -1 : a->inode > b->inode ? 1 : 0; /* int might be < ino_t */ |
|
|
993 | } |
|
|
994 | |
802 | /* read a full directory */ |
995 | /* read a full directory */ |
803 | static void |
996 | static void |
804 | eio__scandir (eio_req *req, worker *self) |
997 | eio__scandir (eio_req *req, etp_worker *self) |
805 | { |
998 | { |
806 | DIR *dirp; |
999 | DIR *dirp; |
807 | union |
|
|
808 | { |
|
|
809 | EIO_STRUCT_DIRENT d; |
|
|
810 | char b [offsetof (EIO_STRUCT_DIRENT, d_name) + NAME_MAX + 1]; |
|
|
811 | } *u; |
|
|
812 | EIO_STRUCT_DIRENT *entp; |
1000 | EIO_STRUCT_DIRENT *entp; |
813 | char *name, *names; |
1001 | unsigned char *name, *names; |
814 | int memlen = 4096; |
1002 | int namesalloc = 4096; |
815 | int memofs = 0; |
1003 | int namesoffs = 0; |
|
|
1004 | int flags = req->int1; |
|
|
1005 | eio_dirent *dents = 0; |
|
|
1006 | int dentalloc = 128; |
816 | int res = 0; |
1007 | int dentoffs = 0; |
817 | |
1008 | |
|
|
1009 | req->result = -1; |
|
|
1010 | |
|
|
1011 | if (!(flags & EIO_READDIR_DENTS)) |
|
|
1012 | flags &= ~(EIO_READDIR_DIRS_FIRST | EIO_READDIR_STAT_ORDER); |
|
|
1013 | |
818 | X_LOCK (etplock); |
1014 | X_LOCK (wrklock); |
|
|
1015 | |
|
|
1016 | /* the corresponding closedir is in ETP_WORKER_CLEAR */ |
819 | self->dirp = dirp = opendir (req->ptr1); |
1017 | self->dirp = dirp = opendir (req->ptr1); |
820 | self->dbuf = u = malloc (sizeof (*u)); |
|
|
821 | req->flags |= EIO_FLAG_PTR2_FREE; |
1018 | req->flags |= EIO_FLAG_PTR1_FREE | EIO_FLAG_PTR2_FREE; |
822 | req->ptr2 = names = malloc (memlen); |
1019 | req->ptr1 = names = malloc (namesalloc); |
|
|
1020 | req->ptr2 = dents = flags ? malloc (dentalloc * sizeof (eio_dirent)) : 0; |
|
|
1021 | |
823 | X_UNLOCK (etplock); |
1022 | X_UNLOCK (wrklock); |
824 | |
1023 | |
825 | if (dirp && u && names) |
1024 | if (dirp && names && (!flags || dents)) |
826 | for (;;) |
1025 | for (;;) |
827 | { |
1026 | { |
828 | errno = 0; |
1027 | errno = 0; |
829 | readdir_r (dirp, &u->d, &entp); |
1028 | entp = readdir (dirp); |
830 | |
1029 | |
831 | if (!entp) |
1030 | if (!entp) |
|
|
1031 | { |
|
|
1032 | if (errno) |
|
|
1033 | break; |
|
|
1034 | |
|
|
1035 | /* sort etc. */ |
|
|
1036 | req->int1 = flags; |
|
|
1037 | req->result = dentoffs; |
|
|
1038 | |
|
|
1039 | if (flags & EIO_READDIR_STAT_ORDER || !(~flags & (EIO_READDIR_DIRS_FIRST | EIO_READDIR_FOUND_UNKNOWN)) |
|
|
1040 | { |
|
|
1041 | /* pray your qsort doesn't use quicksort */ |
|
|
1042 | qsort (dents, dentoffs, sizeof (*dents), eio_dent_cmp); /* score depends of DIRS_FIRST */ |
|
|
1043 | } |
|
|
1044 | else if (flags & EIO_READDIR_DIRS_FIRST && !(flags & EIO_READDIR_FOUND_UNKNOWN)) |
|
|
1045 | { |
|
|
1046 | /* in this case, all is known, and we just put dirs first and sort them */ |
|
|
1047 | eio_dirent *ent = dents + dentoffs; |
|
|
1048 | eio_dirent *dir = dents; |
|
|
1049 | |
|
|
1050 | while (ent > dir) |
|
|
1051 | { |
|
|
1052 | if (dir->type == DT_DIR) |
|
|
1053 | ++dir; |
|
|
1054 | else |
|
|
1055 | { |
|
|
1056 | --ent; |
|
|
1057 | |
|
|
1058 | if (ent->type == DT_DIR) |
|
|
1059 | { |
|
|
1060 | eio_dirent tmp = *dir; |
|
|
1061 | *dir = *ent; |
|
|
1062 | *ent = tmp; |
|
|
1063 | |
|
|
1064 | ++dir; |
|
|
1065 | } |
|
|
1066 | } |
|
|
1067 | } |
|
|
1068 | |
|
|
1069 | /* now sort the dirs only */ |
|
|
1070 | qsort (dents, dir - dents, sizeof (*dents), eio_dent_cmp); |
|
|
1071 | } |
|
|
1072 | |
|
|
1073 | {int i; for(i=0;i<dentoffs;++i){eio_dirent *e=dents+i; printf ("%9ld %3d %s\n", e->inode,e->score,e->name);}}//D |
|
|
1074 | |
832 | break; |
1075 | break; |
|
|
1076 | } |
833 | |
1077 | |
834 | name = entp->d_name; |
1078 | name = entp->d_name; |
835 | |
1079 | |
836 | if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) |
1080 | if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) |
837 | { |
1081 | { |
838 | int len = strlen (name) + 1; |
1082 | int len = strlen (name) + 1; |
839 | |
1083 | |
840 | res++; |
1084 | while (expect_false (namesoffs + len > namesalloc)) |
841 | |
|
|
842 | while (memofs + len > memlen) |
|
|
843 | { |
1085 | { |
844 | memlen *= 2; |
1086 | namesalloc *= 2; |
845 | X_LOCK (etplock); |
1087 | X_LOCK (wrklock); |
846 | req->ptr2 = names = realloc (names, memlen); |
1088 | req->ptr1 = names = realloc (names, namesalloc); |
847 | X_UNLOCK (etplock); |
1089 | X_UNLOCK (wrklock); |
848 | |
1090 | |
849 | if (!names) |
1091 | if (!names) |
850 | break; |
1092 | break; |
851 | } |
1093 | } |
852 | |
1094 | |
853 | memcpy (names + memofs, name, len); |
1095 | memcpy (names + namesoffs, name, len); |
|
|
1096 | |
|
|
1097 | if (dents) |
|
|
1098 | { |
|
|
1099 | struct eio_dirent *ent; |
|
|
1100 | |
|
|
1101 | if (expect_false (dentoffs == dentalloc)) |
|
|
1102 | { |
|
|
1103 | dentalloc *= 2; |
|
|
1104 | X_LOCK (wrklock); |
|
|
1105 | req->ptr2 = dents = realloc (dents, dentalloc * sizeof (eio_dirent)); |
|
|
1106 | X_UNLOCK (wrklock); |
|
|
1107 | |
|
|
1108 | if (!dents) |
|
|
1109 | break; |
|
|
1110 | } |
|
|
1111 | |
|
|
1112 | ent = dents + dentoffs; |
|
|
1113 | |
|
|
1114 | ent->name = names + namesoffs; |
|
|
1115 | ent->namelen = len - 1; |
|
|
1116 | ent->inode = D_INO (entp); |
|
|
1117 | |
|
|
1118 | switch (D_TYPE (entp)) |
|
|
1119 | { |
|
|
1120 | default: |
|
|
1121 | ent->type = EIO_DT_UNKNOWN; |
|
|
1122 | flags |= EIO_READDIR_FOUND_UNKNOWN; |
|
|
1123 | break; |
|
|
1124 | |
|
|
1125 | #ifdef DT_FIFO |
|
|
1126 | case DT_FIFO: ent->type = EIO_DT_FIFO; break; |
|
|
1127 | #endif |
|
|
1128 | #ifdef DT_CHR |
|
|
1129 | case DT_CHR: ent->type = EIO_DT_CHR; break; |
|
|
1130 | #endif |
|
|
1131 | #ifdef DT_DIR |
|
|
1132 | case DT_DIR: ent->type = EIO_DT_DIR; break; |
|
|
1133 | #endif |
|
|
1134 | #ifdef DT_BLK |
|
|
1135 | case DT_BLK: ent->type = EIO_DT_BLK; break; |
|
|
1136 | #endif |
|
|
1137 | #ifdef DT_REG |
|
|
1138 | case DT_REG: ent->type = EIO_DT_REG; break; |
|
|
1139 | #endif |
|
|
1140 | #ifdef DT_LNK |
|
|
1141 | case DT_LNK: ent->type = EIO_DT_LNK; break; |
|
|
1142 | #endif |
|
|
1143 | #ifdef DT_SOCK |
|
|
1144 | case DT_SOCK: ent->type = EIO_DT_SOCK; break; |
|
|
1145 | #endif |
|
|
1146 | #ifdef DT_WHT |
|
|
1147 | case DT_WHT: ent->type = EIO_DT_WHT; break; |
|
|
1148 | #endif |
|
|
1149 | } |
|
|
1150 | |
|
|
1151 | ent->score = 0; |
|
|
1152 | |
|
|
1153 | if (flags & EIO_READDIR_DIRS_FIRST) |
|
|
1154 | { |
|
|
1155 | if (ent->type == EIO_DT_UNKNOWN) |
|
|
1156 | { |
|
|
1157 | if (*name == '.') /* leading dots are likely directories, and, in any case, rare */ |
|
|
1158 | ent->score = 98; |
|
|
1159 | else if (!strchr (name, '.')) /* absense of dots indicate likely dirs */ |
|
|
1160 | ent->score = len <= 4 ? 5 : len <= 7 ? 4 : 1; /* shorter == more likely dir, but avoid too many classes */ |
|
|
1161 | } |
|
|
1162 | else if (ent->type == DT_DIR) |
|
|
1163 | ent->score = 100; |
|
|
1164 | } |
|
|
1165 | } |
|
|
1166 | |
854 | memofs += len; |
1167 | namesoffs += len; |
|
|
1168 | ++dentoffs; |
855 | } |
1169 | } |
856 | } |
1170 | } |
857 | |
1171 | else |
858 | if (errno) |
|
|
859 | res = -1; |
|
|
860 | |
|
|
861 | req->result = res; |
1172 | req->result = -1; |
|
|
1173 | |
|
|
1174 | /* if user doesn't want the dents, do not provide it */ |
|
|
1175 | if (!(flags & EIO_READDIR_DENTS)) |
|
|
1176 | { |
|
|
1177 | X_LOCK (wrklock); |
|
|
1178 | free (dents); |
|
|
1179 | req->ptr2 = req->ptr1; |
|
|
1180 | req->ptr1 = 0; |
|
|
1181 | X_UNLOCK (wrklock); |
|
|
1182 | } |
|
|
1183 | } |
|
|
1184 | |
|
|
1185 | #if !(_POSIX_MAPPED_FILES && _POSIX_SYNCHRONIZED_IO) |
|
|
1186 | # undef msync |
|
|
1187 | # define msync(a,b,c) ((errno = ENOSYS), -1) |
|
|
1188 | #endif |
|
|
1189 | |
|
|
1190 | int |
|
|
1191 | eio__mtouch (void *mem, size_t len, int flags) |
|
|
1192 | { |
|
|
1193 | intptr_t addr = (intptr_t)mem; |
|
|
1194 | intptr_t end = addr + len; |
|
|
1195 | #ifdef PAGESIZE |
|
|
1196 | const intptr_t page = PAGESIZE; |
|
|
1197 | #else |
|
|
1198 | static intptr_t page; |
|
|
1199 | |
|
|
1200 | if (!page) |
|
|
1201 | page = sysconf (_SC_PAGESIZE); |
|
|
1202 | #endif |
|
|
1203 | |
|
|
1204 | addr &= ~(page - 1); /* assume page size is always a power of two */ |
|
|
1205 | |
|
|
1206 | if (addr < end) |
|
|
1207 | if (flags) /* modify */ |
|
|
1208 | do { *((volatile sig_atomic_t *)addr) |= 0; } while ((addr += page) < len); |
|
|
1209 | else |
|
|
1210 | do { *((volatile sig_atomic_t *)addr) ; } while ((addr += page) < len); |
|
|
1211 | |
|
|
1212 | return 0; |
862 | } |
1213 | } |
863 | |
1214 | |
864 | /*****************************************************************************/ |
1215 | /*****************************************************************************/ |
865 | |
1216 | |
866 | #define ALLOC(len) \ |
1217 | #define ALLOC(len) \ |
867 | if (!req->ptr2) \ |
1218 | if (!req->ptr2) \ |
868 | { \ |
1219 | { \ |
869 | X_LOCK (etplock); \ |
1220 | X_LOCK (wrklock); \ |
870 | req->flags |= EIO_FLAG_PTR2_FREE; \ |
1221 | req->flags |= EIO_FLAG_PTR2_FREE; \ |
871 | X_UNLOCK (etplock); \ |
1222 | X_UNLOCK (wrklock); \ |
872 | req->ptr2 = malloc (len); \ |
1223 | req->ptr2 = malloc (len); \ |
873 | if (!req->ptr2) \ |
1224 | if (!req->ptr2) \ |
874 | { \ |
1225 | { \ |
875 | errno = ENOMEM; \ |
1226 | errno = ENOMEM; \ |
876 | req->result = -1; \ |
1227 | req->result = -1; \ |
877 | break; \ |
1228 | break; \ |
878 | } \ |
1229 | } \ |
879 | } |
1230 | } |
880 | |
1231 | |
881 | X_THREAD_PROC (eio_proc) |
1232 | X_THREAD_PROC (etp_proc) |
882 | { |
1233 | { |
883 | eio_req *req; |
1234 | ETP_REQ *req; |
884 | struct timespec ts; |
1235 | struct timespec ts; |
885 | worker *self = (worker *)thr_arg; |
1236 | etp_worker *self = (etp_worker *)thr_arg; |
886 | |
1237 | |
887 | /* try to distribute timeouts somewhat randomly */ |
1238 | /* try to distribute timeouts somewhat randomly */ |
888 | ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); |
1239 | ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); |
889 | |
1240 | |
890 | for (;;) |
1241 | for (;;) |
891 | { |
1242 | { |
892 | ts.tv_sec = time (0) + IDLE_TIMEOUT; |
|
|
893 | |
|
|
894 | X_LOCK (reqlock); |
1243 | X_LOCK (reqlock); |
895 | |
1244 | |
896 | for (;;) |
1245 | for (;;) |
897 | { |
1246 | { |
898 | self->req = req = reqq_shift (&req_queue); |
1247 | self->req = req = reqq_shift (&req_queue); |
… | |
… | |
900 | if (req) |
1249 | if (req) |
901 | break; |
1250 | break; |
902 | |
1251 | |
903 | ++idle; |
1252 | ++idle; |
904 | |
1253 | |
|
|
1254 | ts.tv_sec = time (0) + IDLE_TIMEOUT; |
905 | if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) == ETIMEDOUT) |
1255 | if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) == ETIMEDOUT) |
906 | { |
1256 | { |
907 | if (idle > max_idle) |
1257 | if (idle > max_idle) |
908 | { |
1258 | { |
909 | --idle; |
1259 | --idle; |
910 | X_UNLOCK (reqlock); |
1260 | X_UNLOCK (reqlock); |
911 | X_LOCK (etplock); |
1261 | X_LOCK (wrklock); |
912 | --started; |
1262 | --started; |
913 | X_UNLOCK (etplock); |
1263 | X_UNLOCK (wrklock); |
914 | goto quit; |
1264 | goto quit; |
915 | } |
1265 | } |
916 | |
1266 | |
917 | /* we are allowed to idle, so do so without any timeout */ |
1267 | /* we are allowed to idle, so do so without any timeout */ |
918 | X_COND_WAIT (reqwait, reqlock); |
1268 | X_COND_WAIT (reqwait, reqlock); |
919 | ts.tv_sec = time (0) + IDLE_TIMEOUT; |
|
|
920 | } |
1269 | } |
921 | |
1270 | |
922 | --idle; |
1271 | --idle; |
923 | } |
1272 | } |
924 | |
1273 | |
925 | --nready; |
1274 | --nready; |
926 | |
1275 | |
927 | X_UNLOCK (reqlock); |
1276 | X_UNLOCK (reqlock); |
928 | |
1277 | |
929 | errno = 0; /* strictly unnecessary */ |
1278 | if (req->type < 0) |
|
|
1279 | goto quit; |
930 | |
1280 | |
931 | if (!EIO_CANCELLED (req)) |
1281 | if (!EIO_CANCELLED (req)) |
932 | switch (req->type) |
1282 | ETP_EXECUTE (self, req); |
933 | { |
|
|
934 | case EIO_READ: ALLOC (req->size); |
|
|
935 | req->result = req->offs >= 0 |
|
|
936 | ? pread (req->int1, req->ptr2, req->size, req->offs) |
|
|
937 | : read (req->int1, req->ptr2, req->size); break; |
|
|
938 | case EIO_WRITE: req->result = req->offs >= 0 |
|
|
939 | ? pwrite (req->int1, req->ptr2, req->size, req->offs) |
|
|
940 | : write (req->int1, req->ptr2, req->size); break; |
|
|
941 | |
|
|
942 | case EIO_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break; |
|
|
943 | case EIO_SENDFILE: req->result = eio__sendfile (req->int1, req->int2, req->offs, req->size, self); break; |
|
|
944 | |
|
|
945 | case EIO_STAT: ALLOC (sizeof (EIO_STRUCT_STAT)); |
|
|
946 | req->result = stat (req->ptr1, (EIO_STRUCT_STAT *)req->ptr2); break; |
|
|
947 | case EIO_LSTAT: ALLOC (sizeof (EIO_STRUCT_STAT)); |
|
|
948 | req->result = lstat (req->ptr1, (EIO_STRUCT_STAT *)req->ptr2); break; |
|
|
949 | case EIO_FSTAT: ALLOC (sizeof (EIO_STRUCT_STAT)); |
|
|
950 | req->result = fstat (req->int1, (EIO_STRUCT_STAT *)req->ptr2); break; |
|
|
951 | |
|
|
952 | case EIO_CHOWN: req->result = chown (req->ptr1, req->int2, req->int3); break; |
|
|
953 | case EIO_FCHOWN: req->result = fchown (req->int1, req->int2, req->int3); break; |
|
|
954 | case EIO_CHMOD: req->result = chmod (req->ptr1, (mode_t)req->int2); break; |
|
|
955 | case EIO_FCHMOD: req->result = fchmod (req->int1, (mode_t)req->int2); break; |
|
|
956 | case EIO_TRUNCATE: req->result = truncate (req->ptr1, req->offs); break; |
|
|
957 | case EIO_FTRUNCATE: req->result = ftruncate (req->int1, req->offs); break; |
|
|
958 | |
|
|
959 | case EIO_OPEN: req->result = open (req->ptr1, req->int1, (mode_t)req->int2); break; |
|
|
960 | case EIO_CLOSE: req->result = close (req->int1); break; |
|
|
961 | case EIO_DUP2: req->result = dup2 (req->int1, req->int2); break; |
|
|
962 | case EIO_UNLINK: req->result = unlink (req->ptr1); break; |
|
|
963 | case EIO_RMDIR: req->result = rmdir (req->ptr1); break; |
|
|
964 | case EIO_MKDIR: req->result = mkdir (req->ptr1, (mode_t)req->int2); break; |
|
|
965 | case EIO_RENAME: req->result = rename (req->ptr1, req->ptr2); break; |
|
|
966 | case EIO_LINK: req->result = link (req->ptr1, req->ptr2); break; |
|
|
967 | case EIO_SYMLINK: req->result = symlink (req->ptr1, req->ptr2); break; |
|
|
968 | case EIO_MKNOD: req->result = mknod (req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break; |
|
|
969 | |
|
|
970 | case EIO_READLINK: ALLOC (NAME_MAX); |
|
|
971 | req->result = readlink (req->ptr1, req->ptr2, NAME_MAX); break; |
|
|
972 | |
|
|
973 | case EIO_SYNC: req->result = 0; sync (); break; |
|
|
974 | case EIO_FSYNC: req->result = fsync (req->int1); break; |
|
|
975 | case EIO_FDATASYNC: req->result = fdatasync (req->int1); break; |
|
|
976 | |
|
|
977 | case EIO_READDIR: eio__scandir (req, self); break; |
|
|
978 | |
|
|
979 | case EIO_BUSY: |
|
|
980 | #ifdef _WIN32 |
|
|
981 | Sleep (req->nv1 * 1000.); |
|
|
982 | #else |
|
|
983 | { |
|
|
984 | struct timeval tv; |
|
|
985 | |
|
|
986 | tv.tv_sec = req->nv1; |
|
|
987 | tv.tv_usec = (req->nv1 - tv.tv_sec) * 1000000.; |
|
|
988 | |
|
|
989 | req->result = select (0, 0, 0, 0, &tv); |
|
|
990 | } |
|
|
991 | #endif |
|
|
992 | break; |
|
|
993 | |
|
|
994 | case EIO_UTIME: |
|
|
995 | case EIO_FUTIME: |
|
|
996 | { |
|
|
997 | struct timeval tv[2]; |
|
|
998 | struct timeval *times; |
|
|
999 | |
|
|
1000 | if (req->nv1 != -1. || req->nv2 != -1.) |
|
|
1001 | { |
|
|
1002 | tv[0].tv_sec = req->nv1; |
|
|
1003 | tv[0].tv_usec = (req->nv1 - tv[0].tv_sec) * 1000000.; |
|
|
1004 | tv[1].tv_sec = req->nv2; |
|
|
1005 | tv[1].tv_usec = (req->nv2 - tv[1].tv_sec) * 1000000.; |
|
|
1006 | |
|
|
1007 | times = tv; |
|
|
1008 | } |
|
|
1009 | else |
|
|
1010 | times = 0; |
|
|
1011 | |
|
|
1012 | |
|
|
1013 | req->result = req->type == EIO_FUTIME |
|
|
1014 | ? futimes (req->int1, times) |
|
|
1015 | : utimes (req->ptr1, times); |
|
|
1016 | } |
|
|
1017 | |
|
|
1018 | case EIO_GROUP: |
|
|
1019 | case EIO_NOP: |
|
|
1020 | req->result = 0; |
|
|
1021 | break; |
|
|
1022 | |
|
|
1023 | case EIO_QUIT: |
|
|
1024 | goto quit; |
|
|
1025 | |
|
|
1026 | default: |
|
|
1027 | req->result = -1; |
|
|
1028 | break; |
|
|
1029 | } |
|
|
1030 | |
|
|
1031 | req->errorno = errno; |
|
|
1032 | |
1283 | |
1033 | X_LOCK (reslock); |
1284 | X_LOCK (reslock); |
1034 | |
1285 | |
1035 | ++npending; |
1286 | ++npending; |
1036 | |
1287 | |
1037 | if (!reqq_push (&res_queue, req) && etp.want_poll_cb) |
1288 | if (!reqq_push (&res_queue, req) && want_poll_cb) |
1038 | etp.want_poll_cb (); |
1289 | want_poll_cb (); |
1039 | |
1290 | |
1040 | self->req = 0; |
1291 | self->req = 0; |
1041 | worker_clear (self); |
1292 | etp_worker_clear (self); |
1042 | |
1293 | |
1043 | X_UNLOCK (reslock); |
1294 | X_UNLOCK (reslock); |
1044 | } |
1295 | } |
1045 | |
1296 | |
1046 | quit: |
1297 | quit: |
1047 | X_LOCK (etplock); |
1298 | X_LOCK (wrklock); |
1048 | worker_free (self); |
1299 | etp_worker_free (self); |
1049 | X_UNLOCK (etplock); |
1300 | X_UNLOCK (wrklock); |
1050 | |
1301 | |
1051 | return 0; |
1302 | return 0; |
1052 | } |
1303 | } |
1053 | |
1304 | |
1054 | /*****************************************************************************/ |
1305 | /*****************************************************************************/ |
1055 | |
1306 | |
1056 | int eio_init (void (*want_poll)(void), void (*done_poll)(void)) |
1307 | int eio_init (void (*want_poll)(void), void (*done_poll)(void)) |
1057 | { |
1308 | { |
1058 | etp_init (&etp, want_poll, done_poll); |
1309 | return etp_init (want_poll, done_poll); |
1059 | } |
1310 | } |
1060 | |
1311 | |
1061 | static void eio_api_destroy (eio_req *req) |
1312 | static void eio_api_destroy (eio_req *req) |
1062 | { |
1313 | { |
1063 | free (req); |
1314 | free (req); |
… | |
… | |
1085 | { \ |
1336 | { \ |
1086 | eio_api_destroy (req); \ |
1337 | eio_api_destroy (req); \ |
1087 | return 0; \ |
1338 | return 0; \ |
1088 | } |
1339 | } |
1089 | |
1340 | |
|
|
1341 | static void eio_execute (etp_worker *self, eio_req *req) |
|
|
1342 | { |
|
|
1343 | errno = 0; |
|
|
1344 | |
|
|
1345 | switch (req->type) |
|
|
1346 | { |
|
|
1347 | case EIO_READ: ALLOC (req->size); |
|
|
1348 | req->result = req->offs >= 0 |
|
|
1349 | ? pread (req->int1, req->ptr2, req->size, req->offs) |
|
|
1350 | : read (req->int1, req->ptr2, req->size); break; |
|
|
1351 | case EIO_WRITE: req->result = req->offs >= 0 |
|
|
1352 | ? pwrite (req->int1, req->ptr2, req->size, req->offs) |
|
|
1353 | : write (req->int1, req->ptr2, req->size); break; |
|
|
1354 | |
|
|
1355 | case EIO_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break; |
|
|
1356 | case EIO_SENDFILE: req->result = eio__sendfile (req->int1, req->int2, req->offs, req->size, self); break; |
|
|
1357 | |
|
|
1358 | case EIO_STAT: ALLOC (sizeof (EIO_STRUCT_STAT)); |
|
|
1359 | req->result = stat (req->ptr1, (EIO_STRUCT_STAT *)req->ptr2); break; |
|
|
1360 | case EIO_LSTAT: ALLOC (sizeof (EIO_STRUCT_STAT)); |
|
|
1361 | req->result = lstat (req->ptr1, (EIO_STRUCT_STAT *)req->ptr2); break; |
|
|
1362 | case EIO_FSTAT: ALLOC (sizeof (EIO_STRUCT_STAT)); |
|
|
1363 | req->result = fstat (req->int1, (EIO_STRUCT_STAT *)req->ptr2); break; |
|
|
1364 | |
|
|
1365 | case EIO_CHOWN: req->result = chown (req->ptr1, req->int2, req->int3); break; |
|
|
1366 | case EIO_FCHOWN: req->result = fchown (req->int1, req->int2, req->int3); break; |
|
|
1367 | case EIO_CHMOD: req->result = chmod (req->ptr1, (mode_t)req->int2); break; |
|
|
1368 | case EIO_FCHMOD: req->result = fchmod (req->int1, (mode_t)req->int2); break; |
|
|
1369 | case EIO_TRUNCATE: req->result = truncate (req->ptr1, req->offs); break; |
|
|
1370 | case EIO_FTRUNCATE: req->result = ftruncate (req->int1, req->offs); break; |
|
|
1371 | |
|
|
1372 | case EIO_OPEN: req->result = open (req->ptr1, req->int1, (mode_t)req->int2); break; |
|
|
1373 | case EIO_CLOSE: req->result = close (req->int1); break; |
|
|
1374 | case EIO_DUP2: req->result = dup2 (req->int1, req->int2); break; |
|
|
1375 | case EIO_UNLINK: req->result = unlink (req->ptr1); break; |
|
|
1376 | case EIO_RMDIR: req->result = rmdir (req->ptr1); break; |
|
|
1377 | case EIO_MKDIR: req->result = mkdir (req->ptr1, (mode_t)req->int2); break; |
|
|
1378 | case EIO_RENAME: req->result = rename (req->ptr1, req->ptr2); break; |
|
|
1379 | case EIO_LINK: req->result = link (req->ptr1, req->ptr2); break; |
|
|
1380 | case EIO_SYMLINK: req->result = symlink (req->ptr1, req->ptr2); break; |
|
|
1381 | case EIO_MKNOD: req->result = mknod (req->ptr1, (mode_t)req->int2, (dev_t)req->int3); break; |
|
|
1382 | |
|
|
1383 | case EIO_READLINK: ALLOC (NAME_MAX); |
|
|
1384 | req->result = readlink (req->ptr1, req->ptr2, NAME_MAX); break; |
|
|
1385 | |
|
|
1386 | case EIO_SYNC: req->result = 0; sync (); break; |
|
|
1387 | case EIO_FSYNC: req->result = fsync (req->int1); break; |
|
|
1388 | case EIO_FDATASYNC: req->result = fdatasync (req->int1); break; |
|
|
1389 | case EIO_MSYNC: req->result = msync (req->ptr2, req->size, req->int1); break; |
|
|
1390 | case EIO_MTOUCH: req->result = eio__mtouch (req->ptr2, req->size, req->int1); break; |
|
|
1391 | case EIO_SYNC_FILE_RANGE: req->result = eio__sync_file_range (req->int1, req->offs, req->size, req->int2); break; |
|
|
1392 | |
|
|
1393 | case EIO_READDIR: eio__scandir (req, self); break; |
|
|
1394 | |
|
|
1395 | case EIO_BUSY: |
|
|
1396 | #ifdef _WIN32 |
|
|
1397 | Sleep (req->nv1 * 1000.); |
|
|
1398 | #else |
|
|
1399 | { |
|
|
1400 | struct timeval tv; |
|
|
1401 | |
|
|
1402 | tv.tv_sec = req->nv1; |
|
|
1403 | tv.tv_usec = (req->nv1 - tv.tv_sec) * 1000000.; |
|
|
1404 | |
|
|
1405 | req->result = select (0, 0, 0, 0, &tv); |
|
|
1406 | } |
|
|
1407 | #endif |
|
|
1408 | break; |
|
|
1409 | |
|
|
1410 | case EIO_UTIME: |
|
|
1411 | case EIO_FUTIME: |
|
|
1412 | { |
|
|
1413 | struct timeval tv[2]; |
|
|
1414 | struct timeval *times; |
|
|
1415 | |
|
|
1416 | if (req->nv1 != -1. || req->nv2 != -1.) |
|
|
1417 | { |
|
|
1418 | tv[0].tv_sec = req->nv1; |
|
|
1419 | tv[0].tv_usec = (req->nv1 - tv[0].tv_sec) * 1000000.; |
|
|
1420 | tv[1].tv_sec = req->nv2; |
|
|
1421 | tv[1].tv_usec = (req->nv2 - tv[1].tv_sec) * 1000000.; |
|
|
1422 | |
|
|
1423 | times = tv; |
|
|
1424 | } |
|
|
1425 | else |
|
|
1426 | times = 0; |
|
|
1427 | |
|
|
1428 | |
|
|
1429 | req->result = req->type == EIO_FUTIME |
|
|
1430 | ? futimes (req->int1, times) |
|
|
1431 | : utimes (req->ptr1, times); |
|
|
1432 | } |
|
|
1433 | break; |
|
|
1434 | |
|
|
1435 | case EIO_GROUP: |
|
|
1436 | abort (); /* handled in eio_request */ |
|
|
1437 | |
|
|
1438 | case EIO_NOP: |
|
|
1439 | req->result = 0; |
|
|
1440 | break; |
|
|
1441 | |
|
|
1442 | case EIO_CUSTOM: |
|
|
1443 | ((void (*)(eio_req *))req->feed) (req); |
|
|
1444 | break; |
|
|
1445 | |
|
|
1446 | default: |
|
|
1447 | req->result = -1; |
|
|
1448 | break; |
|
|
1449 | } |
|
|
1450 | |
|
|
1451 | req->errorno = errno; |
|
|
1452 | } |
|
|
1453 | |
1090 | #ifndef EIO_NO_WRAPPERS |
1454 | #ifndef EIO_NO_WRAPPERS |
1091 | |
1455 | |
1092 | eio_req *eio_nop (int pri, eio_cb cb, void *data) |
1456 | eio_req *eio_nop (int pri, eio_cb cb, void *data) |
1093 | { |
1457 | { |
1094 | REQ (EIO_NOP); SEND; |
1458 | REQ (EIO_NOP); SEND; |
… | |
… | |
1105 | } |
1469 | } |
1106 | |
1470 | |
1107 | eio_req *eio_fsync (int fd, int pri, eio_cb cb, void *data) |
1471 | eio_req *eio_fsync (int fd, int pri, eio_cb cb, void *data) |
1108 | { |
1472 | { |
1109 | REQ (EIO_FSYNC); req->int1 = fd; SEND; |
1473 | REQ (EIO_FSYNC); req->int1 = fd; SEND; |
|
|
1474 | } |
|
|
1475 | |
|
|
1476 | eio_req *eio_msync (void *addr, size_t length, int flags, int pri, eio_cb cb, void *data) |
|
|
1477 | { |
|
|
1478 | REQ (EIO_MSYNC); req->ptr2 = addr; req->size = length; req->int1 = flags; SEND; |
|
|
1479 | } |
|
|
1480 | |
|
|
1481 | eio_req *eio_mtouch (void *addr, size_t length, int flags, int pri, eio_cb cb, void *data) |
|
|
1482 | { |
|
|
1483 | REQ (EIO_MTOUCH); req->ptr2 = addr; req->size = length; req->int1 = flags; SEND; |
|
|
1484 | } |
|
|
1485 | |
|
|
1486 | eio_req *eio_sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags, int pri, eio_cb cb, void *data) |
|
|
1487 | { |
|
|
1488 | REQ (EIO_SYNC_FILE_RANGE); req->int1 = fd; req->offs = offset; req->size = nbytes; req->int2 = flags; SEND; |
1110 | } |
1489 | } |
1111 | |
1490 | |
1112 | eio_req *eio_fdatasync (int fd, int pri, eio_cb cb, void *data) |
1491 | eio_req *eio_fdatasync (int fd, int pri, eio_cb cb, void *data) |
1113 | { |
1492 | { |
1114 | REQ (EIO_FDATASYNC); req->int1 = fd; SEND; |
1493 | REQ (EIO_FDATASYNC); req->int1 = fd; SEND; |
… | |
… | |
1228 | eio_req *eio_rmdir (const char *path, int pri, eio_cb cb, void *data) |
1607 | eio_req *eio_rmdir (const char *path, int pri, eio_cb cb, void *data) |
1229 | { |
1608 | { |
1230 | return eio__1path (EIO_RMDIR, path, pri, cb, data); |
1609 | return eio__1path (EIO_RMDIR, path, pri, cb, data); |
1231 | } |
1610 | } |
1232 | |
1611 | |
1233 | eio_req *eio_readdir (const char *path, int pri, eio_cb cb, void *data) |
1612 | eio_req *eio_readdir (const char *path, int flags, int pri, eio_cb cb, void *data) |
1234 | { |
1613 | { |
1235 | return eio__1path (EIO_READDIR, path, pri, cb, data); |
1614 | REQ (EIO_READDIR); PATH; req->int1 = flags; SEND; |
1236 | } |
1615 | } |
1237 | |
1616 | |
1238 | eio_req *eio_mknod (const char *path, mode_t mode, dev_t dev, int pri, eio_cb cb, void *data) |
1617 | eio_req *eio_mknod (const char *path, mode_t mode, dev_t dev, int pri, eio_cb cb, void *data) |
1239 | { |
1618 | { |
1240 | REQ (EIO_MKNOD); PATH; req->int2 = (long)mode; req->int2 = (long)dev; SEND; |
1619 | REQ (EIO_MKNOD); PATH; req->int2 = (long)mode; req->int3 = (long)dev; SEND; |
1241 | } |
1620 | } |
1242 | |
1621 | |
1243 | static eio_req * |
1622 | static eio_req * |
1244 | eio__2path (int type, const char *path, const char *new_path, int pri, eio_cb cb, void *data) |
1623 | eio__2path (int type, const char *path, const char *new_path, int pri, eio_cb cb, void *data) |
1245 | { |
1624 | { |
… | |
… | |
1269 | eio_req *eio_rename (const char *path, const char *new_path, int pri, eio_cb cb, void *data) |
1648 | eio_req *eio_rename (const char *path, const char *new_path, int pri, eio_cb cb, void *data) |
1270 | { |
1649 | { |
1271 | return eio__2path (EIO_RENAME, path, new_path, pri, cb, data); |
1650 | return eio__2path (EIO_RENAME, path, new_path, pri, cb, data); |
1272 | } |
1651 | } |
1273 | |
1652 | |
|
|
1653 | eio_req *eio_custom (eio_cb execute, int pri, eio_cb cb, void *data) |
|
|
1654 | { |
|
|
1655 | REQ (EIO_CUSTOM); req->feed = (void (*)(eio_req *))execute; SEND; |
|
|
1656 | } |
|
|
1657 | |
1274 | #endif |
1658 | #endif |
1275 | |
1659 | |
1276 | eio_req *eio_grp (eio_cb cb, void *data) |
1660 | eio_req *eio_grp (eio_cb cb, void *data) |
1277 | { |
1661 | { |
1278 | const int pri = EIO_PRI_MAX; |
1662 | const int pri = EIO_PRI_MAX; |
… | |
… | |
1304 | |
1688 | |
1305 | void eio_grp_add (eio_req *grp, eio_req *req) |
1689 | void eio_grp_add (eio_req *grp, eio_req *req) |
1306 | { |
1690 | { |
1307 | assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2)); |
1691 | assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2)); |
1308 | |
1692 | |
|
|
1693 | grp->flags |= EIO_FLAG_GROUPADD; |
|
|
1694 | |
1309 | ++grp->size; |
1695 | ++grp->size; |
1310 | req->grp = grp; |
1696 | req->grp = grp; |
1311 | |
1697 | |
1312 | req->grp_prev = 0; |
1698 | req->grp_prev = 0; |
1313 | req->grp_next = grp->grp_first; |
1699 | req->grp_next = grp->grp_first; |
… | |
… | |
1321 | /*****************************************************************************/ |
1707 | /*****************************************************************************/ |
1322 | /* misc garbage */ |
1708 | /* misc garbage */ |
1323 | |
1709 | |
1324 | ssize_t eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count) |
1710 | ssize_t eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count) |
1325 | { |
1711 | { |
1326 | worker wrk; |
1712 | etp_worker wrk; |
1327 | |
1713 | |
1328 | wrk.dbuf = 0; |
1714 | wrk.dbuf = 0; |
1329 | |
1715 | |
1330 | eio__sendfile (ofd, ifd, offset, count, &wrk); |
1716 | eio__sendfile (ofd, ifd, offset, count, &wrk); |
1331 | |
1717 | |