SemaphoreSet.st
changeset 757 93d5f6b86e98
child 769 84cc1b36f27e
equal deleted inserted replaced
756:f3f56229c300 757:93d5f6b86e98
       
     1 'From Smalltalk/X, Version:2.10.8 on 14-dec-1995 at 18:58:53'                   !
       
     2 
       
     3 IdentitySet subclass:#SemaphoreSet
       
     4 	instanceVariableNames:''
       
     5 	classVariableNames:''
       
     6 	poolDictionaries:''
       
     7 	category:'Kernel-Processes'
       
     8 !
       
     9 
       
    10 !SemaphoreSet class methodsFor:'documentation'!
       
    11 
       
    12 documentation
       
    13 "
       
    14     SemaphoreSet allow waiting until one of several semaphores become available.
       
    15 "
       
    16 !
       
    17 
       
    18 examples
       
    19 "
       
    20     |sema1 sema2 semaSet proc|
       
    21 
       
    22     sema1 := Semaphore new.
       
    23     sema2 := Semaphore new.
       
    24     semaSet := SemaphoreSet with:sema1 with:sema2.
       
    25 
       
    26     proc := [
       
    27         [
       
    28             |ret name|
       
    29 
       
    30             ret := semaSet wait.
       
    31             ret == sema1 ifTrue:[
       
    32                 name := 'sema1'
       
    33             ] ifFalse:[ 
       
    34                 ret == sema2 ifTrue:[
       
    35                     name := 'sema2'
       
    36                 ]
       
    37             ].
       
    38             Transcript showCr: name, ' raised'.
       
    39             ret == sema2 ifTrue:[
       
    40                 proc terminate
       
    41             ]
       
    42         ] loop
       
    43     ] fork.
       
    44 
       
    45     (Delay forSeconds:3) wait.
       
    46     sema1 signal.
       
    47     (Delay forSeconds:3) wait.
       
    48     sema2 signal.
       
    49 "
       
    50 !
       
    51 
       
    52 history
       
    53     "Created: 14.12.1995 / 12:23:21 / stefan"
       
    54 ! !
       
    55 
       
    56 !SemaphoreSet methodsFor:'wait'!
       
    57 
       
    58 wait
       
    59     |currentProcess gotSema wasBlocked|
       
    60 
       
    61     currentProcess := Processor activeProcess.
       
    62 
       
    63     wasBlocked := OperatingSystem blockInterrupts.
       
    64     [
       
    65         gotSema := self detect:[:sema|
       
    66             sema checkAndRegisterProcess:currentProcess
       
    67         ] ifNone:[
       
    68             currentProcess suspendWithState:#wait.
       
    69             nil
       
    70         ].
       
    71     ] doWhile:[gotSema isNil].
       
    72 
       
    73     "
       
    74       we finaly got one of our semaphores.
       
    75       Now unregister from any semaphore, we are registered on.
       
    76     "
       
    77     gotSema notNil ifTrue:[
       
    78         self detect:[:sema|
       
    79             sema == gotSema ifTrue:[
       
    80                 true
       
    81             ] ifFalse:[
       
    82                 sema unregisterProcess:currentProcess.
       
    83                 false
       
    84             ]
       
    85         ] ifNone:[]
       
    86     ].
       
    87     wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
       
    88     ^ gotSema
       
    89 
       
    90     "Modified: 14.12.1995 / 13:16:57 / stefan"
       
    91 ! !
       
    92 
       
    93 !SemaphoreSet class methodsFor:'documentation'!
       
    94 
       
    95 version
       
    96 "
       
    97 $Header: /cvs/stx/stx/libbasic/SemaphoreSet.st,v 1.1 1995-12-14 22:42:02 stefan Exp $
       
    98 "
       
    99 ! !