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.251 by root, Sat Jan 6 01:04:42 2018 UTC vs.
Revision 1.264 by root, Tue Aug 14 11:44:52 2018 UTC

4 4
5#include "EXTERN.h" 5#include "EXTERN.h"
6#include "perl.h" 6#include "perl.h"
7#include "XSUB.h" 7#include "XSUB.h"
8 8
9#include "schmorp.h" 9#if !defined mg_findext
10# define mg_findext(sv,type,vtbl) mg_find (sv, type)
11#endif
10 12
11#include <stddef.h> 13#include <stddef.h>
12#include <stdlib.h> 14#include <stdlib.h>
13#include <errno.h> 15#include <errno.h>
14#include <sys/types.h> 16#include <sys/types.h>
17#include <sys/socket.h>
15#include <sys/stat.h> 18#include <sys/stat.h>
16#include <limits.h> 19#include <limits.h>
17#include <fcntl.h> 20#include <fcntl.h>
18#include <sched.h> 21#include <sched.h>
22
23#if HAVE_SYS_SYSMACROS_H
24# include <sys/sysmacros.h>
25#endif
19 26
20#if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES 27#if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES
21# include <sys/mman.h> 28# include <sys/mman.h>
22#endif 29#endif
23 30
104#define expect_false(expr) expect ((expr) != 0, 0) 111#define expect_false(expr) expect ((expr) != 0, 0)
105#define expect_true(expr) expect ((expr) != 0, 1) 112#define expect_true(expr) expect ((expr) != 0, 1)
106 113
107/*****************************************************************************/ 114/*****************************************************************************/
108 115
109#include "libeio/config.h" 116#include "config.h"
117
118#if HAVE_ST_XTIMENSEC
119# define ATIMENSEC PL_statcache.st_atimensec
120# define MTIMENSEC PL_statcache.st_mtimensec
121# define CTIMENSEC PL_statcache.st_ctimensec
122#elif HAVE_ST_XTIMESPEC
123# define ATIMENSEC PL_statcache.st_atim.tv_nsec
124# define MTIMENSEC PL_statcache.st_mtim.tv_nsec
125# define CTIMENSEC PL_statcache.st_ctim.tv_nsec
126#else
127# define ATIMENSEC 0
128# define MTIMENSEC 0
129# define CTIMENSEC 0
130#endif
131
132#include "schmorp.h"
133
134#if HAVE_EVENTFD
135# include <sys/eventfd.h>
136#endif
137
138#if HAVE_TIMERFD
139# include <sys/timerfd.h>
140#endif
110 141
111#if HAVE_RLIMITS 142#if HAVE_RLIMITS
112 #include <sys/time.h> 143 #include <sys/time.h>
113 #include <sys/resource.h> 144 #include <sys/resource.h>
114#endif 145#endif
151# else 182# else
152# define MAP_ANONYMOUS MAP_FIXED /* and hope this fails */ 183# define MAP_ANONYMOUS MAP_FIXED /* and hope this fails */
153# endif 184# endif
154#endif 185#endif
155 186
156/* defines all sorts of constants to 0 unless they are already defined */
157/* also provides const_iv_ and const_niv_ macros for them */
158#include "def0.h"
159
160#ifndef makedev 187#ifndef makedev
161# define makedev(maj,min) (((maj) << 8) | (min)) 188# define makedev(maj,min) (((maj) << 8) | (min))
162#endif 189#endif
163#ifndef major 190#ifndef major
164# define major(dev) ((dev) >> 8) 191# define major(dev) ((dev) >> 8)
172#endif 199#endif
173 200
174/*****************************************************************************/ 201/*****************************************************************************/
175 202
176#if !_POSIX_MAPPED_FILES 203#if !_POSIX_MAPPED_FILES
177# define mmap(addr,length,prot,flags,fd,offs) EIO_ENOSYS () 204# define mmap(addr,length,prot,flags,fd,offs) (errno = ENOSYS, (void *)-1)
178# define munmap(addr,length) EIO_ENOSYS () 205# define munmap(addr,length) EIO_ENOSYS ()
179#endif 206#endif
180 207
181#if !_POSIX_MEMORY_PROTECTION 208#if !_POSIX_MEMORY_PROTECTION
182# define mprotect(addr,len,prot) EIO_ENOSYS () 209# define mprotect(addr,len,prot) EIO_ENOSYS ()
210#endif
211
212#if !MREMAP_MAYMOVE
213# define mremap(old_address,old_size,new_size,flags,new_address) (errno = ENOSYS, (void *)-1)
183#endif 214#endif
184 215
185#define FOREIGN_MAGIC PERL_MAGIC_ext 216#define FOREIGN_MAGIC PERL_MAGIC_ext
186 217
187static int ecb_cold 218static int ecb_cold
259{ 290{
260 /* todo: iterate over magic and only free ours, but of course */ 291 /* todo: iterate over magic and only free ours, but of course */
261 /* the perl5porters will call that (correct) behaviour buggy */ 292 /* the perl5porters will call that (correct) behaviour buggy */
262 sv_unmagic (sv, FOREIGN_MAGIC); 293 sv_unmagic (sv, FOREIGN_MAGIC);
263} 294}
295
296/*****************************************************************************/
297
298/* defines all sorts of constants to 0 unless they are already defined */
299/* also provides const_iv_ and const_niv_ macros for them */
300#include "def0.h"
264 301
265/*****************************************************************************/ 302/*****************************************************************************/
266 303
267static void 304static void
268fiemap (eio_req *req) 305fiemap (eio_req *req)
699 { 736 {
700 req->flags &= ~EIO_FLAG_PTR2_FREE; 737 req->flags &= ~EIO_FLAG_PTR2_FREE;
701 sv_set_foreign (req->sv2, &sysfree_vtbl, req->ptr2, req->result); 738 sv_set_foreign (req->sv2, &sysfree_vtbl, req->ptr2, req->result);
702 } 739 }
703 else 740 else
741 {
704 SvCUR_set (req->sv2, req->result); 742 SvCUR_set (req->sv2, req->result);
743 *SvEND (req->sv2) = 0;
744 SvPOK_only (req->sv2);
745 }
746
747 SvSETMAGIC (req->sv2);
705 } 748 }
706 749
707 PUSHs (sv_result); 750 PUSHs (sv_result);
708 } 751 }
709 break; 752 break;
963 req_set_path1 (req, fh_or_path); 1006 req_set_path1 (req, fh_or_path);
964 break; 1007 break;
965 } 1008 }
966} 1009}
967 1010
1011/*****************************************************************************/
1012
1013static void
1014ts_set (struct timespec *ts, NV value)
1015{
1016 ts->tv_sec = value;
1017 ts->tv_nsec = (value - ts->tv_sec) * 1e9;
1018}
1019
1020static NV
1021ts_get (const struct timespec *ts)
1022{
1023 return ts->tv_sec + ts->tv_nsec * 1e-9;
1024}
1025
1026/*****************************************************************************/
1027
968XS(boot_IO__AIO) ecb_cold; 1028XS(boot_IO__AIO) ecb_cold;
969 1029
970MODULE = IO::AIO PACKAGE = IO::AIO 1030MODULE = IO::AIO PACKAGE = IO::AIO
971 1031
972PROTOTYPES: ENABLE 1032PROTOTYPES: ENABLE
1003 const_niv (MADV_RANDOM , POSIX_MADV_RANDOM) 1063 const_niv (MADV_RANDOM , POSIX_MADV_RANDOM)
1004 const_niv (MADV_WILLNEED , POSIX_MADV_WILLNEED) 1064 const_niv (MADV_WILLNEED , POSIX_MADV_WILLNEED)
1005 const_niv (MADV_DONTNEED , POSIX_MADV_DONTNEED) 1065 const_niv (MADV_DONTNEED , POSIX_MADV_DONTNEED)
1006 1066
1007 /* the second block will be 0 when missing */ 1067 /* the second block will be 0 when missing */
1068 const_iv (O_ACCMODE)
1069
1008 const_iv (O_RDONLY) 1070 const_iv (O_RDONLY)
1009 const_iv (O_WRONLY) 1071 const_iv (O_WRONLY)
1010 const_iv (O_RDWR) 1072 const_iv (O_RDWR)
1011 const_iv (O_CREAT) 1073 const_iv (O_CREAT)
1012 const_iv (O_TRUNC) 1074 const_iv (O_TRUNC)
1072 const_iv (MAP_GROWSDOWN) 1134 const_iv (MAP_GROWSDOWN)
1073 const_iv (MAP_32BIT) 1135 const_iv (MAP_32BIT)
1074 const_iv (MAP_HUGETLB) 1136 const_iv (MAP_HUGETLB)
1075 const_iv (MAP_STACK) 1137 const_iv (MAP_STACK)
1076 1138
1139 const_iv (MREMAP_MAYMOVE)
1140 const_iv (MREMAP_FIXED)
1141
1077 const_iv (F_DUPFD_CLOEXEC) 1142 const_iv (F_DUPFD_CLOEXEC)
1143
1144 const_iv (MSG_CMSG_CLOEXEC)
1145 const_iv (SOCK_CLOEXEC)
1078 1146
1079 const_iv (F_OFD_GETLK) 1147 const_iv (F_OFD_GETLK)
1080 const_iv (F_OFD_SETLK) 1148 const_iv (F_OFD_SETLK)
1081 const_iv (F_OFD_GETLKW) 1149 const_iv (F_OFD_GETLKW)
1082 1150
1155 const_iv (SPLICE_F_MOVE) 1223 const_iv (SPLICE_F_MOVE)
1156 const_iv (SPLICE_F_NONBLOCK) 1224 const_iv (SPLICE_F_NONBLOCK)
1157 const_iv (SPLICE_F_MORE) 1225 const_iv (SPLICE_F_MORE)
1158 const_iv (SPLICE_F_GIFT) 1226 const_iv (SPLICE_F_GIFT)
1159 1227
1228 const_iv (EFD_CLOEXEC)
1229 const_iv (EFD_NONBLOCK)
1230 const_iv (EFD_SEMAPHORE)
1231
1232 const_iv (CLOCK_REALTIME)
1233 const_iv (CLOCK_MONOTONIC)
1234 const_iv (CLOCK_BOOTTIME)
1235 const_iv (CLOCK_REALTIME_ALARM)
1236 const_iv (CLOCK_BOOTTIME_ALARM)
1237
1238 const_iv (TFD_NONBLOCK)
1239 const_iv (TFD_CLOEXEC)
1240
1241 const_iv (TFD_TIMER_ABSTIME)
1242 const_iv (TFD_TIMER_CANCEL_ON_SET)
1243
1160 /* these are libeio constants, and are independent of gendef0 */ 1244 /* these are libeio constants, and are independent of gendef0 */
1161 const_eio (SEEK_SET) 1245 const_eio (SEEK_SET)
1162 const_eio (SEEK_CUR) 1246 const_eio (SEEK_CUR)
1163 const_eio (SEEK_END) 1247 const_eio (SEEK_END)
1164 1248
1415 } 1499 }
1416 else 1500 else
1417 { 1501 {
1418 /* read: check type and grow scalar as necessary */ 1502 /* read: check type and grow scalar as necessary */
1419 if (!SvPOK (data) || SvLEN (data) >= SvCUR (data)) 1503 if (!SvPOK (data) || SvLEN (data) >= SvCUR (data))
1420 svptr = sv_grow (data, len + dataoffset); 1504 svptr = sv_grow (data, len + dataoffset + 1);
1421 else if (SvCUR (data) < len + dataoffset) 1505 else if (SvCUR (data) < len + dataoffset)
1422 croak ("length + dataoffset outside of scalar, and cannot grow"); 1506 croak ("length + dataoffset outside of scalar, and cannot grow");
1423 } 1507 }
1424 1508
1425 { 1509 {
1546 1630
1547 req_set_fh_or_path (req, ix, ix == EIO_STATVFS ? EIO_FSTATVFS : EIO_FSTAT, fh_or_path); 1631 req_set_fh_or_path (req, ix, ix == EIO_STATVFS ? EIO_FSTATVFS : EIO_FSTAT, fh_or_path);
1548 1632
1549 REQ_SEND; 1633 REQ_SEND;
1550} 1634}
1635
1636void
1637st_xtime ()
1638 ALIAS:
1639 st_atime = 1
1640 st_mtime = 2
1641 st_ctime = 4
1642 st_xtime = 7
1643 PPCODE:
1644 EXTEND (SP, 3);
1645 if (ix & 1) PUSHs (newSVnv (PL_statcache.st_atime + 1e-9 * ATIMENSEC));
1646 if (ix & 2) PUSHs (newSVnv (PL_statcache.st_mtime + 1e-9 * MTIMENSEC));
1647 if (ix & 4) PUSHs (newSVnv (PL_statcache.st_ctime + 1e-9 * CTIMENSEC));
1648
1649void
1650st_xtimensec ()
1651 ALIAS:
1652 st_atimensec = 1
1653 st_mtimensec = 2
1654 st_ctimensec = 4
1655 st_xtimensec = 7
1656 PPCODE:
1657 EXTEND (SP, 3);
1658 if (ix & 1) PUSHs (newSViv (ATIMENSEC));
1659 if (ix & 2) PUSHs (newSViv (MTIMENSEC));
1660 if (ix & 4) PUSHs (newSViv (CTIMENSEC));
1551 1661
1552UV 1662UV
1553major (UV dev) 1663major (UV dev)
1554 ALIAS: 1664 ALIAS:
1555 minor = 1 1665 minor = 1
1819 sv_clear_foreign (data); 1929 sv_clear_foreign (data);
1820 1930
1821 if (length) /* known length, directly read into scalar */ 1931 if (length) /* known length, directly read into scalar */
1822 { 1932 {
1823 if (!SvPOK (data) || SvLEN (data) >= SvCUR (data)) 1933 if (!SvPOK (data) || SvLEN (data) >= SvCUR (data))
1824 svptr = sv_grow (data, length); 1934 svptr = sv_grow (data, length + 1);
1825 else if (SvCUR (data) < length) 1935 else if (SvCUR (data) < length)
1826 croak ("length outside of scalar, and cannot grow"); 1936 croak ("length outside of scalar, and cannot grow");
1827 else 1937 else
1828 svptr = SvPVbyte_nolen (data); 1938 svptr = SvPVbyte_nolen (data);
1829 } 1939 }
2010void 2120void
2011munmap (SV *scalar) 2121munmap (SV *scalar)
2012 CODE: 2122 CODE:
2013 sv_clear_foreign (scalar); 2123 sv_clear_foreign (scalar);
2014 2124
2125SV *
2126mremap (SV *scalar, STRLEN new_length, int flags = MREMAP_MAYMOVE, IV new_address = 0)
2127 CODE:
2128{
2129 MAGIC *mg = mg_findext (scalar, FOREIGN_MAGIC, &mmap_vtbl);
2130 void *new;
2131
2132 if (!mg || SvPVX (scalar) != mg->mg_ptr)
2133 croak ("IO::AIO::mremap: scalar not mapped by IO::AIO::mmap or improperly modified");
2134
2135 new = mremap (mg->mg_ptr, (size_t)mg->mg_obj, new_length, flags, (void *)new_address);
2136
2137 RETVAL = &PL_sv_no;
2138
2139 if (new != (void *)-1)
2140 {
2141 RETVAL = new == (void *)mg->mg_ptr
2142 ? newSVpvn ("0 but true", 10)
2143 : &PL_sv_yes;
2144
2145 mg->mg_ptr = (char *)new;
2146 mg->mg_obj = (SV *)new_length;
2147
2148 SvPVX (scalar) = mg->mg_ptr;
2149 SvCUR_set (scalar, new_length);
2150 }
2151}
2152 OUTPUT:
2153 RETVAL
2154
2015int 2155int
2016madvise (SV *scalar, STRLEN offset = 0, SV *length = &PL_sv_undef, IV advice_or_prot) 2156madvise (SV *scalar, STRLEN offset = 0, SV *length = &PL_sv_undef, IV advice_or_prot)
2017 ALIAS: 2157 ALIAS:
2018 mprotect = 1 2158 mprotect = 1
2019 CODE: 2159 CODE:
2148 { 2288 {
2149 EXTEND (SP, 2); 2289 EXTEND (SP, 2);
2150 PUSHs (newmortalFH (fd[0], O_RDONLY)); 2290 PUSHs (newmortalFH (fd[0], O_RDONLY));
2151 PUSHs (newmortalFH (fd[1], O_WRONLY)); 2291 PUSHs (newmortalFH (fd[1], O_WRONLY));
2152 } 2292 }
2293}
2294
2295void
2296eventfd (unsigned int initval = 0, int flags = 0)
2297 PPCODE:
2298{
2299 int fd;
2300#if HAVE_EVENTFD
2301 fd = eventfd (initval, flags);
2302#else
2303 fd = (errno = ENOSYS, -1);
2304#endif
2305
2306 XPUSHs (newmortalFH (fd, O_RDWR));
2307}
2308
2309void
2310timerfd_create (int clockid, int flags = 0)
2311 PPCODE:
2312{
2313 int fd;
2314#if HAVE_TIMERFD
2315 fd = timerfd_create (clockid, flags);
2316#else
2317 fd = (errno = ENOSYS, -1);
2318#endif
2319
2320 XPUSHs (newmortalFH (fd, O_RDWR));
2321}
2322
2323void
2324timerfd_settime (SV *fh, int flags, NV interval, NV value)
2325 PPCODE:
2326{
2327 int fd = s_fileno_croak (fh, 0);
2328#if HAVE_TIMERFD
2329 int res;
2330 struct itimerspec its, ots;
2331
2332 ts_set (&its.it_interval, interval);
2333 ts_set (&its.it_value , value);
2334 res = timerfd_settime (fd, flags, &its, &ots);
2335
2336 if (!res)
2337 {
2338 EXTEND (SP, 2);
2339 PUSHs (newSVnv (ts_get (&ots.it_interval)));
2340 PUSHs (newSVnv (ts_get (&ots.it_value)));
2341 }
2342#else
2343 errno = ENOSYS;
2344#endif
2345}
2346
2347void
2348timerfd_gettime (SV *fh)
2349 PPCODE:
2350{
2351 int fd = s_fileno_croak (fh, 0);
2352#if HAVE_TIMERFD
2353 int res;
2354 struct itimerspec ots;
2355 res = timerfd_gettime (fd, &ots);
2356
2357 if (!res)
2358 {
2359 EXTEND (SP, 2);
2360 PUSHs (newSVnv (ts_get (&ots.it_interval)));
2361 PUSHs (newSVnv (ts_get (&ots.it_value)));
2362 }
2363#else
2364 errno = ENOSYS;
2365#endif
2153} 2366}
2154 2367
2155UV 2368UV
2156get_fdlimit () 2369get_fdlimit ()
2157 CODE: 2370 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines