eXept Software AG Logo

Smalltalk/X Webserver

Documentation of class 'SemaphoreSet':

Home

Documentation
www.exept.de
Everywhere
for:
[back]

Class: SemaphoreSet


Inheritance:

   Object
   |
   +--Collection
      |
      +--Set
         |
         +--IdentitySet
            |
            +--SemaphoreSet

Package:
stx:libbasic
Category:
Kernel-Processes
Version:
rev: 1.26 date: 2019/06/25 12:22:02
user: cg
file: SemaphoreSet.st directory: libbasic
module: stx stc-classLibrary: libbasic
Author:
Stefan Vogel

Description:


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.


Related information:

    Semaphore
    Process
    ProcessorScheduler

Instance protocol:

waiting
o  wait
wait for any of the semaphores in the set to be signalled.
Return the (first) semaphore which is triggered.

o  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.

The argument may be a time duration or the number of seconds as integer
or float (i.e. use 0.1 for a 100ms timeout).
With zero timeout, this can be used to poll a semaphore (returning
the receiver if the semaphore is available, nil if not).
However, polling is not the intended use of semaphores, though.
If the argument is nil, wait without timeout (forever).

o  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.


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.
   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.


ST/X 7.2.0.0; WebServer 1.670 at bd0aa1f87cdd.unknown:8081; Fri, 26 Apr 2024 04:48:29 GMT