curl / Mailing Lists / curl-library / Single Mail
Buy commercial curl support from WolfSSL. We help you work out your issues, debug your libcurl applications, use the API, port to new platforms, add new features and more. With a team lead by the curl founder himself.

RE: Curl_poll and Windows

From: Jeff Mears via curl-library <>
Date: Tue, 19 Nov 2019 20:30:18 +0000

Sorry if the formatting is messed up. Microsoft Outlook doesn't handle text-only mailing lists very well.

> From: Daniel Stenberg <>
> Sent: Monday, November 18, 2019 2:11 PM
> > First, Windows Vista and later have an API called WSAPoll. It works
> > almost the same as UNIX poll.
> Almost is the key there. We once used it and ditched it due to the "almost"
> not being good enough. I blogged about that back in the day
> Not saying it can't be used, it just needs to be done with care and attention to details.

Yikes. Thank you for pointing that out. It saved me from making a big mistake in my code >.<

It's ridiculous that this has been broken since 2006 (Vista) without this behavior being documented. I was rather mad at Microsoft when I read your email yesterday >.<

> > FD_SET is really slow in Windows.
> So slow it actually matters and is worth it for us to replace it with something else you think?

In Windows, FD_SET takes O(n) time where n is the number of sockets already in the fd_set. So in a loop to add a bunch of sockets to an fd_set, it's O(n^2). But that only really matters if there are a lot of curl connections at once.
> We'll happily welcome patches that improve libcurl, in this area as well as others! This is all news to me and yet I've toyed with these things for a while... albeit, not much on Windows.

Sure, I'll consider it. The only issue that comes to mind is, in curl-land, how would I malloc() memory that is aligned to at least _Alignof(SOCKET)? I suppose that this ought to happen automatically if the hosting application's malloc is sane.

> > A correct Windows implementation of Curl_wait_ms that is intended to
> > mimic
> > select() would use SleepEx with the second parameter TRUE, in a loop
> > because unlike select(), SleepEx will abort if an APC occurs.
> I don't even know what an APC is, but again, we welcome pull requests! =)
> I'm not aware of our use of Sleep() there have ever been an issue. That could of course also be because it doesn't matter much if the sleep would return prematurely once every blue moon.

An APC ("asynchronous procedure call") is a lot like a signal with a signal handler. When some operation completes, the APC will trigger on the thread that requested it. The big difference from UNIX signals is that APCs don't get dispatched unless the target thread is ready for them: the thread has to be in an "alertable wait state". The thread must be executing one of the many waiting functions with a flag saying that APCs are OK during this wait.

Windows's Sleep(n) is the same as SleepEx(n, FALSE). Sleep therefore won't allow APCs to be dispatched during the wait. This is different from select(), which is always an alertable wait state. The difference is rather minor, and nothing in curl uses APCs. I doubt it's caused problems so far that Sleep(n) isn't equivalent to waiting n milliseconds in select().

Received on 2019-11-19