ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
35.19k stars 2.57k forks source link

Add `select()` to `std` for darwin. #16382

Open plaukiu opened 1 year ago

plaukiu commented 1 year ago

select() is left out from zig's std. Yet, it still has an important role in darwin systems: darwin does not support poll(), nor kqueue() for polling /dev/tty. It appears the only way to do it, sadly, is to use select().

It would be nice if it was made available in standard library for darwin.

ifreund commented 1 year ago

My first thought was "Please no, select is always a footgun because of the fd limit" but from the article linked it seems macOS has a workaround for that:

The avid reader might be wondering: “what if we have more than 1024 file descriptors? select is not going to work!” You’re right! This was a problem, so let’s enter The OSX select(2) Trick II: _DARWIN_UNLIMITED_SELECT.

This little gem hidden in the manual page tells us that if _DARWIN_UNLIMITED_SELECT is defined at compilation time, we are allowed to go beyond the FD_SETSIZE limit! We cannot create the fd_set as usual nor use FD_ZERO, we’ll need to manually allocate it and zero it with memset: 1, 2, 3.

I think that it would be reasonable to force usage of this darwin workaround for select if adding it to the standard library.

I don't think select should be added for any target for which it is not required, it's too big of a footgun on most unix like systems.

mnemnion commented 6 months ago

Worth pointing out that both BSD and Windows allow FD_SETSIZE to be redefined at compile time to an arbitrary value. Linux is the odd one out with its hard-coded limit of 1024. It isn't 'most Unix-like systems' where select(2) is a footgun, it's Linux.