12 ms·
Fibers: An Elegant Windows API
- uasm 8y agoHmm, let's read this one. > "March 28, 2019" > "Fibers: the Most Elegant Windows API" > "The Windows API — a.k.a. Win32 — is notorious for being clunky, ugly, and lacking good taste. Microsoft has done a pretty commendable job with backwards compatibility, but the trade-off is that the API is filled to the brim with historical cruft. Every hasty, poor design over the decades is carried forward forever, and, in many cases, even built upon, which essentially doubles down on past mistakes. POSIX certainly has its own ugly corners, but those are the exceptions. In the Windows API, elegance is the exception." I'm glad OP just discovered in mid-2019 an API that has been there since Windows XP (ca. 2001). Why then, does OP start by crapping all over the Windows codebase? You think the Fibers API is elegant? Have a look at IOCP. IO in Windows in general actually, since the days of Windows 95. Event management. Threading and parallelism. The kernel subsystem. The APIs in Windows have remained stable, consistent, forward and backwards compatible - from pretty much day one.
- deleted 8y ago[deleted]
- teh_klev 8y ago> I'm glad OP just discovered in mid-2019 an API that has been there since Windows XP Can I bring to your attention the second para: "That’s why, when I recently revisited the Fibers API, I was pleasantly surprised." I don't think it's the author's first rodeo. The article reads like a retrospective look at the fibers API and how well it's aged.
- userbinator 8y agoIt's been there since NT 3.51, according to https://www.geoffchappell.com/studies/windows/win32/kernel32/api/index.htm https://www.geoffchappell.com/studies/windows/win32/kernel32... and my copy of Win32.hlp (from back when MS documentation was actually proofread before release...)
- PaulHoule 8y agoIOCP is particularly strong when you compare it to select, poll, epoll, kpoll, and all of the other attempts to make asyncio work in Linux.
- machinecoffee 8y agoIOCP in Windows is a good API (although a bit non-obvious), but to be fair, they did have a few tries and missteps before they got there.
- deleted 8y ago[deleted]
- zzzcpan 8y agoIt's not. IOCP is the worst of all of them.
- kentonv 8y agoEh. I used to think that, until I actually wrote event systems based on all of the above. epoll turns out to be my favorite. Buffer allocation with IOCP feels incredibly ugly. You must allocate a buffer before you initiate a read(), and then you must leave that buffer alone until the read() completes. You would think that this has the advantage that the kernel doesn't need to allocate its own buffer, and you get some sort of zero-copy magic where bytes land directly in userspace. But that's not really the case. The socket still needs a buffer on the kernel side in case bytes arrive while no read() is pending. So now you have to allocate a buffer for each socket and the kernel also has to allocate a buffer for each socket, which seems like a waste. This rabbit hole gets deeper. What happens if the socket receives two packets in rapid succession? In a naive implementation, the first packet completes the read(), and signals the completion on the IOCP. The app now has to process that event and start a new read() when it's ready. But in the meantime, the second packet arrives. Whoops, no read() is pending, so now the packet has to go into a kernel-side buffer only to be copied later. But that's a naive implementation. Apparently, in reality, the kernel implements some sort of nagle-like algorithm where it tries to wait a bit for additional packets before it actually signals completion of a read(). But this introduces delay, and many projects (e.g. Chrome) have discovered this delay is rather harmful to certain kinds of performance. I read somewhere that Chrome and others have given up on IOCP and use WSAPoll instead -- but I can't remember where I read this because all the lore about Windows event handling is hidden in random forum threads and Github gists rather than proper documentation. It seems to me that the right way to do what Windows was trying to do here would be for userspace to allocate a ring buffer for the kernel to use, and then the app and the kernel would coordinate the start and end pointers of the ring buffer. Then the kernel needs no buffer of its own; it can always deliver to userspace. If the buffer fills up, the kernel can do exactly what it would do in the case of a regular kernel-side buffer filling up -- apply backpressure and force the peer to retransmit later.
- jstimpfle 8y agoI had a hard time with IOCP and eventuall ditched it because I didn't find a way to avoid creating a separate read buffer for each and every handle. Suppose I want to watch 10K connections, or files, or such. 4K should be a reasonable read buffer size. Do I really need to spend 40M of memory just to read from the handles simultaneously?
- kbenson 8y agoI'm confused (but I'm also not familiar with the API). It sounds like you're saying you want to be able to read from 10k connections simultaneously while allocating less than a byte of buffer space to each, so I assume I'm misunderstanding some aspect of what you're trying to say (or some aspect of the API that puts this into the correct context).
- jstimpfle 8y agoNo I'm saying I want to read from N handles simultaneously using M buffers, where M <= N. Of course, the OS shouldn't write to one of my read buffers that currently contains other buffered data. But that doesn't mean that it has to be N = M, since that is a waste. I guess M = 10 * number of OS threads should usually be plenty to achieve good performance. But I'm happy to be corrected by people more experienced with systems performance.
- kbenson 8y agoI believe I understand. By not allocating in the api call itself you can pre-allocate your own buffers and keep track of which are in-use and not in-use yourself and use some subset of the total amount that would be allocated in each api call be reusing buffers after a particular connection is done with it. That makes sense.
- zzzcpan 8y agoOn unixy systems kernel manages buffers by itself, using as little memory as possible, not allocating anything when no data is received, and userspace doesn't need to allocate any memory either until it is notified and decides to non-blockingly read the data from the kernel. Meaning that waiting for data from connections costs nothing, while on windows it requires preallocating buffer per connection. Basically the same problem that synchronous APIs have. The myth that IOCP is elegant has to die.
- spapas82 8y agoI think this is a must read for new developers: https://www.joelonsoftware.com/2004/06/13/how-microsoft-lost-the-api-war/ https://www.joelonsoftware.com/2004/06/13/how-microsoft-lost... (yes it's from 2004)
- DarkWiiPlayer 8y agoI just fell in love with that API. That's seriously the first thing windows has which I really REALLY wish linux would just copy as is. It's simple and to the point, but there's nothing missing either. Perfect.
- jstimpfle 8y agoI once built something on it and remember it as quite a nice experience.
- gpderetta 8y agoI wouldn't call it elegant at all, the convertthreadtofiber is a pain for a library. I like swapcontext much more in theory. In practice the issue with swapcontext is that it does not integrate well with the rest of posix (what happens if you move a fiber to another thread?) and the requirements to restore the signal mask pretty much require a very slow implementation. On the other hand, Windows Fibers are somewhat efficient (one can still do significantly better), but most importantly, the interaction with the rest of the system is well understood. I wrote my thoughts on the API here about (/checks calendar) 13 years ago: http://www.crystalclearsoftware.com/soc/coroutine/coroutine/fibers.html http://www.crystalclearsoftware.com/soc/coroutine/coroutine/...
- amelius 8y agoHow do you manage stack memory (allocation, increasing capacity, freeing, moving, etc.)? Is it possible to suspend a fiber to disk?
- CodesInChaos 8y agoA Fiber has its own OS stack and cannot outlive the process (just like a thread). So the memory cost is the same as a normal thread. The main difference is that it uses cooperative scheduling.
- CodesInChaos 8y agoFibers have some annoying edge cases related to thread local storage, which can bite fiber unaware code. Which is why Microsoft recently added User-Mode Scheduling, which behave much closer to actual threads, except that they still have the manual yielding behaviour of fibers. https://docs.microsoft.com/en-us/windows/desktop/procthread/user-mode-scheduling https://docs.microsoft.com/en-us/windows/desktop/procthread/...
- petermcneeley 8y agoYes this is actually a significant issue since TLS is used in many cases to implement locks and mutex.
- jstimpfle 8y agoThat's because what you need is Fiber-local storage no?
- dblohm7 8y agoWhat happens when you call an API that isn't fiber-aware and uses thread-local storage in its implementation? Basically your entire stack needs to be fiber-aware, or you can't use them.
- jepler 8y agoOuch. POSIX did an awful job when it wrote the API for "makecontext". You can't even pass a pointer to the context's function in the natural way, because only 'int's can be passed, and on any real computer, sizeof(int) < sizeof(void*)! And anyway, POSIX removed makecontext in POSIX.1-2008 so new code shouldn't be using it. This is too bad, as there are legitimate reasons to use these kinds of cooperative threading abstractions. I guess we still have gnu pth, which hasn't seen a release in 13 years, but it also has a much bigger (pthreads-like) interface compared to the mere 3 functions in POSIX for cooperative threading.
- klodolph 8y ago> You can't even pass a pointer to the context's function in the natural way, because only 'int's can be passed, and on any real computer, sizeof(int) < sizeof(void*)! I checked the API. It looks like a variadic function, so you can pass any arguments to it?
- supernes 8y agoI recently had an idea about an app and a clever way to hook it into fairly modern, user-facing parts of Windows and macOS (Search/Cortana and Spotlight, respectively.) I did some feasibility research on Windows first, and very soon hit some ancient win32 interfaces I had to implement, used by parts of the shell that have existed since the earliest days, something I have no intention of wasting my time on. I was under the impression that macOS would be much better, but was surprised to find that there's no public or documented API, just a 3rd party open source framework implemented as a hack that combines javascript and python somehow, but seems to get the job done. This got me thinking that you need a serious amount of expertise to implement new experiences on modern operating systems, never mind crossing platforms. Puts the appeal of technologies like Electron that do platform integration for you in perspective.
- Crinus 8y agoOn Windows, you do need some effort if you have no idea what is going on, but it isn't like those systems were sloppily put together, there are some design ideas and you get used to them... eventually. But once you do, things more or less fall into place. A biggest issue with working on older Windows APIs is that modern web-based MSDN documentation is simply awful and the best you can do is to find old MSDN CDs/DVDs that often contained more information than the site (especially articles), better organization and -especially- much faster interface (being local and all, but also the cdroms around late 90s / early 2000s were in CHM format are lightweight). And 99.999% of the documentation you'll find on these CDs will have the same text as the documentation (if it still there) on MSDN (and very often the same text that is on the much older WinHelp WIN32SDK.HLP files too). Like most things Microsoft on desktop, everything went downhill since .NET people took over and decided to redo things their way (yes, .NET brought some nice things too, but it created an internal schism - before .NET became a thing, desktop tech in Windows was mostly coherent) and now history is repeated with UWP.
- maxxxxx 8y agoTotally agree. I worked for a while with pure Win32. Once you get the hang of it it’s quite consistent and pretty well thought out. Also agree about the documentation. Seems MS is determined to make it worse every year, break more links and provide less useful information. The old MSDN CDs were a piece of beauty.
- blattimwind 8y ago> POSIX certainly has its own ugly corners, but those are the exceptions. In the Windows API, elegance is the exception. Makes me wonder how seriously OP used POSIX APIs, and across how many platforms... because large parts of POSIX are pretty bad. And as an OS interface, the Windows API is clearly superior in almost every regard (if there wasn't this tiny portability issue).
- jstimpfle 8y agoI don't know what parts of Win32 you mean, but one task I did recently that should be dead simple was listing the contents of a directory. Why did I end up appending "* . *" (spaces inserted to avoid triggering HN's italic formatting) patterns to the directory name, and why do I need to find the first file with FindFirstFile() and all the remaining files with FindNextFile()? Maybe I was particularly dumb that day, but it's the best I could figure out and I don't think that's elegant.
- magicalhippo 8y ago> Why did I end up appending "* . *" (spaces inserted to avoid triggering HN's italic formatting) patterns to the directory name Because sometimes you just want get all the XML files in a directory, and it's nice to not have to filter yourself. > why do I need to find the first file with FindFirstFile() and all the remaining files with FindNextFile()? Why do you consider this a poor API and what would you consider a better API?
- jstimpfle 8y agoYeah, but please let me just do the filtering myself and don't force me to allocate a new string to append a silly pattern that makes me wonder if matching things without a dot in their name is supported. Yes, "sometimes" the API consumer just wants to match XML files. For example, when creating one of these silly file dialogs that never show what I'm looking for. > Why do you consider this a poor API and what would you consider a better API? POSIX gets that just right. No fuss, expose the technical reality in straightforward abstractions. You call opendir() and get an DIR* handle (representing an open directory). Then you call readdir() with that handle in a loop to get all the files in a directory. I'm not saying I'm strictly against bells and whistles, but often these only work for a first prototype, and if API designers don't offer a straightforward way so I can simply do what needs to be done, they really failed at their job.
- newnewpdro 8y agoIt's not that elegant. Win32 fibers can't return like UNIX/ucontext.h supports with the uc_link member. So you end up having to wrap your win32 fiber functions with your own trampoline to let them return without exiting the program and Do The Right Thing. Furthermore there's this thread-global fibers-enabled state win32 toggles with ConvertThreadToFiber() requiring any transparent coroutines library supporting win32 fibers to carefully initialize once per thread while also supporting reentrant use. It's mildly annoying when you want to generically support things like nested bundles or heirarchies of coroutines. This last bit seems completely strange considering a fiber's state should be a subset of what forms a thread, and you're always running on a thread. There should be nothing to do when "converting" to a fiber. Just make the fiber state always accessible to userspace via GetCurrentFiber() and always allow calling CreateFiber() and SwitchToFiber(). Another strange wart in win32 fibers is the C srand()/rand() state is per-fiber for some reason. It's undefined by C since C says nothing of coroutines, but it's very strange and generally unexpected behavior to see your coroutines producing identical sequences of values coming out of rand(). On *NIX the rand() state is per-process, pthreads share user memory so they all share the rand() state, and ucontext shares the thread so of course its contexts share the single state as well. I much prefer the UNIX/ucontext API, they just fucked up the operands for makecontext(). Instead of deprecating ucontext POSIX should have just fixed makecontext() to explicitly take a pointer. C programs have no supported system-supplied coroutine API on UNIX now, which is bullshit.
- dblohm7 8y agoFibers still have a lot of problems. They're incompatible with any APIs that have thread affinity. Windowing, single-threaded apartment COM, CRITICAL_SECTIONs, etc...
- aidenn0 8y agoFWIW Russ Cox wrote a similar API on top of the -context() calls named libtask.
- rwmj 8y agoThe big news seems to be that makecontext et al are deprecated. Does anyone know why that happened? Lots of "green threads" / coroutines implementations are based on it.
- gpderetta 8y agoBasically their signature can no longer be expressed in ISO C, so instead of fixing it POSIX decided to just deprecate them.