|
Class: SemaphoreSet
Object
|
+--Collection
|
+--Set
|
+--IdentitySet
|
+--SemaphoreSet
- Package:
- stx:libbasic
- Category:
- Kernel-Processes
- Version:
- rev:
1.24
date: 2017/02/21 14:47:04
- user: stefan
- file: SemaphoreSet.st directory: libbasic
- module: stx stc-classLibrary: libbasic
- Author:
- Stefan Vogel
SemaphoreSets allow waiting until one of several semaphores becomes available.
They provide a waiting protocol which is compatible to Semaphore,
i.e. #wait and #waitWithTimeOut.
Semaphore
Process
ProcessorScheduler
waiting
-
wait
-
wait for any of the semaphores in the set to be signalled.
Return the (first) semaphore which is triggered.
-
waitWithTimeout: secondsOrNilOrTimeDuration
-
wait for any of the semaphore, but abort the wait after some time (seconds).
Return the (first) triggered semaphore if any, nil if we return due to a timeout.
-
waitWithTimeoutMs: milliSeconds
-
wait for any of the semaphore, but abort the wait after some time.
Return the (first) triggered semaphore if any, nil if we return due to a timeout.
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.
sema2 := Semaphore new.
semaSet := SemaphoreSet with:sema1 with:sema2.
proc := [
[
|ret name|
ret := semaSet waitWithTimeout:5.
ret == sema1 ifTrue:[
name := 'sema1'
] ifFalse:[
ret == sema2 ifTrue:[
name := 'sema2'
] ifFalse:[
name := ret printString
]
].
Transcript showCR: name, ' raised'.
ret isNil 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 input
to arrive on any of 2 sharedQueues (with timeout)
The main thread writes data into those queues.
|q1 q2 semaSet proc|
q1 := SharedQueue new.
q2 := SharedQueue new.
semaSet := SemaphoreSet with:(q1 readSemaphore) with:(q2 readSemaphore).
proc := [
[
|ret whatHappened|
ret := semaSet waitWithTimeout:5.
ret == q1 readSemaphore ifTrue:[
Transcript show:'q1 has data: '; show:q1 next; cr.
] ifFalse:[
ret == q2 readSemaphore ifTrue:[
Transcript show:'q2 has data: '; show:q2 next; cr.
] ifFalse:[
Transcript showCR:'timeout'
]
].
] loop
] fork.
(Delay forSeconds:3) wait.
q1 nextPut:'one'.
(Delay forSeconds:2) wait.
q1 nextPut:'two'.
(Delay forSeconds:2) wait.
q1 nextPut:'three'.
(Delay forSeconds:6) wait.
proc terminate.
|
|