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.97 by root, Sun Jan 7 22:59:57 2007 UTC vs.
Revision 1.98 by root, Wed May 9 06:45:12 2007 UTC

1/* solaris */ 1#include "xthread.h"
2#define _POSIX_PTHREAD_SEMANTICS 1
3
4#if __linux && !defined(_GNU_SOURCE)
5# define _GNU_SOURCE
6#endif
7
8/* just in case */
9#define _REENTRANT 1
10 2
11#include <errno.h> 3#include <errno.h>
12 4
13#include "EXTERN.h" 5#include "EXTERN.h"
14#include "perl.h" 6#include "perl.h"
15#include "XSUB.h" 7#include "XSUB.h"
16 8
17#include "autoconf/config.h" 9#include "autoconf/config.h"
18
19#include <pthread.h>
20 10
21#include <stddef.h> 11#include <stddef.h>
22#include <stdlib.h> 12#include <stdlib.h>
23#include <errno.h> 13#include <errno.h>
24#include <sys/time.h> 14#include <sys/time.h>
52/* used for struct dirent, AIX doesn't provide it */ 42/* used for struct dirent, AIX doesn't provide it */
53#ifndef NAME_MAX 43#ifndef NAME_MAX
54# define NAME_MAX 4096 44# define NAME_MAX 4096
55#endif 45#endif
56 46
57#ifndef PTHREAD_STACK_MIN
58/* care for broken platforms, e.g. windows */
59# define PTHREAD_STACK_MIN 16384
60#endif
61
62#if __ia64
63# define STACKSIZE 65536
64#elif __i386 || __x86_64 /* 16k is unreasonably high :( */
65# define STACKSIZE PTHREAD_STACK_MIN
66#else
67# define STACKSIZE 16384
68#endif
69
70/* wether word reads are potentially non-atomic.
71 * this is conservatice, likely most arches this runs
72 * on have atomic word read/writes.
73 */
74#ifndef WORDACCESS_UNSAFE
75# if __i386 || __x86_64
76# define WORDACCESS_UNSAFE 0
77# else
78# define WORDACCESS_UNSAFE 1
79# endif
80#endif
81
82/* buffer size for various temporary buffers */ 47/* buffer size for various temporary buffers */
83#define AIO_BUFSIZE 65536 48#define AIO_BUFSIZE 65536
84 49
85#define dBUF \ 50#define dBUF \
86 char *aio_buf; \ 51 char *aio_buf; \
161{ 126{
162 return (tv2->tv_sec - tv1->tv_sec ) * AIO_TICKS 127 return (tv2->tv_sec - tv1->tv_sec ) * AIO_TICKS
163 + ((tv2->tv_usec - tv1->tv_usec) >> 10); 128 + ((tv2->tv_usec - tv1->tv_usec) >> 10);
164} 129}
165 130
166static pthread_t main_tid; 131static thread_t main_tid;
167static int main_sig; 132static int main_sig;
168static int block_sig_level; 133static int block_sig_level;
169 134
170void block_sig () 135void block_sig ()
171{ 136{
199 164
200static int next_pri = DEFAULT_PRI + PRI_BIAS; 165static int next_pri = DEFAULT_PRI + PRI_BIAS;
201 166
202static unsigned int started, idle, wanted; 167static unsigned int started, idle, wanted;
203 168
204#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP)
205# define AIO_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
206#else
207# define AIO_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
208#endif
209
210#define LOCK(mutex) pthread_mutex_lock (&(mutex))
211#define UNLOCK(mutex) pthread_mutex_unlock (&(mutex))
212
213/* worker threads management */ 169/* worker threads management */
214static pthread_mutex_t wrklock = AIO_MUTEX_INIT; 170static mutex_t wrklock = MUTEX_INIT;
215 171
216typedef struct worker { 172typedef struct worker {
217 /* locked by wrklock */ 173 /* locked by wrklock */
218 struct worker *prev, *next; 174 struct worker *prev, *next;
219 175
220 pthread_t tid; 176 thread_t tid;
221 177
222 /* locked by reslock, reqlock or wrklock */ 178 /* locked by reslock, reqlock or wrklock */
223 aio_req req; /* currently processed request */ 179 aio_req req; /* currently processed request */
224 void *dbuf; 180 void *dbuf;
225 DIR *dirp; 181 DIR *dirp;
253static volatile unsigned int nreqs, nready, npending; 209static volatile unsigned int nreqs, nready, npending;
254static volatile unsigned int max_idle = 4; 210static volatile unsigned int max_idle = 4;
255static volatile unsigned int max_outstanding = 0xffffffff; 211static volatile unsigned int max_outstanding = 0xffffffff;
256static int respipe [2]; 212static int respipe [2];
257 213
258static pthread_mutex_t reslock = AIO_MUTEX_INIT; 214static mutex_t reslock = MUTEX_INIT;
259static pthread_mutex_t reqlock = AIO_MUTEX_INIT; 215static mutex_t reqlock = MUTEX_INIT;
260static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; 216static cond_t reqwait = COND_INIT;
261 217
262#if WORDACCESS_UNSAFE 218#if WORDACCESS_UNSAFE
263 219
264static unsigned int get_nready () 220static unsigned int get_nready ()
265{ 221{
619 575
620static void *aio_proc(void *arg); 576static void *aio_proc(void *arg);
621 577
622static void start_thread (void) 578static void start_thread (void)
623{ 579{
624 sigset_t fullsigset, oldsigset;
625 pthread_attr_t attr;
626
627 worker *wrk = calloc (1, sizeof (worker)); 580 worker *wrk = calloc (1, sizeof (worker));
628 581
629 if (!wrk) 582 if (!wrk)
630 croak ("unable to allocate worker thread data"); 583 croak ("unable to allocate worker thread data");
631 584
632 pthread_attr_init (&attr);
633 pthread_attr_setstacksize (&attr, STACKSIZE);
634 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
635#ifdef PTHREAD_SCOPE_PROCESS
636 pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS);
637#endif
638
639 sigfillset (&fullsigset);
640
641 LOCK (wrklock); 585 LOCK (wrklock);
642 pthread_sigmask (SIG_SETMASK, &fullsigset, &oldsigset);
643 586
644 if (pthread_create (&wrk->tid, &attr, aio_proc, (void *)wrk) == 0) 587 if (thread_create (&wrk->tid, aio_proc, (void *)wrk))
645 { 588 {
646 wrk->prev = &wrk_first; 589 wrk->prev = &wrk_first;
647 wrk->next = wrk_first.next; 590 wrk->next = wrk_first.next;
648 wrk_first.next->prev = wrk; 591 wrk_first.next->prev = wrk;
649 wrk_first.next = wrk; 592 wrk_first.next = wrk;
650 ++started; 593 ++started;
651 } 594 }
652 else 595 else
653 free (wrk); 596 free (wrk);
654 597
655 pthread_sigmask (SIG_SETMASK, &oldsigset, 0);
656 UNLOCK (wrklock); 598 UNLOCK (wrklock);
657} 599}
658 600
659static void maybe_start_thread () 601static void maybe_start_thread ()
660{ 602{
675 ++nreqs; 617 ++nreqs;
676 618
677 LOCK (reqlock); 619 LOCK (reqlock);
678 ++nready; 620 ++nready;
679 reqq_push (&req_queue, req); 621 reqq_push (&req_queue, req);
680 pthread_cond_signal (&reqwait); 622 COND_SIGNAL (reqwait);
681 UNLOCK (reqlock); 623 UNLOCK (reqlock);
682 624
683 unblock_sig (); 625 unblock_sig ();
684 626
685 maybe_start_thread (); 627 maybe_start_thread ();
694 req->type = REQ_QUIT; 636 req->type = REQ_QUIT;
695 req->pri = PRI_MAX + PRI_BIAS; 637 req->pri = PRI_MAX + PRI_BIAS;
696 638
697 LOCK (reqlock); 639 LOCK (reqlock);
698 reqq_push (&req_queue, req); 640 reqq_push (&req_queue, req);
699 pthread_cond_signal (&reqwait); 641 COND_SIGNAL (reqwait);
700 UNLOCK (reqlock); 642 UNLOCK (reqlock);
701 643
702 LOCK (wrklock); 644 LOCK (wrklock);
703 --started; 645 --started;
704 UNLOCK (wrklock); 646 UNLOCK (wrklock);
857/* 799/*
858 * make our pread/pwrite safe against themselves, but not against 800 * make our pread/pwrite safe against themselves, but not against
859 * normal read/write by using a mutex. slows down execution a lot, 801 * normal read/write by using a mutex. slows down execution a lot,
860 * but that's your problem, not mine. 802 * but that's your problem, not mine.
861 */ 803 */
862static pthread_mutex_t preadwritelock = PTHREAD_MUTEX_INITIALIZER; 804static mutex_t preadwritelock = MUTEX_INIT;
863 805
864static ssize_t pread (int fd, void *buf, size_t count, off_t offset) 806static ssize_t pread (int fd, void *buf, size_t count, off_t offset)
865{ 807{
866 ssize_t res; 808 ssize_t res;
867 off_t ooffset; 809 off_t ooffset;
918#endif 860#endif
919 861
920#if !HAVE_READDIR_R 862#if !HAVE_READDIR_R
921# define readdir_r aio_readdir_r 863# define readdir_r aio_readdir_r
922 864
923static pthread_mutex_t readdirlock = PTHREAD_MUTEX_INITIALIZER; 865static mutex_t readdirlock = MUTEX_INIT;
924 866
925static int readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) 867static int readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res)
926{ 868{
927 struct dirent *e; 869 struct dirent *e;
928 int errorno; 870 int errorno;
1127 if (req) 1069 if (req)
1128 break; 1070 break;
1129 1071
1130 ++idle; 1072 ++idle;
1131 1073
1132 if (pthread_cond_timedwait (&reqwait, &reqlock, &ts) 1074 if (COND_TIMEDWAIT (reqwait, reqlock, ts)
1133 == ETIMEDOUT) 1075 == ETIMEDOUT)
1134 { 1076 {
1135 if (idle > max_idle) 1077 if (idle > max_idle)
1136 { 1078 {
1137 --idle; 1079 --idle;
1141 UNLOCK (wrklock); 1083 UNLOCK (wrklock);
1142 goto quit; 1084 goto quit;
1143 } 1085 }
1144 1086
1145 /* we are allowed to idle, so do so without any timeout */ 1087 /* we are allowed to idle, so do so without any timeout */
1146 pthread_cond_wait (&reqwait, &reqlock); 1088 COND_WAIT (reqwait, reqlock);
1147 ts.tv_sec = time (0) + IDLE_TIMEOUT; 1089 ts.tv_sec = time (0) + IDLE_TIMEOUT;
1148 } 1090 }
1149 1091
1150 --idle; 1092 --idle;
1151 } 1093 }
1334 newCONSTSUB (stash, "O_TRUNC", newSViv (O_TRUNC)); 1276 newCONSTSUB (stash, "O_TRUNC", newSViv (O_TRUNC));
1335 newCONSTSUB (stash, "S_IFIFO", newSViv (S_IFIFO)); 1277 newCONSTSUB (stash, "S_IFIFO", newSViv (S_IFIFO));
1336 newCONSTSUB (stash, "SIGIO", newSViv (SIGIO)); 1278 newCONSTSUB (stash, "SIGIO", newSViv (SIGIO));
1337 1279
1338 create_pipe (); 1280 create_pipe ();
1339 pthread_atfork (atfork_prepare, atfork_parent, atfork_child); 1281 ATFORK (atfork_prepare, atfork_parent, atfork_child);
1340} 1282}
1341 1283
1342void 1284void
1343max_poll_reqs (int nreqs) 1285max_poll_reqs (int nreqs)
1344 PROTOTYPE: $ 1286 PROTOTYPE: $

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines