Skip to content

Commit ad7c24e

Browse files
committed
IoUring: implement pipe and pipe_direct operations
Add dedicated Pipe2 flags for pipe2 as O contains some fields which are invalid in that context Signed-off-by: Bernard Assan <mega.alpha100@gmail.com>
1 parent 18c2d02 commit ad7c24e

2 files changed

Lines changed: 95 additions & 146 deletions

File tree

lib/std/os/linux.zig

Lines changed: 63 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -496,159 +496,88 @@ pub const O = switch (native_arch) {
496496
else => @compileError("missing std.os.linux.O constants for this architecture"),
497497
};
498498

499+
/// flags for `pipe2` and `IoUring.pipe`
499500
pub const Pipe2 = switch (native_arch) {
500501
.x86_64, .x86, .riscv32, .riscv64, .loongarch64 => packed struct(u32) {
501502
_: u7 = 0,
502-
EXCL: bool = false, //
503+
notification_pipe: bool = false,
503504
_9: u3 = 0,
504-
NONBLOCK: bool = false, //
505+
nonblock: bool = false,
505506
_13: u2 = 0,
506-
DIRECT: bool = false, //
507+
direct: bool = false,
507508
_16: u4 = 0,
508-
CLOEXEC: bool = false, //
509+
cloexec: bool = false,
509510
_21: u12 = 0,
510511
},
511512
.aarch64, .aarch64_be, .arm, .armeb, .thumb, .thumbeb => packed struct(u32) {
512-
ACCMODE: ACCMODE = .RDONLY,
513-
_2: u4 = 0,
514-
CREAT: bool = false,
515-
EXCL: bool = false, //
516-
NOCTTY: bool = false,
517-
TRUNC: bool = false,
518-
APPEND: bool = false,
519-
NONBLOCK: bool = false, //
520-
DSYNC: bool = false,
521-
ASYNC: bool = false,
522-
DIRECTORY: bool = false,
523-
NOFOLLOW: bool = false,
524-
DIRECT: bool = false, //
525-
LARGEFILE: bool = false,
526-
NOATIME: bool = false,
527-
CLOEXEC: bool = false, //
528-
SYNC: bool = false,
529-
PATH: bool = false,
530-
TMPFILE: bool = false,
531-
_23: u9 = 0,
513+
_: u7 = 0,
514+
notification_pipe: bool = false,
515+
_9: u3 = 0,
516+
nonblock: bool = false,
517+
_13: u4 = 0,
518+
direct: bool = false,
519+
_18: u2 = 0,
520+
cloexec: bool = false,
521+
_21: u12 = 0,
532522
},
533523
.sparc64 => packed struct(u32) {
534-
ACCMODE: ACCMODE = .RDONLY,
535-
_2: u1 = 0,
536-
APPEND: bool = false,
537-
_4: u2 = 0,
538-
ASYNC: bool = false,
539-
_7: u2 = 0,
540-
CREAT: bool = false,
541-
TRUNC: bool = false,
542-
EXCL: bool = false, //
543-
_12: u1 = 0,
544-
DSYNC: bool = false,
545-
NONBLOCK: bool = false, //
546-
NOCTTY: bool = false,
547-
DIRECTORY: bool = false,
548-
NOFOLLOW: bool = false,
549-
_18: u2 = 0,
550-
DIRECT: bool = false, //
551-
NOATIME: bool = false,
552-
CLOEXEC: bool = false, //
553-
SYNC: bool = false,
554-
PATH: bool = false,
555-
TMPFILE: bool = false,
556-
_27: u6 = 0,
524+
_: u11 = 0,
525+
notification_pipe: bool = false,
526+
_13: u2 = 0,
527+
nonblock: bool = false,
528+
_16: u5 = 0,
529+
direct: bool = false,
530+
_22: u1 = 0,
531+
cloexec: bool = false,
532+
_24: u9 = 0,
557533
},
558534
.mips, .mipsel, .mips64, .mips64el => packed struct(u32) {
559-
ACCMODE: ACCMODE = .RDONLY,
560-
_2: u1 = 0,
561-
APPEND: bool = false,
562-
DSYNC: bool = false,
563-
_5: u2 = 0,
564-
NONBLOCK: bool = false, //
565-
CREAT: bool = false,
566-
TRUNC: bool = false,
567-
EXCL: bool = false, //
568-
NOCTTY: bool = false,
569-
ASYNC: bool = false,
570-
LARGEFILE: bool = false,
571-
SYNC: bool = false,
572-
DIRECT: bool = false, //
573-
DIRECTORY: bool = false,
574-
NOFOLLOW: bool = false,
575-
NOATIME: bool = false, //
576-
CLOEXEC: bool = false,
577-
_20: u1 = 0,
578-
PATH: bool = false,
579-
TMPFILE: bool = false,
580-
_23: u9 = 0,
535+
_: u7 = 0,
536+
nonblock: bool = false,
537+
_9: u2 = 0,
538+
notification_pipe: bool = false,
539+
_12: u4 = 0,
540+
direct: bool = false,
541+
_17: u3 = 0,
542+
cloexec: bool = false,
543+
_21: u12 = 0,
581544
},
582545
.powerpc, .powerpcle, .powerpc64, .powerpc64le => packed struct(u32) {
583-
ACCMODE: ACCMODE = .RDONLY,
584-
_2: u4 = 0,
585-
CREAT: bool = false,
586-
EXCL: bool = false, //
587-
NOCTTY: bool = false,
588-
TRUNC: bool = false,
589-
APPEND: bool = false,
590-
NONBLOCK: bool = false, //
591-
DSYNC: bool = false,
592-
ASYNC: bool = false,
593-
DIRECTORY: bool = false,
594-
NOFOLLOW: bool = false,
595-
LARGEFILE: bool = false,
596-
DIRECT: bool = false, //
597-
NOATIME: bool = false,
598-
CLOEXEC: bool = false, //
599-
SYNC: bool = false,
600-
PATH: bool = false,
601-
TMPFILE: bool = false,
602-
_23: u9 = 0,
546+
_: u7 = 0,
547+
notification_pipe: bool = false,
548+
_9: u3 = 0,
549+
nonblock: bool = false,
550+
_13: u5 = 0,
551+
direct: bool = false,
552+
_19: u1 = 0,
553+
cloexec: bool = false,
554+
_21: u12 = 0,
603555
},
556+
// matches `x86_64` above check if the full struct matches
604557
.hexagon, .or1k, .s390x => packed struct(u32) {
605-
ACCMODE: ACCMODE = .RDONLY,
606-
_2: u4 = 0,
607-
CREAT: bool = false,
608-
EXCL: bool = false, //
609-
NOCTTY: bool = false,
610-
TRUNC: bool = false,
611-
APPEND: bool = false,
612-
NONBLOCK: bool = false, //
613-
DSYNC: bool = false,
614-
ASYNC: bool = false,
615-
DIRECT: bool = false, //
616-
LARGEFILE: bool = false,
617-
DIRECTORY: bool = false,
618-
NOFOLLOW: bool = false,
619-
NOATIME: bool = false,
620-
CLOEXEC: bool = false, //
621-
_20: u1 = 0,
622-
PATH: bool = false,
623-
_22: u10 = 0,
624-
625-
// #define O_RSYNC 04010000
626-
// #define O_SYNC 04010000
627-
// #define O_TMPFILE 020200000
628-
// #define O_NDELAY O_NONBLOCK
558+
_: u7 = 0,
559+
notification_pipe: bool = false,
560+
_9: u3 = 0,
561+
nonblock: bool = false,
562+
_13: u2 = 0,
563+
direct: bool = false,
564+
_16: u4 = 0,
565+
cloexec: bool = false,
566+
_21: u12 = 0,
629567
},
568+
// matches `aarch64` above check if the full struct matches
630569
.m68k => packed struct(u32) {
631-
ACCMODE: ACCMODE = .RDONLY,
632-
_2: u4 = 0,
633-
CREAT: bool = false,
634-
EXCL: bool = false, //
635-
NOCTTY: bool = false,
636-
TRUNC: bool = false,
637-
APPEND: bool = false,
638-
NONBLOCK: bool = false, //
639-
DSYNC: bool = false,
640-
ASYNC: bool = false,
641-
DIRECTORY: bool = false,
642-
NOFOLLOW: bool = false,
643-
DIRECT: bool = false, //
644-
LARGEFILE: bool = false,
645-
NOATIME: bool = false,
646-
CLOEXEC: bool = false, //
647-
_20: u1 = 0,
648-
PATH: bool = false,
649-
_22: u10 = 0,
570+
_: u7 = 0,
571+
notification_pipe: bool = false,
572+
_9: u3 = 0,
573+
nonblock: bool = false,
574+
_13: u4 = 0,
575+
direct: bool = false,
576+
_18: u2 = 0,
577+
cloexec: bool = false,
578+
_21: u12 = 0,
650579
},
651-
else => @compileError("missing std.os.linux.O constants for this architecture"),
580+
else => @compileError("missing std.os.linux.Pipe2 flags for this architecture"),
652581
};
653582

