|
|
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 | |
|
|
5 | #define _XOPEN_SOURCE 500 |
|
|
6 | |
6 | |
7 | #include <sys/types.h> |
7 | #include <sys/types.h> |
8 | #include <sys/stat.h> |
8 | #include <sys/stat.h> |
9 | |
9 | |
10 | #include <unistd.h> |
10 | #include <unistd.h> |
… | |
… | |
54 | } aio_cb; |
54 | } aio_cb; |
55 | |
55 | |
56 | typedef aio_cb *aio_req; |
56 | typedef aio_cb *aio_req; |
57 | |
57 | |
58 | static int started; |
58 | static int started; |
59 | static int nreqs; |
59 | static volatile int nreqs; |
60 | static int max_outstanding = 1<<30; |
60 | static int max_outstanding = 1<<30; |
61 | static int respipe [2]; |
61 | static int respipe [2]; |
62 | |
62 | |
63 | static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
63 | static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
64 | static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; |
64 | static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; |
… | |
… | |
68 | static volatile aio_req ress, rese; /* queue start, queue end */ |
68 | static volatile aio_req ress, rese; /* queue start, queue end */ |
69 | |
69 | |
70 | static void |
70 | static void |
71 | poll_wait () |
71 | poll_wait () |
72 | { |
72 | { |
73 | if (!nreqs) |
73 | if (nreqs && !ress) |
74 | return; |
74 | { |
75 | |
|
|
76 | fd_set rfd; |
75 | fd_set rfd; |
77 | FD_ZERO(&rfd); |
76 | FD_ZERO(&rfd); |
78 | FD_SET(respipe [0], &rfd); |
77 | FD_SET(respipe [0], &rfd); |
79 | |
78 | |
80 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
79 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
|
|
80 | } |
81 | } |
81 | } |
82 | |
82 | |
83 | static int |
83 | static int |
84 | poll_cb () |
84 | poll_cb () |
85 | { |
85 | { |
86 | dSP; |
86 | dSP; |
87 | int count = 0; |
87 | int count = 0; |
88 | aio_req req; |
88 | aio_req req, prv; |
89 | |
89 | |
|
|
90 | static int rl;//D |
|
|
91 | //printf ("%d ENTER\n", ++rl);//D |
|
|
92 | |
|
|
93 | pthread_mutex_lock (&reslock); |
|
|
94 | |
90 | { |
95 | { |
91 | /* read and signals sent by the worker threads */ |
96 | /* read any signals sent by the worker threads */ |
92 | char buf [32]; |
97 | char buf [32]; |
93 | while (read (respipe [0], buf, 32) > 0) |
98 | while (read (respipe [0], buf, 32) > 0) |
94 | ; |
99 | ; |
95 | } |
100 | } |
96 | |
101 | |
97 | for (;;) |
102 | req = ress; |
|
|
103 | ress = rese = 0; |
|
|
104 | |
|
|
105 | pthread_mutex_unlock (&reslock); |
|
|
106 | |
|
|
107 | while (req) |
98 | { |
108 | { |
99 | pthread_mutex_lock (&reslock); |
|
|
100 | |
|
|
101 | req = ress; |
|
|
102 | |
|
|
103 | if (ress) |
|
|
104 | { |
|
|
105 | ress = ress->next; |
|
|
106 | if (!ress) rese = 0; |
|
|
107 | } |
|
|
108 | |
|
|
109 | pthread_mutex_unlock (&reslock); |
|
|
110 | |
|
|
111 | if (!req) |
|
|
112 | break; |
|
|
113 | |
|
|
114 | nreqs--; |
109 | nreqs--; |
|
|
110 | //printf ("%d count %d %p->%p\n", rl, count, req, req->next);//D |
115 | |
111 | |
116 | if (req->type == REQ_QUIT) |
112 | if (req->type == REQ_QUIT) |
117 | started--; |
113 | started--; |
118 | else |
114 | else |
119 | { |
115 | { |
… | |
… | |
166 | |
162 | |
167 | errno = errorno; |
163 | errno = errorno; |
168 | count++; |
164 | count++; |
169 | } |
165 | } |
170 | |
166 | |
|
|
167 | prv = req; |
|
|
168 | req = req->next; |
171 | Safefree (req); |
169 | Safefree (prv); |
|
|
170 | |
|
|
171 | /* TODO: croak on errors? */ |
172 | } |
172 | } |
173 | |
173 | |
|
|
174 | //printf ("%d LEAVE %p %p\n", rl--, ress, rese);//D |
174 | return count; |
175 | return count; |
175 | } |
176 | } |
176 | |
177 | |
177 | static void *aio_proc(void *arg); |
178 | static void *aio_proc(void *arg); |
178 | |
179 | |