SemaphoreSet.st
author Claus Gittinger <cg@exept.de>
Tue, 09 Jul 2019 20:55:17 +0200
changeset 24417 03b083548da2
parent 24357 cd704e0c423f
permissions -rw-r--r--
#REFACTORING by exept class: Smalltalk class changed: #recursiveInstallAutoloadedClassesFrom:rememberIn:maxLevels:noAutoload:packageTop:showSplashInLevels: Transcript showCR:(... bindWith:...) -> Transcript showCR:... with:...
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24197
521267ef7b16 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21505
diff changeset
     1
"{ Encoding: utf8 }"
521267ef7b16 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21505
diff changeset
     2
775
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
     3
"
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
     4
 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
     5
              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
     6
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
     7
 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
     8
 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
     9
 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
    10
 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
    11
 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
    12
 hereby transferred.
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
20185
c32b6064d503 #OTHER by mawalch
mawalch
parents: 16287
diff changeset
    16
"{ NameSpace: Smalltalk }"
c32b6064d503 #OTHER by mawalch
mawalch
parents: 16287
diff changeset
    17
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    18
IdentitySet subclass:#SemaphoreSet
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    19
	instanceVariableNames:''
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    20
	classVariableNames:''
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    21
	poolDictionaries:''
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    22
	category:'Kernel-Processes'
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    23
!
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    24
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    25
!SemaphoreSet class methodsFor:'documentation'!
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    26
775
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    27
copyright
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    28
"
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    29
 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
    30
              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
    31
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    32
 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
    33
 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
    34
 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
    35
 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
    36
 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
    37
 hereby transferred.
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
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    40
!
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    41
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    42
documentation
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    43
"
2876
fb3fed7470be oops - must unregister from individual semaphores
Claus Gittinger <cg@exept.de>
parents: 1801
diff changeset
    44
    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
    45
    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
    46
    i.e. #wait and #waitWithTimeOut.
1273
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1031
diff changeset
    47
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1031
diff changeset
    48
    [see also:]
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1031
diff changeset
    49
        Semaphore
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1031
diff changeset
    50
        Process ProcessorScheduler
1294
e26bbb61f6b2 documentation
Claus Gittinger <cg@exept.de>
parents: 1273
diff changeset
    51
e26bbb61f6b2 documentation
Claus Gittinger <cg@exept.de>
parents: 1273
diff changeset
    52
    [author:]
e26bbb61f6b2 documentation
Claus Gittinger <cg@exept.de>
parents: 1273
diff changeset
    53
        Stefan Vogel
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    54
"
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    55
!
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
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
examples
769
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
    58
"
775
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    59
 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
    60
 of sema1, sema2 to be signalled. The main thread signals those.
1316
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
    61
                                                                        [exBegin]
775
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    62
    |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
    63
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    64
    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
    65
    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
    66
    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
    67
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    68
    proc := [
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    69
        [
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    70
            |ret name|
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    71
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    72
            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
    73
            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
    74
                name := 'sema1'
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    75
            ] ifFalse:[ 
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    76
                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
    77
                    name := 'sema2'
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    78
                ]
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    79
            ].
