This repository was archived by the owner on Apr 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
/
Copy patheio.c
2128 lines (1696 loc) · 52.3 KB
/
eio.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* libeio implementation
*
* Copyright (c) 2007,2008,2009,2010 Marc Alexander Lehmann <libeio@schmorp.de>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modifica-
* tion, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
* CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
* CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-
* ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Alternatively, the contents of this file may be used under the terms of
* the GNU General Public License ("GPL") version 2 or any later version,
* in which case the provisions of the GPL are applicable instead of
* the above. If you wish to allow the use of your version of this file
* only under the terms of the GPL and not to allow others to use your
* version of this file under the BSD license, indicate your decision
* by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL. If you do not delete the
* provisions above, a recipient may use your version of this file under
* either the BSD or the GPL.
*/
#include "eio.h"
#ifdef EIO_STACKSIZE
# define XTHREAD_STACKSIZE EIO_STACKSIZE
#endif
// For statically-linked pthreads-w32, use:
// #ifdef _WIN32
// # define PTW32_STATIC_LIB 1
// #endif
#include "xthread.h"
#include <errno.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <limits.h>
#include <fcntl.h>
#include <assert.h>
#ifndef _WIN32
#include <sys/statvfs.h>
#endif
#ifndef EIO_FINISH
# define EIO_FINISH(req) ((req)->finish) && !EIO_CANCELLED (req) ? (req)->finish (req) : 0
#endif
#ifndef EIO_DESTROY
# define EIO_DESTROY(req) do { if ((req)->destroy) (req)->destroy (req); } while (0)
#endif
#ifndef EIO_FEED
# define EIO_FEED(req) do { if ((req)->feed ) (req)->feed (req); } while (0)
#endif
#ifdef _WIN32
# include <errno.h>
# include <sys/time.h>
# include <unistd.h>
# include <utime.h>
# include <signal.h>
# include <dirent.h>
# include <windows.h>
# define ENOTSOCK WSAENOTSOCK
# define EOPNOTSUPP WSAEOPNOTSUPP
# define ECANCELED 140
# ifndef EIO_STRUCT_DIRENT
# define EIO_STRUCT_DIRENT struct dirent
# endif
#else
# include "config.h"
# include <sys/time.h>
# include <sys/select.h>
# include <unistd.h>
# include <utime.h>
# include <signal.h>
# include <dirent.h>
#if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES
# include <sys/mman.h>
#endif
/* POSIX_SOURCE is useless on bsd's, and XOPEN_SOURCE is unreliable there, too */
# if __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__
# define _DIRENT_HAVE_D_TYPE /* sigh */
# define D_INO(de) (de)->d_fileno
# define D_NAMLEN(de) (de)->d_namlen
# elif __linux || defined d_ino || _XOPEN_SOURCE >= 600
# define D_INO(de) (de)->d_ino
# endif
#ifdef _D_EXACT_NAMLEN
# undef D_NAMLEN
# define D_NAMLEN(de) _D_EXACT_NAMLEN (de)
#endif
# ifdef _DIRENT_HAVE_D_TYPE
# define D_TYPE(de) (de)->d_type
# endif
# ifndef EIO_STRUCT_DIRENT
# define EIO_STRUCT_DIRENT struct dirent
# endif
#endif
#if HAVE_SENDFILE
# if __linux
# include <sys/sendfile.h>
# elif __FreeBSD__ || defined __APPLE__
# include <sys/socket.h>
# include <sys/uio.h>
# elif __hpux
# include <sys/socket.h>
# elif __solaris
# include <sys/sendfile.h>
# else
# error sendfile support requested but not available
# endif
#endif
#ifndef D_TYPE
# define D_TYPE(de) 0
#endif
#ifndef D_INO
# define D_INO(de) 0
#endif
#ifndef D_NAMLEN
# define D_NAMLEN(de) strlen ((de)->d_name)
#endif
/* number of seconds after which an idle threads exit */
#define IDLE_TIMEOUT 10
/* used for struct dirent, AIX doesn't provide it */
#ifndef NAME_MAX
# define NAME_MAX 4096
#endif
/* used for readlink etc. */
#ifndef PATH_MAX
# define PATH_MAX 4096
#endif
/* buffer size for various temporary buffers */
#define EIO_BUFSIZE 65536
#define dBUF \
char *eio_buf; \
ETP_WORKER_LOCK (self); \
self->dbuf = eio_buf = malloc (EIO_BUFSIZE); \
ETP_WORKER_UNLOCK (self); \
errno = ENOMEM; \
if (!eio_buf) \
return -1;
#define EIO_TICKS ((1000000 + 1023) >> 10)
/*****************************************************************************/
#if __GNUC__ >= 3
# define expect(expr,value) __builtin_expect ((expr),(value))
#else
# define expect(expr,value) (expr)
#endif
#define expect_false(expr) expect ((expr) != 0, 0)
#define expect_true(expr) expect ((expr) != 0, 1)
/*****************************************************************************/
#define ETP_PRI_MIN EIO_PRI_MIN
#define ETP_PRI_MAX EIO_PRI_MAX
struct etp_worker;
#define ETP_REQ eio_req
#define ETP_DESTROY(req) eio_destroy (req)
static int eio_finish (eio_req *req);
#define ETP_FINISH(req) eio_finish (req)
static void eio_execute (struct etp_worker *self, eio_req *req);
#define ETP_EXECUTE(wrk,req) eio_execute (wrk,req)
#define ETP_WORKER_CLEAR(req) \
if (wrk->dbuf) \
{ \
free (wrk->dbuf); \
wrk->dbuf = 0; \
} \
\
if (wrk->dirp) \
{ \
closedir (wrk->dirp); \
wrk->dirp = 0; \
}
#define ETP_WORKER_COMMON \
void *dbuf; \
DIR *dirp;
/*****************************************************************************/
#define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1)
/* calculate time difference in ~1/EIO_TICKS of a second */
static int tvdiff (struct timeval *tv1, struct timeval *tv2)
{
return (tv2->tv_sec - tv1->tv_sec ) * EIO_TICKS
+ ((tv2->tv_usec - tv1->tv_usec) >> 10);
}
static unsigned int started, idle, wanted = 4;
static void (*want_poll_cb) (void);
static void (*done_poll_cb) (void);
static unsigned int max_poll_time; /* reslock */
static unsigned int max_poll_reqs; /* reslock */
static volatile unsigned int nreqs; /* reqlock */
static volatile unsigned int nready; /* reqlock */
static volatile unsigned int npending; /* reqlock */
static volatile unsigned int max_idle = 4;
static xmutex_t wrklock = X_MUTEX_INIT;
static xmutex_t reslock = X_MUTEX_INIT;
static xmutex_t reqlock = X_MUTEX_INIT;
static xcond_t reqwait = X_COND_INIT;
#if defined (__APPLE__)
static xmutex_t apple_bug_writelock = X_MUTEX_INIT;
#endif
#if !HAVE_PREADWRITE
/*
* make our pread/pwrite emulation safe against themselves, but not against
* normal read/write by using a mutex. slows down execution a lot,
* but that's your problem, not mine.
*/
static xmutex_t preadwritelock = X_MUTEX_INIT;
#endif
typedef struct etp_worker
{
/* locked by wrklock */
struct etp_worker *prev, *next;
xthread_t tid;
/* locked by reslock, reqlock or wrklock */
ETP_REQ *req; /* currently processed request */
ETP_WORKER_COMMON
} etp_worker;
static etp_worker wrk_first = { &wrk_first, &wrk_first, 0 }; /* NOT etp */
#define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock)
#define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock)
/* worker threads management */
static void etp_worker_clear (etp_worker *wrk)
{
ETP_WORKER_CLEAR (wrk);
}
static void etp_worker_free (etp_worker *wrk)
{
wrk->next->prev = wrk->prev;
wrk->prev->next = wrk->next;
free (wrk);
}
static unsigned int etp_nreqs (void)
{
int retval;
if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
retval = nreqs;
if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
return retval;
}
static unsigned int etp_nready (void)
{
unsigned int retval;
if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
retval = nready;
if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
return retval;
}
static unsigned int etp_npending (void)
{
unsigned int retval;
if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
retval = npending;
if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
return retval;
}
static unsigned int etp_nthreads (void)
{
unsigned int retval;
if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
retval = started;
if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
return retval;
}
/*
* a somewhat faster data structure might be nice, but
* with 8 priorities this actually needs <20 insns
* per shift, the most expensive operation.
*/
typedef struct {
ETP_REQ *qs[ETP_NUM_PRI], *qe[ETP_NUM_PRI]; /* qstart, qend */
int size;
} etp_reqq;
static etp_reqq req_queue;
static etp_reqq res_queue;
static int reqq_push (etp_reqq *q, ETP_REQ *req)
{
int pri = req->pri;
req->next = 0;
if (q->qe[pri])
{
q->qe[pri]->next = req;
q->qe[pri] = req;
}
else
q->qe[pri] = q->qs[pri] = req;
return q->size++;
}
static ETP_REQ *reqq_shift (etp_reqq *q)
{
int pri;
if (!q->size)
return 0;
--q->size;
for (pri = ETP_NUM_PRI; pri--; )
{
eio_req *req = q->qs[pri];
if (req)
{
if (!(q->qs[pri] = (eio_req *)req->next))
q->qe[pri] = 0;
return req;
}
}
abort ();
}
static void etp_atfork_prepare (void)
{
X_LOCK (wrklock);
X_LOCK (reqlock);
X_LOCK (reslock);
#if !HAVE_PREADWRITE
X_LOCK (preadwritelock);
#endif
}
static void etp_atfork_parent (void)
{
#if !HAVE_PREADWRITE
X_UNLOCK (preadwritelock);
#endif
X_UNLOCK (reslock);
X_UNLOCK (reqlock);
X_UNLOCK (wrklock);
}
static void etp_atfork_child (void)
{
ETP_REQ *prv;
while ((prv = reqq_shift (&req_queue)))
ETP_DESTROY (prv);
while ((prv = reqq_shift (&res_queue)))
ETP_DESTROY (prv);
while (wrk_first.next != &wrk_first)
{
etp_worker *wrk = wrk_first.next;
if (wrk->req)
ETP_DESTROY (wrk->req);
etp_worker_clear (wrk);
etp_worker_free (wrk);
}
started = 0;
idle = 0;
nreqs = 0;
nready = 0;
npending = 0;
etp_atfork_parent ();
}
static void
etp_once_init (void)
{
X_THREAD_ATFORK (etp_atfork_prepare, etp_atfork_parent, etp_atfork_child);
}
static int
etp_init (void (*want_poll)(void), void (*done_poll)(void))
{
static pthread_once_t doinit = PTHREAD_ONCE_INIT;
pthread_once (&doinit, etp_once_init);
want_poll_cb = want_poll;
done_poll_cb = done_poll;
return 0;
}
X_THREAD_PROC (etp_proc);
static void etp_start_thread (void)
{
etp_worker *wrk = calloc (1, sizeof (etp_worker));
/*TODO*/
assert (("unable to allocate worker thread data", wrk));
X_LOCK (wrklock);
if (thread_create (&wrk->tid, etp_proc, (void *)wrk))
{
wrk->prev = &wrk_first;
wrk->next = wrk_first.next;
wrk_first.next->prev = wrk;
wrk_first.next = wrk;
++started;
}
else
free (wrk);
X_UNLOCK (wrklock);
}
static void etp_maybe_start_thread (void)
{
if (expect_true (etp_nthreads () >= wanted))
return;
/* todo: maybe use idle here, but might be less exact */
if (expect_true (0 <= (int)etp_nthreads () + (int)etp_npending () - (int)etp_nreqs ()))
return;
etp_start_thread ();
}
static void etp_end_thread (void)
{
eio_req *req = calloc (1, sizeof (eio_req));
req->type = -1;
req->pri = ETP_PRI_MAX - ETP_PRI_MIN;
X_LOCK (reqlock);
reqq_push (&req_queue, req);
X_COND_SIGNAL (reqwait);
X_UNLOCK (reqlock);
X_LOCK (wrklock);
--started;
X_UNLOCK (wrklock);
}
static int etp_poll (void)
{
unsigned int maxreqs;
unsigned int maxtime;
struct timeval tv_start, tv_now;
X_LOCK (reslock);
maxreqs = max_poll_reqs;
maxtime = max_poll_time;
X_UNLOCK (reslock);
if (maxtime)
gettimeofday (&tv_start, 0);
for (;;)
{
ETP_REQ *req;
etp_maybe_start_thread ();
X_LOCK (reslock);
req = reqq_shift (&res_queue);
if (req)
{
--npending;
if (!res_queue.size && done_poll_cb)
done_poll_cb ();
}
X_UNLOCK (reslock);
if (!req)
return 0;
X_LOCK (reqlock);
--nreqs;
X_UNLOCK (reqlock);
if (expect_false (req->type == EIO_GROUP && req->size))
{
req->int1 = 1; /* mark request as delayed */
continue;
}
else
{
int res = ETP_FINISH (req);
if (expect_false (res))
return res;
}
if (expect_false (maxreqs && !--maxreqs))
break;
if (maxtime)
{
gettimeofday (&tv_now, 0);
if (tvdiff (&tv_start, &tv_now) >= maxtime)
break;
}
}
errno = EAGAIN;
return -1;
}
static void etp_cancel (ETP_REQ *req)
{
X_LOCK (wrklock);
req->flags |= EIO_FLAG_CANCELLED;
X_UNLOCK (wrklock);
eio_grp_cancel (req);
}
static void etp_submit (ETP_REQ *req)
{
req->pri -= ETP_PRI_MIN;
if (expect_false (req->pri < ETP_PRI_MIN - ETP_PRI_MIN)) req->pri = ETP_PRI_MIN - ETP_PRI_MIN;
if (expect_false (req->pri > ETP_PRI_MAX - ETP_PRI_MIN)) req->pri = ETP_PRI_MAX - ETP_PRI_MIN;
if (expect_false (req->type == EIO_GROUP))
{
/* I hope this is worth it :/ */
X_LOCK (reqlock);
++nreqs;
X_UNLOCK (reqlock);
X_LOCK (reslock);
++npending;
if (!reqq_push (&res_queue, req) && want_poll_cb)
want_poll_cb ();
X_UNLOCK (reslock);
}
else
{
X_LOCK (reqlock);
++nreqs;
++nready;
reqq_push (&req_queue, req);
X_COND_SIGNAL (reqwait);
X_UNLOCK (reqlock);
etp_maybe_start_thread ();
}
}
static void etp_set_max_poll_time (double nseconds)
{
if (WORDACCESS_UNSAFE) X_LOCK (reslock);
max_poll_time = nseconds * EIO_TICKS;
if (WORDACCESS_UNSAFE) X_UNLOCK (reslock);
}
static void etp_set_max_poll_reqs (unsigned int maxreqs)
{
if (WORDACCESS_UNSAFE) X_LOCK (reslock);
max_poll_reqs = maxreqs;
if (WORDACCESS_UNSAFE) X_UNLOCK (reslock);
}
static void etp_set_max_idle (unsigned int nthreads)
{
if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
max_idle = nthreads <= 0 ? 1 : nthreads;
if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
}
static void etp_set_min_parallel (unsigned int nthreads)
{
if (wanted < nthreads)
wanted = nthreads;
}
static void etp_set_max_parallel (unsigned int nthreads)
{
if (wanted > nthreads)
wanted = nthreads;
while (started > wanted)
etp_end_thread ();
}
/*****************************************************************************/
static void grp_try_feed (eio_req *grp)
{
while (grp->size < grp->int2 && !EIO_CANCELLED (grp))
{
grp->flags &= ~EIO_FLAG_GROUPADD;
EIO_FEED (grp);
/* stop if no progress has been made */
if (!(grp->flags & EIO_FLAG_GROUPADD))
{
grp->feed = 0;
break;
}
}
}
static int grp_dec (eio_req *grp)
{
--grp->size;
/* call feeder, if applicable */
grp_try_feed (grp);
/* finish, if done */
if (!grp->size && grp->int1)
return eio_finish (grp);
else
return 0;
}
void eio_destroy (eio_req *req)
{
if ((req)->flags & EIO_FLAG_PTR1_FREE) free (req->ptr1);
if ((req)->flags & EIO_FLAG_PTR2_FREE) free (req->ptr2);
EIO_DESTROY (req);
}
static int eio_finish (eio_req *req)
{
int res = EIO_FINISH (req);
if (req->grp)
{
int res2;
eio_req *grp = req->grp;
/* unlink request */
if (req->grp_next) req->grp_next->grp_prev = req->grp_prev;
if (req->grp_prev) req->grp_prev->grp_next = req->grp_next;
if (grp->grp_first == req)
grp->grp_first = req->grp_next;
res2 = grp_dec (grp);
if (!res && res2)
res = res2;
}
eio_destroy (req);
return res;
}
void eio_grp_cancel (eio_req *grp)
{
for (grp = grp->grp_first; grp; grp = grp->grp_next)
eio_cancel (grp);
}
void eio_cancel (eio_req *req)
{
etp_cancel (req);
}
void eio_submit (eio_req *req)
{
etp_submit (req);
}
unsigned int eio_nreqs (void)
{
return etp_nreqs ();
}
unsigned int eio_nready (void)
{
return etp_nready ();
}
unsigned int eio_npending (void)
{
return etp_npending ();
}
unsigned int eio_nthreads (void)
{
return etp_nthreads ();
}
void eio_set_max_poll_time (double nseconds)
{
etp_set_max_poll_time (nseconds);
}
void eio_set_max_poll_reqs (unsigned int maxreqs)
{
etp_set_max_poll_reqs (maxreqs);
}
void eio_set_max_idle (unsigned int nthreads)
{
etp_set_max_idle (nthreads);
}
void eio_set_min_parallel (unsigned int nthreads)
{
etp_set_min_parallel (nthreads);
}
void eio_set_max_parallel (unsigned int nthreads)
{
etp_set_max_parallel (nthreads);
}
int eio_poll (void)
{
return etp_poll ();
}
/*****************************************************************************/
/* work around various missing functions */
#if !HAVE_PREADWRITE
# undef pread
# undef pwrite
# define pread eio__pread
# define pwrite eio__pwrite
ssize_t
eio__pread (int fd, void *buf, size_t count, off_t offset)
{
ssize_t res;
off_t ooffset;
X_LOCK (preadwritelock);
ooffset = lseek (fd, 0, SEEK_CUR);
lseek (fd, offset, SEEK_SET);
res = read (fd, buf, count);
lseek (fd, ooffset, SEEK_SET);
X_UNLOCK (preadwritelock);
return res;
}
ssize_t
eio__pwrite (int fd, void *buf, size_t count, off_t offset)
{
ssize_t res;
off_t ooffset;
X_LOCK (preadwritelock);
ooffset = lseek (fd, 0, SEEK_CUR);
lseek (fd, offset, SEEK_SET);
res = write (fd, buf, count);
lseek (fd, ooffset, SEEK_SET);
X_UNLOCK (preadwritelock);
return res;
}
#endif
#ifndef HAVE_UTIMES
# undef utimes
# define utimes(path,times) eio__utimes (path, times)
static int
eio__utimes (const char *filename, const struct timeval times[2])
{
if (times)
{
struct utimbuf buf;
buf.actime = times[0].tv_sec;
buf.modtime = times[1].tv_sec;
return utime (filename, &buf);
}
else
return utime (filename, 0);
}
#endif
#ifndef HAVE_FUTIMES
# undef futimes
# define futimes(fd,times) eio__futimes (fd, times)
static int eio__futimes (int fd, const struct timeval tv[2])
{
errno = ENOSYS;
return -1;
}
#endif
#ifdef _WIN32
# define fsync(fd) (FlushFileBuffers((HANDLE)_get_osfhandle(fd)) ? 0 : -1)
#endif
#if !HAVE_FDATASYNC
# undef fdatasync
# define fdatasync(fd) fsync (fd)
#endif
// Use unicode and big file aware stat on windows
#ifdef _WIN32
# undef stat
# undef fstat
# define stat _stati64
# define fstat _fstati64
#endif
/* sync_file_range always needs emulation */
int
eio__sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags)
{
#if HAVE_SYNC_FILE_RANGE
int res;
if (EIO_SYNC_FILE_RANGE_WAIT_BEFORE != SYNC_FILE_RANGE_WAIT_BEFORE
|| EIO_SYNC_FILE_RANGE_WRITE != SYNC_FILE_RANGE_WRITE
|| EIO_SYNC_FILE_RANGE_WAIT_AFTER != SYNC_FILE_RANGE_WAIT_AFTER)
{
flags = 0
| (flags & EIO_SYNC_FILE_RANGE_WAIT_BEFORE ? SYNC_FILE_RANGE_WAIT_BEFORE : 0)
| (flags & EIO_SYNC_FILE_RANGE_WRITE ? SYNC_FILE_RANGE_WRITE : 0)
| (flags & EIO_SYNC_FILE_RANGE_WAIT_AFTER ? SYNC_FILE_RANGE_WAIT_AFTER : 0);
}
res = sync_file_range (fd, offset, nbytes, flags);
if (!res || errno != ENOSYS)
return res;
#endif
/* even though we could play tricks with the flags, it's better to always
* call fdatasync, as that matches the expectation of its users best */
return fdatasync (fd);
}
#if !HAVE_READAHEAD
# undef readahead
# define readahead(fd,offset,count) eio__readahead (fd, offset, count, self)
static ssize_t
eio__readahead (int fd, off_t offset, size_t count, etp_worker *self)
{
size_t todo = count;
dBUF;
while (todo > 0)
{
size_t len = todo < EIO_BUFSIZE ? todo : EIO_BUFSIZE;
pread (fd, eio_buf, len, offset);
offset += len;
todo -= len;
}
errno = 0;
return count;
}
#endif
/* sendfile always needs emulation */
static ssize_t
eio__sendfile (int ofd, int ifd, off_t offset, size_t count, etp_worker *self)
{
ssize_t res;
if (!count)
return 0;
#if HAVE_SENDFILE
# if __linux
res = sendfile (ofd, ifd, &offset, count);
# elif __FreeBSD__
/*
* Of course, the freebsd sendfile is a dire hack with no thoughts
* wasted on making it similar to other I/O functions.
*/
{
off_t sbytes;
res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0);
#if 0 /* according to the manpage, this is correct, but broken behaviour */
/* freebsd' sendfile will return 0 on success */
/* freebsd 8 documents it as only setting *sbytes on EINTR and EAGAIN, but */
/* not on e.g. EIO or EPIPE - sounds broken */
if ((res < 0 && (errno == EAGAIN || errno == EINTR) && sbytes) || res == 0)
res = sbytes;
#endif
/* according to source inspection, this is correct, and useful behaviour */
if (sbytes)
res = sbytes;
}
# elif defined (__APPLE__)
{
off_t sbytes = count;
res = sendfile (ifd, ofd, offset, &sbytes, 0, 0);
/* according to the manpage, sbytes is always valid */
if (sbytes)
res = sbytes;
}
# elif __hpux