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.72 by root, Tue Oct 24 17:22:17 2006 UTC vs.
Revision 1.76 by root, Thu Oct 26 12:38:04 2006 UTC

1/* solaris */ 1/* solaris */
2#define _POSIX_PTHREAD_SEMANTICS 1 2#define _POSIX_PTHREAD_SEMANTICS 1
3 3
4#if __linux 4#if __linux && !defined(_GNU_SOURCE)
5# define _GNU_SOURCE 5# define _GNU_SOURCE
6#endif 6#endif
7 7
8/* just in case */
8#define _REENTRANT 1 9#define _REENTRANT 1
9 10
10#include <errno.h> 11#include <errno.h>
11 12
12#include "EXTERN.h" 13#include "EXTERN.h"
47/* used for struct dirent, AIX doesn't provide it */ 48/* used for struct dirent, AIX doesn't provide it */
48#ifndef NAME_MAX 49#ifndef NAME_MAX
49# define NAME_MAX 4096 50# define NAME_MAX 4096
50#endif 51#endif
51 52
53#ifndef PTHREAD_STACK_MIN
54/* care for broken platforms, e.g. windows */
55# define PTHREAD_STACK_MIN 16384
56#endif
57
52#if __ia64 58#if __ia64
53# define STACKSIZE 65536 59# define STACKSIZE 65536
54#elif __i386 || __x86_64 /* 16k is unreasonably high :( */ 60#elif __i386 || __x86_64 /* 16k is unreasonably high :( */
55# define STACKSIZE PTHREAD_STACK_MIN 61# define STACKSIZE PTHREAD_STACK_MIN
56#else 62#else
128 134
129static int next_pri = DEFAULT_PRI + PRI_BIAS; 135static int next_pri = DEFAULT_PRI + PRI_BIAS;
130 136
131static int started, wanted; 137static int started, wanted;
132static volatile int nreqs; 138static volatile int nreqs;
133static int max_outstanding = 1<<30;
134static int respipe [2]; 139static int respipe [2];
135 140
136#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) 141#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP)
137# define AIO_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP 142# define AIO_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
138#else 143#else
140#endif 145#endif
141 146
142#define LOCK(mutex) pthread_mutex_lock (&(mutex)) 147#define LOCK(mutex) pthread_mutex_lock (&(mutex))
143#define UNLOCK(mutex) pthread_mutex_unlock (&(mutex)) 148#define UNLOCK(mutex) pthread_mutex_unlock (&(mutex))
144 149
145/* worker threasd management */ 150/* worker threads management */
146static pthread_mutex_t wrklock = AIO_MUTEX_INIT; 151static pthread_mutex_t wrklock = AIO_MUTEX_INIT;
147 152
148typedef struct worker { 153typedef struct worker {
149 /* locked by wrklock */ 154 /* locked by wrklock */
150 struct worker *prev, *next; 155 struct worker *prev, *next;
238 } 243 }
239 244
240 abort (); 245 abort ();
241} 246}
242 247
248static int poll_cb (int max);
243static void req_invoke (aio_req req); 249static void req_invoke (aio_req req);
244static void req_free (aio_req req); 250static void req_free (aio_req req);
245static void req_cancel (aio_req req); 251static void req_cancel (aio_req req);
246 252
247/* must be called at most once */ 253/* must be called at most once */
490 req->flags |= FLAG_CANCELLED; 496 req->flags |= FLAG_CANCELLED;
491 497
492 req_cancel_subs (req); 498 req_cancel_subs (req);
493} 499}
494 500
495static int poll_cb () 501static int poll_cb (int max)
496{ 502{
497 dSP; 503 dSP;
498 int count = 0; 504 int count = 0;
499 int do_croak = 0; 505 int do_croak = 0;
500 aio_req req; 506 aio_req req;
501 507
502 for (;;) 508 while (max <= 0 || count < max)
503 { 509 {
504 LOCK (reslock); 510 LOCK (reslock);
505 req = reqq_shift (&res_queue); 511 req = reqq_shift (&res_queue);
506 512
507 if (req) 513 if (req)
557 563
558static void *aio_proc(void *arg); 564static void *aio_proc(void *arg);
559 565
560static void start_thread (void) 566static void start_thread (void)
561{ 567{
568 sigset_t fullsigset, oldsigset;
569 pthread_attr_t attr;
570
562 worker *wrk = calloc (1, sizeof (worker)); 571 worker *wrk = calloc (1, sizeof (worker));
563 572
564 if (!wrk) 573 if (!wrk)
565 croak ("unable to allocate worker thread data"); 574 croak ("unable to allocate worker thread data");
566
567 sigset_t fullsigset, oldsigset;
568 pthread_attr_t attr;
569 575
570 pthread_attr_init (&attr); 576 pthread_attr_init (&attr);
571 pthread_attr_setstacksize (&attr, STACKSIZE); 577 pthread_attr_setstacksize (&attr, STACKSIZE);
572 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); 578 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
573 579
600 606
601 LOCK (reqlock); 607 LOCK (reqlock);
602 reqq_push (&req_queue, req); 608 reqq_push (&req_queue, req);
603 pthread_cond_signal (&reqwait); 609 pthread_cond_signal (&reqwait);
604 UNLOCK (reqlock); 610 UNLOCK (reqlock);
605
606 if (nreqs > max_outstanding)
607 for (;;)
608 {
609 poll_cb ();
610
611 if (nreqs <= max_outstanding)
612 break;
613
614 poll_wait ();
615 }
616} 611}
617 612
618static void end_thread (void) 613static void end_thread (void)
619{ 614{
620 aio_req req; 615 aio_req req;
647 } 642 }
648 643
649 while (started > wanted) 644 while (started > wanted)
650 { 645 {
651 poll_wait (); 646 poll_wait ();
652 poll_cb (); 647 poll_cb (0);
653 } 648 }
654} 649}
655 650
656static void create_pipe () 651static void create_pipe ()
657{ 652{
713#if !HAVE_FDATASYNC 708#if !HAVE_FDATASYNC
714# define fdatasync fsync 709# define fdatasync fsync
715#endif 710#endif
716 711
717#if !HAVE_READAHEAD 712#if !HAVE_READAHEAD
718# define readahead aio_readahead 713# define readahead(fd,offset,count) aio_readahead (fd, offset, count, self)
719 714
720static ssize_t readahead (int fd, off_t offset, size_t count) 715static ssize_t aio_readahead (int fd, off_t offset, size_t count, worker *self)
721{ 716{
722 dBUF; 717 dBUF;
723 718
724 while (count > 0) 719 while (count > 0)
725 { 720 {
730 count -= len; 725 count -= len;
731 } 726 }
732 727
733 errno = 0; 728 errno = 0;
734} 729}
730
735#endif 731#endif
736 732
737#if !HAVE_READDIR_R 733#if !HAVE_READDIR_R
738# define readdir_r aio_readdir_r 734# define readdir_r aio_readdir_r
739 735
874 int errorno; 870 int errorno;
875 871
876 LOCK (wrklock); 872 LOCK (wrklock);
877 self->dirp = dirp = opendir (req->dataptr); 873 self->dirp = dirp = opendir (req->dataptr);
878 self->dbuf = u = malloc (sizeof (*u)); 874 self->dbuf = u = malloc (sizeof (*u));
875 req->data2ptr = names = malloc (memlen);
879 UNLOCK (wrklock); 876 UNLOCK (wrklock);
880
881 req->data2ptr = names = malloc (memlen);
882 877
883 if (dirp && u && names) 878 if (dirp && u && names)
884 for (;;) 879 for (;;)
885 { 880 {
886 errno = 0; 881 errno = 0;
1116void 1111void
1117max_parallel (nthreads) 1112max_parallel (nthreads)
1118 int nthreads 1113 int nthreads
1119 PROTOTYPE: $ 1114 PROTOTYPE: $
1120 1115
1121int
1122max_outstanding (nreqs)
1123 int nreqs
1124 PROTOTYPE: $
1125 CODE:
1126 RETVAL = max_outstanding;
1127 max_outstanding = nreqs;
1128
1129void 1116void
1130aio_open (pathname,flags,mode,callback=&PL_sv_undef) 1117aio_open (pathname,flags,mode,callback=&PL_sv_undef)
1131 SV * pathname 1118 SV * pathname
1132 int flags 1119 int flags
1133 int mode 1120 int mode
1409 PROTOTYPE: 1396 PROTOTYPE:
1410 CODE: 1397 CODE:
1411 while (nreqs) 1398 while (nreqs)
1412 { 1399 {
1413 poll_wait (); 1400 poll_wait ();
1414 poll_cb (); 1401 poll_cb (0);
1415 } 1402 }
1416 1403
1417void 1404void
1418poll() 1405poll()
1419 PROTOTYPE: 1406 PROTOTYPE:
1420 CODE: 1407 CODE:
1421 if (nreqs) 1408 if (nreqs)
1422 { 1409 {
1423 poll_wait (); 1410 poll_wait ();
1424 poll_cb (); 1411 poll_cb (0);
1425 } 1412 }
1426 1413
1427int 1414int
1428poll_fileno() 1415poll_fileno()
1429 PROTOTYPE: 1416 PROTOTYPE:
1434 1421
1435int 1422int
1436poll_cb(...) 1423poll_cb(...)
1437 PROTOTYPE: 1424 PROTOTYPE:
1438 CODE: 1425 CODE:
1439 RETVAL = poll_cb (); 1426 RETVAL = poll_cb (0);
1427 OUTPUT:
1428 RETVAL
1429
1430int
1431poll_some(int max = 0)
1432 PROTOTYPE: $
1433 CODE:
1434 RETVAL = poll_cb (max);
1440 OUTPUT: 1435 OUTPUT:
1441 RETVAL 1436 RETVAL
1442 1437
1443void 1438void
1444poll_wait() 1439poll_wait()

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines