home *** CD-ROM | disk | FTP | other *** search
/ Aminet 10 / aminetcdnumber101996.iso / Aminet / util / misc / fifolib38_1.lha / fifolib.doc < prev    next >
Text File  |  1995-12-20  |  13KB  |  386 lines

  1.  
  2.                   FIFO.LIBRARY
  3.  
  4.                MANUAL, VERSION 3.2
  5.  
  6. By: Matthew Dillon
  7.  
  8.  
  9. fifo.library/--FifoNotes--
  10. fifo.library/OpenFifo
  11. fifo.library/CloseFifo
  12. fifo.library/ReadFifo
  13. fifo.library/WriteFifo
  14. fifo.library/RequestFifo
  15. fifo.library/BufSizeFifo
  16.  
  17. fifo.library/--FifoNotes--                fifo.library/--FifoNotes--
  18.  
  19.     FIFO.LIBRARY is a general fifo library implementation with the following
  20.     features:
  21.  
  22.     * named fifos
  23.     * support for writing to a fifo from a hardware exception
  24.     * multiple readers on a fifo supported, each get the same data
  25.       stream
  26.     * efficient reading (no buffer copies on read)
  27.     * automatic or manual (via message passing) flow control
  28.  
  29.     The uses for this library are limitless.  To use the library, open
  30.     it via OpenLibrary("fifo.library", 0);, the base variable is named:
  31.  
  32.     FifoBase
  33.  
  34.     Please refer to the test program for an example of usage.
  35.  
  36.     LINK LIBRARIES:    fifo.lib    standard amiga (join type) library,
  37.                     large-data model
  38.  
  39.             fifos.lib   standard amiga (joing type) library,
  40.                     small-data model
  41.  
  42.  
  43. fifo.library/OpenFifo                      fifo.library/OpenFifo
  44.  
  45.     NAME
  46.     OpenFifo - open a rendezvous FIFO by name
  47.  
  48.     SYNOPSIS
  49.     fifo = OpenFifo(name, bufsize, flags)
  50.     D0         D0    D1     A0
  51.  
  52.     void *fifo;    /*  returned fifo handle        */
  53.     char *name;    /*  rendezvous name - case sensitive    */
  54.     long bufsize;    /*  must be a power of 2        */
  55.     long flags;    /*  open flags                */
  56.  
  57.     FUNCTION
  58.     OpenFifo() creates a FIFO if <name> does not exist, else
  59.     references an already existing FIFO named <name>.  OpenFifo()
  60.     returns a fifo handle to be used for further operations.
  61.  
  62.     bufsize must be a power of 2.  For example, 1024.
  63.  
  64.     NOTE: two OpenFifo()s opening the same fifo are NOT guaranteed
  65.     to return the same handle, even though they reference the same
  66.     FIFO.
  67.  
  68.     FLAGS
  69.     FIFOF_READ
  70.         file handle intended to be used for reading, do not specify
  71.         unless you read from the handle.
  72.  
  73.     FIFOF_WRITE
  74.         file handle intended to be used for writing, do not specify
  75.         unless you write to the handle.
  76.  
  77.         If a previous CloseFifo() activated FIFOF_EOF, then the flag is
  78.         cleared.
  79.  
  80.     FIFOF_NORMAL
  81.         normal (task based) calls to library, else is assumed calls
  82.         will be made from an interrupt or hardware exception.
  83.  
  84.         NOTE: non-normal fifo writes are unable to wake up blocked
  85.         library calls.  Thus, anybody reading a fifo written to by
  86.         an interrupt or exception (read: enforcer), must poll the
  87.         fifo.  Eventually automatic polling will be an option to make
  88.         operation with non-normal writers transparent to the reader.
  89.  
  90.     FIFOF_KEEPIFD
  91.         When this FIFO is eventually closed, do not destroy it if
  92.         unread data is pending.  Allows a 'writer' to completely finish
  93.         and exit before a read even has the chance to open the fifo.
  94.  
  95.     FIFOF_RREQUIRED
  96.         Cause any write operations to fail when there are no readers
  97.         (i.e. broken pipe), prevents a lockup from occurring if the
  98.         reader is ^C'd.
  99.  
  100.     FIFOF_NBIO
  101.         Specify that ReadFifo() and WriteFifo() calls are not to block.
  102.  
  103.     BUGS
  104.     FIFOF_EOF is not cleared. EOF is received continuously.
  105.  
  106.  
  107. fifo.library/CloseFifo                      fifo.library/CloseFifo
  108.  
  109.     NAME
  110.     CloseFifo - close a previously opened FIFO
  111.  
  112.     SYNOPSIS
  113.     void CloseFifo(fifo, flags)
  114.             D0     D1
  115.  
  116.     void *fifo;    /*  fifo handle previously returned by OpenFifo() */
  117.     long flags;    /*  see below    */
  118.  
  119.     FUNCTION
  120.     CloseFifo() is the reverse of OpenFifo(), dereferencing a Fifo.
  121.  
  122.     The last close of a shared fifo will result in the deletion of that
  123.     fifo ONLY if all data has been read, allowing unsynchronized
  124.     programs to use the Fifo.
  125.  
  126.     A future flag will cause the last close of a FIFO to delete the
  127.     fifo and throw away any data.
  128.  
  129.     FLAGS
  130.  
  131.     FIFOF_EOF
  132.         If specified, the FIFO will generate an EOF to readers
  133.         after the last writer has closed.  This particular handle
  134.         need not be the last writer.
  135.  
  136.         If not specified, any active readers are left hanging
  137.         (allowing another program to 'take up' where this one left
  138.         off without the readers knowing)
  139.  
  140.         NOTE that any new writer that OpenFifo()s the fifo will
  141.         clear this flag.
  142.  
  143.         NOTE that the EOF condition will be returned only once
  144.         to a given reader, then cleared (causing the reader to
  145.         block again).  This is because shells generally ignore
  146.         EOF and we would otherwise get into an infinite loop.
  147.  
  148.         THIS FLAG HAS NO EFFECT IF THE HANDLE WAS NOT ORIGINALLY
  149.         OPENED FIFOF_WRITE.
  150.  
  151.     BUGS
  152.     The EOF condition is not given once only, but continously (probably
  153.     in an attempt to stop programs), a future flag may address this and
  154.     provide both possibilities.
  155.  
  156.  
  157. fifo.library/ReadFifo                      fifo.library/ReadFifo
  158.  
  159.     NAME
  160.     ReadFifo -  read data from a FIFO
  161.  
  162.     SYNOPSIS
  163.     long ready = ReadFifo(fifo, pptr, skip)
  164.            D0           D0    D1   A0
  165.  
  166.     void *fifo;    /*  fifo handle previously returned by OpenFifo() */
  167.     char **pptr;    /*  address of pointer to store buffer pointer      */
  168.     long skip;    /*  # of bytes to skip (delete)     */
  169.     long ready;    /*  # of bytes available to read, -1 = EOF    */
  170.  
  171.     WARNING
  172.     ReadFifo() uses UNCONVENTIONAL ARGUMENTS
  173.  
  174.     FUNCTION
  175.     ReadFifo() first deletes (skip) bytes from the FIFO, then sets a
  176.     pointer into the fifo's buffer and returns the number of bytes
  177.     immediately available for reading.
  178.  
  179.     THE DATA POINTER TO MAY NOT BE MODIFIED UNDER ANY CIRCUMSTANCES
  180.  
  181.     The call is efficient because, in many cases, a buffer copy can be
  182.     completely avoided by the program.  The loop code in the example
  183.     below shows generally how operation works.  For further
  184.     information, refer to the test program (TEST.C).
  185.  
  186.     The number of bytes available (ready), is independent of the
  187.     number of bytes you threw away (skip).    You get a certain amount
  188.     of lookahead for free, but it should be stressed that the (ready)
  189.     value is not necessarily all the available data.  Data in the
  190.     fifo is not always contiguous and so there may be more data
  191.     available than you can immediately determine.  For example, the
  192.     call might return 1 when in fact 500 bytes are available.  But
  193.     only 1 byte is available contiguously (based at the pointer).
  194.     After processing the 1 byte your next read would return 499.
  195.  
  196.     NON-BLOCKING IO
  197.     If FIFOF_NBIO was specified on open, this call will return
  198.     immediately and 0 is a valid return.  If 0 is returned there
  199.     is no data ready and the program must poll the fifo.  But
  200.     wait, there is a better way to do it ... instead of polling
  201.     the fifo you can opt to have a message sent to you when data
  202.     becomes available.  see the RequestFifo() function call and
  203.     look at TEST.C
  204.  
  205.     If FIFOF_NBIO is *not* specified, then 0 will never be returned
  206.     by this call.  Instead, either >0 or -1 (EOF) will be returned.
  207.  
  208.     MULTIPLE-READERS
  209.  
  210.     Multiple readers may exist for a given FIFO.  This is completely
  211.     supported.  If you have one writer and two readers, and the
  212.     writer writes "ABC", then both readers will each read "ABC".
  213.     Because the FIFO buffer is shared, readers may NOT modify any
  214.     data returned in (pptr) because this could effect other readers
  215.     (depending on whether they have already read the data or not).
  216.  
  217.     INTERRUPTS / EXCEPTIONS
  218.  
  219.     ReadFifo() may not be called from an interrupt or a hardware
  220.     exception.  ReadFifo() may ONLY be called from task or process.
  221.  
  222.     EXAMPLE
  223.  
  224.     /*
  225.      *  dump fifo ff.  FIFO was opened without FIFOF_NBIO.
  226.      */
  227.  
  228.     {
  229.         long i = 0;         /*    delete nothing on first loop  */
  230.         long n;
  231.         char *ptr;
  232.  
  233.         while ((n = ReadFifo(ff, &ptr, i)) > 0) {
  234.         if (n > 64)        /*    can easily limit read-size if you */
  235.             n = 64;        /*    want to (optional, obviously)      */
  236.         i = n;            /*    drain this many on next loop    */
  237.         write(1, ptr, n);
  238.         }
  239.         /*
  240.          *    note, if you break out of the loop you may need a final
  241.          *    ReadFifo(ff, &ptr, i); to clear out what you have already
  242.          *    processed.
  243.          */
  244.     }
  245.  
  246.  
  247. fifo.library/WriteFifo                      fifo.library/WriteFifo
  248.  
  249.     NAME
  250.     WriteFifo - write data to a FIFO
  251.  
  252.     SYNOPSIS
  253.     long actual = WriteFifo(fifo, buf, len)
  254.            D0        D0    D1   A0
  255.  
  256.     void *fifo;    /*  fifo handle previously returned by OpenFifo() */
  257.     void *buf;    /*  buffer to read data into    */
  258.     long max;    /*  maximum # of bytes to read    */
  259.  
  260.     FUNCTION
  261.     WriteFifo() writes data to a FIFO.  WriteFifo() will either return
  262.     actual = len if the write succeeded, 0 if the FIFO is full, or -1
  263.     if an error occurs.  No intermediate values are currently returned
  264.     (i.e. writes are atomic)
  265.  
  266.     If two writers attempt to write to the same FIFO at the same time
  267.     and the FIFOF_NORMAL flag was not specified on open (i.e. writer
  268.     writes from an interrupt or exception), then it is possible for
  269.     a collision to occur in which case one of the calls will return
  270.     an error (-1).
  271.  
  272.     WARNING WARNING
  273.     You may not write blocks larger than 1/2 the fifo buffer size,
  274.     this will generate an immediate error (-2) regardless of how
  275.     much space is actually available.  The reason for this is simply
  276.     that writes are guarenteed to be atomic, and cannot be guarenteed
  277.     if larger blocks are written.  Also, flow control breaks down
  278.     with larger blocks so I figure it is best to nip it in the bud and
  279.     return a formal error.
  280.  
  281.     NON-BLOCKING-IO (FIFOF_NORMAL fifos only)
  282.     Writes are atomic.  If a write fails and FIFOF_NBIO was specified
  283.     on open, the write will return with a failure code of -1.  You
  284.     may optionally poll or use the RequestFifo() call.
  285.  
  286.     Note that even if RequestFifo() returns the message, a write may
  287.     fail due to other processes writing to the fifo before you have
  288.     a chance to, in which case you have to RequestFifo() again and
  289.     loop.
  290.  
  291.     If FIFOF_NBIO was not specified on open, then the write will not
  292.     return until the data has been successfully written, or -2 will
  293.     be returned if you specified too large a buffer.
  294.  
  295.     INTERRUPTS/EXCEPTIONS
  296.     To write to a fifo from an interrupt or exception you must open the
  297.     fifo WITHOUT the FIFOF_NORMAL flag, and be prepared to handle
  298.     possible collisions with other writers.
  299.  
  300.     In this case a failure code of -1 could mean either that the FIFO
  301.     is full or that a collision has occured and you were the side that
  302.     lost out.
  303.  
  304.     WARNING:  any writes via a fifo not opened with the FIFOF_NORMAL
  305.     flag will *NOT* wakeup anybody blocked waiting to read from the
  306.     fifo, whether by RequestFifo() (the message is not returned),
  307.     or via normal opens (without the FIFOF_NBIO flag).  Readers must
  308.     POLL in this case.
  309.  
  310. fifo.library/RequestFifo                fifo.library/RequestFifo
  311.  
  312.     NAME
  313.     RequestFifo - request this message be returned when read data is
  314.               available or write buffer space is available.
  315.  
  316.  
  317.     SYNOPSIS
  318.     RequestFifo(fifo, msg, req)
  319.              D0   D1    A0
  320.  
  321.     void *fifo;
  322.     struct Message *msg;    /*  message in question    */
  323.     long req;        /*  request            */
  324.  
  325.     FUNCTION
  326.     Normally this call is used to queue a message to the fifo.library
  327.     that will be ReplyMsg()d to your task when the specified condition
  328.     occurs.  There are three possible requests.
  329.  
  330.     FREQ_RPEND
  331.         Request that the specified message be returned when read data
  332.         is pending on the fifo.
  333.  
  334.         note: if data is immediately available, the message will be
  335.         immediately replied.  If an EOF condition exists or occurs the
  336.         message will also be replied.
  337.  
  338.     FREQ_WAVAIL
  339.         Request that the specified message be returned when the fifo is
  340.         more than half empty.  It is possible that an attempt to write
  341.         after getting your message back will fail due to other writers
  342.         getting their writes in ahead of you.
  343.  
  344.     FREQ_ABORT
  345.         Request that this message, which was previously queued via
  346.         FREQ_RPEND or FREQ_WAVAIL, be returned immediately.
  347.  
  348.         May be called even if message has already been returned but
  349.         not processed.
  350.  
  351.     You may queue multiple messages, even several messages all for the
  352.     same thing.  When you queue a message, it's node becomes owned by
  353.     the library until replied.  You CANNOT queue the same message
  354.     multiple times at once.  Generally programmers will queue one
  355.     message for write-avail-space and another message for
  356.     read-data-ready, then poll the fifo when these messages are
  357.     returned and re-queue the message(s).
  358.  
  359.     ReadFifo() and WriteFifo() call RequestFifo() internally when the
  360.     FIFOF_NBIO flag is NOT used.  This is how they wait for events when
  361.     a call blocks due to lack of data or lack of buffer space.
  362.  
  363.  
  364. fifo.library/BufSizeFifo                fifo.library/BufSizeFifo
  365.  
  366.     NAME
  367.     BufSizeFifo - return size of fifo's buffer
  368.  
  369.     SYNOPSIS
  370.     long bytes = BufSizeFifo(fifo)
  371.            D0         D0
  372.  
  373.     void *fifo;
  374.  
  375.     FUNCTION
  376.     This function returns the size of the specified fifo's buffer.
  377.  
  378.     Note that you may not make WriteFifo() calls with lengths
  379.     larger than 1/2 the size of the fifo's buffer.  This function
  380.     allows you to determine the fifo's buffer size to properly
  381.     observe this limit.  You cannot depend on the buffer size you
  382.     specified in OpenFifo() because only the first OpenFifo() for
  383.     a given fifo actually sets the buffer size.
  384.  
  385.  
  386.