1422
9a0b792f2953 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
    80
            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
    81
            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
    82
                proc terminate
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    83
            ]
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    84
        ] loop
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    85
    ] fork.
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    86
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    87
    (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
    88
    sema1 signal.
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    89
    (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
    90
    sema2 signal.
1316
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
    91
                                                                        [exEnd]
775
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
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    94
 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
    95
 of sema1, sema2 to be signalled, or a timeout to occur.
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.
1316
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   135
                                                                        [exBegin]
775
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   136
    |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
   137
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   138
    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
   139
    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
   140
    semaSet := SemaphoreSet with:(q1 readSemaphore) with:(q2 readSemaphore).
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   141
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   142
    proc := [
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   143
        [
775
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   144
            |ret whatHappened|
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   145
775
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   146
            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
   147
            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
   148
                Transcript show:'q1 has data: '; show:q1 next; cr.
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   149
            ] ifFalse:[ 
775
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   150
                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
   151
                    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
   152
                ] ifFalse:[
1422
9a0b792f2953 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
   153
                    Transcript showCR:'timeout'
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   154
                ]
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   155
            ].
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   156
        ] loop
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   157
    ] fork.
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   158
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   159
    (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
   160
    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
   161
    (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
   162
    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
   163
    (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
   164
    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
   165
    (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
   166
    proc terminate.
1316
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   167
                                                                        [exEnd]
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   168
"
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   169
! !
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   170
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   171
!SemaphoreSet methodsFor:'waiting'!
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   172
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   173
wait
775
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   174
    "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
   175
     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
   176
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   177
    |currentProcess gotSema wasBlocked mustUnregisterAllSemas|
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   178
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   179
    currentProcess := Processor activeProcess.
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   180
    mustUnregisterAllSemas := false.
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   181
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   182
    wasBlocked := OperatingSystem blockInterrupts.
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   183
    [
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   184
        gotSema := self detect:[:eachSema|
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   185
            eachSema checkAndAddWaitingProcess:currentProcess
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   186
        ] ifNone:[].
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   187
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   188
        gotSema isNil ifTrue:[
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   189
            mustUnregisterAllSemas := true.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   190
            [
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   191
                currentProcess suspendWithState:#wait.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   192
            ] ifCurtailed:[
11257
9689500b7608 care for interrupts when curtailed
ca
parents: 11252
diff changeset
   193
                OperatingSystem blockInterrupts.
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   194
                self do:[:eachSema |
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   195
                    eachSema removeWaitingProcess:currentProcess.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   196
                ].
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   197
                wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   198
            ].
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   199
        ].
16287
b989ab286181 class: SemaphoreSet
Stefan Vogel <sv@exept.de>
parents: 14769
diff changeset
   200
b989ab286181 class: SemaphoreSet
Stefan Vogel <sv@exept.de>
parents: 14769
diff changeset
   201
        gotSema isNil
b989ab286181 class: SemaphoreSet
Stefan Vogel <sv@exept.de>
parents: 14769
diff changeset
   202
    ] whileTrue.
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   203
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   204
    "
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   205
      we finally got at least one of our semaphores.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   206
      Now unregister from any semaphore, we are registered on.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   207
    "
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   208
    self do:[:eachSema|
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   209
        (eachSema == gotSema and:[mustUnregisterAllSemas not]) ifTrue:[
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   210
            "done with registered semaphores"
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   211
            wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   212
            ^ gotSema
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   213
        ].
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   214
        eachSema removeWaitingProcess:currentProcess.
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   215
    ].
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   216
    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   217
    ^ gotSema
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   218
769
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   219
    "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
   220
    "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
   221
!
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   222
21505
e23b21205f48 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20255
diff changeset
   223
waitWithTimeout:secondsOrNilOrTimeDuration
20185
c32b6064d503 #OTHER by mawalch
mawalch
parents: 16287
diff changeset
   224
    "wait for any of the semaphore, but abort the wait after some time (seconds).
24357
cd704e0c423f #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 24197
diff changeset
   225
     Return the (first) triggered semaphore if any, nil if we return due to a timeout.
cd704e0c423f #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 24197
diff changeset
   226
     
cd704e0c423f #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 24197
diff changeset
   227
     The argument may be a time duration or the number of seconds as integer
cd704e0c423f #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 24197
diff changeset
   228
     or float (i.e. use 0.1 for a 100ms timeout).
cd704e0c423f #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 24197
diff changeset
   229
     With zero timeout, this can be used to poll a semaphore (returning
cd704e0c423f #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 24197
diff changeset
   230
     the receiver if the semaphore is available, nil if not).
cd704e0c423f #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 24197
diff changeset
   231
     However, polling is not the intended use of semaphores, though.
cd704e0c423f #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 24197
diff changeset
   232
     If the argument is nil, wait without timeout (forever)."
770
402958905760 protocol of waitWithTImeOut: changed
Claus Gittinger <cg@exept.de>
parents: 769
diff changeset
   233
6787
a1703f7cd4fd waitWithTimeout: protocol as in Semaphore
Claus Gittinger <cg@exept.de>
parents: 6421
diff changeset
   234
    |millis|
a1703f7cd4fd waitWithTimeout: protocol as in Semaphore
Claus Gittinger <cg@exept.de>
parents: 6421
diff changeset
   235
21505
e23b21205f48 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20255
diff changeset
   236
    secondsOrNilOrTimeDuration notNil ifTrue:[
e23b21205f48 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20255
diff changeset
   237
        secondsOrNilOrTimeDuration isNumber ifTrue:[
24197
521267ef7b16 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21505
diff changeset
   238
            millis := (secondsOrNilOrTimeDuration * 1000) asInteger.
21505
e23b21205f48 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20255
diff changeset
   239
        ] ifFalse:[
24197
521267ef7b16 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21505
diff changeset
   240
            "a TimeDuration"
521267ef7b16 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21505
diff changeset
   241
            millis := secondsOrNilOrTimeDuration asTruncatedMilliseconds.
21505
e23b21205f48 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20255
diff changeset
   242
        ].
6787
a1703f7cd4fd waitWithTimeout: protocol as in Semaphore
Claus Gittinger <cg@exept.de>
parents: 6421
diff changeset
   243
    ].
a1703f7cd4fd waitWithTimeout: protocol as in Semaphore
Claus Gittinger <cg@exept.de>
parents: 6421
diff changeset
   244
    ^ self waitWithTimeoutMs:millis.
a1703f7cd4fd waitWithTimeout: protocol as in Semaphore
Claus Gittinger <cg@exept.de>
parents: 6421
diff changeset
   245
21505
e23b21205f48 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20255
diff changeset
   246
    "Modified: / 20-08-1997 / 18:33:23 / cg"
e23b21205f48 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20255
diff changeset
   247
    "Modified: / 21-02-2017 / 14:48:48 / stefan"
24197
521267ef7b16 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21505
diff changeset
   248
    "Modified: / 27-05-2019 / 21:45:55 / Stefan Vogel"
24357
cd704e0c423f #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 24197
diff changeset
   249
    "Modified (comment): / 25-06-2019 / 14:21:40 / Claus Gittinger"
6787
a1703f7cd4fd waitWithTimeout: protocol as in Semaphore
Claus Gittinger <cg@exept.de>
parents: 6421
diff changeset
   250
!
a1703f7cd4fd waitWithTimeout: protocol as in Semaphore
Claus Gittinger <cg@exept.de>
parents: 6421
diff changeset
   251
a1703f7cd4fd waitWithTimeout: protocol as in Semaphore
Claus Gittinger <cg@exept.de>
parents: 6421
diff changeset
   252
waitWithTimeoutMs:milliSeconds
20185
c32b6064d503 #OTHER by mawalch
mawalch
parents: 16287
diff changeset
   253
    "wait for any of the semaphore, but abort the wait after some time.
6787
a1703f7cd4fd waitWithTimeout: protocol as in Semaphore
Claus Gittinger <cg@exept.de>
parents: 6421
diff changeset
   254
     Return the (first) triggered semaphore if any, nil if we return due to a timeout."
a1703f7cd4fd waitWithTimeout: protocol as in Semaphore
Claus Gittinger <cg@exept.de>
parents: 6421
diff changeset
   255
20255
09746889c3be #OTHER by mawalch
mawalch
parents: 20185
diff changeset
   256
    |currentProcess gotSema wasBlocked timeoutBlock timeoutOccurred mustUnregisterAllSemas|
769
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   257
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   258
    currentProcess := Processor activeProcess.
20255
09746889c3be #OTHER by mawalch
mawalch
parents: 20185
diff changeset
   259
    timeoutOccurred := false.
769
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   260
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   261
    wasBlocked := OperatingSystem blockInterrupts.
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   262
    [
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   263
        gotSema := self detect:[:eachSema|
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   264
            eachSema checkAndAddWaitingProcess:currentProcess
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   265
        ] ifNone:[].
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   266
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   267
        gotSema isNil ifTrue:[
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   268
            mustUnregisterAllSemas := true.
20255
09746889c3be #OTHER by mawalch
mawalch
parents: 20185
diff changeset
   269
            (milliSeconds notNil and:[timeoutBlock isNil]) ifTrue:[
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   270
                |now endTime|
769
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   271
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   272
                milliSeconds == 0 ifTrue:[
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   273
                    "with zero-timeout, this is a poll"
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   274
                    self do:[:eachSema |
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   275
                        eachSema removeWaitingProcess:currentProcess.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   276
                    ].
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   277
                    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   278
                    ^ nil
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   279
                ].
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   280
                "calculate the end-time"
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   281
                now := OperatingSystem getMillisecondTime.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   282
                endTime := OperatingSystem millisecondTimeAdd:now and:milliSeconds.
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   283
14769
898e4754340b class: SemaphoreSet
Stefan Vogel <sv@exept.de>
parents: 12698
diff changeset
   284
                timeoutBlock := [
20255
09746889c3be #OTHER by mawalch
mawalch
parents: 20185
diff changeset
   285
                        timeoutOccurred := true.
09746889c3be #OTHER by mawalch
mawalch
parents: 20185
diff changeset
   286
                        timeoutBlock := nil.
14769
898e4754340b class: SemaphoreSet
Stefan Vogel <sv@exept.de>
parents: 12698
diff changeset
   287
                        Processor resume:currentProcess.
898e4754340b class: SemaphoreSet
Stefan Vogel <sv@exept.de>
parents: 12698
diff changeset
   288
                    ].
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   289
                Processor addTimedBlock:timeoutBlock for:currentProcess atMilliseconds:endTime.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   290
            ].
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   291
            [
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   292
                currentProcess suspendWithState:#wait.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   293
            ] ifCurtailed:[
11282
5e04b4bd310a changed #waitWithTimeoutMs: - comments
Stefan Vogel <sv@exept.de>
parents: 11257
diff changeset
   294
                "interrupts are not blocked when entered through Processor>>#interruptActive"
11257
9689500b7608 care for interrupts when curtailed
ca
parents: 11252
diff changeset
   295
                OperatingSystem blockInterrupts.
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   296
                timeoutBlock notNil ifTrue:[
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   297
                    Processor removeTimedBlock:timeoutBlock.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   298
                    timeoutBlock := nil.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   299
                ].
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   300
                self do:[:eachSema |
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   301
                    eachSema removeWaitingProcess:currentProcess.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   302
                ].
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   303
                wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   304
            ].
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   305
        ].
16287
b989ab286181 class: SemaphoreSet
Stefan Vogel <sv@exept.de>
parents: 14769
diff changeset
   306
20255
09746889c3be #OTHER by mawalch
mawalch
parents: 20185
diff changeset
   307
        gotSema isNil and:[timeoutOccurred not]
16287
b989ab286181 class: SemaphoreSet
Stefan Vogel <sv@exept.de>
parents: 14769
diff changeset
   308
    ] whileTrue.
769
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   309
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   310
    timeoutBlock notNil ifTrue:[
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   311
        Processor removeTimedBlock:timeoutBlock.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   312
        timeoutBlock := nil.
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   313
    ].
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   314
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   315
    "we finally got a timeout or at least one of our semaphores.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   316
     Unregister from any semaphore, we are registered with"
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   317
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   318
    self do:[:eachSema|
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   319
        (eachSema == gotSema and:[mustUnregisterAllSemas not]) ifTrue:[
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   320
            "done with registered semaphores"
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   321
            wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   322
            ^ gotSema
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   323
        ].
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   324
    ].
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 6787
diff changeset
   325
    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
769
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   326
    ^ gotSema
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   327
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   328
    "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
   329
    "Modified: 20.8.1997 / 18:33:23 / cg"
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   330
! !
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   331
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   332
!SemaphoreSet class methodsFor:'documentation'!
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   333
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   334
version
20185
c32b6064d503 #OTHER by mawalch
mawalch
parents: 16287
diff changeset
   335
    ^ '$Header$'
12698
a4e7529547bc comment/format in: #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents: 11282
diff changeset
   336
!
a4e7529547bc comment/format in: #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents: 11282
diff changeset
   337
a4e7529547bc comment/format in: #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents: 11282
diff changeset
   338
version_CVS
20185
c32b6064d503 #OTHER by mawalch
mawalch
parents: 16287
diff changeset
   339
    ^ '$Header$'
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   340
! !
14769
898e4754340b class: SemaphoreSet
Stefan Vogel <sv@exept.de>
parents: 12698
diff changeset
   341