documentation / example methods merged into one (easier to browse for category "examples")
authorClaus Gittinger <cg@exept.de>
Sat, 16 Dec 1995 13:54:22 +0100
changeset 775 7b6c00a4708a
parent 774 e0ee05157570
child 776 f3c0c579c0d2
documentation / example methods merged into one (easier to browse for category "examples")
SemaSet.st
SemaphoreSet.st
--- a/SemaSet.st	Sat Dec 16 13:29:09 1995 +0100
+++ b/SemaSet.st	Sat Dec 16 13:54:22 1995 +0100
@@ -1,3 +1,16 @@
+"
+ COPYRIGHT (c) 1995 by Stefan Vogel / Claus Gittinger
+              All Rights Reserved
+
+ This software is furnished under a license and may be used
+ only in accordance with the terms of that license and with the
+ inclusion of the above copyright notice.   This software may not
+ be provided or otherwise made available to, or used by, any
+ other person.  No title to or ownership of the software is
+ hereby transferred.
+"
+
+
 IdentitySet subclass:#SemaphoreSet
 	instanceVariableNames:''
 	classVariableNames:''
@@ -7,14 +20,71 @@
 
 !SemaphoreSet class methodsFor:'documentation'!
 
+copyright
+"
+ COPYRIGHT (c) 1995 by Stefan Vogel / Claus Gittinger
+              All Rights Reserved
+
+ This software is furnished under a license and may be used
+ only in accordance with the terms of that license and with the
+ inclusion of the above copyright notice.   This software may not
+ be provided or otherwise made available to, or used by, any
+ other person.  No title to or ownership of the software is
+ hereby transferred.
+"
+
+!
+
 documentation
 "
-    SemaphoreSet allow waiting until one of several semaphores become available.
+    SemaphoreSets allow waiting until one of several semaphores become available.
+    They provide a waiting protocol compatible to Semaphore, i.e. #wait and
+    #waitWithTimeOut.
 "
 !
 
-example2
+examples
 "
+ the following example forks a process which waits on any
+ of sema1, sema2 to be signalled. The main thread signals those.
+ ---------------------------------------------------------------
+
+    |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.
+
+
+
+ the following example forks a process which waits on any
+ of sema1, sema2 to be signalled, or a timeout to occur.
+ ---------------------------------------------------------------
+
     |sema1 sema2 semaSet proc|
 
     sema1 := Semaphore new.
@@ -46,41 +116,49 @@
     sema1 signal.
     (Delay forSeconds:3) wait.
     sema2 signal.
-"
-!
+
+
+
 
-examples
-"
-    |sema1 sema2 semaSet proc|
+ the following example forks a process which waits on input
+ to arrive on any of 2 sharedQueues (with timeout)
+ The main thread writes data into those queues.
+ ---------------------------------------------------------------
 
