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.30 date: 2023/05/23 14:44:08
user: cg
file: SemaphoreSet.st directory: libbasic
module: stx stc-classLibrary: libbasic

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.

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.

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,
or 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: milliSecondsOrNil
wait for any of the semaphoreSet, but abort the wait after some time.
Return the (first) triggered semaphore if any, nil if we return due to a timeout.
With zero-timeout, this is a poll.
With nil-timeout, this may wait forevfer (danger alert).


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.7.0.0; WebServer 1.702 at 20f6060372b9.unknown:8081; Sat, 21 Dec 2024 16:42:21 GMT