home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / devddemo.zip / LOCK.C < prev    next >
Text File  |  1991-01-31  |  6KB  |  99 lines

  1. /****************************************************************************/
  2. /* Routines for locking and unlocking segments                              */
  3. /****************************************************************************/
  4. /* Lock a segment                                                           */
  5. /*                                                                          */
  6. /* long lock(sel)                                                           */
  7. /* unsigned sel; - Selector to segment to lock down                         */
  8. /*                                                                          */
  9. /* Return value - Lock handle of segment, 0 if the lock failed              */
  10. /*                                                                          */
  11. /* This function requests OS/2 lock the segment.  The request is for a      */
  12. /* short term lock.  THis is done because a long term lock causes OS/2 to   */
  13. /* swap something out, degrading performance to an unacceptable level.  The */
  14. /* short term lock doesn't seem to do that.  The function returns the 'lock */
  15. /* handle' that is needed to unlock the segment later.  It returns a lock   */
  16. /* handle of 0 if the lock fails.  It also increments the variable          */
  17. /* lock_count.  This is a useful debug tool as the single most common cause */
  18. /* of the dreaded "Internal Error Detected at.."  message is an error in    */
  19. /* lock/unlock.  Either we are passing an invalid lock handle to UNLOCK or  */
  20. /* we locked down too much without unlocking.  This counter will be very    */
  21. /* high (on the order of 0x180 or more if the latter is the case.           */
  22. /*                                                                          */
  23. /****************************************************************************/
  24. /* Unlock a segment                                                         */
  25. /*                                                                          */
  26. /* unsigned unlock(handle)                                                  */
  27. /* long handle; - Lock handle of segment to unlock                          */
  28. /*                                                                          */
  29. /* Return value - SUCCESS or -1 if the unlock failed                        */
  30. /*                                                                          */
  31. /* This function requests OS/2 unlock a previously locked segment.  If the  */
  32. /* handle passed is 0, the function just returns SUCCESS.  This lets the DD */
  33. /* unlock a segment and store 0 in the place of the lock handle.  Later, if */
  34. /* it tries to unlock the same segment again, the function will recognize   */
  35. /* this fact and act as if the unlock was successful.  This lets the unlock */
  36. /* activity use a small number of common routines.                          */
  37. /*                                                                          */
  38. /* The function also decrements the lock_count variable and sets in_unlock  */
  39. /* to TRUE while the device helper routine is being called.  This is        */
  40. /* another debug facility to help detect when a bad lock handle is passed.  */
  41. /*                                                                          */
  42. /****************************************************************************/
  43. #include <stdio.h>
  44. #include "demo.h"
  45.  
  46. boolean near in_unlock = FALSE;  /* in_unlock starts out FALSE */
  47. word    near lock_count = 0;     /* lock_count starts out 0    */
  48.  
  49. unsigned long far lock(sel)
  50. word sel;
  51. {
  52.   physaddr handle;
  53.   union cpu_regs in_regs;
  54.   union cpu_regs out_regs;
  55.  
  56.   in_regs.W.AX = sel;                              /* AX holds the selector  */
  57.   in_regs.B.BH = 0;                                /* Short term block       */
  58.   in_regs.B.BL = 0;                                /* Block - I'll wait      */
  59.   in_regs.W.es_valid = FALSE;                      /* Use current ES and DS  */
  60.   in_regs.W.ds_valid = FALSE;
  61.   in_regs.B.DL = devhlp_Lock;                      /* Do it                  */
  62.   dev_help(&in_regs,&out_regs);
  63.   if ((out_regs.W.flags & 0x0001) != 0) {          /* If it failed           */
  64.      return(0L);                                   /*    Return 0            */
  65.      }                                             /* Endif                  */
  66.   lock_count++;                                    /* Increment lock count   */
  67.   handle = (out_regs.W.AX * 0x10000) + out_regs.W.BX;  /* Build lock handle  */
  68.   return(handle);                                  /* And return it to caller*/
  69. }
  70.  
  71. word far unlock(han)
  72. unsigned long han;
  73. {
  74.   union cpu_regs in_regs;
  75.   union cpu_regs out_regs;
  76.   _32bits        handle;
  77.  
  78.  
  79.      /* If the lock handle is 0, already unlocked, just say we did it */
  80.      handle.phys = han;
  81.      if (handle.phys == 0L) { return(SUCCESS); }
  82.  
  83.      in_regs.W.AX = handle._2words.high;       /* AX:BX hold the lock handle */
  84.      in_regs.W.BX = handle._2words.low;
  85.      in_regs.W.es_valid = FALSE;               /* Use current ES and DS      */
  86.      in_regs.W.ds_valid = FALSE;
  87.      in_unlock = TRUE;                         /* Mark that we are in UNLOCK */
  88.      in_regs.B.DL = devhlp_Unlock;             /* Do it                      */
  89.      dev_help(&in_regs,&out_regs);
  90.      in_unlock = FALSE;                        /* Done unlocking             */
  91.      if ((out_regs.W.flags & 0x0001) != 0) {   /* If it failed               */
  92.         return(-1);                            /*     Return -1              */
  93.         }                                      /* Endif
  94.      lock_count--;                             /* Decrement lock count       */
  95.  
  96.      return(SUCCESS);                          /* Return SUCCESS             */
  97. }
  98.  
  99.