SemaphoreSet.st
author Claus Gittinger <cg@exept.de>
Tue, 26 Feb 2002 14:02:26 +0100
changeset 6421 58dca33cf0fc
parent 5570 e6e14f50d721
child 6787 a1703f7cd4fd
permissions -rw-r--r--
#valueNowOrOnUnwindDo: -> #ensure:
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
775
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
     1
"
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
     2
 COPYRIGHT (c) 1995 by Stefan Vogel / Claus Gittinger
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
     3
              All Rights Reserved
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
     4
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
     5
 This software is furnished under a license and may be used
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
     6
 only in accordance with the terms of that license and with the
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
     8
 be provided or otherwise made available to, or used by, any
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
     9
 other person.  No title to or ownership of the software is
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    10
 hereby transferred.
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    11
"
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    12
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    13
5570
e6e14f50d721 category change
Claus Gittinger <cg@exept.de>
parents: 2876
diff changeset
    14
"{ Package: 'stx:libbasic' }"
e6e14f50d721 category change
Claus Gittinger <cg@exept.de>
parents: 2876
diff changeset
    15
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    16
IdentitySet subclass:#SemaphoreSet
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    17
	instanceVariableNames:''
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    18
	classVariableNames:''
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    19
	poolDictionaries:''
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    20
	category:'Kernel-Processes'
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    21
!
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    22
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    23
!SemaphoreSet class methodsFor:'documentation'!
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    24
775
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    25
copyright
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    26
"
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    27
 COPYRIGHT (c) 1995 by Stefan Vogel / Claus Gittinger
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    28
              All Rights Reserved
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    29
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    30
 This software is furnished under a license and may be used
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    31
 only in accordance with the terms of that license and with the
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    32
 inclusion of the above copyright notice.   This software may not
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    33
 be provided or otherwise made available to, or used by, any
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    34
 other person.  No title to or ownership of the software is
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    35
 hereby transferred.
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    36
"
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    37
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    38
!
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    39
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    40
documentation
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    41
"
2876
fb3fed7470be oops - must unregister from individual semaphores
Claus Gittinger <cg@exept.de>
parents: 1801
diff changeset
    42
    SemaphoreSets allow waiting until one of several semaphores becomes available.
fb3fed7470be oops - must unregister from individual semaphores
Claus Gittinger <cg@exept.de>
parents: 1801
diff changeset
    43
    They provide a waiting protocol which is compatible to Semaphore, 
fb3fed7470be oops - must unregister from individual semaphores
Claus Gittinger <cg@exept.de>
parents: 1801
diff changeset
    44
    i.e. #wait and #waitWithTimeOut.
1273
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1031
diff changeset
    45
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1031
diff changeset
    46
    [see also:]
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1031
diff changeset
    47
        Semaphore
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1031
diff changeset
    48
        Process ProcessorScheduler
1294
e26bbb61f6b2 documentation
Claus Gittinger <cg@exept.de>
parents: 1273
diff changeset
    49
e26bbb61f6b2 documentation
Claus Gittinger <cg@exept.de>
parents: 1273
diff changeset
    50
    [author:]
e26bbb61f6b2 documentation
Claus Gittinger <cg@exept.de>
parents: 1273
diff changeset
    51
        Stefan Vogel
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    52
"
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    53
!
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    54
775
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    55
examples
769
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
    56
"
775
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    57
 the following example forks a process which waits on any
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    58
 of sema1, sema2 to be signalled. The main thread signals those.
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    59
 ---------------------------------------------------------------
1316
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
    60
                                                                        [exBegin]
775
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    61
    |sema1 sema2 semaSet proc|
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    62
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    63
    sema1 := Semaphore new.
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    64
    sema2 := Semaphore new.
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    65
    semaSet := SemaphoreSet with:sema1 with:sema2.
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    66
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    67
    proc := [
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    68
        [
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    69
            |ret name|
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    70
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    71
            ret := semaSet wait.
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    72
            ret == sema1 ifTrue:[
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    73
                name := 'sema1'
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    74
            ] ifFalse:[ 
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    75
                ret == sema2 ifTrue:[
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    76
                    name := 'sema2'
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    77
                ]
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    78
            ].
1422
9a0b792f2953 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
    79
            Transcript showCR: name, ' raised'.
775
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    80
            ret == sema2 ifTrue:[
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    81
                proc terminate
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    82
            ]
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    83
        ] loop
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    84
    ] fork.
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    85
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    86
    (Delay forSeconds:3) wait.
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    87
    sema1 signal.
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    88
    (Delay forSeconds:3) wait.
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    89
    sema2 signal.
1316
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
    90
                                                                        [exEnd]
775
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    91
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    92
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    93
 the following example forks a process which waits on any
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    94
 of sema1, sema2 to be signalled, or a timeout to occur.
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    95
 ---------------------------------------------------------------
1316
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
    96
                                                                        [exBegin]
769
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
    97
    |sema1 sema2 semaSet proc|
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
    98
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
    99
    sema1 := Semaphore new.
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   100
    sema2 := Semaphore new.
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   101
    semaSet := SemaphoreSet with:sema1 with:sema2.
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   102
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   103
    proc := [
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   104
        [
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   105
            |ret name|
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   106
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   107
            ret := semaSet waitWithTimeout:5.
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   108
            ret == sema1 ifTrue:[
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   109
                name := 'sema1'
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   110
            ] ifFalse:[ 
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   111
                ret == sema2 ifTrue:[
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   112
                    name := 'sema2'
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   113
                ] ifFalse:[
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   114
                    name := ret printString
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   115
                ]
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   116
            ].
1422
9a0b792f2953 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
   117
            Transcript showCR: name, ' raised'.
769
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   118
            ret isNil ifTrue:[
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   119
                proc terminate
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   120
            ]
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   121
        ] loop
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   122
    ] fork.
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   123
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   124
    (Delay forSeconds:3) wait.
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   125
    sema1 signal.
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   126
    (Delay forSeconds:3) wait.
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   127
    sema2 signal.
1316
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   128
                                                                        [exEnd]
775
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   129
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   130
769
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   131
775
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   132
 the following example forks a process which waits on input
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   133
 to arrive on any of 2 sharedQueues (with timeout)
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   134
 The main thread writes data into those queues.
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   135
 ---------------------------------------------------------------
1316
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   136
                                                                        [exBegin]
775
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   137
    |q1 q2 semaSet proc|
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   138
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   139
    q1 := SharedQueue new.
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   140
    q2 := SharedQueue new.
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   141
    semaSet := SemaphoreSet with:(q1 readSemaphore) with:(q2 readSemaphore).
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   142
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   143
    proc := [
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   144
        [
775
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   145
            |ret whatHappened|
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   146
775
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   147
            ret := semaSet waitWithTimeout:5.
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   148
            ret == q1 readSemaphore ifTrue:[
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   149
                Transcript show:'q1 has data: '; show:q1 next; cr.
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   150
            ] ifFalse:[ 
775
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   151
                ret == q2 readSemaphore ifTrue:[
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   152
                    Transcript show:'q2 has data: '; show:q2 next; cr.
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   153
                ] ifFalse:[
1422
9a0b792f2953 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
   154
                    Transcript showCR:'timeout'
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   155
                ]
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   156
            ].
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   157
        ] loop
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   158
    ] fork.
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   159
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   160
    (Delay forSeconds:3) wait.
775
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   161
    q1 nextPut:'one'.
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   162
    (Delay forSeconds:2) wait.
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   163
    q1 nextPut:'two'.
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   164
    (Delay forSeconds:2) wait.
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   165
    q1 nextPut:'three'.
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   166
    (Delay forSeconds:6) wait.
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   167
    proc terminate.
1316
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   168
                                                                        [exEnd]
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   169
"
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   170
! !
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   171
5570
e6e14f50d721 category change
Claus Gittinger <cg@exept.de>
parents: 2876
diff changeset
   172
!SemaphoreSet methodsFor:'wait & signal'!
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   173
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   174
wait
775
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   175
    "wait for any of the semaphores in the set to be signalled.
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   176
     Return the (first) semaphore which is triggered."
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   177
769
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   178
    |currentProcess gotSema wasBlocked registeredAllSemas|
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   179
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   180
    currentProcess := Processor activeProcess.
769
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   181
    registeredAllSemas := false.
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   182
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   183
    wasBlocked := OperatingSystem blockInterrupts.
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   184
    [
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   185
        [
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   186
            gotSema := self detect:[:sema|
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   187
                sema checkAndRegisterProcess:currentProcess
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   188
            ] ifNone:[
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   189
                registeredAllSemas := true.
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   190
                currentProcess suspendWithState:#wait.
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   191
                nil
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   192
            ].
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   193
        ] doWhile:[gotSema isNil].
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   194
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   195
        "
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   196
          we finaly got at least one of our semaphores.
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   197
          Now unregister from any semaphore, we are registered on.
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   198
        "
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   199
        self detect:[:sema|
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   200
            (sema == gotSema) ifTrue:[
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   201
                registeredAllSemas not        
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   202
            ] ifFalse:[        
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   203
                sema unregisterProcess:currentProcess.
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   204
                false
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   205
            ]
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   206
        ] ifNone:[].
6421
58dca33cf0fc #valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents: 5570
diff changeset
   207
    ] ensure:[
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   208
        wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
2876
fb3fed7470be oops - must unregister from individual semaphores
Claus Gittinger <cg@exept.de>
parents: 1801
diff changeset
   209
        self do:[:aSema |
fb3fed7470be oops - must unregister from individual semaphores
Claus Gittinger <cg@exept.de>
parents: 1801
diff changeset
   210
            aSema unregisterProcess:currentProcess.
fb3fed7470be oops - must unregister from individual semaphores
Claus Gittinger <cg@exept.de>
parents: 1801
diff changeset
   211
        ]
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   212
    ].
769
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   213
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   214
    ^ gotSema
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   215
769
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   216
    "Modified: 15.12.1995 / 23:10:07 / stefan"
2876
fb3fed7470be oops - must unregister from individual semaphores
Claus Gittinger <cg@exept.de>
parents: 1801
diff changeset
   217
    "Modified: 20.8.1997 / 18:33:09 / cg"
769
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   218
!
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   219
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   220
waitWithTimeout:seconds
770
402958905760 protocol of waitWithTImeOut: changed
Claus Gittinger <cg@exept.de>
parents: 769
diff changeset
   221
    "wait for any of the the semaphore, but abort the wait after some time.
775
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   222
     Return the (first) triggered semaphore if any, nil if we return due to a timeout."
770
402958905760 protocol of waitWithTImeOut: changed
Claus Gittinger <cg@exept.de>
parents: 769
diff changeset
   223
769
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   224
    |currentProcess gotSema wasBlocked now endTime unblock timeoutOccured registeredAllSemas|
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   225
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   226
    currentProcess := Processor activeProcess.
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   227
    timeoutOccured := false.
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   228
    registeredAllSemas := false.
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   229
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   230
    wasBlocked := OperatingSystem blockInterrupts.
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   231
    [
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   232
        seconds notNil ifTrue:[
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   233
            "
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   234
             calculate the end-time
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   235
            "
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   236
            now := OperatingSystem getMillisecondTime.
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   237
            endTime := OperatingSystem millisecondTimeAdd:now and:(seconds * 1000).
769
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   238
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   239
            unblock := [timeoutOccured := true. Processor resume:currentProcess].
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   240
            Processor addTimedBlock:unblock for:currentProcess atMilliseconds:endTime.
769
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   241
        ].
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   242
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   243
        [
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   244
            gotSema := self detect:[:sema|
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   245
                sema checkAndRegisterProcess:currentProcess
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   246
            ] ifNone:[
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   247
                registeredAllSemas := true.
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   248
                currentProcess suspendWithState:#wait.
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   249
                nil
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   250
            ].
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   251
        ] doWhile:[gotSema isNil and:[timeoutOccured not]].
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   252
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   253
        (timeoutOccured not and:[unblock notNil]) ifTrue:[
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   254
            Processor removeTimedBlock:unblock.
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   255
        ].
769
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   256
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   257
        "
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   258
          we finaly got at least one of our semaphores.
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   259
          Now unregister from any semaphore, we are registered on.
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   260
        "
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   261
        self detect:[:sema|
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   262
            (sema == gotSema) ifTrue:[
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   263
                registeredAllSemas not        
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   264
            ] ifFalse:[        
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   265
                sema unregisterProcess:currentProcess.
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   266
                false
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   267
            ]
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   268
        ] ifNone:[].
6421
58dca33cf0fc #valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents: 5570
diff changeset
   269
    ] ensure:[
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   270
        wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
2876
fb3fed7470be oops - must unregister from individual semaphores
Claus Gittinger <cg@exept.de>
parents: 1801
diff changeset
   271
        self do:[:aSema |
fb3fed7470be oops - must unregister from individual semaphores
Claus Gittinger <cg@exept.de>
parents: 1801
diff changeset
   272
            aSema unregisterProcess:currentProcess.
fb3fed7470be oops - must unregister from individual semaphores
Claus Gittinger <cg@exept.de>
parents: 1801
diff changeset
   273
        ]
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   274
    ].
769
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   275
    ^ gotSema
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   276
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   277
    "Modified: 15.12.1995 / 23:10:54 / stefan"
2876
fb3fed7470be oops - must unregister from individual semaphores
Claus Gittinger <cg@exept.de>
parents: 1801
diff changeset
   278
    "Modified: 20.8.1997 / 18:33:23 / cg"
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   279
! !
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   280
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   281
!SemaphoreSet class methodsFor:'documentation'!
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   282
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   283
version
6421
58dca33cf0fc #valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents: 5570
diff changeset
   284
    ^ '$Header: /cvs/stx/stx/libbasic/SemaphoreSet.st,v 1.13 2002-02-26 13:00:29 cg Exp $'
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   285
! !