Re: The Tcl notifier and Console on Windows



Alex wrote:
David Gravereaux wrote:
Yup, it is funny. Apparently the WaitFor.. is too sensitive and
responds to all events (like mouse, too) and ends up blocking on
ReadFile waiting for just characters instead which seems to be corrupted
by the WaitFor..
Take out the WaitFor.. and the corruption disappears. So in this case,
ReadFile is expected to be used in blocking mode.

Wow. I'm having a hard time silencing my anti-Windows rant here...
Do they call THAT an OS ? ^H^H^H^H^H^H^H^H^H^H sorry :-)

So let me guess... So when you are waiting on a handle that can be
triggered for numerous types of events and you're just looking for a
subset of those types with no way of setting a mask (like
WSAEventSelect), its the fault of the OS?

For doing overlapped-I/O, you'd use ReadFileEx and either use APC or a
CompletionRoutine to allow the filled buffers to come back. It gets a
bit complicated, but has great performance (or low overhead, whichever
your perspective).

Imagine we want to reimplement [vwait] with no extra threads, and we
have a fileevent readable on stdin. So the thing to wait for is
GetStdHandle(STD_INPUT_HANDLE), or an event object bound to it through
an OVERLAPPED structure.

Well, no actually. All the generic aspects of the notifier/event loop
are just fine the way they are. The part that could change is only
tclWinConsole.c

Yes, you could event trigger on an overlapped ReadFileEx coming back or
instead have a single thread dedicated to servicing a completion port.
I prefer the later myself.

The notifier (the main thread) itself does wait in an APC compliant
manner and *could* be used to service an APC completion routine, but
there could be a starvation issue when you need to emulate blocking in
the Tcl_DriverOutputProc. I guess as long as the block is APC happy, it
could work _without_ the use of additional service threads. APC
compliant waits are functions such as WaitForMultipleObjectsEx that set
bAlertable to TRUE.

Hmm.. Now you got me thinking about the details :)

But how do we alter this existing handle to be in overlapped mode,
since it is already opened (it may be a pipe, file, or console), in
non-overlapped mode ???

Change the opening code in tclWinChan.c:TclpOpenFileChannel prior to
creating the channel for it with TclWinOpenConsoleChannel(). There is
an example of that already for serial channels.

All that's really important is that when the channel becomes serviceable
and we have an async path for it to let us know, that we call
Tcl_AlertNotifier so the notifier wait ends and the open channel list is
queried for new jobs. Oh.. and blocking emulation is working, too.

So in summary, I think ReadFileEx using a completion routine operated in
overlapped mode would be an excellent way to do asynchronous/alertable
reads from a console for just characters.
.



Relevant Pages

  • Re: How to perform a non-blocking synchronous ReadFile call/operation with NDISPROT ?
    ... However there is no mention of "non blocking" synchronous I/O. ... When reading from a communications device, the behavior of ReadFile is ... You can examine the queued packets without blocking. ...
    (microsoft.public.development.device.drivers)
  • Re: f0dders Fabulous Wait States.
    ... > The argument on the dependence on polling technology cannot be lost ... > for a very simple reason, with Von Neuman architecture (read ... >> are all blocking (with the exception of ReadFile in OVERLAPPED mode). ...
    (alt.lang.asm)
  • Re: [java.nio] Blocking mode for a while - How ?
    ... if your channel inherits from an AbstractSelectableChannel (all ... Object mutex = channel.blockingLock ... >making readiness selection on it. ... >with clients channels with blocking mode... ...
    (comp.lang.java.programmer)
  • Re: about serial port communication and new operator
    ... It is unreliable to assume that you will receive a fixed number of bytes from ReadFile. ... if settings are wrong or the cable is disconnected a blocking call willl cause your entire program to lock up. ... It is best to use overlapped reads with timeouts, and buffer whatever number of bytes you receive, and reassemble and validate messages from multiple calls in your buffer. ... This article and associated sample code do serial communication well: ...
    (microsoft.public.vc.mfc)
  • Re: Python & Go
    ... routine might vary its execution thread depending on which threads are ... value from more than one go routine. ... You can declare and pass around channel variables. ... To write to a channel (blocking if it is full): ...
    (comp.lang.python)