ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/IO-AIO/AIO.xs
(Generate patch)

Comparing IO-AIO/AIO.xs (file contents):
Revision 1.8 by root, Mon Jul 11 02:53:59 2005 UTC vs.
Revision 1.11 by root, Wed Jul 20 21:55:27 2005 UTC

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
16typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ 20typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */
17typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ 21typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */
18typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ 22typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */
19 23
50} aio_cb; 54} aio_cb;
51 55
52typedef aio_cb *aio_req; 56typedef aio_cb *aio_req;
53 57
54static int started; 58static int started;
55static int nreqs; 59static volatile int nreqs;
56static int max_outstanding = 1<<30; 60static int max_outstanding = 1<<30;
57static int respipe [2]; 61static int respipe [2];
58 62
59static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; 63static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER;
60static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; 64static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER;
64static volatile aio_req ress, rese; /* queue start, queue end */ 68static volatile aio_req ress, rese; /* queue start, queue end */
65 69
66static void 70static void
67poll_wait () 71poll_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
79static int 83static int
80poll_cb () 84poll_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 static int rl;//D
91 //printf ("%d ENTER\n", ++rl);//D
92
93 pthread_mutex_lock (&reslock);
94
86 { 95 {
87 /* read and signals sent by the worker threads */ 96 /* read any signals sent by the worker threads */
88 char buf [32]; 97 char buf [32];
89 while (read (respipe [0], buf, 32) > 0) 98 while (read (respipe [0], buf, 32) > 0)
90 ; 99 ;
91 } 100 }
92 101
93 for (;;) 102 req = ress;
103 ress = rese = 0;
104
105 pthread_mutex_unlock (&reslock);
106
107 while (req)
94 { 108 {
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--; 109 nreqs--;
110 //printf ("%d count %d %p->%p\n", rl, count, req, req->next);//D
111 111
112 if (req->type == REQ_QUIT) 112 if (req->type == REQ_QUIT)
113 started--; 113 started--;
114 else 114 else
115 { 115 {
162 162
163 errno = errorno; 163 errno = errorno;
164 count++; 164 count++;
165 } 165 }
166 166
167 prv = req;
168 req = req->next;
167 Safefree (req); 169 Safefree (prv);
170
171 /* TODO: croak on errors? */
168 } 172 }
169 173
174 //printf ("%d LEAVE %p %p\n", rl--, ress, rese);//D
170 return count; 175 return count;
171} 176}
172 177
173static void *aio_proc(void *arg); 178static void *aio_proc(void *arg);
174 179
309 314
310 type = req->type; 315 type = req->type;
311 316
312 switch (type) 317 switch (type)
313 { 318 {
314 case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length, req->offset); break; 319 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; 320 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break;
316#if SYS_readahead 321#if SYS_readahead
317 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; 322 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break;
318#else 323#else
319 case REQ_READAHEAD: req->result = -1; errno = ENOSYS; break; 324 case REQ_READAHEAD: req->result = -1; errno = ENOSYS; break;
320#endif 325#endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines