home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tolkit45.zip / os2tk45 / samples / rexx / semcls.cmd < prev    next >
OS/2 REXX Batch file  |  1999-05-11  |  14KB  |  224 lines

  1. /******************************************************************************/
  2. /*  semcls                   Object REXX Samples                              */
  3. /*                                                                            */
  4. /*  An Object REXX semaphore class.                                           */
  5. /*                                                                            */
  6. /* This file implements a semaphore class in Object REXX.  The class will be  */
  7. /* defined to the Global OREXX Environment.  The following methods are        */
  8. /* defined for this class:                                                    */
  9. /*   init - initialize a new semaphore.  Will accept the following positional */
  10. /*          parameters:                                                       */
  11. /*                             'name' - global name for this semaphore        */
  12. /*                                if named default to set name in             */
  13. /*                                the class semDirectory                      */
  14. /*                             noshare -  don't define named semaphore        */
  15. /*                                into class semDirectory                     */
  16. /*                             Inital state (0 or 1)                          */
  17. /*   setInitialState - Allow for subclass to have some post initialization,   */
  18. /*     and do setup based on inital state of semaphore.                       */
  19. /*   Waiting - number of objects waiting on this semaphore                    */
  20. /*   Shared - is this semaphore shared(Global)                                */
  21. /*   Named  - is this semaphore named                                         */
  22. /*   Name   - name of a named semaphore                                       */
  23. /*   setSem   - Set the semaphore, and return previous state                  */
  24. /*   resetSem - set state to unSet                                            */
  25. /*   querySem - return current state of semaphore                             */
  26. /*                                                                            */
  27. /* SemaphoreMeta - is the metaclass for the semaphore classes.  This class is */
  28. /*   setup so that when a namedSemaphore is shared, it maintains these        */
  29. /*   named/shared semaphores as part of its state.  These semaphores are      */
  30. /*   maintained in a directory, and an UNKNOWN method is installed on the     */
  31. /*   class to forward unknown messages to the directory.  In this way the     */
  32. /*   class can function as a class and a "like" a directory, so [] sytax may  */
  33. /*   be used to retrieve a semaphore from the class.                          */
  34. /*                                                                            */
  35. /*                                                                            */
  36. /* following are in the subclass EventSemaphore                               */
  37. /*                                                                            */
  38. /*   Post  - post this semaphore                                              */
  39. /*   Query - number of posts since the last reset                             */
  40. /*   Reset - reset the semaphore                                              */
  41. /*   Wait  - wait on this semaphore                                           */
  42. /*                                                                            */
  43. /*                                                                            */
  44. /* following are in the subclass MutexSemaphore                               */
  45. /*                                                                            */
  46. /*   requestMutex - get exclusive use of semaphore                            */
  47. /*   releaseMutex - release to allow someone else to use semaphore.           */
  48. /*        NOTE: currently anyone may issue release. not forced to be owner... */
  49. /******************************************************************************/
  50.  
  51. /* ========================================================================== */
  52. /* ===         Start of Semaphore class ......                            === */
  53. /* ========================================================================== */
  54. ::class SemaphoreMeta subclass class
  55. ::method init
  56.   expose semDict
  57.                                             /* be sure to initialize parent   */
  58.   .message~new(self, .array~of('INIT', super), 'a', arg(1,'a'))~send
  59.   semDict = .directory~new
  60.  
  61. ::method unknown
  62.   expose semDict
  63.   use arg msgName, args
  64.                                             /* forward all unknown messages to*/
  65.                                             /* the semaphore dictionary       */
  66.   .message~new(semDict, msgName, 'a', args)~send
  67.   if var('RESULT') then
  68.     return result
  69.   else
  70.     return 
  71.  
  72.  
  73. ::class Semaphore subclass object metaclass SemaphoreMeta
  74.  
  75. ::method init
  76.   expose sem waits shared name
  77.   use arg semname, shr, state
  78.  
  79.   waits = 0                                 /* no one waiting                 */
  80.   name = ''                                 /* assume unnamed                 */
  81.   shared = 0                                /* assume not shared              */
  82.   sem = 0                                   /* default to not posted          */
  83.                                                                                
  84.   if state = 1 Then                         /* should initial state be set    */
  85.    sem = 1                                                                     
  86.                                             /* was a name specified?          */
  87.   if VAR('SEMNAME') & semname \= '' Then Do
  88.    name = semname                           /* yes, so set the name           */ 
  89.                                                                                 
  90.                                                                                 
  91.    if shr \= 'NOSHARE' Then Do              /* do we want to share this sem   */ 
  92.      shared = 1                             /* yes, mark it shared            */ 
  93.                                             /* shared add to semDict          */ 
  94.      self~class[name] = self                                                    
  95.    End                                                                          
  96.                                                                                 
  97.   End                                                                           
  98.   self~setInitialState(sem)                 /* initialize initial stat        */ 
  99.                                                                                 
  100. ::method setInitialState                                                        
  101.                                             /* this method intended to be     */ 
  102.   nop                                       /* overriden by subclasses.       */ 
  103.                                                                                 
  104.                                                                                 
  105. ::method setSem                                                                 
  106.   expose sem                                                                    
  107.   oldState = sem                                                                
  108.   sem = 1                                   /* set new state to 1.            */ 
  109.   return oldState                                                               
  110.                                                                                 
  111. ::method resetSem                                                               
  112.   expose sem                                                                    
  113.   sem = 0                                                                       
  114.   return 0                                                                      
  115.                                                                                 
  116. ::method querySem                                                               
  117.   expose sem                                                                    
  118.   return sem                                                                    
  119.                                                                                 
  120.                                                                                 
  121. ::method shared                                                                 
  122.   expose shared                                                                 
  123.   return shared                             /* return true 1 or false 0       */ 
  124.                                                                                 
  125. ::method named                                                                  
  126.   expose name                                                                   
  127.                                             /* does semaphore have a name     */ 
  128.   if name = '' Then return 0                /* nope, not named                */ 
  129.                Else return 1                /* yes, its named                 */ 
  130.                                                                                 
  131. ::method name                                                                   
  132.   expose name                                                                   
  133.   return name                               /* return name or ''              */ 
  134.                                                                                 
  135. ::method incWaits                                                               
  136.   expose waits                                                                  
  137.   waits = waits + 1                         /* one more object waiting        */ 
  138.                                                                                 
  139. ::method decWaits                                                               
  140.   expose Waits                                                                  
  141.   waits = waits - 1                         /* one less object waiting        */ 
  142.                                                                                 
  143. ::method Waiting                                                                
  144.   expose Waits                                                                  
  145.   return waits                              /* return num of objects waiting  */ 
  146.  
  147.  
  148.  
  149. /* ========================================================================== */
  150. /* ===         Start of EventSemaphore class ......                       === */
  151. /* ========================================================================== */
  152.  
  153. ::class EventSemaphore subclass Semaphore public
  154. ::method setInitialState
  155.   expose posted posts
  156.   use arg posted
  157.  
  158.   if posted  then posts = 1
  159.              else posts = 0
  160. ::method post
  161.   expose posts posted
  162.  
  163.   self~setSem                               /* set semaphore state            */
  164.   posted = 1                                /* mark as posted                 */
  165.   reply                                                                        
  166.   posts = posts + 1                         /* increase the number of posts   */
  167.                                                                                
  168. ::method wait                                                                  
  169.   expose posted                                                                
  170.                                                                                
  171.   self~incWaits                             /* increment number waiting.      */
  172.   guard off                                                                    
  173.   guard on when posted                      /* now wait until posted.         */
  174.   reply                                     /* return to caller.              */
  175.   self~decWaits                             /* cleanup, 1 less waiting.       */
  176.                                                                                
  177. ::method reset                                                                 
  178.   expose posts posted                                                          
  179.                                                                                
  180.   posted = self~resetSem                    /* reset semaphore                */
  181.   reply                                     /* do an early reply              */
  182.   posts = 0                                 /* reset number of posts          */
  183.  
  184. ::method query
  185.   expose posts
  186.                                             /* return number of times         */
  187.   return posts                              /* semaphore has been posted.     */
  188.  
  189.  
  190.  
  191. /* ========================================================================== */
  192. /* ===         Start of MutexSemaphore class ......                       === */
  193. /* ========================================================================== */
  194.  
  195. ::class MutexSemaphore subclass Semaphore public
  196.   
  197. ::method setInitialState
  198.   expose owned 
  199.   use arg owned 
  200.  
  201.  
  202. ::method requestMutex
  203.   expose Owned
  204.  
  205.   Do forever                                /* do until we get the semaphore  */
  206.    owned = self~setSem                                                         
  207.    if Owned = 0                             /* was semaphore already set?     */
  208.     Then leave                              /* wasn't owned, we now have it.  */
  209.     else Do                                                                    
  210.      self~incWaits                                                             
  211.      guard off                              /* turn off guard status to let   */
  212.                                             /* others come in.                */
  213.      guard on when \Owned                   /* wait until not owned and get   */
  214.                                             /* guard.                         */
  215.      self~decWaits                          /* one less waiting for MUTEX.    */
  216.     End                                                                        
  217.                                             /* go up an see if we can get it  */
  218.   End                                                                             
  219.  
  220.  
  221. ::method releaseMutex
  222.   expose owned
  223.   owned = self~resetSem                     /* reset semaphore                */
  224.