SemaphoreSet.st
changeset 757 93d5f6b86e98
child 769 84cc1b36f27e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SemaphoreSet.st	Thu Dec 14 23:42:02 1995 +0100
@@ -0,0 +1,99 @@
+'From Smalltalk/X, Version:2.10.8 on 14-dec-1995 at 18:58:53'                   !
+
+IdentitySet subclass:#SemaphoreSet
+	instanceVariableNames:''
+	classVariableNames:''
+	poolDictionaries:''
+	category:'Kernel-Processes'
+!
+
+!SemaphoreSet class methodsFor:'documentation'!
+
+documentation
+"
+    SemaphoreSet allow waiting until one of several semaphores become available.
+"
+!
+
+examples
+"
+    |sema1 sema2 semaSet proc|
+
+    sema1 := Semaphore new.
+    sema2 := Semaphore new.
+    semaSet := SemaphoreSet with:sema1 with:sema2.
+
+    proc := [
+        [
+            |ret name|
+
+            ret := semaSet wait.
+            ret == sema1 ifTrue:[
+                name := 'sema1'
+            ] ifFalse:[ 
+                ret == sema2 ifTrue:[
+                    name := 'sema2'
+                ]
+            ].
+            Transcript showCr: name, ' raised'.
+            ret == sema2 ifTrue:[
+                proc terminate
+            ]
+        ] loop
+    ] fork.
+
+    (Delay forSeconds:3) wait.
+    sema1 signal.
+    (Delay forSeconds:3) wait.
+    sema2 signal.
+"
+!
+
+history
+    "Created: 14.12.1995 / 12:23:21 / stefan"
+! !
+
+!SemaphoreSet methodsFor:'wait'!
+
+wait
+    |currentProcess gotSema wasBlocked|
+
+    currentProcess := Processor activeProcess.
+
+    wasBlocked := OperatingSystem blockInterrupts.
+    [
+        gotSema := self detect:[:sema|
+            sema checkAndRegisterProcess:currentProcess
+        ] ifNone:[
+            currentProcess suspendWithState:#wait.
+            nil
+        ].
+    ] doWhile:[gotSema isNil].
+
+    "
+      we finaly got one of our semaphores.
+      Now unregister from any semaphore, we are registered on.
+    "
+    gotSema notNil ifTrue:[
+        self detect:[:sema|
+            sema == gotSema ifTrue:[
+                true
+            ] ifFalse:[
+                sema unregisterProcess:currentProcess.
+                false
+            ]
+        ] ifNone:[]
+    ].
+    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
+    ^ gotSema
+
+    "Modified: 14.12.1995 / 13:16:57 / stefan"
+! !
+
+!SemaphoreSet class methodsFor:'documentation'!
+
+version
+"
+$Header: /cvs/stx/stx/libbasic/SemaphoreSet.st,v 1.1 1995-12-14 22:42:02 stefan Exp $
+"
+! !