home *** CD-ROM | disk | FTP | other *** search
/ PC Press 1997 July / Sezamfile97_1.iso / msdos / c / cbase11.a03 / CBASE11.ZIP / LSEQ / LSLOCK.C < prev    next >
C/C++ Source or Header  |  1993-01-01  |  6KB  |  263 lines

  1. /*
  2.  *    Copyright (c) 1989-1992 Citadel Software, Inc.
  3.  *    All Rights Reserved
  4.  */
  5.  
  6. /* #ident    "@(#)lslock.c    1.7 - 93/01/01" */
  7.  
  8. #include <port.h>
  9.  
  10. /* standard headers */
  11. #include <errno.h>
  12.  
  13. /* library headers */
  14. #include <blkio.h>
  15.  
  16. /* local headers */
  17. #include "lseq_.h"
  18.  
  19. /* function declarations */
  20. #ifdef AC_PROTO
  21. static int resync(lseq_t *lsp);
  22. #else
  23. static int resync();
  24. #endif
  25.  
  26. /*man---------------------------------------------------------------------------
  27. NAME
  28.      lslock - lseq lock
  29.  
  30. SYNOPSIS
  31.      #include <lseq.h>
  32.  
  33.      int lslock(lsp, ltype)
  34.      lseq_t *lsp;
  35.      int ltype;
  36.  
  37. DESCRIPTION
  38.      The lslock function controls the lock status of an lseq.  The lsp
  39.      argument is an open lseq.  ltype indicates the target status of
  40.      the lock on the lseq.
  41.  
  42.      The lock types available are:
  43.  
  44.           LS_UNLCK - unlock lseq
  45.           LS_RDLCK - lock lseq for reading
  46.           LS_WRLCK - lock lseq for reading and writing
  47.           LS_RDLKW - lock lseq for reading (wait)
  48.           LS_WRLKW - lock lseq for reading and writing (wait)
  49.  
  50.      For the lock types which wait, lslock will not return until the
  51.      lock is available.  For the lock types which do not wait, if the
  52.      lock is unavailable because of a lock held by another process  a
  53.      value of -1 is returned and errno set to EAGAIN.
  54.  
  55.      When an lseq is unlocked, its cursor is set to null.
  56.  
  57.      lslock will fail if one or more of the following is true:
  58.  
  59.      [EAGAIN]       ltype is LS_RDLCK and lsp is already
  60.                     write locked by another process, or
  61.                     ltype is LS_WRLCK and lsp is already
  62.                     read or write locked by another process.
  63.      [EINVAL]       lsp is is not a valid lseq pointer.
  64.      [EINVAL]       ltype is not one of the valid lock
  65.                     types.
  66.      [LSENOPEN]     lsp is not open.
  67.      [LSENOPEN]     ltype is LS_RDLCK or LS_RDLKW and lsp
  68.                     is not opened for reading or ltype is
  69.                     LS_WRLCK or LS_WRLKW and lsp is not open
  70.                     for writing.
  71.  
  72. SEE ALSO
  73.      lsgetlck.
  74.  
  75. DIAGNOSTICS
  76.      Upon successful completion, a value of 0 is returned.  Otherwise,
  77.      a value of -1 is returned, and errno set to indicate the error.
  78.  
  79. ------------------------------------------------------------------------------*/
  80. #ifdef AC_PROTO
  81. int lslock(lseq_t *lsp, int ltype)
  82. #else
  83. int lslock(lsp, ltype)
  84. lseq_t *lsp;
  85. int ltype;
  86. #endif
  87. {
  88.     int    bltype    = 0;    /* blkio lock type */
  89.  
  90.     /* validate arguments */
  91.     if (!ls_valid(lsp)) {
  92.         errno = EINVAL;
  93.         return -1;
  94.     }
  95.  
  96.     /* check if lseq not open */
  97.     if (!(lsp->flags & LSOPEN)) {
  98.         errno = LSENOPEN;
  99.         return -1;
  100.     }
  101.  
  102.     /* check if lseq not open for lock ltype */
  103.     switch (ltype) {
  104.     case LS_UNLCK:
  105.         /* if previously write-locked, reset modify bit to pre-lock state */
  106.         if (lsp->flags & LSWRLCK) {
  107.             if (!lsp->flags & LSMOD) {
  108.                 lsp->lshdr.flags &= ~LSHMOD;
  109.             }
  110.             if (bputhf(lsp->bp, sizeof(bpos_t),
  111.                 (void *)((char *)&lsp->lshdr + sizeof(bpos_t)),
  112.                 sizeof(lshdr_t) - sizeof(bpos_t)
  113.             ) == -1) {
  114.                 LSERRLOG;
  115.                 return -1;
  116.             }
  117.             if (bsync(lsp->bp) == -1) {
  118.                 LSERRLOG;
  119.                 return -1;
  120.             }
  121.         }
  122.         lsp->clspos = NIL;    /* set cursor to null */
  123.         bltype = B_UNLCK;
  124.         break;
  125.     case LS_RDLCK:
  126.         if (!(lsp->flags & LSREAD)) {
  127.             errno = LSENOPEN;
  128.             return -1;
  129.         }
  130.         bltype = B_RDLCK;
  131.         break;
  132.     case LS_RDLKW:
  133.         if (!(lsp->flags & LSREAD)) {
  134.             errno = LSENOPEN;
  135.             return -1;
  136.         }
  137.         bltype = B_RDLKW;
  138.         break;
  139.     case LS_WRLCK:
  140.         if (!(lsp->flags & LSWRITE)) {
  141.             errno = LSENOPEN;
  142.             return -1;
  143.         }
  144.         bltype = B_WRLCK;
  145.         break;
  146.     case LS_WRLKW:
  147.         if (!(lsp->flags & LSWRITE)) {
  148.             errno = LSENOPEN;
  149.             return -1;
  150.         }
  151.         bltype = B_WRLKW;
  152.         break;
  153.     default:
  154.         errno = EINVAL;
  155.         return -1;
  156.         break;
  157.     }
  158.  
  159.     /* lock lseq file */
  160.     if (lockb(lsp->bp, bltype, (bpos_t)0, (bpos_t)0) == -1) {
  161. #ifdef DEBUG
  162.         if (errno != EAGAIN) LSERRLOG;
  163. #endif
  164.         return -1;
  165.     }
  166.  
  167.     /* set status bits in lseq control structure */
  168.     switch (ltype) {
  169.     case LS_UNLCK:
  170.         lsp->flags &= ~LSLOCKS;
  171.         break;
  172.     case LS_RDLCK:
  173.     case LS_RDLKW:
  174.         /* if previously unlocked, re-sync with file */
  175.         if (!(lsp->flags & LSLOCKS)) {
  176.             if (resync(lsp) == -1) {
  177.                 LSERRLOG;
  178.                 return -1;
  179.             }
  180.         }
  181.         lsp->flags |= LSRDLCK;
  182.         lsp->flags &= ~LSWRLCK;
  183.         break;
  184.     case LS_WRLCK:
  185.     case LS_WRLKW:
  186.         /* if previously unlocked, re-sync with file */
  187.         if (!(lsp->flags & LSLOCKS)) {
  188.             if (resync(lsp) == -1) {
  189.                 LSERRLOG;
  190.                 return -1;
  191.             }
  192.         }
  193.         /* set modify bit in header */
  194.         lsp->lshdr.flags |= LSHMOD;
  195.         if (bputhf(lsp->bp, sizeof(bpos_t),
  196.             (void *)((char *)&lsp->lshdr + sizeof(bpos_t)),
  197.             sizeof(lshdr_t) - sizeof(bpos_t)
  198.         ) == -1) {
  199.             LSERRLOG;
  200.             return -1;
  201.         }
  202.         if (bsync(lsp->bp) == -1) {
  203.             LSERRLOG;
  204.             return -1;
  205.         }
  206.         lsp->flags |= (LSRDLCK | LSWRLCK);
  207.         break;
  208.     default:
  209.         LSERRLOG;
  210.         errno = LSEFATAL;
  211.         return -1;
  212.         break;
  213.     }
  214.  
  215.     return 0;
  216. }
  217.  
  218. /* resync:  re-synchronize with file */
  219. #ifdef AC_PROTO
  220. static int resync(lseq_t *lsp)
  221. #else
  222. static int resync(lsp)
  223. lseq_t *lsp;
  224. #endif
  225. {
  226.     size_t    oldrecsize    = lsp->lshdr.recsize;    /* record size */
  227.     int    terrno        = 0;            /* tmp errno */
  228.  
  229.     /* read in header */
  230.     if (bgeth(lsp->bp, &lsp->lshdr) == -1) {
  231.         LSERRLOG;
  232.         return -1;
  233.     }
  234.  
  235.     /* set LSMOD according to LSHMOD */
  236.     if (lsp->lshdr.flags & LSHMOD) {
  237.         lsp->flags |= LSMOD;
  238.     } else {
  239.         lsp->flags &= ~LSMOD;
  240.     }
  241.  
  242.     /* if first time locked, do setup */
  243.     if (oldrecsize == 0) {
  244.         /* allocate memory for lseq */
  245.         if (ls_alloc(lsp) == -1) {
  246.             LSERRLOG;
  247.             lsp->lshdr.recsize = 0;
  248.             return -1;
  249.         }
  250.         /* set up buffering */
  251.         if (bsetvbuf(lsp->bp, NULL, ls_blksize(lsp), LSBUFCNT) == -1) {
  252.             LSERRLOG;
  253.             terrno = errno;
  254.             ls_free(lsp);
  255.             lsp->lshdr.recsize = 0;
  256.             errno = terrno;
  257.             return -1;
  258.         }
  259.     }
  260.  
  261.     return 0;
  262. }
  263.