654583
/// Set by startup code, used by `getauxval`.
@@ -1425,7 +1354,7 @@ pub fn pipe(fd: *[2]i32) usize {
14251354
}
14261355
}
14271356

1428-
pub fn pipe2(fd: *[2]i32, flags: O) usize {
1357+
pub fn pipe2(fd: *[2]i32, flags: Pipe2) usize {
14291358
return syscall2(.pipe2, @intFromPtr(fd), @as(u32, @bitCast(flags)));
14301359
}
14311360

lib/std/os/linux/IoUring.zig

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1861,10 +1861,23 @@ pub fn pipe(
18611861
self: *IoUring,
18621862
user_data: u64,
18631863
fds: *[2]linux.fd_t,
1864-
flags: uflags.,
1864+
flags: linux.Pipe2,
18651865
) !*Sqe {
18661866
const sqe = try self.get_sqe();
1867-
sqe.prep_pipe(fds, offset, nbytes);
1867+
sqe.prep_pipe(fds, flags);
1868+
sqe.user_data = user_data;
1869+
return sqe;
1870+
}
1871+
1872+
pub fn pipe_direct(
1873+
self: *IoUring,
1874+
user_data: u64,
1875+
fds: *[2]linux.fd_t,
1876+
flags: linux.Pipe2,
1877+
file_index: u32,
1878+
) !*Sqe {
1879+
const sqe = try self.get_sqe();
1880+
sqe.prep_pipe_direct(fds, flags, file_index);
18681881
sqe.user_data = user_data;
18691882
return sqe;
18701883
}
@@ -3422,22 +3435,29 @@ pub const Sqe = extern struct {
34223435

34233436
pub fn prep_pipe(
34243437
sqe: *Sqe,
3425-
fd: linux.fd_t,
3426-
offset: u64,
3427-
nbytes: u64,
3438+
fds: *[2]linux.fd_t,
3439+
flags: linux.Pipe2,
34283440
) void {
34293441
sqe.prep_rw(
3430-
.uring_cmd,
3431-
fd,
3432-
undefined,
3442+
.pipe,
3443+
0,
3444+
@intFromPtr(fds),
34333445
0,
34343446
0,
34353447
);
3436-
// sqe.off maps to sqe.cmd_op in liburing
3437-
sqe.off = constants.BLOCK_URING_CMD_DISCARD;
3438-
sqe.addr = offset;
3439-
sqe.addr3 = nbytes;
3448+
sqe.rw_flags = @bitCast(flags);
34403449
}
3450+
3451+
pub fn prep_pipe_direct(
3452+
sqe: *Sqe,
3453+
fds: *[2]linux.fd_t,
3454+
flags: linux.Pipe2,
3455+
file_index: u32,
3456+
) void {
3457+
sqe.prep_pipe(fds, flags);
3458+
sqe.set_target_fixed_file(file_index);
3459+
}
3460+
34413461
// TODO: maybe remove unused flag fields?
34423462
pub fn prep_bind(
34433463
sqe: *Sqe,

0 commit comments

Comments
 (0)