home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / pc / c / cbase.zoo / lseq101.zoo / lslock.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-20  |  4.2 KB  |  180 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)lslock.c    1.4 - 90/06/20" */
  5.  
  6. /* ansi headers */
  7. #include <errno.h>
  8.  
  9. /* library headers */
  10. #include <blkio.h>
  11.  
  12. /* local headers */
  13. #include "lseq_.h"
  14.  
  15. /*man---------------------------------------------------------------------------
  16. NAME
  17.      lslock - lseq lock
  18.  
  19. SYNOPSIS
  20.      #include <lseq.h>
  21.  
  22.      int lslock(lsp, ltype)
  23.      lseq_t *lsp;
  24.      int ltype;
  25.  
  26. DESCRIPTION
  27.      The lslock function controls the lock status of an lseq.  The lsp
  28.      argument is an open lseq.  ltype indicates the target status of
  29.      the lock on the lseq.
  30.  
  31.      The lock types available are:
  32.  
  33.           LS_UNLCK - unlock lseq
  34.           LS_RDLCK - lock lseq for reading
  35.           LS_WRLCK - lock lseq for reading and writing
  36.           LS_RDLKW - lock lseq for reading (wait)
  37.           LS_WRLKW - lock lseq for reading and writing (wait)
  38.  
  39.      For the lock types which wait, lslock will not return until the
  40.      lock is available.  For the lock types which do not wait, if the
  41.      lock is unavailable because of a lock held by another process  a
  42.      value of -1 is returned and errno set to EAGAIN.
  43.  
  44.      When an lseq is unlocked, its cursor is set to null.
  45.  
  46.      lslock will fail if one or more of the following is true:
  47.  
  48.      [EAGAIN]       ltype is LS_RDLCK and lsp is already
  49.                     write locked by another process, or
  50.                     ltype is LS_WRLCK and lsp is already
  51.                     read or write locked by another process.
  52.      [EINVAL]       lsp is is not a valid lseq pointer.
  53.      [EINVAL]       ltype is not one of the valid lock
  54.                     types.
  55.      [LSECORRUPT]   lsp is corrupt.
  56.      [LSENOPEN]     lsp is not open.
  57.      [LSENOPEN]     ltype is LS_RDLCK or LS_RDLKW and lsp
  58.                     is not opened for reading or ltype is
  59.                     LS_WRLCK or LS_WRLKW and lsp is not open
  60.                     for writing.
  61.  
  62. SEE ALSO
  63.      lsgetlck.
  64.  
  65. DIAGNOSTICS
  66.      Upon successful completion, a value of 0 is returned.  Otherwise,
  67.      a value of -1 is returned, and errno set to indicate the error.
  68.  
  69. ------------------------------------------------------------------------------*/
  70. int lslock(lsp, ltype)
  71. lseq_t *lsp;
  72. int ltype;
  73. {
  74.     int bltype = 0;    /* blkio lock type */
  75.  
  76.     /* validate arguments */
  77.     if (!ls_valid(lsp)) {
  78.         errno = EINVAL;
  79.         return -1;
  80.     }
  81.  
  82.     /* check if lseq not open */
  83.     if (!(lsp->flags & LSOPEN)) {
  84.         errno = LSENOPEN;
  85.         return -1;
  86.     }
  87.  
  88.     /* check if lseq not open for lock ltype */
  89.     switch (ltype) {
  90.     case LS_UNLCK:
  91.         lsp->clspos = NIL;    /* set cursor to null */
  92.         bltype = B_UNLCK;
  93.         break;
  94.     case LS_RDLCK:
  95.         if (!(lsp->flags & LSREAD)) {
  96.             errno = LSENOPEN;
  97.             return -1;
  98.         }
  99.         bltype = B_RDLCK;
  100.         break;
  101.     case LS_RDLKW:
  102.         if (!(lsp->flags & LSREAD)) {
  103.             errno = LSENOPEN;
  104.             return -1;
  105.         }
  106.         bltype = B_RDLKW;
  107.         break;
  108.     case LS_WRLCK:
  109.         if (!(lsp->flags & LSWRITE)) {
  110.             errno = LSENOPEN;
  111.             return -1;
  112.         }
  113.         bltype = B_WRLCK;
  114.         break;
  115.     case LS_WRLKW:
  116.         if (!(lsp->flags & LSWRITE)) {
  117.             errno = LSENOPEN;
  118.             return -1;
  119.         }
  120.         bltype = B_WRLKW;
  121.         break;
  122.     default:
  123.         errno = EINVAL;
  124.         return -1;
  125.         break;
  126.     }
  127.  
  128.     /* lock lseq file */
  129.     if (lockb(lsp->bp, bltype, (bpos_t)0, (bpos_t)0) == -1) {
  130.         if (errno != EAGAIN) LSEPRINT;
  131.         return -1;
  132.     }
  133.  
  134.     /* set status bits in lseq control structure */
  135.     switch (ltype) {
  136.     case LS_UNLCK:
  137.         lsp->flags &= ~LSLOCKS;
  138.         break;
  139.     case LS_RDLCK:
  140.     case LS_RDLKW:
  141.         /* if previously unlocked, read header */
  142.         if (!(lsp->flags & LSLOCKS)) {
  143.             if (bgeth(lsp->bp, &lsp->lshdr) == -1) {
  144.                 LSEPRINT;
  145.                 return -1;
  146.             }
  147.             if (lsp->lshdr.flags & LSHMOD) {
  148.                 errno = LSECORRUPT;
  149.                 return -1;
  150.             }
  151.         }
  152.         lsp->flags |= LSRDLCK;
  153.         lsp->flags &= ~LSWRLCK;
  154.         break;
  155.     case LS_WRLCK:
  156.     case LS_WRLKW:
  157.         /* if previously unlocked, read header */
  158.         if (!(lsp->flags & LSLOCKS)) {
  159.             if (bgeth(lsp->bp, &lsp->lshdr) == -1) {
  160.                 LSEPRINT;
  161.                 return -1;
  162.             }
  163.             if (lsp->lshdr.flags & LSHMOD) {
  164.                 errno = LSECORRUPT;
  165.                 return -1;
  166.             }
  167.         }
  168.         lsp->flags |= (LSRDLCK | LSWRLCK);
  169.         break;
  170.     default:
  171.         LSEPRINT;
  172.         errno = LSEPANIC;
  173.         return -1;
  174.         break;
  175.     }
  176.  
  177.     errno = 0;
  178.     return 0;
  179. }
  180.