-    sema1 := Semaphore new.
-    sema2 := Semaphore new.
-    semaSet := SemaphoreSet with:sema1 with:sema2.
+    |q1 q2 semaSet proc|
+
+    q1 := SharedQueue new.
+    q2 := SharedQueue new.
+    semaSet := SemaphoreSet with:(q1 readSemaphore) with:(q2 readSemaphore).
 
     proc := [
         [
-            |ret name|
+            |ret whatHappened|
 
-            ret := semaSet wait.
-            ret == sema1 ifTrue:[
-                name := 'sema1'
+            ret := semaSet waitWithTimeout:5.
+            ret == q1 readSemaphore ifTrue:[
+                Transcript show:'q1 has data: '; show:q1 next; cr.
             ] ifFalse:[ 
-                ret == sema2 ifTrue:[
-                    name := 'sema2'
+                ret == q2 readSemaphore ifTrue:[
+                    Transcript show:'q2 has data: '; show:q2 next; cr.
+                ] ifFalse:[
+                    Transcript showCr:'timeout'
                 ]
             ].
-            Transcript showCr: name, ' raised'.
-            ret == sema2 ifTrue:[
-                proc terminate
-            ]
         ] loop
     ] fork.
 
     (Delay forSeconds:3) wait.
-    sema1 signal.
-    (Delay forSeconds:3) wait.
-    sema2 signal.
+    q1 nextPut:'one'.
+    (Delay forSeconds:2) wait.
+    q1 nextPut:'two'.
+    (Delay forSeconds:2) wait.
+    q1 nextPut:'three'.
+    (Delay forSeconds:6) wait.
+    proc terminate.
 "
+
+
 !
 
 history
@@ -90,6 +168,9 @@
 !SemaphoreSet methodsFor:'wait'!
 
 wait
+    "wait for any of the semaphores in the set to be signalled.
+     Return the (first) semaphore which is triggered."
+
     |currentProcess gotSema wasBlocked registeredAllSemas|
 
     currentProcess := Processor activeProcess.
@@ -123,11 +204,12 @@
     ^ gotSema
 
     "Modified: 15.12.1995 / 23:10:07 / stefan"
+    "Modified: 16.12.1995 / 13:53:21 / cg"
 !
 
 waitWithTimeout:seconds
     "wait for any of the the semaphore, but abort the wait after some time.
-     Return the first triggered semaphore if any, nil if we return due to a timeout."
+     Return the (first) triggered semaphore if any, nil if we return due to a timeout."
 
     |currentProcess gotSema wasBlocked now endTime unblock timeoutOccured registeredAllSemas|
 
@@ -179,13 +261,11 @@
     ^ gotSema
 
     "Modified: 15.12.1995 / 23:10:54 / stefan"
-    "Modified: 16.12.1995 / 02:22:53 / cg"
+    "Modified: 16.12.1995 / 13:53:31 / cg"
 ! !
 
 !SemaphoreSet class methodsFor:'documentation'!
 
 version
-"
-$Header: /cvs/stx/stx/libbasic/Attic/SemaSet.st,v 1.3 1995-12-16 01:23:30 cg Exp $
-"
+    ^ '$Header: /cvs/stx/stx/libbasic/Attic/SemaSet.st,v 1.4 1995-12-16 12:54:22 cg Exp $'
 ! !
--- a/SemaphoreSet.st	Sat Dec 16 13:29:09 1995 +0100
+++ b/SemaphoreSet.st	Sat Dec 16 13:54:22 1995 +0100
@@ -1,3 +1,16 @@
+"
+ COPYRIGHT (c) 1995 by Stefan Vogel / Claus Gittinger
+              All Rights Reserved
+
+ This software is furnished under a license and may be used
+ only in accordance with the terms of that license and with the
+ inclusion of the above copyright notice.   This software may not
+ be provided or otherwise made available to, or used by, any
+ other person.  No title to or ownership of the software is
+ hereby transferred.
+"
+
+
 IdentitySet subclass:#SemaphoreSet
 	instanceVariableNames:''
 	classVariableNames:''
@@ -7,14 +20,71 @@
 
 !SemaphoreSet class methodsFor:'documentation'!
 
+copyright
+"
+ COPYRIGHT (c) 1995 by Stefan Vogel / Claus Gittinger
+              All Rights Reserved
+
+ This software is furnished under a license and may be used
+ only in accordance with the terms of that license and with the
+ inclusion of the above copyright notice.   This software may not
+ be provided or otherwise made available to, or used by, any
+ other person.  No title to or ownership of the software is
+ hereby transferred.
+"
+
+!
+
 documentation
 "
-    SemaphoreSet allow waiting until one of several semaphores become available.
+    SemaphoreSets allow waiting until one of several semaphores become available.
+    They provide a waiting protocol compatible to Semaphore, i.e. #wait and
+    #waitWithTimeOut.
 "
 !
 
-example2
+examples
 "
+ the following example forks a process which waits on any
+ of sema1, sema2 to be signalled. The main thread signals those.
+ ---------------------------------------------------------------
+
+    |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.
+
+
+
+ the following example forks a process which waits on any
+ of sema1, sema2 to be signalled, or a timeout to occur.
+ ---------------------------------------------------------------
+
     |sema1 sema2 semaSet proc|
 
     sema1 := Semaphore new.
@@ -46,41 +116,49 @@
     sema1 signal.
     (Delay forSeconds:3) wait.
     sema2 signal.
-"
-!
+
+
+
 
-examples
-"
-    |sema1 sema2 semaSet proc|
+ the following example forks a process which waits on input
+ to arrive on any of 2 sharedQueues (with timeout)
+ The main thread writes data into those queues.
+ ---------------------------------------------------------------
 
-    sema1 := Semaphore new.
-    sema2 := Semaphore new.
-    semaSet := SemaphoreSet with:sema1 with:sema2.
+    |q1 q2 semaSet proc|
+
+    q1 := SharedQueue new.
+    q2 := SharedQueue new.
+    semaSet := SemaphoreSet with:(q1 readSemaphore) with:(q2 readSemaphore).
 
     proc := [
         [
-            |ret name|
+            |ret whatHappened|
 
-            ret := semaSet wait.
-            ret == sema1 ifTrue:[
-                name := 'sema1'
+            ret := semaSet waitWithTimeout:5.
+            ret == q1 readSemaphore ifTrue:[
+                Transcript show:'q1 has data: '; show:q1 next; cr.
             ] ifFalse:[ 
-                ret == sema2 ifTrue:[
-                    name := 'sema2'
+                ret == q2 readSemaphore ifTrue:[
+                    Transcript show:'q2 has data: '; show:q2 next; cr.
+                ] ifFalse:[
+                    Transcript showCr:'timeout'
                 ]
             ].
-            Transcript showCr: name, ' raised'.
-            ret == sema2 ifTrue:[
-                proc terminate
-            ]
         ] loop
     ] fork.
 
     (Delay forSeconds:3) wait.
-    sema1 signal.
-    (Delay forSeconds:3) wait.
-    sema2 signal.
+    q1 nextPut:'one'.
+    (Delay forSeconds:2) wait.
+    q1 nextPut:'two'.
+    (Delay forSeconds:2) wait.
+    q1 nextPut:'three'.
+    (Delay forSeconds:6) wait.
+    proc terminate.
 "
+
+
 !
 
 history
@@ -90,6 +168,9 @@
 !SemaphoreSet methodsFor:'wait'!
 
 wait
+    "wait for any of the semaphores in the set to be signalled.
+     Return the (first) semaphore which is triggered."
+
     |currentProcess gotSema wasBlocked registeredAllSemas|
 
     currentProcess := Processor activeProcess.
@@ -123,11 +204,12 @@
     ^ gotSema
 
     "Modified: 15.12.1995 / 23:10:07 / stefan"
+    "Modified: 16.12.1995 / 13:53:21 / cg"
 !
 
 waitWithTimeout:seconds
     "wait for any of the the semaphore, but abort the wait after some time.
-     Return the first triggered semaphore if any, nil if we return due to a timeout."
+     Return the (first) triggered semaphore if any, nil if we return due to a timeout."
 
     |currentProcess gotSema wasBlocked now endTime unblock timeoutOccured registeredAllSemas|
 
@@ -179,13 +261,11 @@
     ^ gotSema
 
     "Modified: 15.12.1995 / 23:10:54 / stefan"
-    "Modified: 16.12.1995 / 02:22:53 / cg"
+    "Modified: 16.12.1995 / 13:53:31 / cg"
 ! !
 
 !SemaphoreSet class methodsFor:'documentation'!
 
 version
-"
-$Header: /cvs/stx/stx/libbasic/SemaphoreSet.st,v 1.3 1995-12-16 01:23:30 cg Exp $
-"
+    ^ '$Header: /cvs/stx/stx/libbasic/SemaphoreSet.st,v 1.4 1995-12-16 12:54:22 cg Exp $'
 ! !