Skip to content

Commit 9c08a33

Browse files
authored
Merge pull request #8750 from lithdew/master
x/io, x/os: async i/o reactor, cross-platform socket syscalls and bits
2 parents 05b677f + 4909aa1 commit 9c08a33

24 files changed

+646
-203
lines changed

lib/std/atomic.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ test "std.atomic" {
1919
_ = @import("atomic/Atomic.zig");
2020
}
2121

22-
pub fn fence(comptime ordering: Ordering) callconv(.Inline) void {
22+
pub inline fn fence(comptime ordering: Ordering) void {
2323
switch (ordering) {
2424
.Acquire, .Release, .AcqRel, .SeqCst => {
2525
@fence(ordering);
@@ -30,7 +30,7 @@ pub fn fence(comptime ordering: Ordering) callconv(.Inline) void {
3030
}
3131
}
3232

33-
pub fn compilerFence(comptime ordering: Ordering) callconv(.Inline) void {
33+
pub inline fn compilerFence(comptime ordering: Ordering) void {
3434
switch (ordering) {
3535
.Acquire, .Release, .AcqRel, .SeqCst => asm volatile ("" ::: "memory"),
3636
else => @compileLog(ordering, " only applies to a given memory location"),
@@ -45,7 +45,7 @@ test "fence/compilerFence" {
4545
}
4646

4747
/// Signals to the processor that the caller is inside a busy-wait spin-loop.
48-
pub fn spinLoopHint() callconv(.Inline) void {
48+
pub inline fn spinLoopHint() void {
4949
const hint_instruction = switch (target.cpu.arch) {
5050
// No-op instruction that can hint to save (or share with a hardware-thread) pipelining/power resources
5151
// https://software.intel.com/content/www/us/en/develop/articles/benefitting-power-and-performance-sleep-loops.html

lib/std/atomic/Atomic.zig

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -48,38 +48,38 @@ pub fn Atomic(comptime T: type) type {
4848
};
4949
}
5050

51-
pub fn swap(self: *Self, value: T, comptime ordering: Ordering) callconv(.Inline) T {
51+
pub inline fn swap(self: *Self, value: T, comptime ordering: Ordering) T {
5252
return self.rmw(.Xchg, value, ordering);
5353
}
5454

55-
pub fn compareAndSwap(
55+
pub inline fn compareAndSwap(
5656
self: *Self,
5757
compare: T,
5858
exchange: T,
5959
comptime success: Ordering,
6060
comptime failure: Ordering,
61-
) callconv(.Inline) ?T {
61+
) ?T {
6262
return self.cmpxchg(true, compare, exchange, success, failure);
6363
}
6464

65-
pub fn tryCompareAndSwap(
65+
pub inline fn tryCompareAndSwap(
6666
self: *Self,
6767
compare: T,
6868
exchange: T,
6969
comptime success: Ordering,
7070
comptime failure: Ordering,
71-
) callconv(.Inline) ?T {
71+
) ?T {
7272
return self.cmpxchg(false, compare, exchange, success, failure);
7373
}
7474

75-
fn cmpxchg(
75+
inline fn cmpxchg(
7676
self: *Self,
7777
comptime is_strong: bool,
7878
compare: T,
7979
exchange: T,
8080
comptime success: Ordering,
8181
comptime failure: Ordering,
82-
) callconv(.Inline) ?T {
82+
) ?T {
8383
if (success == .Unordered or failure == .Unordered) {
8484
@compileError(@tagName(Ordering.Unordered) ++ " is only allowed on atomic loads and stores");
8585
}
@@ -103,12 +103,12 @@ pub fn Atomic(comptime T: type) type {
103103
};
104104
}
105105

106-
fn rmw(
106+
inline fn rmw(
107107
self: *Self,
108108
comptime op: std.builtin.AtomicRmwOp,
109109
value: T,
110110
comptime ordering: Ordering,
111-
) callconv(.Inline) T {
111+
) T {
112112
return @atomicRmw(T, &self.value, op, value, ordering);
113113
}
114114

@@ -117,37 +117,37 @@ pub fn Atomic(comptime T: type) type {
117117
}
118118

119119
pub usingnamespace exportWhen(std.meta.trait.isNumber(T), struct {
120-
pub fn fetchAdd(self: *Self, value: T, comptime ordering: Ordering) callconv(.Inline) T {
120+
pub inline fn fetchAdd(self: *Self, value: T, comptime ordering: Ordering) T {
121121
return self.rmw(.Add, value, ordering);
122122
}
123123

124-
pub fn fetchSub(self: *Self, value: T, comptime ordering: Ordering) callconv(.Inline) T {
124+
pub inline fn fetchSub(self: *Self, value: T, comptime ordering: Ordering) T {
125125
return self.rmw(.Sub, value, ordering);
126126
}
127127

128-
pub fn fetchMin(self: *Self, value: T, comptime ordering: Ordering) callconv(.Inline) T {
128+
pub inline fn fetchMin(self: *Self, value: T, comptime ordering: Ordering) T {
129129
return self.rmw(.Min, value, ordering);
130130
}
131131

132-
pub fn fetchMax(self: *Self, value: T, comptime ordering: Ordering) callconv(.Inline) T {
132+
pub inline fn fetchMax(self: *Self, value: T, comptime ordering: Ordering) T {
133133
return self.rmw(.Max, value, ordering);
134134
}
135135
});
136136

137137
pub usingnamespace exportWhen(std.meta.trait.isIntegral(T), struct {
138-
pub fn fetchAnd(self: *Self, value: T, comptime ordering: Ordering) callconv(.Inline) T {
138+
pub inline fn fetchAnd(self: *Self, value: T, comptime ordering: Ordering) T {
139139
return self.rmw(.And, value, ordering);
140140
}
141141

142-
pub fn fetchNand(self: *Self, value: T, comptime ordering: Ordering) callconv(.Inline) T {
142+
pub inline fn fetchNand(self: *Self, value: T, comptime ordering: Ordering) T {
143143
return self.rmw(.Nand, value, ordering);
144144
}
145145

146-
pub fn fetchOr(self: *Self, value: T, comptime ordering: Ordering) callconv(.Inline) T {
146+
pub inline fn fetchOr(self: *Self, value: T, comptime ordering: Ordering) T {
147147
return self.rmw(.Or, value, ordering);
148148
}
149149

150-
pub fn fetchXor(self: *Self, value: T, comptime ordering: Ordering) callconv(.Inline) T {
150+
pub inline fn fetchXor(self: *Self, value: T, comptime ordering: Ordering) T {
151151
return self.rmw(.Xor, value, ordering);
152152
}
153153

@@ -158,24 +158,24 @@ pub fn Atomic(comptime T: type) type {
158158
Toggle,
159159
};
160160

161-
pub fn bitSet(self: *Self, bit: Bit, comptime ordering: Ordering) callconv(.Inline) u1 {
161+
pub inline fn bitSet(self: *Self, bit: Bit, comptime ordering: Ordering) u1 {
162162
return bitRmw(self, .Set, bit, ordering);
163163
}
164164

165-
pub fn bitReset(self: *Self, bit: Bit, comptime ordering: Ordering) callconv(.Inline) u1 {
165+
pub inline fn bitReset(self: *Self, bit: Bit, comptime ordering: Ordering) u1 {
166166
return bitRmw(self, .Reset, bit, ordering);
167167
}
168168

169-
pub fn bitToggle(self: *Self, bit: Bit, comptime ordering: Ordering) callconv(.Inline) u1 {
169+
pub inline fn bitToggle(self: *Self, bit: Bit, comptime ordering: Ordering) u1 {
170170
return bitRmw(self, .Toggle, bit, ordering);
171171
}
172172

173-
fn bitRmw(
173+
inline fn bitRmw(
174174
self: *Self,
175175
comptime op: BitRmwOp,
176176
bit: Bit,
177177
comptime ordering: Ordering,
178-
) callconv(.Inline) u1 {
178+
) u1 {
179179
// x86 supports dedicated bitwise instructions
180180
if (comptime target.cpu.arch.isX86() and @sizeOf(T) >= 2 and @sizeOf(T) <= 8) {
181181
const instruction = switch (op) {

lib/std/c.zig

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,16 +166,18 @@ pub extern "c" fn sendto(
166166
dest_addr: ?*const sockaddr,
167167
addrlen: socklen_t,
168168
) isize;
169+
pub extern "c" fn sendmsg(sockfd: fd_t, msg: *const std.x.os.Socket.Message, flags: c_int) isize;
169170

170-
pub extern fn recv(sockfd: fd_t, arg1: ?*c_void, arg2: usize, arg3: c_int) isize;
171-
pub extern fn recvfrom(
171+
pub extern "c" fn recv(sockfd: fd_t, arg1: ?*c_void, arg2: usize, arg3: c_int) isize;
172+
pub extern "c" fn recvfrom(
172173
sockfd: fd_t,
173174
noalias buf: *c_void,
174175
len: usize,
175176
flags: u32,
176177
noalias src_addr: ?*sockaddr,
177178
noalias addrlen: ?*socklen_t,
178179
) isize;
180+
pub extern "c" fn recvmsg(sockfd: fd_t, msg: *std.x.os.Socket.Message, flags: c_int) isize;
179181

180182
pub usingnamespace switch (builtin.os.tag) {
181183
.netbsd => struct {

lib/std/json.zig

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2111,10 +2111,7 @@ test "parse into struct with duplicate field" {
21112111
const ballast = try testing.allocator.alloc(u64, 1);
21122112
defer testing.allocator.free(ballast);
21132113

2114-
const options_first = ParseOptions{
2115-
.allocator = testing.allocator,
2116-
.duplicate_field_behavior = .UseFirst,
2117-
};
2114+
const options_first = ParseOptions{ .allocator = testing.allocator, .duplicate_field_behavior = .UseFirst };
21182115

21192116
const options_last = ParseOptions{
21202117
.allocator = testing.allocator,

lib/std/mem.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,7 +1171,7 @@ test "mem.indexOf" {
11711171
test "mem.indexOf multibyte" {
11721172
{
11731173
// make haystack and needle long enough to trigger boyer-moore-horspool algorithm
1174-
const haystack = [1]u16{0} ** 100 ++ [_]u16 { 0xbbaa, 0xccbb, 0xddcc, 0xeedd, 0xffee, 0x00ff };
1174+
const haystack = [1]u16{0} ** 100 ++ [_]u16{ 0xbbaa, 0xccbb, 0xddcc, 0xeedd, 0xffee, 0x00ff };
11751175
const needle = [_]u16{ 0xbbaa, 0xccbb, 0xddcc, 0xeedd, 0xffee };
11761176
try testing.expectEqual(indexOfPos(u16, &haystack, 0, &needle), 100);
11771177

@@ -1184,7 +1184,7 @@ test "mem.indexOf multibyte" {
11841184

11851185
{
11861186
// make haystack and needle long enough to trigger boyer-moore-horspool algorithm
1187-
const haystack = [_]u16 { 0xbbaa, 0xccbb, 0xddcc, 0xeedd, 0xffee, 0x00ff } ++ [1]u16{0} ** 100;
1187+
const haystack = [_]u16{ 0xbbaa, 0xccbb, 0xddcc, 0xeedd, 0xffee, 0x00ff } ++ [1]u16{0} ** 100;
11881188
const needle = [_]u16{ 0xbbaa, 0xccbb, 0xddcc, 0xeedd, 0xffee };
11891189
try testing.expectEqual(lastIndexOf(u16, &haystack, &needle), 0);
11901190

@@ -2201,7 +2201,7 @@ pub fn collapseRepeatsLen(comptime T: type, slice: []T, elem: T) usize {
22012201

22022202
/// Collapse consecutive duplicate elements into one entry.
22032203
pub fn collapseRepeats(comptime T: type, slice: []T, elem: T) []T {
2204-
return slice[0 .. collapseRepeatsLen(T, slice, elem)];
2204+
return slice[0..collapseRepeatsLen(T, slice, elem)];
22052205
}
22062206

22072207
fn testCollapseRepeats(str: []const u8, elem: u8, expected: []const u8) !void {

lib/std/os.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4998,7 +4998,7 @@ pub fn sendmsg(
49984998
flags: u32,
49994999
) SendMsgError!usize {
50005000
while (true) {
5001-
const rc = system.sendmsg(sockfd, &msg, flags);
5001+
const rc = system.sendmsg(sockfd, @ptrCast(*const std.x.os.Socket.Message, &msg), @intCast(c_int, flags));
50025002
if (builtin.os.tag == .windows) {
50035003
if (rc == windows.ws2_32.SOCKET_ERROR) {
50045004
switch (windows.ws2_32.WSAGetLastError()) {

lib/std/os/bits/darwin.zig

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,7 @@ pub const sockaddr = extern struct {
2323
family: sa_family_t,
2424
data: [14]u8,
2525
};
26-
pub const sockaddr_storage = extern struct {
27-
len: u8,
28-
family: sa_family_t,
29-
__pad1: [5]u8,
30-
__align: i64,
31-
__pad2: [112]u8,
32-
};
26+
pub const sockaddr_storage = std.x.os.Socket.Address.Native.Storage;
3327
pub const sockaddr_in = extern struct {
3428
len: u8 = @sizeOf(sockaddr_in),
3529
family: sa_family_t = AF_INET,

lib/std/os/bits/dragonfly.zig

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,8 @@ pub const sockaddr = extern struct {
396396
data: [14]u8,
397397
};
398398

399+
pub const sockaddr_storage = std.x.os.Socket.Address.Native.Storage;
400+
399401
pub const Kevent = extern struct {
400402
ident: usize,
401403
filter: c_short,
@@ -694,14 +696,6 @@ pub const in_port_t = u16;
694696
pub const sa_family_t = u8;
695697
pub const socklen_t = u32;
696698

697-
pub const sockaddr_storage = extern struct {
698-
ss_len: u8,
699-
ss_family: sa_family_t,
700-
__ss_pad1: [5]u8,
701-
__ss_align: i64,
702-
__ss_pad2: [112]u8,
703-
};
704-
705699
pub const sockaddr_in = extern struct {
706700
len: u8 = @sizeOf(sockaddr_in),
707701
family: sa_family_t = AF_INET,
@@ -768,6 +762,11 @@ pub const dl_phdr_info = extern struct {
768762
dlpi_phdr: [*]std.elf.Phdr,
769763
dlpi_phnum: u16,
770764
};
765+
pub const cmsghdr = extern struct {
766+
cmsg_len: socklen_t,
767+
cmsg_level: c_int,
768+
cmsg_type: c_int,
769+
};
771770
pub const msghdr = extern struct {
772771
msg_name: ?*c_void,
773772
msg_namelen: socklen_t,
@@ -777,11 +776,6 @@ pub const msghdr = extern struct {
777776
msg_controllen: socklen_t,
778777
msg_flags: c_int,
779778
};
780-
pub const cmsghdr = extern struct {
781-
cmsg_len: socklen_t,
782-
cmsg_level: c_int,
783-
cmsg_type: c_int,
784-
};
785779
pub const cmsgcred = extern struct {
786780
cmcred_pid: pid_t,
787781
cmcred_uid: uid_t,

lib/std/os/bits/freebsd.zig

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -206,13 +206,7 @@ pub const sockaddr = extern struct {
206206
data: [14]u8,
207207
};
208208

209-
pub const sockaddr_storage = extern struct {
210-
len: u8,
211-
family: sa_family_t,
212-
__pad1: [5]u8,
213-
__align: i64,
214-
__pad2: [112]u8,
215-
};
209+
pub const sockaddr_storage = std.x.os.Socket.Address.Native.Storage;
216210

217211
pub const sockaddr_in = extern struct {
218212
len: u8 = @sizeOf(sockaddr_in),

lib/std/os/bits/haiku.zig

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -239,13 +239,7 @@ pub const sockaddr = extern struct {
239239
data: [14]u8,
240240
};
241241

242-
pub const sockaddr_storage = extern struct {
243-
len: u8,
244-
family: sa_family_t,
245-
__pad1: [5]u8,
246-
__align: i64,
247-
__pad2: [112]u8,
248-
};
242+
pub const sockaddr_storage = std.x.os.Socket.Address.Native.Storage;
249243

250244
pub const sockaddr_in = extern struct {
251245
len: u8 = @sizeOf(sockaddr_in),

0 commit comments

Comments
 (0)