Go to the first, previous, next, last section, table of contents.


|readAnyStreamPacket

Function: |readAnyStreamPacket { [streams] nf millisecs -> [values] tag who s }
file: job.t
package: muf
status: alpha

This is Muq's closest analogue to the C/Unix select() call: It waits until input is available on one of the given streams and returns a block from that stream.

The [streams] argument must be a block of streams. There may currently be at most thirteen streams in the block.

The nf argument is same as the noFragments readStreamPacket[ argument.

The millisecs argument must be either nil or a positive (not zero!) integer limit on the number of milliseconds which the job should wait for input to arrive. If this limit is exceeded, the job will return [ | nil nil nil. (Zero millisecs values are disallowed in order to discourage waste of CPU resources in busy-wait loops.)

The [values] tag and who return values are as in readStreamPacket[.

The s return value is the particular stream from which the packet was read, or nil if the call timed out before input arrived. (The logically separate operations of finding a nonempty stream and reading from it are combined into a single operation in order to avoid race events -- if they were separate, some other job might empty the stream between the two operations.)

Example. Here is a simple implementation of sleepJob based on |readAnyStreamPacket:

:   my-sleep-job { $ -> } -> millisecs
    [ | t millisecs
    |readAnyStreamPacket
    pop pop pop ]pop
;

Example. Here is a loop that merges two input streams into one output stream:

:   merge-streams { $ $ $ -> @ } -> outstream -> instream2 -> instream1
    do{
        ( Read a block from either input stream: )
        [ instream1 instream2 | t nil
            |readAnyStreamPacket
            -> stream
            -> who
            -> tag

            ( Write the block to output stream: )
	    tag t outstream
	    |writeStreamPacket

	    ( Pop the block and continue: )
            pop pop
        ]pop
    }
;

See section readStreamPacket[.


Go to the first, previous, next, last section, table of contents.