Semaphore.st
changeset 22238 ce1753b20f6d
parent 22171 fe0e08536dd2
child 23036 e11ebfd9362c
equal deleted inserted replaced
22237:fb6d54514410 22238:ce1753b20f6d
   856     wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
   856     wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
   857 
   857 
   858     ^ count
   858     ^ count
   859 !
   859 !
   860 
   860 
       
   861 consumeIfPossible
       
   862     "if the semaphore is currently free,
       
   863      acquire it, lock it and return true.
       
   864      Otherwise, do not wait, but return false immediately."
       
   865 
       
   866     |wasBlocked|
       
   867 
       
   868     wasBlocked := OperatingSystem blockInterrupts.
       
   869     count <= 0 ifTrue:[
       
   870         wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
       
   871         ^ false.
       
   872     ].    
       
   873 
       
   874     "if we come here, we have acquired the semaphore"
       
   875     count := count - 1.
       
   876     count == 0 ifTrue:[
       
   877         lastOwnerId := Processor activeProcessId.
       
   878     ].
       
   879     wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
       
   880     ^ true
       
   881 
       
   882     "Created: / 31-08-2017 / 23:05:19 / cg"
       
   883 !
       
   884 
   861 critical:aBlock
   885 critical:aBlock
   862     "evaluate aBlock as a critical region; the receiver must be
   886     "evaluate aBlock as a critical region; the receiver must be
   863      created using Semaphore>>forMutualExclusion"
   887      created using Semaphore>>forMutualExclusion"
   864 
   888 
   865     |retVal wasBlocked needsReschedule gotSema|
   889     |retVal wasBlocked needsReschedule gotSema|