|
|
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> |
|
|
14 | #if __linux |
|
|
15 | #include <sys/syscall.h> |
|
|
16 | #endif |
11 | |
17 | |
12 | #include <pthread.h> |
18 | #include <pthread.h> |
13 | #include <sys/syscall.h> |
|
|
14 | |
19 | |
15 | 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 ;) */ |
16 | 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 ;) */ |
17 | 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 ;) */ |
18 | |
23 | |
… | |
… | |
49 | } aio_cb; |
54 | } aio_cb; |
50 | |
55 | |
51 | typedef aio_cb *aio_req; |
56 | typedef aio_cb *aio_req; |
52 | |
57 | |
53 | static int started; |
58 | static int started; |
54 | static int nreqs; |
59 | static volatile int nreqs; |
55 | static int max_outstanding = 1<<30; |
60 | static int max_outstanding = 1<<30; |
56 | static int respipe [2]; |
61 | static int respipe [2]; |
57 | |
62 | |
58 | static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
63 | static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
59 | static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; |
64 | static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; |
… | |
… | |
63 | static volatile aio_req ress, rese; /* queue start, queue end */ |
68 | static volatile aio_req ress, rese; /* queue start, queue end */ |
64 | |
69 | |
65 | static void |
70 | static void |
66 | poll_wait () |
71 | poll_wait () |
67 | { |
72 | { |
68 | if (!nreqs) |
73 | if (nreqs && !ress) |
69 | return; |
74 | { |
70 | |
|
|
71 | fd_set rfd; |
75 | fd_set rfd; |
72 | FD_ZERO(&rfd); |
76 | FD_ZERO(&rfd); |
73 | FD_SET(respipe [0], &rfd); |
77 | FD_SET(respipe [0], &rfd); |
74 | |
78 | |
75 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
79 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
|
|
80 | } |
76 | } |
81 | } |
77 | |
82 | |
78 | static int |
83 | static int |
79 | poll_cb () |
84 | poll_cb () |
80 | { |
85 | { |
81 | dSP; |
86 | dSP; |
82 | int count = 0; |
87 | int count = 0; |
83 | aio_req req; |
88 | aio_req req, prv; |
84 | |
89 | |
|
|
90 | pthread_mutex_lock (&reslock); |
|
|
91 | |
85 | { |
92 | { |
86 | /* read and signals sent by the worker threads */ |
93 | /* read any signals sent by the worker threads */ |
87 | char buf [32]; |
94 | char buf [32]; |
88 | while (read (respipe [0], buf, 32) > 0) |
95 | while (read (respipe [0], buf, 32) > 0) |
89 | ; |
96 | ; |
90 | } |
97 | } |
91 | |
98 | |
92 | for (;;) |
99 | req = ress; |
|
|
100 | ress = rese = 0; |
|
|
101 | |
|
|
102 | pthread_mutex_unlock (&reslock); |
|
|
103 | |
|
|
104 | while (req) |
93 | { |
105 | { |
94 | pthread_mutex_lock (&reslock); |
|
|
95 | |
|
|
96 | req = ress; |
|
|
97 | |
|
|
98 | if (ress) |
|
|
99 | { |
|
|
100 | ress = ress->next; |
|
|
101 | if (!ress) rese = 0; |
|
|
102 | } |
|
|
103 | |
|
|
104 | pthread_mutex_unlock (&reslock); |
|
|
105 | |
|
|
106 | if (!req) |
|
|
107 | break; |
|
|
108 | |
|
|
109 | nreqs--; |
106 | nreqs--; |
110 | |
107 | |
111 | if (req->type == REQ_QUIT) |
108 | if (req->type == REQ_QUIT) |
112 | started--; |
109 | started--; |
113 | else |
110 | else |
… | |
… | |
161 | |
158 | |
162 | errno = errorno; |
159 | errno = errorno; |
163 | count++; |
160 | count++; |
164 | } |
161 | } |
165 | |
162 | |
|
|
163 | prv = req; |
|
|
164 | req = req->next; |
166 | Safefree (req); |
165 | Safefree (prv); |
|
|
166 | |
|
|
167 | /* TODO: croak on errors? */ |
167 | } |
168 | } |
168 | |
169 | |
169 | return count; |
170 | return count; |
170 | } |
171 | } |
171 | |
172 | |
… | |
… | |
308 | |
309 | |
309 | type = req->type; |
310 | type = req->type; |
310 | |
311 | |
311 | switch (type) |
312 | switch (type) |
312 | { |
313 | { |
313 | 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; |
314 | 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; |
315 | #if SYS_readahead |
316 | #if SYS_readahead |
316 | 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; |
317 | #else |
318 | #else |
318 | case REQ_READAHEAD: req->result = -1; errno = ENOSYS; break; |
319 | case REQ_READAHEAD: req->result = -1; errno = ENOSYS; break; |
319 | #endif |
320 | #endif |