home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / ckscripts / semaphore < prev    next >
Text File  |  2020-01-01  |  2KB  |  77 lines

  1. ; From: Dat Thuc Nguyen
  2. ; Date: Tue Jun 15 14:34:09 1999
  3. ; URL: http://www.smalltickle.com
  4. ;
  5. ; WE USE SEMAPHORES TO COORDINATE COMPUTING TASKS,
  6. ; SHARE RESOURCES, ETC.
  7. ; THE FOLLOWING SCRIPT DEFINES A SEMAPHORE CLASS.
  8.  
  9. ;********************************************************
  10. ;    DEFINITION OF THE SEMAPHORE CLASS                  *
  11. ;********************************************************
  12. define semaphore {
  13.     _assign semaphore::\%1 0    ; Initialize semaphore with 0
  14.     _define \%1 {
  15.         semaphore::\%1 \v(macro) \%2
  16.     }
  17. }
  18. ;******************************************************
  19. ;    IMPLEMENTATION OF THE PUBLIC USAGE INTERFACE     *
  20. ;    OF THE SEMAPHORE CLASS                           *
  21. ;******************************************************
  22. define semaphore::up {
  23.     _assign semaphore::\%1 \feval(\m(semaphore::\%1) + 1)
  24.     echo \m(semaphore::\%1)
  25. }
  26.  
  27. define semaphore::down {
  28.     _assign semaphore::\%1 \feval(\m(semaphore::\%1) - 1)
  29.     echo \m(semaphore::\%1)
  30. }
  31.  
  32. define semaphore::query {
  33.     return \m(semaphore::\%1)      
  34. }
  35.  
  36. ;********************************************************
  37. ;    USAGE SAMPLES                                      *
  38. ;********************************************************
  39. take semaphore.ksc
  40. semaphore com_port                      ; Create a semaphore com_port
  41.  
  42. ; In a user-defined function:
  43. ;
  44. xif = \fexec(com_port query) 0 {        ; If resource is available
  45.     com_port up                         ; Register for resource
  46.     do_some_processing                  ; 
  47.     com_port down                       ; Release resource
  48. }
  49.  
  50. ; In another user-defined function
  51. ;
  52. xif = \fexec(com_port query) 0 {        ; If resource is available
  53.     com_port up                         : register for resource
  54.     if \fexec(do_something) {           ; If do_something completed
  55.         com_port down                   ; Release resource
  56.     } else {                            ; Keep resource, release later
  57.         do_other                        ; do something else
  58.     }
  59. }
  60.  
  61.  
  62. ; The script above follows the Smalltalk model.
  63. ;  C++ programmer can easily extend the class usage interface as follows:
  64. ;
  65. define semaphore::++ {
  66.     _assign semaphore::\%1 \feval(\m(semaphore::\%1) + 1)
  67.     echo \m(semaphore::\%1)
  68. }
  69.  
  70. define semaphore::-- {
  71.     _assign semaphore::\%1 \feval(\m(semaphore::\%1) - 1)
  72.     echo \m(semaphore::\%1)
  73. }
  74.  
  75. ; This demonstrate that the C-Kermit interpreter can accomodate several
  76. ; preferences.
  77.