|
|
1 | #define _XOPEN_SOURCE 500 |
|
|
2 | |
1 | #include "EXTERN.h" |
3 | #include "EXTERN.h" |
2 | #include "perl.h" |
4 | #include "perl.h" |
3 | #include "XSUB.h" |
5 | #include "XSUB.h" |
4 | |
6 | |
5 | #include <sys/types.h> |
7 | #include <sys/types.h> |
6 | #include <sys/stat.h> |
8 | #include <sys/stat.h> |
|
|
9 | |
7 | #include <unistd.h> |
10 | #include <unistd.h> |
8 | #include <fcntl.h> |
11 | #include <fcntl.h> |
9 | #include <signal.h> |
12 | #include <signal.h> |
10 | #include <sched.h> |
13 | #include <sched.h> |
11 | #include <endian.h> |
14 | #if __linux |
|
|
15 | #include <sys/syscall.h> |
|
|
16 | #endif |
12 | |
17 | |
13 | #include <pthread.h> |
18 | #include <pthread.h> |
14 | #include <sys/syscall.h> |
|
|
15 | |
19 | |
16 | typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
20 | typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
17 | typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
21 | typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
18 | typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
22 | typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
19 | |
23 | |
… | |
… | |
50 | } aio_cb; |
54 | } aio_cb; |
51 | |
55 | |
52 | typedef aio_cb *aio_req; |
56 | typedef aio_cb *aio_req; |
53 | |
57 | |
54 | static int started; |
58 | static int started; |
55 | static int nreqs; |
59 | static volatile int nreqs; |
56 | static int max_outstanding = 1<<30; |
60 | static int max_outstanding = 1<<30; |
57 | static int respipe [2]; |
61 | static int respipe [2]; |
58 | |
62 | |
59 | static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
63 | static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
60 | static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; |
64 | static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; |
… | |
… | |
64 | static volatile aio_req ress, rese; /* queue start, queue end */ |
68 | static volatile aio_req ress, rese; /* queue start, queue end */ |
65 | |
69 | |
66 | static void |
70 | static void |
67 | poll_wait () |
71 | poll_wait () |
68 | { |
72 | { |
69 | if (!nreqs) |
73 | if (nreqs && !ress) |
70 | return; |
74 | { |
71 | |
|
|
72 | fd_set rfd; |
75 | fd_set rfd; |
73 | FD_ZERO(&rfd); |
76 | FD_ZERO(&rfd); |
74 | FD_SET(respipe [0], &rfd); |
77 | FD_SET(respipe [0], &rfd); |
75 | |
78 | |
76 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
79 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
|
|
80 | } |
77 | } |
81 | } |
78 | |
82 | |
79 | static int |
83 | static int |
80 | poll_cb () |
84 | poll_cb () |
81 | { |
85 | { |
82 | dSP; |
86 | dSP; |
83 | int count = 0; |
87 | int count = 0; |
84 | aio_req req; |
88 | aio_req req, prv; |
85 | |
89 | |
|
|
90 | pthread_mutex_lock (&reslock); |
|
|
91 | |
86 | { |
92 | { |
87 | /* read and signals sent by the worker threads */ |
93 | /* read any signals sent by the worker threads */ |
88 | char buf [32]; |
94 | char buf [32]; |
89 | while (read (respipe [0], buf, 32) > 0) |
95 | while (read (respipe [0], buf, 32) > 0) |
90 | ; |
96 | ; |
91 | } |
97 | } |
92 | |
98 | |
93 | for (;;) |
99 | req = ress; |
|
|
100 | ress = rese = 0; |
|
|
101 | |
|
|
102 | pthread_mutex_unlock (&reslock); |
|
|
103 | |
|
|
104 | while (req) |
94 | { |
105 | { |
95 | pthread_mutex_lock (&reslock); |
|
|
96 | |
|
|
97 | req = ress; |
|
|
98 | |
|
|
99 | if (ress) |
|
|
100 | { |
|
|
101 | ress = ress->next; |
|
|
102 | if (!ress) rese = 0; |
|
|
103 | } |
|
|
104 | |
|
|
105 | pthread_mutex_unlock (&reslock); |
|
|
106 | |
|
|
107 | if (!req) |
|
|
108 | break; |
|
|
109 | |
|
|
110 | nreqs--; |
106 | nreqs--; |
111 | |
107 | |
112 | if (req->type == REQ_QUIT) |
108 | if (req->type == REQ_QUIT) |
113 | started--; |
109 | started--; |
114 | else |
110 | else |
… | |
… | |
162 | |
158 | |
163 | errno = errorno; |
159 | errno = errorno; |
164 | count++; |
160 | count++; |
165 | } |
161 | } |
166 | |
162 | |
|
|
163 | prv = req; |
|
|
164 | req = req->next; |
167 | Safefree (req); |
165 | Safefree (prv); |
|
|
166 | |
|
|
167 | /* TODO: croak on errors? */ |
168 | } |
168 | } |
169 | |
169 | |
170 | return count; |
170 | return count; |
171 | } |
171 | } |
172 | |
172 | |
… | |
… | |
309 | |
309 | |
310 | type = req->type; |
310 | type = req->type; |
311 | |
311 | |
312 | switch (type) |
312 | switch (type) |
313 | { |
313 | { |
314 | case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length, req->offset); break; |
314 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
315 | case REQ_WRITE: req->result = pwrite64 (req->fd, req->dataptr, req->length, req->offset); break; |
315 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
316 | #if SYS_readahead |
316 | #if SYS_readahead |
317 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
317 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
318 | #else |
318 | #else |
319 | case REQ_READAHEAD: req->result = -1; errno = ENOSYS; break; |
319 | case REQ_READAHEAD: req->result = -1; errno = ENOSYS; break; |
320 | #endif |
320 | #endif |