public inbox for cygwin-developers@cygwin.com
 help / color / mirror / Atom feed
* Question about non-blocking Windows pipes
@ 2021-04-01 14:39 Ken Brown
  2021-04-06 12:57 ` Corinna Vinschen
  0 siblings, 1 reply; 3+ messages in thread
From: Ken Brown @ 2021-04-01 14:39 UTC (permalink / raw)
  To: cygwin-devel

Hi Corinna,

There are several places in fhandler_socket_unix.cc where you make a distinction 
between the blocking and nonblocking cases with code like this:

   cygwait (evt ?: get_handle (),...)

Here evt is an event handle in the blocking case and is NULL in the nonblocking 
case.  See, for example, fhandler_socket_unix::listen_pipe.

What's the reasoning behind this?  Why not just always create an event or always 
use the handle?  I guess I don't understand what Windows does differently in the 
two cases.

Thanks.

Ken

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Question about non-blocking Windows pipes
  2021-04-01 14:39 Question about non-blocking Windows pipes Ken Brown
@ 2021-04-06 12:57 ` Corinna Vinschen
  2021-04-06 13:33   ` Ken Brown
  0 siblings, 1 reply; 3+ messages in thread
From: Corinna Vinschen @ 2021-04-06 12:57 UTC (permalink / raw)
  To: cygwin-developers

Hi Ken,

On Apr  1 10:39, Ken Brown via Cygwin-developers wrote:
> Hi Corinna,
> 
> There are several places in fhandler_socket_unix.cc where you make a
> distinction between the blocking and nonblocking cases with code like this:
> 
>   cygwait (evt ?: get_handle (),...)

I only see this in fhandler_socket_unix::listen_pipe, actually.

> Here evt is an event handle in the blocking case and is NULL in the
> nonblocking case.  See, for example, fhandler_socket_unix::listen_pipe.
> 
> What's the reasoning behind this?  Why not just always create an event or
> always use the handle?

In the nonblocking case, the status code returned from NtFsControlFile
is either a useful status code like STATUS_SUCCESS or an error code,
or it is STATUS_PENDING.  STATUS_PENDING only means the call is still
not finished.  To get a useful result, you still need a useful status
code.  You get that by waiting for the handle.  Note that waiting for
the handle doesn't mean to wait for the connecting client.  Rather, it's
signalled as soon as the async NtFsControlFile call finished.  If the
status code is STATUS_PIPE_LISTENING then, you know that no client tries
to connect, so you can return EAGAIN.

The completion event object OTOH, is only signalled if a client actually
connected, so that's blocking mode.

Two problems with using an event object in nonblocking mode:

- The event object is referenced in the call.  If NtFsControlFile returns
  STATUS_PENDING and you leave the function, you have to use a globally
  available event object, because this address is used as event object
  until completion of the NtFsControlFile call (and a client connected).

- You also have a pending NtFsControlFile until a client connects.
  This is contrary to what you want in a non-blocking call:  You only
  want to know *if* a client connects, not wait for it either way.

Does that help?  I'm not claiming there isn't another way to handle this
scenario, that's just what I came up with.


Corinna

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Question about non-blocking Windows pipes
  2021-04-06 12:57 ` Corinna Vinschen
@ 2021-04-06 13:33   ` Ken Brown
  0 siblings, 0 replies; 3+ messages in thread
From: Ken Brown @ 2021-04-06 13:33 UTC (permalink / raw)
  To: cygwin-developers

On 4/6/2021 8:57 AM, Corinna Vinschen via Cygwin-developers wrote:
> Hi Ken,
> 
> On Apr  1 10:39, Ken Brown via Cygwin-developers wrote:
>> Hi Corinna,
>>
>> There are several places in fhandler_socket_unix.cc where you make a
>> distinction between the blocking and nonblocking cases with code like this:
>>
>>    cygwait (evt ?: get_handle (),...)
> 
> I only see this in fhandler_socket_unix::listen_pipe, actually.

You're right.  I was remembering something in sendmsg, but that's a very 
different situation.

>> Here evt is an event handle in the blocking case and is NULL in the
>> nonblocking case.  See, for example, fhandler_socket_unix::listen_pipe.
>>
>> What's the reasoning behind this?  Why not just always create an event or
>> always use the handle?
> 
> In the nonblocking case, the status code returned from NtFsControlFile
> is either a useful status code like STATUS_SUCCESS or an error code,
> or it is STATUS_PENDING.  STATUS_PENDING only means the call is still
> not finished.  To get a useful result, you still need a useful status
> code.  You get that by waiting for the handle.  Note that waiting for
> the handle doesn't mean to wait for the connecting client.  Rather, it's
> signalled as soon as the async NtFsControlFile call finished.

Ah, that's what I was missing.

>  If the
> status code is STATUS_PIPE_LISTENING then, you know that no client tries
> to connect, so you can return EAGAIN.
> 
> The completion event object OTOH, is only signalled if a client actually
> connected, so that's blocking mode.
> 
> Two problems with using an event object in nonblocking mode:
> 
> - The event object is referenced in the call.  If NtFsControlFile returns
>    STATUS_PENDING and you leave the function, you have to use a globally
>    available event object, because this address is used as event object
>    until completion of the NtFsControlFile call (and a client connected).
> 
> - You also have a pending NtFsControlFile until a client connects.
>    This is contrary to what you want in a non-blocking call:  You only
>    want to know *if* a client connects, not wait for it either way.
> 
> Does that help?  I'm not claiming there isn't another way to handle this
> scenario, that's just what I came up with.

Yes, that clears it up completely.  Thanks.

Ken

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-04-06 13:34 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-01 14:39 Question about non-blocking Windows pipes Ken Brown
2021-04-06 12:57 ` Corinna Vinschen
2021-04-06 13:33   ` Ken Brown

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).