@@ -44,8 +44,6 @@ fn C.htons(__hostshort u16) u16
4444
4545fn C.fcntl (fd int , cmd int , arg int ) int
4646
47- fn C.accept4 (sockfd int , addr & net.Addr, addrlen & u32 , flags int ) int
48-
4947pub struct Slice {
5048pub :
5149 start int
@@ -178,7 +176,13 @@ fn create_server_socket(port int) int {
178176
179177fn handle_accept_loop (mut notifier notify.FdNotifier, listen_fd int ) {
180178 for {
181- client_fd := C.accept4 (listen_fd, C.NULL, C.NULL, C.SOCK_NONBLOCK)
179+ client_fd := $if linux {
180+ // On Linux, we can use accept4 with SOCK_NONBLOCK to avoid an extra fcntl call
181+ C.accept4 (listen_fd, C.NULL, C.NULL, C.SOCK_NONBLOCK)
182+ } $else {
183+ C.accept (listen_fd, C.NULL, C.NULL)
184+ }
185+
182186 if client_fd < 0 {
183187 if C.errno == C.EAGAIN || C.errno == C.EWOULDBLOCK {
184188 break // No more incoming connections; exit loop.
@@ -187,6 +191,11 @@ fn handle_accept_loop(mut notifier notify.FdNotifier, listen_fd int) {
187191 C.perror (c 'Accept failed' )
188192 break
189193 }
194+
195+ $if ! linux {
196+ set_blocking (client_fd, false )
197+ }
198+
190199 // Enable TCP_NODELAY for lower latency
191200 opt := 1
192201 C.setsockopt (client_fd, C.IPPROTO_TCP, C.TCP_NODELAY, & opt, sizeof (opt))
@@ -228,6 +237,8 @@ fn process_events(mut server Server, listen_fd int) {
228237 request_buffer.flags.set (.noslices | .nogrow | .noshrink)
229238 }
230239 for {
240+ // It is a little bit slow to allocate on each iteration, but it
241+ // is simpler to implement. Optimization can be done later if needed.
231242 for event in notifier.wait (time.infinite) {
232243 if event.fd == listen_fd {
233244 handle_accept_loop (mut notifier, listen_fd)
0 commit comments