Semaphore.st
author Claus Gittinger <cg@exept.de>
Tue, 09 Jul 2019 20:55:17 +0200
changeset 24417 03b083548da2
parent 24392 aa300a0e1b6c
child 25268 9b31493cf516
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:
24358
8b8dae901519 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 24351
diff changeset
     1
"{ Encoding: utf8 }"
8b8dae901519 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 24351
diff changeset
     2
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     3
"
a27a279701f8 Initial revision
claus
parents:
diff changeset
     4
 COPYRIGHT (c) 1993 by Claus Gittinger
6616
70d54ffebc91 valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents: 6615
diff changeset
     5
	      All Rights Reserved
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     6
a27a279701f8 Initial revision
claus
parents:
diff changeset
     7
 This software is furnished under a license and may be used
a27a279701f8 Initial revision
claus
parents:
diff changeset
     8
 only in accordance with the terms of that license and with the
a27a279701f8 Initial revision
claus
parents:
diff changeset
     9
 inclusion of the above copyright notice.   This software may not
a27a279701f8 Initial revision
claus
parents:
diff changeset
    10
 be provided or otherwise made available to, or used by, any
a27a279701f8 Initial revision
claus
parents:
diff changeset
    11
 other person.  No title to or ownership of the software is
a27a279701f8 Initial revision
claus
parents:
diff changeset
    12
 hereby transferred.
a27a279701f8 Initial revision
claus
parents:
diff changeset
    13
"
5556
1056cc5d6ce0 category change
Claus Gittinger <cg@exept.de>
parents: 5256
diff changeset
    14
"{ Package: 'stx:libbasic' }"
1056cc5d6ce0 category change
Claus Gittinger <cg@exept.de>
parents: 5256
diff changeset
    15
19189
48f29d2c0455 #DOCUMENTATION
matilk
parents: 16903
diff changeset
    16
"{ NameSpace: Smalltalk }"
48f29d2c0455 #DOCUMENTATION
matilk
parents: 16903
diff changeset
    17
375
claus
parents: 362
diff changeset
    18
Object subclass:#Semaphore
19927
b99e02676fb6 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 19192
diff changeset
    19
	instanceVariableNames:'count waitingProcesses lastOwnerId name owner'
6616
70d54ffebc91 valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents: 6615
diff changeset
    20
	classVariableNames:''
70d54ffebc91 valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents: 6615
diff changeset
    21
	poolDictionaries:''
70d54ffebc91 valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents: 6615
diff changeset
    22
	category:'Kernel-Processes'
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    23
!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    24
1812
a048a98aa299 kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
    25
!Semaphore class methodsFor:'documentation'!
69
4564b6328136 *** empty log message ***
claus
parents: 25
diff changeset
    26
88
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    27
copyright
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    28
"
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    29
 COPYRIGHT (c) 1993 by Claus Gittinger
6616
70d54ffebc91 valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents: 6615
diff changeset
    30
	      All Rights Reserved
88
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    31
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    32
 This software is furnished under a license and may be used
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    33
 only in accordance with the terms of that license and with the
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    34
 inclusion of the above copyright notice.   This software may not
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    35
 be provided or otherwise made available to, or used by, any
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    36
 other person.  No title to or ownership of the software is
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    37
 hereby transferred.
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    38
"
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    39
!
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    40
69
4564b6328136 *** empty log message ***
claus
parents: 25
diff changeset
    41
documentation
4564b6328136 *** empty log message ***
claus
parents: 25
diff changeset
    42
"
4564b6328136 *** empty log message ***
claus
parents: 25
diff changeset
    43
    Semaphores are used to synchronize processes providing a nonBusy wait
4564b6328136 *** empty log message ***
claus
parents: 25
diff changeset
    44
    mechanism. A process can wait for the availability of some resource by
93
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
    45
    performing a Semaphore>>wait, which will suspend the process until the
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
    46
    resource becomes available. Signalling is done by (another process performing)
93
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
    47
    Semaphore>>signal.
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
    48
    If the resource has been already available before the wait, no suspending is
69
4564b6328136 *** empty log message ***
claus
parents: 25
diff changeset
    49
    done, but the resource immediately allocated.
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
    50
    The resource internally keeps a count, the number of times the resource can be
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
    51
    allocated. If the semaphore is created with a count greater than 1, the sema
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
    52
    can be waited-upon that many times without blocking.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
    53
    On the other hand, if initialized with a negative count, the semaphore
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
    54
    must be signalled that many times more often in order for a wait to not block.
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
    55
    In other words: whenever the semaphore has a count greater than zero,
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
    56
    the wait operation will proceed. Otherwise, it will block until the count has
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
    57
    been incremented by signal operations to be greater than zero.
2234
21fe6c17fe8d commentary
Claus Gittinger <cg@exept.de>
parents: 2142
diff changeset
    58
93
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
    59
    There are also semaphores for mutual access to a critical region
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
    60
    (Semaphore>>forMutualExclusion and Semaphore>>critical:).
88
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    61
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
    62
    Additional protocol is provided for oneShot semaphores,
2234
21fe6c17fe8d commentary
Claus Gittinger <cg@exept.de>
parents: 2142
diff changeset
    63
    (#signalOnce) and for conditional signalling (#signalIf).
21fe6c17fe8d commentary
Claus Gittinger <cg@exept.de>
parents: 2142
diff changeset
    64
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
    65
    You can also attach semaphores to external events (such as I/O arrival or
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
    66
    timer events).
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
    67
    This is done by telling the Processor to signal the semaphore
183
7899f380e77d signalIf
claus
parents: 159
diff changeset
    68
    under some condition.
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
    69
    See 'Processor>>signal:afterSeconds:', 'Processor>>signal:onInput:' etc.
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
    70
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
    71
    See examples in doc/coding (found in the CodingExamples-nameSpace).
1273
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1185
diff changeset
    72
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
    73
    Warning/Note/Hint:
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
    74
	a Semaphore-forMutualExclusion does NEVER allow for the critical
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
    75
	region to be entered twice - NOT EVEN by the same process.
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
    76
	That means, that a recursive attempt to enter that section leads
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
    77
	to a deadlock.
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
    78
	Use a RecursionLock instead, to avoid this.
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
    79
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
    80
    Hint:
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
    81
	now (Jul2002), Semaphores now allow for a negative count; this allows for
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
    82
	a sync-point to be implemented easily (i.e. to wait for multiple other processes
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
    83
	to arrive at a sync-point).
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
    84
	See examples.
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
    85
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
    86
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
    87
    [instance variables:]
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
    88
	count                   <SmallInteger>          the number of waits, that will go through
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
    89
							without blocking.
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
    90
							Incremented on #signal; decremented on #wait.
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
    91
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
    92
	waitingProcesses        <OrderedCollection>     waiting processes - will be served first
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
    93
							come first served when signalled.
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
    94
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
    95
	lastOwnerId             <SmallInteger>          a debugging aid: set when count drops
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
    96
							to zero to the current processes id.
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
    97
							Helps in finding deadlocks.
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
    98
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
    99
	name                    <String>                a debugging aid: an optional userFriendly
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   100
							name; helps to identify a semaphore easier.
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   101
1273
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1185
diff changeset
   102
    [see also:]
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   103
	SemaphoreSet RecursionLock Monitor
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   104
	SharedQueue Delay
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   105
	Process ProcessorScheduler
1294
e26bbb61f6b2 documentation
Claus Gittinger <cg@exept.de>
parents: 1273
diff changeset
   106
e26bbb61f6b2 documentation
Claus Gittinger <cg@exept.de>
parents: 1273
diff changeset
   107
    [author:]
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   108
	Claus Gittinger
3392
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   109
"
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   110
!
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   111
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   112
examples
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   113
"
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   114
    two processes synchronizing on a sema:
20116
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   115
                                                        [exBegin]
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   116
        |sema thread1 thread2|
3392
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   117
20116
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   118
        sema := Semaphore new.
3392
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   119
20116
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   120
        thread1 := [
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   121
                        Transcript showCR:'here is thread 1; now waiting ...'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   122
                        sema wait.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   123
                        Transcript showCR:'here is thread 1 again.'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   124
                   ] newProcess.
3392
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   125
20116
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   126
        thread2 := [
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   127
                        Transcript showCR:'here is thread 2; delaying a bit ...'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   128
                        Delay waitForSeconds:5.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   129
                        Transcript showCR:'here is thread 2 again; now signalling the sema'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   130
                        sema signal.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   131
                        Transcript showCR:'here is thread 2 after the signalling.'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   132
                  ] newProcess.
3392
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   133
20116
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   134
        thread1 priority:7.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   135
        thread2 priority:6.
3392
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   136
20116
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   137
        thread1 resume.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   138
        thread2 resume.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   139
                                                        [exEnd]
3392
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   140
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   141
    semaphore for critical regions:
20116
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   142
                                                        [exBegin]
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   143
        |accessLock|
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   144
20116
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   145
        accessLock := Semaphore forMutualExclusion.
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   146
20116
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   147
        [
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   148
            5 timesRepeat:[
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   149
                Delay waitForSeconds:2.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   150
                accessLock critical:[
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   151
                    Transcript showCR:'thread1 in critical region'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   152
                    Delay waitForSeconds:1.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   153
                    Transcript showCR:'thread1 leaving critical region'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   154
                ].
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   155
            ]
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   156
        ] forkAt:5.
3392
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   157
20116
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   158
        [
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   159
            5 timesRepeat:[
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   160
                Delay waitForSeconds:1.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   161
                accessLock critical:[
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   162
                    Transcript showCR:'thread2 in critical region'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   163
                    Delay waitForSeconds:2.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   164
                    Transcript showCR:'thread2 leaving critical region'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   165
                ].
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   166
            ]
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   167
        ] forkAt:4.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   168
                                                        [exEnd]
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   169
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   170
    a deadlock due to recursive enter of a critical region:
20116
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   171
                                                        [exBegin]
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   172
        |accessLock block|
3392
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   173
20116
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   174
        accessLock := Semaphore forMutualExclusion.
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   175
20116
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   176
        block := [:arg |
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   177
                    Transcript showCR:'about to enter'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   178
                    accessLock critical:[
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   179
                        Transcript showCR:'entered - doing action'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   180
                        arg value
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   181
                    ].
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   182
                    Transcript showCR:'left region'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   183
                 ].
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   184
20116
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   185
        block value:[].                 'this works'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   186
        block value:[block value:[] ].  'this deadlocks'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   187
                                                        [exEnd]
3392
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   188
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   189
    Avoid the deadlock by using a RecursionLock instead:
20116
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   190
                                                        [exBegin]
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   191
        |accessLock block|
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   192
20116
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   193
        accessLock := RecursionLock new.
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   194
20116
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   195
        block := [:arg |
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   196
                    Transcript showCR:'about to enter'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   197
                    accessLock critical:[
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   198
                        Transcript showCR:'entered - doing action'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   199
                        arg value
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   200
                    ].
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   201
                    Transcript showCR:'left region'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   202
                 ].
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   203
20116
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   204
        block value:[].                 'this works'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   205
        block value:[block value:[] ].  'this deadlocks'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   206
                                                        [exEnd]
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   207
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   208
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   209
   Wait for multiple processes to arrive at a sync-point:
20116
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   210
                                                        [exBegin]
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   211
        |syncSema proceedSema thread1 thread2 thread3|
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   212
20116
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   213
        syncSema := Semaphore new.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   214
        syncSema setCount:(1-3).
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   215
        proceedSema := Semaphore new.
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   216
20116
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   217
        thread1 := [
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   218
                        Transcript showCR:'here is thread 1; now busy ...'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   219
                        Delay waitForSeconds:(2 + (Random nextIntegerBetween:2 and:4)).
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   220
                        Transcript showCR:'here is thread 1 again - now syncing.'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   221
                        syncSema signal.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   222
                        Transcript showCR:'thread 1 is waiting for all others...'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   223
                        proceedSema wait.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   224
                        Transcript showCR:'thread 1 done.'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   225
                   ] newProcess.
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   226
20116
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   227
        thread2 := [
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   228
                        Transcript showCR:'here is thread 2; now busy ...'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   229
                        Delay waitForSeconds:(3 + (Random nextIntegerBetween:2 and:4)).
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   230
                        Transcript showCR:'here is thread 2 again - now syncing.'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   231
                        syncSema signal.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   232
                        Transcript showCR:'thread 2 is waiting for all others...'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   233
                        proceedSema wait.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   234
                        Transcript showCR:'thread 2 done.'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   235
                  ] newProcess.
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   236
20116
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   237
        thread3 := [
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   238
                        Transcript showCR:'here is thread 3; now busy ...'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   239
                        Delay waitForSeconds:(4 + (Random nextIntegerBetween:2 and:4)).
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   240
                        Transcript showCR:'here is thread 3 again - now syncing.'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   241
                        syncSema signal.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   242
                        Transcript showCR:'thread 3 is waiting for all others...'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   243
                        proceedSema wait.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   244
                        Transcript showCR:'thread 3 done.'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   245
                  ] newProcess.
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   246
20116
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   247
        thread1 priority:7.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   248
        thread2 priority:6.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   249
        thread3 priority:9.
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   250
20116
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   251
        thread1 resume.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   252
        thread2 resume.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   253
        thread3 resume.
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   254
20116
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   255
        Transcript showCR:'main thread: now waiting for other threads...'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   256
        syncSema wait.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   257
        Transcript showCR:'main thread: all other threads at syncPoint.'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   258
        Delay waitForSeconds:2.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   259
        Transcript showCR:'main thread: now let them proceed...'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   260
        proceedSema signalForAll.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   261
        Transcript showCR:'main thread: done.'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   262
                                                        [exEnd]
6999
b0f293f2780c comment
ca
parents: 6986
diff changeset
   263
20116
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   264
   waitWithTimeout:0 can also be used to conditionally acquire the semaphore
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   265
   i.e. only acquire it if it is available.
6999
b0f293f2780c comment
ca
parents: 6986
diff changeset
   266
     |s|
b0f293f2780c comment
ca
parents: 6986
diff changeset
   267
b0f293f2780c comment
ca
parents: 6986
diff changeset
   268
     s := Semaphore new.
b0f293f2780c comment
ca
parents: 6986
diff changeset
   269
     [
20116
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   270
          (s waitWithTimeout:0) notNil ifTrue:[
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   271
                Transcript showCR:'process1 got the sema'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   272
                Delay waitForSeconds:1.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   273
                Transcript showCR:'process1 signals sema'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   274
                s signal.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   275
          ] ifFalse:[
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   276
                Transcript showCR:'process1 has NOT got the sema'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   277
          ].
6999
b0f293f2780c comment
ca
parents: 6986
diff changeset
   278
     ] fork.
b0f293f2780c comment
ca
parents: 6986
diff changeset
   279
     [
20116
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   280
          (s waitWithTimeout:0) notNil ifTrue:[
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   281
                Transcript showCR:'process2 got the sema'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   282
                Delay waitForSeconds:1.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   283
                Transcript showCR:'process2 signals sema'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   284
                s signal.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   285
          ] ifFalse:[
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   286
                Transcript showCR:'process2 has NOT got the sema'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   287
          ]
6999
b0f293f2780c comment
ca
parents: 6986
diff changeset
   288
     ] fork.
b0f293f2780c comment
ca
parents: 6986
diff changeset
   289
     s signal.
b0f293f2780c comment
ca
parents: 6986
diff changeset
   290
     Delay waitForSeconds:0.5.
b0f293f2780c comment
ca
parents: 6986
diff changeset
   291
     Transcript showCR:'master waits for sema'.
b0f293f2780c comment
ca
parents: 6986
diff changeset
   292
     s wait.
b0f293f2780c comment
ca
parents: 6986
diff changeset
   293
     Transcript showCR:'master got the sema'.
69
4564b6328136 *** empty log message ***
claus
parents: 25
diff changeset
   294
"
4564b6328136 *** empty log message ***
claus
parents: 25
diff changeset
   295
! !
4564b6328136 *** empty log message ***
claus
parents: 25
diff changeset
   296
1812
a048a98aa299 kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   297
!Semaphore class methodsFor:'instance creation'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   298
14509
2c8531dbf456 added: #cleanup
anwild
parents: 13579
diff changeset
   299
cleanup
2c8531dbf456 added: #cleanup
anwild
parents: 13579
diff changeset
   300
    "an emergency helper: manually signal all semaphores which were held by a now dead process.
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   301
     Can only (;-?) happen, if a semaphore-holding process was hard terminated
14509
2c8531dbf456 added: #cleanup
anwild
parents: 13579
diff changeset
   302
     (i.e. no ensure handling happened), and semas remain in a bad state."
2c8531dbf456 added: #cleanup
anwild
parents: 13579
diff changeset
   303
22076
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   304
    self allSubInstancesDo:[:sema |
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   305
        |lastOwner|
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   306
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   307
        (sema count == 0
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   308
         and:[(lastOwner := sema lastOwner) notNil
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   309
         and:[lastOwner isDead]]) ifTrue:[
14509
2c8531dbf456 added: #cleanup
anwild
parents: 13579
diff changeset
   310
            sema signal
2c8531dbf456 added: #cleanup
anwild
parents: 13579
diff changeset
   311
        ]
2c8531dbf456 added: #cleanup
anwild
parents: 13579
diff changeset
   312
    ]
22076
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   313
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   314
    "Modified: / 20-02-2017 / 15:58:34 / stefan"
14509
2c8531dbf456 added: #cleanup
anwild
parents: 13579
diff changeset
   315
!
2c8531dbf456 added: #cleanup
anwild
parents: 13579
diff changeset
   316
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   317
forMutualExclusion
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   318
    "create & return a new semaphore which allows exactly one process to
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   319
     wait on it without blocking. This type of semaphore is used
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   320
     for mutual exclusion from critical regions (see #critical:).
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   321
     Also see RecursionLock, to avoid deadlock in case of recursive entered
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   322
     critical regions."
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   323
22171
fe0e08536dd2 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22155
diff changeset
   324
    ^ self basicNew setCount:1 name:'criticalRegionSema'
fe0e08536dd2 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22155
diff changeset
   325
fe0e08536dd2 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22155
diff changeset
   326
    "Modified: / 18-02-2017 / 17:27:14 / stefan"
fe0e08536dd2 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22155
diff changeset
   327
    "Modified: / 09-08-2017 / 11:50:15 / cg"
fe0e08536dd2 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22155
diff changeset
   328
!
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   329
22171
fe0e08536dd2 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22155
diff changeset
   330
name:aString
fe0e08536dd2 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22155
diff changeset
   331
    "create & return a new semaphore which blocks until a signal is sent"
fe0e08536dd2 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22155
diff changeset
   332
fe0e08536dd2 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22155
diff changeset
   333
    ^ self basicNew setCount:0 name:aString
fe0e08536dd2 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22155
diff changeset
   334
fe0e08536dd2 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22155
diff changeset
   335
    "Created: / 09-08-2017 / 11:49:18 / cg"
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   336
!
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   337
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   338
new
a27a279701f8 Initial revision
claus
parents:
diff changeset
   339
    "create & return a new semaphore which blocks until a signal is sent"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   340
22076
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   341
    ^ self basicNew setCount:0
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   342
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   343
    "Modified: / 20-02-2017 / 10:26:04 / stefan"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   344
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   345
a27a279701f8 Initial revision
claus
parents:
diff changeset
   346
new:n
a27a279701f8 Initial revision
claus
parents:
diff changeset
   347
    "create & return a new semaphore which allows n waits before
a27a279701f8 Initial revision
claus
parents:
diff changeset
   348
     blocking"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   349
22076
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   350
    ^ self basicNew setCount:n
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   351
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   352
    "Modified: / 18-02-2017 / 17:27:26 / stefan"
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   353
! !
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   354
9067
a8b1ac38b89d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8617
diff changeset
   355
!Semaphore methodsFor:'Compatibility-Squeak'!
a8b1ac38b89d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8617
diff changeset
   356
a8b1ac38b89d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8617
diff changeset
   357
isSignaled
a8b1ac38b89d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8617
diff changeset
   358
    ^ self wouldBlock not
12567
eb78ade59632 added: #waitTimeoutSeconds:
Stefan Vogel <sv@exept.de>
parents: 11281
diff changeset
   359
!
eb78ade59632 added: #waitTimeoutSeconds:
Stefan Vogel <sv@exept.de>
parents: 11281
diff changeset
   360
12589
b8964a49b462 added: #waitTimeoutMSecs:
Stefan Vogel <sv@exept.de>
parents: 12567
diff changeset
   361
waitTimeoutMSecs:milliSeconds
22080
379aa47c41ad #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22076
diff changeset
   362
    ^ self waitWithTimeoutMs:milliSeconds state:#wait
379aa47c41ad #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22076
diff changeset
   363
379aa47c41ad #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22076
diff changeset
   364
    "Modified: / 24-07-2017 / 21:18:01 / cg"
12589
b8964a49b462 added: #waitTimeoutMSecs:
Stefan Vogel <sv@exept.de>
parents: 12567
diff changeset
   365
!
b8964a49b462 added: #waitTimeoutMSecs:
Stefan Vogel <sv@exept.de>
parents: 12567
diff changeset
   366
12567
eb78ade59632 added: #waitTimeoutSeconds:
Stefan Vogel <sv@exept.de>
parents: 11281
diff changeset
   367
waitTimeoutSeconds:seconds
eb78ade59632 added: #waitTimeoutSeconds:
Stefan Vogel <sv@exept.de>
parents: 11281
diff changeset
   368
    ^ self waitWithTimeout:seconds
9067
a8b1ac38b89d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8617
diff changeset
   369
! !
a8b1ac38b89d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8617
diff changeset
   370
19927
b99e02676fb6 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 19192
diff changeset
   371
!Semaphore methodsFor:'accessing'!
b99e02676fb6 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 19192
diff changeset
   372
b99e02676fb6 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 19192
diff changeset
   373
owner
19928
e2bf8e38979c #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19927
diff changeset
   374
    "an optional reference to someone who owns this semaphore,
e2bf8e38979c #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19927
diff changeset
   375
     typically a shared queue or a windowgroup or similar.
e2bf8e38979c #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19927
diff changeset
   376
     This has no semantic meaning and is only used to support debugging"
e2bf8e38979c #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19927
diff changeset
   377
     
19927
b99e02676fb6 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 19192
diff changeset
   378
    ^ owner
b99e02676fb6 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 19192
diff changeset
   379
!
b99e02676fb6 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 19192
diff changeset
   380
b99e02676fb6 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 19192
diff changeset
   381
owner:something
19928
e2bf8e38979c #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19927
diff changeset
   382
    "an optional reference to someone who owns this semaphore,
e2bf8e38979c #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19927
diff changeset
   383
     typically a shared queue or a windowgroup or similar.
e2bf8e38979c #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19927
diff changeset
   384
     This has no semantic meaning and is only used to support debugging"
e2bf8e38979c #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19927
diff changeset
   385
19927
b99e02676fb6 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 19192
diff changeset
   386
    owner := something.
b99e02676fb6 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 19192
diff changeset
   387
! !
b99e02676fb6 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 19192
diff changeset
   388
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   389
!Semaphore methodsFor:'printing & storing'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   390
13355
442f69dcc5b8 added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents: 13152
diff changeset
   391
displayOn:aGCOrStream
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   392
    "return a string to display the receiver - include the
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   393
     count for your convenience"
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   394
13355
442f69dcc5b8 added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents: 13152
diff changeset
   395
    "/ what a kludge - Dolphin and Squeak mean: printOn: a stream;
21527
95a27612eafe #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 21190
diff changeset
   396
    "/ old ST80 means: draw-yourself on a GC.
13355
442f69dcc5b8 added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents: 13152
diff changeset
   397
    (aGCOrStream isStream) ifFalse:[
21527
95a27612eafe #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 21190
diff changeset
   398
        ^ super displayOn:aGCOrStream
13355
442f69dcc5b8 added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents: 13152
diff changeset
   399
    ].
442f69dcc5b8 added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents: 13152
diff changeset
   400
    aGCOrStream
24392
aa300a0e1b6c #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 24359
diff changeset
   401
        nextPutAll:self className;
21527
95a27612eafe #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 21190
diff changeset
   402
        nextPut:$(.
13355
442f69dcc5b8 added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents: 13152
diff changeset
   403
    count printOn:aGCOrStream.
442f69dcc5b8 added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents: 13152
diff changeset
   404
    aGCOrStream nextPutAll:' name: '.
442f69dcc5b8 added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents: 13152
diff changeset
   405
    (name ? 'unnamed') printOn:aGCOrStream.
442f69dcc5b8 added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents: 13152
diff changeset
   406
    aGCOrStream nextPut:$).
442f69dcc5b8 added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents: 13152
diff changeset
   407
21527
95a27612eafe #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 21190
diff changeset
   408
    "Modified: / 28-06-1997 / 16:21:09 / cg"
95a27612eafe #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 21190
diff changeset
   409
    "Modified (comment): / 22-02-2017 / 16:47:54 / cg"
24392
aa300a0e1b6c #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 24359
diff changeset
   410
    "Modified: / 28-06-2019 / 09:12:00 / Claus Gittinger"
2265
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   411
!
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   412
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   413
name
24194
8004d231cbba #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24079
diff changeset
   414
    "return the semaphore's userFriendly name (only used by semaphore- and process monitors)"
2265
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   415
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   416
    ^ name
21556
73fd8daf236a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21527
diff changeset
   417
73fd8daf236a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21527
diff changeset
   418
    "Modified (comment): / 23-02-2017 / 16:54:32 / cg"
24194
8004d231cbba #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24079
diff changeset
   419
    "Modified (comment): / 27-05-2019 / 17:31:52 / Claus Gittinger"
2265
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   420
!
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   421
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   422
name:aString
24194
8004d231cbba #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24079
diff changeset
   423
    "set the semaphore's userFriendly name (only used by semaphore- and process monitors)"
2265
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   424
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   425
    name := aString
21556
73fd8daf236a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21527
diff changeset
   426
73fd8daf236a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21527
diff changeset
   427
    "Modified (comment): / 23-02-2017 / 16:54:35 / cg"
24194
8004d231cbba #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24079
diff changeset
   428
    "Modified (comment): / 27-05-2019 / 17:31:44 / Claus Gittinger"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   429
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   430
6986
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   431
!Semaphore methodsFor:'private'!
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   432
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   433
addWaitingProcess:aProcess
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   434
    "add aProcess to the list of waiting processes.
7717
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   435
     all processes are ordered first-come-first-serve.
6986
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   436
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   437
     NOTE: must be called with blocked interrupts"
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   438
7717
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   439
    waitingProcesses isNil ifTrue:[
21190
138b90edd609 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21115
diff changeset
   440
        "for now - assume that there is probably only one waiter"
138b90edd609 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21115
diff changeset
   441
        waitingProcesses := Array with:aProcess
7717
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   442
    ] ifFalse:[
21190
138b90edd609 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21115
diff changeset
   443
        waitingProcesses isArray ifTrue:[
138b90edd609 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21115
diff changeset
   444
            "add 2 to reserve space for additional waiters"
138b90edd609 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21115
diff changeset
   445
            waitingProcesses := (OrderedCollection new:waitingProcesses size + 2)
138b90edd609 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21115
diff changeset
   446
                                    addAll:waitingProcesses;
138b90edd609 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21115
diff changeset
   447
                                    yourself.
138b90edd609 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21115
diff changeset
   448
        ].
138b90edd609 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21115
diff changeset
   449
        waitingProcesses add:aProcess.
7717
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   450
    ].
6986
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   451
7717
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   452
"/    "Sort, so that higher priority process are resumed first.
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   453
"/    Processes having the same priority are ordered first-come-first-serve."
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   454
"/
6986
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   455
"/    |priority insertIndex|
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   456
"/
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   457
"/    (waitingProcesses size == 0
6986
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   458
"/     or:[(priority := aProcess priority) <= waitingProcesses last priority]) ifTrue:[
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   459
"/        waitingProcesses add:aProcess.
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   460
"/        ^ self.
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   461
"/    ].
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   462
"/
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   463
"/    insertIndex := waitingProcesses findFirst:[:process| process priority < priority].
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   464
"/    waitingProcesses add:aProcess beforeIndex:insertIndex.
7717
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   465
!
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   466
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   467
removeWaitingProcess:aProcess
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   468
    "remove aProcess from the list of waiting processes
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   469
     NO action if it is not in the list.
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   470
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   471
     NOTE: must be called with blocked interrupts"
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   472
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   473
    |nWaiting|
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   474
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   475
    nWaiting := waitingProcesses size.
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   476
    nWaiting == 0 ifTrue:[^ self].
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   477
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   478
    nWaiting == 1 ifTrue:[
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   479
	(waitingProcesses at:1) == aProcess ifTrue:[
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   480
	    waitingProcesses := nil.
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   481
	].
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   482
	^ self.
7717
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   483
    ].
8617
5fb5a717deff *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8569
diff changeset
   484
    waitingProcesses removeIdentical:aProcess ifAbsent:[].
11254
a40a7313b9a8 *** empty log message ***
ca
parents: 11218
diff changeset
   485
!
a40a7313b9a8 *** empty log message ***
ca
parents: 11218
diff changeset
   486
a40a7313b9a8 *** empty log message ***
ca
parents: 11218
diff changeset
   487
wakeupWaiters
a40a7313b9a8 *** empty log message ***
ca
parents: 11218
diff changeset
   488
    "remove all waiting processes from the list of waiting processes
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   489
     and resume them.
22076
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   490
     Answer true, if a higher priority process became ready, false if not.
11254
a40a7313b9a8 *** empty log message ***
ca
parents: 11218
diff changeset
   491
     NOTE: Must be called when known that waitingProcesses is nonNil and
22076
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   492
           also with blocked interrupts.
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   493
           Must not perform an operation that causes a reschedule."
11254
a40a7313b9a8 *** empty log message ***
ca
parents: 11218
diff changeset
   494
15635
1b42005ee234 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 15551
diff changeset
   495
    |processes anyDead needsReschedule|
11254
a40a7313b9a8 *** empty log message ***
ca
parents: 11218
diff changeset
   496
13152
78fdc9d96bc3 comment/format in:
Stefan Vogel <sv@exept.de>
parents: 12697
diff changeset
   497
    processes := waitingProcesses.
22080
379aa47c41ad #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22076
diff changeset
   498
    "/ do not set to nil - a waiting process may be suspended and will not be resumed by #makeRunnable: ...
379aa47c41ad #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22076
diff changeset
   499
    "/    waitingProcesses := nil.
11254
a40a7313b9a8 *** empty log message ***
ca
parents: 11218
diff changeset
   500
22076
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   501
    needsReschedule := anyDead := false.
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   502
    processes do:[:eachProcess |
22076
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   503
        (Processor makeRunnable:eachProcess) notNil ifTrue:[
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   504
            "higher priority process became ready"
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   505
            needsReschedule := true.
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   506
        ] ifFalse:[
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   507
            "if process is nil or dead (or for other reasons) makeRunnable returns false.
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   508
             So check here."
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   509
            (eachProcess isNil or:[eachProcess isDead]) ifTrue:[
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   510
                "printing to Transcript might not be a good idea while interrupts are blocked"
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   511
                'Semaphore>>wakeupWaiters: removing a dead process: ' infoPrint. eachProcess infoPrintCR.
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   512
                anyDead := true.
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   513
            ].
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   514
        ].
14509
2c8531dbf456 added: #cleanup
anwild
parents: 13579
diff changeset
   515
    ].
2c8531dbf456 added: #cleanup
anwild
parents: 13579
diff changeset
   516
    anyDead ifTrue:[
22076
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   517
        "interrupts are already blocked by sender"
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   518
        waitingProcesses := processes reject:[:p | p isNil or:[p isDead]]
14509
2c8531dbf456 added: #cleanup
anwild
parents: 13579
diff changeset
   519
    ].
2c8531dbf456 added: #cleanup
anwild
parents: 13579
diff changeset
   520
14770
87973b889eb8 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 14763
diff changeset
   521
    ^ needsReschedule.
22076
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   522
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   523
    "Modified: / 20-02-2017 / 11:34:42 / stefan"
22080
379aa47c41ad #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22076
diff changeset
   524
    "Modified (format): / 24-07-2017 / 18:03:47 / cg"
6986
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   525
! !
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   526
7258
9ccdbee7d1ad method category rename
Claus Gittinger <cg@exept.de>
parents: 6999
diff changeset
   527
!Semaphore methodsFor:'private-accessing'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   528
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   529
clear
21556
73fd8daf236a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21527
diff changeset
   530
    "clear the semaphore's count"
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   531
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   532
    count := 0
21556
73fd8daf236a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21527
diff changeset
   533
73fd8daf236a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21527
diff changeset
   534
    "Modified (comment): / 23-02-2017 / 16:54:28 / cg"
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   535
!
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   536
2407
6b0e21391915 added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 2352
diff changeset
   537
initSignals
6b0e21391915 added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 2352
diff changeset
   538
    "set the count of the semaphore to zero.
6b0e21391915 added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 2352
diff changeset
   539
     provided for ST-80 compatibility."
6b0e21391915 added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 2352
diff changeset
   540
6b0e21391915 added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 2352
diff changeset
   541
    count := 0
6b0e21391915 added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 2352
diff changeset
   542
6b0e21391915 added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 2352
diff changeset
   543
    "Created: 17.2.1997 / 11:31:19 / cg"
6b0e21391915 added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 2352
diff changeset
   544
!
6b0e21391915 added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 2352
diff changeset
   545
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   546
setCount:n
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   547
    "set the count of the semaphore;
20426
2ec010b1a559 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 20263
diff changeset
   548
     that's the number of possible waits, without blocking"
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   549
7717
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   550
    waitingProcesses := nil.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   551
    count := n
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   552
2407
6b0e21391915 added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 2352
diff changeset
   553
    "Modified: 17.2.1997 / 11:36:40 / cg"
22171
fe0e08536dd2 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22155
diff changeset
   554
!
fe0e08536dd2 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22155
diff changeset
   555
fe0e08536dd2 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22155
diff changeset
   556
setCount:n name:aString
fe0e08536dd2 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22155
diff changeset
   557
    "set the count of the semaphore;
fe0e08536dd2 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22155
diff changeset
   558
     that's the number of possible waits, without blocking"
fe0e08536dd2 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22155
diff changeset
   559
fe0e08536dd2 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22155
diff changeset
   560
    waitingProcesses := nil.
fe0e08536dd2 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22155
diff changeset
   561
    count := n.
fe0e08536dd2 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22155
diff changeset
   562
    name := aString.
fe0e08536dd2 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22155
diff changeset
   563
fe0e08536dd2 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22155
diff changeset
   564
    "Created: / 09-08-2017 / 11:48:00 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   565
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   566
5256
1209e167d63e category rename
Claus Gittinger <cg@exept.de>
parents: 4902
diff changeset
   567
!Semaphore methodsFor:'queries'!
217
a0400fdbc933 *** empty log message ***
claus
parents: 183
diff changeset
   568
2235
c6a15bd9a33c added access to count
Claus Gittinger <cg@exept.de>
parents: 2234
diff changeset
   569
count
c6a15bd9a33c added access to count
Claus Gittinger <cg@exept.de>
parents: 2234
diff changeset
   570
    "return the number of 'already-counted' trigger events.
20426
2ec010b1a559 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 20263
diff changeset
   571
     That's the number of waits which will succeed without blocking"
2235
c6a15bd9a33c added access to count
Claus Gittinger <cg@exept.de>
parents: 2234
diff changeset
   572
c6a15bd9a33c added access to count
Claus Gittinger <cg@exept.de>
parents: 2234
diff changeset
   573
    ^ count
c6a15bd9a33c added access to count
Claus Gittinger <cg@exept.de>
parents: 2234
diff changeset
   574
c6a15bd9a33c added access to count
Claus Gittinger <cg@exept.de>
parents: 2234
diff changeset
   575
    "Created: 23.1.1997 / 02:55:58 / cg"
c6a15bd9a33c added access to count
Claus Gittinger <cg@exept.de>
parents: 2234
diff changeset
   576
!
c6a15bd9a33c added access to count
Claus Gittinger <cg@exept.de>
parents: 2234
diff changeset
   577
13579
db80734f9576 changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents: 13355
diff changeset
   578
lastOwner
db80734f9576 changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents: 13355
diff changeset
   579
    "return the last owning process or nil
db80734f9576 changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents: 13355
diff changeset
   580
     (the one which counted to zero).
db80734f9576 changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents: 13355
diff changeset
   581
     May be very useful in debugging deadLock situations"
db80734f9576 changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents: 13355
diff changeset
   582
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   583
    ^ Processor processWithId:lastOwnerId.
13579
db80734f9576 changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents: 13355
diff changeset
   584
db80734f9576 changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents: 13355
diff changeset
   585
    "Created: / 11-08-2011 / 14:35:36 / cg"
db80734f9576 changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents: 13355
diff changeset
   586
!
db80734f9576 changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents: 13355
diff changeset
   587
2265
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   588
lastOwnerId
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   589
    "return the processId of the last owning process
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   590
     (the one which counted to zero).
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   591
     May be very useful in debugging deadLock situations"
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   592
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   593
    ^ lastOwnerId
2265
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   594
13579
db80734f9576 changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents: 13355
diff changeset
   595
    "Created: / 24-01-1997 / 23:09:33 / cg"
2265
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   596
!
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   597
1329
0a025ca40653 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   598
numberOfWaitingProcesses
0a025ca40653 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   599
    "return the number of processes waiting on the receiver"
0a025ca40653 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   600
0a025ca40653 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   601
    ^ waitingProcesses size
0a025ca40653 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   602
0a025ca40653 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   603
    "Created: 3.5.1996 / 18:06:27 / cg"
0a025ca40653 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   604
!
0a025ca40653 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   605
1577
720f14f90e2c access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents: 1329
diff changeset
   606
waitingProcesses
720f14f90e2c access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents: 1329
diff changeset
   607
    "return the processes waiting on the receiver"
720f14f90e2c access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents: 1329
diff changeset
   608
7717
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   609
    ^ waitingProcesses ? #()
1577
720f14f90e2c access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents: 1329
diff changeset
   610
720f14f90e2c access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents: 1329
diff changeset
   611
    "Created: 18.7.1996 / 20:53:33 / cg"
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   612
! !
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   613
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   614
!Semaphore methodsFor:'semaphoreSet interface'!
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   615
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   616
checkAndAddWaitingProcess:process
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   617
    "interface for SemaphoreSet.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   618
     If the semaphore is available, decrement it and return true.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   619
     Otherwise register our process to be wakened up once the semaphore is available
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   620
     and return false.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   621
     ATTENTION: this must be invoked with OperatingSystem-interrupts-blocked.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   622
    "
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   623
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   624
    count > 0 ifTrue:[
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   625
	count := count - 1.
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   626
	count == 0 ifTrue:[
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   627
	    lastOwnerId := Processor activeProcessId.
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   628
	].
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   629
	^ true
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   630
    ].
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   631
    (waitingProcesses notNil and:[(waitingProcesses includesIdentical:process)]) ifFalse:[
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   632
	self addWaitingProcess:process.
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   633
    ].
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   634
    ^ false
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   635
13579
db80734f9576 changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents: 13355
diff changeset
   636
    "Modified: / 14-12-1995 / 10:32:17 / stefan"
db80734f9576 changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents: 13355
diff changeset
   637
    "Modified: / 11-08-2011 / 14:36:20 / cg"
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   638
! !
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   639
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   640
!Semaphore methodsFor:'signaling'!
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   641
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   642
signal
22076
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   643
    "waking up the highest prio waiter."
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   644
14770
87973b889eb8 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 14763
diff changeset
   645
    |wasBlocked needsReschedule|
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   646
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   647
    wasBlocked := OperatingSystem blockInterrupts.
22076
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   648
    count := count + 1.
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   649
    needsReschedule := waitingProcesses notEmptyOrNil and:[self wakeupWaiters].
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   650
    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   651
14770
87973b889eb8 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 14763
diff changeset
   652
    needsReschedule ifTrue:[
22076
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   653
        "now, schedule the highest priority process"
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   654
        Processor reschedule.
14770
87973b889eb8 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 14763
diff changeset
   655
    ].
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   656
22076
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   657
    "Modified: / 04-02-1998 / 21:01:07 / cg"
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   658
    "Modified: / 24-07-2017 / 11:44:15 / stefan"
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   659
!
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   660
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   661
signal:anInteger
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   662
    "increment semaphore by anInteger waking up the highest prio waiters."
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   663
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   664
    |wasBlocked needsReschedule|
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   665
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   666
    wasBlocked := OperatingSystem blockInterrupts.
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   667
    count := count + anInteger.
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   668
    needsReschedule := waitingProcesses notEmptyOrNil and:[self wakeupWaiters].
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   669
    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   670
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   671
    needsReschedule ifTrue:[
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   672
        "now, schedule the highest priority process"
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   673
        Processor reschedule.
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   674
    ].
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   675
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   676
    "Created: / 22-02-2017 / 14:38:33 / stefan"
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   677
    "Modified: / 24-07-2017 / 11:44:36 / stefan"
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   678
!
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   679
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   680
signalForAll
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   681
    "signal the semaphore for all waiters.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   682
     This can be used for process synchronization, if multiple processes are
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   683
     waiting for a common event."
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   684
14770
87973b889eb8 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 14763
diff changeset
   685
    |wasBlocked needsReschedule|
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   686
22076
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   687
    waitingProcesses notEmptyOrNil ifTrue:[
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   688
        needsReschedule := false.
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   689
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   690
        wasBlocked := OperatingSystem blockInterrupts.
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   691
        "first, make them all runnable, but do not schedule
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   692
         (in case one has higher prio and goes into a wait immediately again.)"
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   693
        waitingProcesses notEmptyOrNil ifTrue:[
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   694
            needsReschedule := self wakeupWaiters.
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   695
            "wakeupWaites may have removed dead processes from waitingProcesses!!"
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   696
            count := count + waitingProcesses size.
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   697
        ].
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   698
        wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   699
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   700
        needsReschedule ifTrue:[
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   701
            "now, schedule the highest priority process"
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   702
            Processor reschedule.
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   703
        ].
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   704
    ]
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   705
22076
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   706
    "Modified: / 05-02-1998 / 10:40:26 / cg"
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   707
    "Modified: / 24-07-2017 / 11:44:49 / stefan"
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   708
!
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   709
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   710
signalIf
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   711
    "signal the semaphore, but only if being waited upon.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   712
     This can be used for one-shot semaphores (i.e. not remembering
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   713
     previous signals)"
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   714
22076
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   715
    |wasBlocked needsReschedule|
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   716
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   717
    waitingProcesses notNil ifTrue:[
22076
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   718
        needsReschedule := false.
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   719
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   720
        wasBlocked := OperatingSystem blockInterrupts.
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   721
        waitingProcesses notEmptyOrNil ifTrue:[
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   722
            count := count + 1.
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   723
            needsReschedule := self wakeupWaiters.
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   724
        ].
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   725
        wasBlocked ifFalse:[
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   726
            OperatingSystem unblockInterrupts.
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   727
        ].
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   728
        needsReschedule ifTrue:[
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   729
            "now, schedule the highest priority process"
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   730
            Processor reschedule.
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   731
        ].
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   732
    ]
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   733
22076
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   734
    "Modified: / 28-02-1996 / 21:23:57 / cg"
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   735
    "Modified: / 24-07-2017 / 11:45:20 / stefan"
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   736
!
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   737
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   738
signalIfWithReturn
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   739
    "signal the semaphore, but only if being waited upon.
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   740
     This can be used for one-shot semaphores (i.e. not remembering
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   741
     previous signals).
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   742
     Answer true if a reschedule is needed, false if not."
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   743
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   744
    |wasBlocked needsReschedule|
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   745
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   746
    needsReschedule := false.
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   747
    waitingProcesses notNil ifTrue:[
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   748
        wasBlocked := OperatingSystem blockInterrupts.
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   749
        waitingProcesses notEmptyOrNil ifTrue:[
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   750
            count := count + 1.
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   751
            needsReschedule := self wakeupWaiters.
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   752
        ].
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   753
        wasBlocked ifFalse:[
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   754
            OperatingSystem unblockInterrupts.
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   755
        ].
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   756
    ].
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   757
    ^ needsReschedule.
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   758
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   759
    "Created: / 19-02-2017 / 18:09:23 / stefan"
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   760
    "Modified: / 24-07-2017 / 11:45:25 / stefan"
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   761
!
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   762
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   763
signalOnce
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   764
    "wakeup waiters - but only once.
9462
822bd68c3c45 new: #consume
Stefan Vogel <sv@exept.de>
parents: 9067
diff changeset
   765
     I.e. if the semaphore has already been signaled, this is ignored."
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   766
14770
87973b889eb8 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 14763
diff changeset
   767
    |wasBlocked needsReschedule|
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   768
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   769
    count <= 0 ifTrue:[
22076
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   770
        needsReschedule := false.
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   771
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   772
        wasBlocked := OperatingSystem blockInterrupts.
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   773
        "/ check again - now interrupts are blocked.
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   774
        count <= 0 ifTrue:[
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   775
            count := count + 1.
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   776
            needsReschedule := (count == 1
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   777
                                and:[waitingProcesses notEmptyOrNil 
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   778
                                and:[self wakeupWaiters]]).
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   779
        ].
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   780
        wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   781
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   782
        needsReschedule ifTrue:[
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   783
            "now, schedule the highest priority process"
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   784
            Processor reschedule.
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   785
        ].
14770
87973b889eb8 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 14763
diff changeset
   786
    ].
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   787
22076
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   788
    "Modified: / 28-02-1996 / 21:24:08 / cg"
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   789
    "Modified: / 24-07-2017 / 11:46:07 / stefan"
22093
be39998ccf1b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22080
diff changeset
   790
!
be39998ccf1b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22080
diff changeset
   791
be39998ccf1b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22080
diff changeset
   792
signalOnceWithoutReschedule
be39998ccf1b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22080
diff changeset
   793
    "wakeup waiters - but only once.
be39998ccf1b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22080
diff changeset
   794
     I.e. if the semaphore has already been signaled, this is ignored."
be39998ccf1b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22080
diff changeset
   795
be39998ccf1b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22080
diff changeset
   796
    |wasBlocked needsReschedule|
be39998ccf1b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22080
diff changeset
   797
be39998ccf1b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22080
diff changeset
   798
    count <= 0 ifTrue:[
be39998ccf1b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22080
diff changeset
   799
        wasBlocked := OperatingSystem blockInterrupts.
be39998ccf1b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22080
diff changeset
   800
        "/ check again - now interrupts are blocked.
be39998ccf1b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22080
diff changeset
   801
        count <= 0 ifTrue:[
be39998ccf1b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22080
diff changeset
   802
            count < 0 ifTrue:[
be39998ccf1b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22080
diff changeset
   803
                'oops - bad sema count' infoPrintCR.
be39998ccf1b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22080
diff changeset
   804
                count := 0.
be39998ccf1b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22080
diff changeset
   805
            ].
be39998ccf1b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22080
diff changeset
   806
            
be39998ccf1b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22080
diff changeset
   807
            count := count + 1.
be39998ccf1b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22080
diff changeset
   808
            needsReschedule := (count == 1
be39998ccf1b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22080
diff changeset
   809
                                and:[waitingProcesses notEmptyOrNil 
be39998ccf1b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22080
diff changeset
   810
                                and:[self wakeupWaiters]]).
be39998ccf1b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22080
diff changeset
   811
        ].
be39998ccf1b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22080
diff changeset
   812
        wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
be39998ccf1b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22080
diff changeset
   813
be39998ccf1b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22080
diff changeset
   814
        ^ needsReschedule
be39998ccf1b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22080
diff changeset
   815
    ].
be39998ccf1b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22080
diff changeset
   816
    ^ false
be39998ccf1b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22080
diff changeset
   817
be39998ccf1b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22080
diff changeset
   818
    "Created: / 25-07-2017 / 11:26:23 / cg"
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   819
! !
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   820
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   821
!Semaphore methodsFor:'testing'!
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   822
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   823
isEmpty
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   824
    "ST80 compatibility - return true if there are no waiters"
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   825
22076
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   826
    ^ waitingProcesses isEmptyOrNil
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   827
22076
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   828
    "Created: / 03-05-1996 / 18:06:27 / cg"
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   829
    "Modified: / 18-06-1998 / 16:07:38 / cg"
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   830
    "Modified: / 24-07-2017 / 11:43:35 / stefan"
1577
720f14f90e2c access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents: 1329
diff changeset
   831
!
720f14f90e2c access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents: 1329
diff changeset
   832
217
a0400fdbc933 *** empty log message ***
claus
parents: 183
diff changeset
   833
wouldBlock
a0400fdbc933 *** empty log message ***
claus
parents: 183
diff changeset
   834
    "return true, if the receiver would block the activeProcess
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   835
     if a wait was performed. False otherwise.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   836
     Attention: if asked without some global lock (blockedInterrupts),
15551
8ee0431a2f39 class: Semaphore
Claus Gittinger <cg@exept.de>
parents: 14773
diff changeset
   837
     the returned value may be outdated right away."
217
a0400fdbc933 *** empty log message ***
claus
parents: 183
diff changeset
   838
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   839
    ^ count <= 0
217
a0400fdbc933 *** empty log message ***
claus
parents: 183
diff changeset
   840
! !
a0400fdbc933 *** empty log message ***
claus
parents: 183
diff changeset
   841
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   842
!Semaphore methodsFor:'waiting'!
5713
993805688c7f added #clear
Claus Gittinger <cg@exept.de>
parents: 5556
diff changeset
   843
9462
822bd68c3c45 new: #consume
Stefan Vogel <sv@exept.de>
parents: 9067
diff changeset
   844
consume
822bd68c3c45 new: #consume
Stefan Vogel <sv@exept.de>
parents: 9067
diff changeset
   845
    "consume the resource without waiting.
822bd68c3c45 new: #consume
Stefan Vogel <sv@exept.de>
parents: 9067
diff changeset
   846
     This works even if the count is 0 (count may become negative).
822bd68c3c45 new: #consume
Stefan Vogel <sv@exept.de>
parents: 9067
diff changeset
   847
     Answer the new count afterwards"
822bd68c3c45 new: #consume
Stefan Vogel <sv@exept.de>
parents: 9067
diff changeset
   848
822bd68c3c45 new: #consume
Stefan Vogel <sv@exept.de>
parents: 9067
diff changeset
   849
    ^ self consume:1
822bd68c3c45 new: #consume
Stefan Vogel <sv@exept.de>
parents: 9067
diff changeset
   850
!
822bd68c3c45 new: #consume
Stefan Vogel <sv@exept.de>
parents: 9067
diff changeset
   851
822bd68c3c45 new: #consume
Stefan Vogel <sv@exept.de>
parents: 9067
diff changeset
   852
consume:n
822bd68c3c45 new: #consume
Stefan Vogel <sv@exept.de>
parents: 9067
diff changeset
   853
    "consume the resource n times without waiting.
822bd68c3c45 new: #consume
Stefan Vogel <sv@exept.de>
parents: 9067
diff changeset
   854
     This works even if the count is 0 (count may become negative).
822bd68c3c45 new: #consume
Stefan Vogel <sv@exept.de>
parents: 9067
diff changeset
   855
     Answer the new count afterwards"
822bd68c3c45 new: #consume
Stefan Vogel <sv@exept.de>
parents: 9067
diff changeset
   856
822bd68c3c45 new: #consume
Stefan Vogel <sv@exept.de>
parents: 9067
diff changeset
   857
    |wasBlocked|
822bd68c3c45 new: #consume
Stefan Vogel <sv@exept.de>
parents: 9067
diff changeset
   858
822bd68c3c45 new: #consume
Stefan Vogel <sv@exept.de>
parents: 9067
diff changeset
   859
    wasBlocked := OperatingSystem blockInterrupts.
822bd68c3c45 new: #consume
Stefan Vogel <sv@exept.de>
parents: 9067
diff changeset
   860
    count := count - n.
822bd68c3c45 new: #consume
Stefan Vogel <sv@exept.de>
parents: 9067
diff changeset
   861
    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
822bd68c3c45 new: #consume
Stefan Vogel <sv@exept.de>
parents: 9067
diff changeset
   862
822bd68c3c45 new: #consume
Stefan Vogel <sv@exept.de>
parents: 9067
diff changeset
   863
    ^ count
822bd68c3c45 new: #consume
Stefan Vogel <sv@exept.de>
parents: 9067
diff changeset
   864
!
822bd68c3c45 new: #consume
Stefan Vogel <sv@exept.de>
parents: 9067
diff changeset
   865
22238
ce1753b20f6d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22171
diff changeset
   866
consumeIfPossible
ce1753b20f6d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22171
diff changeset
   867
    "if the semaphore is currently free,
ce1753b20f6d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22171
diff changeset
   868
     acquire it, lock it and return true.
ce1753b20f6d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22171
diff changeset
   869
     Otherwise, do not wait, but return false immediately."
ce1753b20f6d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22171
diff changeset
   870
ce1753b20f6d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22171
diff changeset
   871
    |wasBlocked|
ce1753b20f6d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22171
diff changeset
   872
ce1753b20f6d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22171
diff changeset
   873
    wasBlocked := OperatingSystem blockInterrupts.
ce1753b20f6d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22171
diff changeset
   874
    count <= 0 ifTrue:[
ce1753b20f6d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22171
diff changeset
   875
        wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
ce1753b20f6d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22171
diff changeset
   876
        ^ false.
ce1753b20f6d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22171
diff changeset
   877
    ].    
ce1753b20f6d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22171
diff changeset
   878
ce1753b20f6d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22171
diff changeset
   879
    "if we come here, we have acquired the semaphore"
ce1753b20f6d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22171
diff changeset
   880
    count := count - 1.
ce1753b20f6d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22171
diff changeset
   881
    count == 0 ifTrue:[
ce1753b20f6d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22171
diff changeset
   882
        lastOwnerId := Processor activeProcessId.
ce1753b20f6d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22171
diff changeset
   883
    ].
ce1753b20f6d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22171
diff changeset
   884
    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
ce1753b20f6d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22171
diff changeset
   885
    ^ true
ce1753b20f6d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22171
diff changeset
   886
ce1753b20f6d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22171
diff changeset
   887
    "Created: / 31-08-2017 / 23:05:19 / cg"
ce1753b20f6d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22171
diff changeset
   888
!
ce1753b20f6d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22171
diff changeset
   889
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   890
critical:aBlock
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   891
    "evaluate aBlock as a critical region; the receiver must be
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   892
     created using Semaphore>>forMutualExclusion"
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   893
22076
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   894
    |retVal wasBlocked needsReschedule gotSema|
7717
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   895
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   896
    wasBlocked := OperatingSystem blockInterrupts.
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   897
22076
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   898
    count > 0 ifTrue:[
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   899
        "/ inlined common case when no wait is required - ugly kludge but helpful
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   900
        count := count - 1.
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   901
        count == 0 ifTrue:[
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   902
            lastOwnerId := Processor activeProcessId.
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   903
        ].
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   904
        wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   905
        retVal := aBlock ifCurtailed:[self signal].
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   906
    ] ifFalse:[
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   907
        "have to wait for Semaphore availability"
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   908
        retVal := [
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   909
            gotSema := self wait.
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   910
            wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   911
            aBlock value.
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   912
        ] ifCurtailed:[
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   913
            wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   914
            gotSema notNil ifTrue:[self signal].
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   915
        ].
7717
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   916
    ].
22076
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   917
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   918
    "this is inlined code from #signal"
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   919
    OperatingSystem blockInterrupts.
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   920
    count := count + 1.
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   921
    needsReschedule := waitingProcesses notEmptyOrNil and:[self wakeupWaiters].
7717
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   922
    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   923
22076
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   924
    needsReschedule ifTrue:[
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   925
        "now, schedule the highest priority process"
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   926
        Processor reschedule.
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   927
    ].
22076
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   928
    "end inlined code from #signal"
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   929
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   930
    ^ retVal.
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   931
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   932
    "
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   933
      the example below is stupid (it should use a SharedQueue,
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   934
      or at least a Queue with critical regions).
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   935
      Anyhow, it demonstrates how two processes lock each other
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   936
      from accessing coll at the same time
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   937
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   938
     |sema coll|
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   939
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   940
     sema := Semaphore forMutualExclusion.
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   941
     coll := OrderedCollection new:10.
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   942
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   943
     [
22076
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   944
        1 to:1000 do:[:i |
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   945
            sema critical:[
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   946
                coll addLast:i.
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   947
                (Delay forSeconds:0.1) wait.
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   948
            ]
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   949
        ]
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   950
     ] forkAt:4.
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   951
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   952
     [
22076
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   953
        1 to:1000 do:[:i |
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   954
            sema critical:[
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   955
                coll removeFirst.
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   956
                (Delay forSeconds:0.1) wait.
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   957
            ]
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   958
        ]
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   959
     ] forkAt:4.
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   960
    "
1185
2b24258b4f24 Fix in critical: don't signal a semaphore that we didn't get when unwinding.
Stefan Vogel <sv@exept.de>
parents: 1122
diff changeset
   961
13579
db80734f9576 changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents: 13355
diff changeset
   962
    "Modified: / 11-08-2011 / 14:36:30 / cg"
22076
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   963
    "Modified: / 24-07-2017 / 11:42:47 / stefan"
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   964
!
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   965
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   966
critical:aBlock ifBlocking:blockingBlock
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   967
    "like critical:, but do not block if the lock cannot be acquired.
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   968
     Instead, return the value of the second argument, blockingBlock."
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   969
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   970
    ^ self critical:aBlock timeoutMs:0 ifBlocking:blockingBlock.
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   971
!
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   972
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   973
critical:aBlock timeoutMs:timeoutMs ifBlocking:blockingBlock
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   974
    "like critical:, but do not block if the lock cannot be acquired 
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   975
     within timeoutMs milliseconds.
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   976
     Instead, return the value of blockingBlock."
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   977
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   978
    |retVal wasBlocked needsReschedule gotSema|
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   979
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   980
    wasBlocked := OperatingSystem blockInterrupts.
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   981
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   982
    count > 0 ifTrue:[
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   983
        "/ inlined common case when no wait is required - ugly kludge but helpful
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   984
        count := count - 1.
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   985
        count == 0 ifTrue:[
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   986
            lastOwnerId := Processor activeProcessId.
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   987
        ].
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   988
        wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   989
        retVal := aBlock ifCurtailed:[self signal].
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   990
        gotSema := true.
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   991
    ] ifFalse:[
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   992
        "have to wait for Semaphore availability"
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   993
        timeoutMs ~= 0 ifTrue:[
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   994
            retVal := [
22080
379aa47c41ad #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22076
diff changeset
   995
                gotSema := self waitWithTimeoutMs:timeoutMs state:#wait.
22076
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   996
                gotSema notNil ifTrue:[
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   997
                    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   998
                    aBlock value.
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
   999
                ].
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
  1000
            ] ifCurtailed:[
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
  1001
                wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
  1002
                gotSema notNil ifTrue:[self signal].
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
  1003
            ].
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
  1004
        ].
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
  1005
    ].
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
  1006
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
  1007
    gotSema notNil ifTrue:[
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
  1008
        "this is inlined code from #signal"
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
  1009
        OperatingSystem blockInterrupts.
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
  1010
        count := count + 1.
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
  1011
        needsReschedule := waitingProcesses notEmptyOrNil and:[self wakeupWaiters].
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
  1012
        wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
  1013
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
  1014
        needsReschedule ifTrue:[
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
  1015
            "now, schedule the highest priority process"
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
  1016
            Processor reschedule.
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
  1017
        ].
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
  1018
        "end inlined code from #signal"
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
  1019
    ] ifFalse:[
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
  1020
        wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
  1021
        retVal := blockingBlock value.
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
  1022
    ].
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
  1023
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
  1024
    ^ retVal.
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
  1025
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
  1026
    "Created: / 18-02-2017 / 22:15:25 / stefan"
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
  1027
    "Modified: / 24-07-2017 / 11:43:19 / stefan"
22080
379aa47c41ad #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22076
diff changeset
  1028
    "Modified: / 24-07-2017 / 21:17:47 / cg"
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
  1029
!
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
  1030
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1031
wait
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1032
    "wait for the semaphore"
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1033
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
  1034
    |activeProcess wasBlocked|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1035
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
  1036
    wasBlocked := OperatingSystem blockInterrupts.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
  1037
    count <= 0 ifTrue:[
16631
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1038
        activeProcess := Processor activeProcess.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1039
        "
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1040
         need a while-loop here, since more than one process may
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1041
         wait for it and another one may also wake up.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1042
         Thus, the count is not always non-zero after returning from
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1043
         suspend.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1044
        "
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1045
        [
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1046
            self addWaitingProcess:activeProcess.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1047
            "
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1048
             for some more descriptive info in processMonitor ...
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1049
             ... set the state to #wait (instead of #suspend)
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1050
            "
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1051
            [
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1052
                activeProcess suspendWithState:#wait
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1053
            ] ifCurtailed:[
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1054
                "interrupts are not blocked when entered through Processor>>#interruptActive"
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1055
                OperatingSystem blockInterrupts.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1056
                self removeWaitingProcess:activeProcess.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1057
                wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1058
            ].
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1059
            self removeWaitingProcess:activeProcess.
16286
4fd3aa76f29e class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 15690
diff changeset
  1060
16631
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1061
            count <= 0
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1062
        ] whileTrue.
93
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
  1063
    ].
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
  1064
22076
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
  1065
    "if we come here, we have acquired the semaphore"
10
claus
parents: 3
diff changeset
  1066
    count := count - 1.
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
  1067
    count == 0 ifTrue:[
16631
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1068
        lastOwnerId := Processor activeProcessId.
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
  1069
    ].
93
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
  1070
    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
752
0259dd855289 new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
  1071
13579
db80734f9576 changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents: 13355
diff changeset
  1072
    "Modified: / 13-12-1995 / 13:26:33 / stefan"
22076
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
  1073
    "Modified (format): / 21-02-2017 / 15:29:51 / stefan"
22080
379aa47c41ad #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22076
diff changeset
  1074
    "Modified: / 24-07-2017 / 23:04:09 / cg"
10
claus
parents: 3
diff changeset
  1075
!
claus
parents: 3
diff changeset
  1076
329
claus
parents: 302
diff changeset
  1077
waitUncounted
claus
parents: 302
diff changeset
  1078
    "wait for the semaphore; do not consume the resource
claus
parents: 302
diff changeset
  1079
     (i.e. do not count down)"
claus
parents: 302
diff changeset
  1080
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
  1081
    |activeProcess wasBlocked|
329
claus
parents: 302
diff changeset
  1082
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
  1083
    count > 0 ifTrue:[
22080
379aa47c41ad #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22076
diff changeset
  1084
        ^ self
329
claus
parents: 302
diff changeset
  1085
    ].
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
  1086
    activeProcess := Processor activeProcess.
329
claus
parents: 302
diff changeset
  1087
claus
parents: 302
diff changeset
  1088
    wasBlocked := OperatingSystem blockInterrupts.
claus
parents: 302
diff changeset
  1089
    "
claus
parents: 302
diff changeset
  1090
     need a while-loop here, since more than one process may
claus
parents: 302
diff changeset
  1091
     wait for it and another one may also wake up.
claus
parents: 302
diff changeset
  1092
     Thus, the count is not always non-zero after returning from
claus
parents: 302
diff changeset
  1093
     suspend.
claus
parents: 302
diff changeset
  1094
    "
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
  1095
    [count <= 0] whileTrue:[
22080
379aa47c41ad #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22076
diff changeset
  1096
        self addWaitingProcess:activeProcess.
379aa47c41ad #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22076
diff changeset
  1097
        "
379aa47c41ad #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22076
diff changeset
  1098
         for some more descriptive info in processMonitor ...
379aa47c41ad #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22076
diff changeset
  1099
         ... set the state to #wait (instead of #suspend)
379aa47c41ad #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22076
diff changeset
  1100
        "
379aa47c41ad #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22076
diff changeset
  1101
        [
379aa47c41ad #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22076
diff changeset
  1102
            activeProcess suspendWithState:#wait
379aa47c41ad #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22076
diff changeset
  1103
        ] ifCurtailed:[
379aa47c41ad #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22076
diff changeset
  1104
            "interrupts are not blocked when entered through Processor>>#interruptActive"
379aa47c41ad #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22076
diff changeset
  1105
            OperatingSystem blockInterrupts.
379aa47c41ad #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22076
diff changeset
  1106
            self removeWaitingProcess:activeProcess.
379aa47c41ad #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22076
diff changeset
  1107
            wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
379aa47c41ad #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22076
diff changeset
  1108
        ].
379aa47c41ad #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22076
diff changeset
  1109
        self removeWaitingProcess:activeProcess.
329
claus
parents: 302
diff changeset
  1110
    ].
claus
parents: 302
diff changeset
  1111
    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
752
0259dd855289 new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
  1112
22080
379aa47c41ad #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22076
diff changeset
  1113
    "Modified: / 13-12-1995 / 13:26:49 / stefan"
379aa47c41ad #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22076
diff changeset
  1114
    "Modified: / 24-07-2017 / 23:04:33 / cg"
329
claus
parents: 302
diff changeset
  1115
!
claus
parents: 302
diff changeset
  1116
24079
8e09e33e97b8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 23231
diff changeset
  1117
waitUncountedWithTimeout:secondsOrNilOrTimeDuration
8e09e33e97b8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 23231
diff changeset
  1118
    "wait for the semaphore, but abort the wait after some time (seconds).
8e09e33e97b8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 23231
diff changeset
  1119
     return the receiver if the semaphore triggered normal, nil if we return
8e09e33e97b8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 23231
diff changeset
  1120
     due to a timeout.
24359
13a53955244e #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24358
diff changeset
  1121
     The argument may be a time duration or the number of seconds as integer
13a53955244e #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24358
diff changeset
  1122
     or float (i.e. use 0.1 for a 100ms timeout).
24079
8e09e33e97b8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 23231
diff changeset
  1123
     With zero timeout, this can be used to poll a semaphore (returning
8e09e33e97b8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 23231
diff changeset
  1124
     the receiver if the semaphore is available, nil if not).
8e09e33e97b8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 23231
diff changeset
  1125
     However, polling is not the intended use of semaphores, though.
8e09e33e97b8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 23231
diff changeset
  1126
     If seconds is nil, wait without timeout."
8e09e33e97b8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 23231
diff changeset
  1127
8e09e33e97b8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 23231
diff changeset
  1128
    <resource: #skipInDebuggersWalkBack>
8e09e33e97b8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 23231
diff changeset
  1129
8e09e33e97b8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 23231
diff changeset
  1130
    |millis|
8e09e33e97b8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 23231
diff changeset
  1131
8e09e33e97b8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 23231
diff changeset
  1132
    secondsOrNilOrTimeDuration notNil ifTrue:[
8e09e33e97b8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 23231
diff changeset
  1133
        secondsOrNilOrTimeDuration isNumber ifTrue:[
8e09e33e97b8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 23231
diff changeset
  1134
            millis := (secondsOrNilOrTimeDuration * 1000) asInteger.
8e09e33e97b8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 23231
diff changeset
  1135
        ] ifFalse:[
8e09e33e97b8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 23231
diff changeset
  1136
            "a TimeDuration"
8e09e33e97b8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 23231
diff changeset
  1137
            millis := secondsOrNilOrTimeDuration asTruncatedMilliseconds.
8e09e33e97b8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 23231
diff changeset
  1138
        ].
8e09e33e97b8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 23231
diff changeset
  1139
    ].
8e09e33e97b8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 23231
diff changeset
  1140
8e09e33e97b8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 23231
diff changeset
  1141
    ^ self waitUncountedWithTimeoutMs:millis state:#wait.
8e09e33e97b8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 23231
diff changeset
  1142
8e09e33e97b8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 23231
diff changeset
  1143
    "Created: / 15-04-2019 / 12:12:52 / Stefan Vogel"
24359
13a53955244e #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24358
diff changeset
  1144
    "Modified (comment): / 25-06-2019 / 14:25:52 / Claus Gittinger"
24079
8e09e33e97b8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 23231
diff changeset
  1145
!
8e09e33e97b8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 23231
diff changeset
  1146
24359
13a53955244e #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24358
diff changeset
  1147
waitUncountedWithTimeoutMs:milliSecondsOrNil
16903
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1148
    "wait for the semaphore; do not consume the resource
20263
9f3ddb907b77 #OTHER by mawalch
mawalch
parents: 20116
diff changeset
  1149
     (i.e. do not count down).
16903
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1150
     Abort the wait after some time.
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1151
     return the receiver if the semaphore triggered normal, nil if we return
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1152
     due to a timeout.
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1153
     With zero timeout, this can be used to poll a semaphore (returning
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1154
     the receiver if the semaphore is available, nil if not).
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1155
     However, polling is not the intended use of semaphores, though.
24359
13a53955244e #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24358
diff changeset
  1156
     If milliSecondsOrNil is nil, wait without timeout."
16903
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1157
23036
e11ebfd9362c #UI_ENHANCEMENT by cg
Claus Gittinger <cg@exept.de>
parents: 22238
diff changeset
  1158
    <resource: #skipInDebuggersWalkBack>
e11ebfd9362c #UI_ENHANCEMENT by cg
Claus Gittinger <cg@exept.de>
parents: 22238
diff changeset
  1159
24359
13a53955244e #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24358
diff changeset
  1160
    self waitUncountedWithTimeoutMs:milliSecondsOrNil state:#wait
22080
379aa47c41ad #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22076
diff changeset
  1161
379aa47c41ad #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22076
diff changeset
  1162
    "Modified: / 13-12-1995 / 13:27:24 / stefan"
379aa47c41ad #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22076
diff changeset
  1163
    "Modified: / 24-07-2017 / 21:53:57 / cg"
23036
e11ebfd9362c #UI_ENHANCEMENT by cg
Claus Gittinger <cg@exept.de>
parents: 22238
diff changeset
  1164
    "Modified: / 30-05-2018 / 13:57:16 / Claus Gittinger"
24359
13a53955244e #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24358
diff changeset
  1165
    "Modified (comment): / 25-06-2019 / 14:24:26 / Claus Gittinger"
22080
379aa47c41ad #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22076
diff changeset
  1166
!
379aa47c41ad #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22076
diff changeset
  1167
24359
13a53955244e #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24358
diff changeset
  1168
waitUncountedWithTimeoutMs:milliSecondsOrNil state:newStateSymbol
22080
379aa47c41ad #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22076
diff changeset
  1169
    "wait for the semaphore; do not consume the resource
379aa47c41ad #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22076
diff changeset
  1170
     (i.e. do not count down).
379aa47c41ad #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22076
diff changeset
  1171
     Abort the wait after some time.
379aa47c41ad #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22076
diff changeset
  1172
     return the receiver if the semaphore triggered normal, nil if we return
379aa47c41ad #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22076
diff changeset
  1173
     due to a timeout.
24351
fd7235316291 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24194
diff changeset
  1174
     With zero timeout, this can be used to poll a semaphore 
fd7235316291 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24194
diff changeset
  1175
     (returning the receiver if the semaphore is available, nil if not).
22080
379aa47c41ad #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22076
diff changeset
  1176
     However, polling is not the intended use of semaphores, though.
24359
13a53955244e #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24358
diff changeset
  1177
     If milliSecondsOrNil is nil, wait without timeout.
24351
fd7235316291 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24194
diff changeset
  1178
     The stateSymbol argument is purely for the ProcessMonitor, to present a nicer
fd7235316291 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24194
diff changeset
  1179
     threadState (#wait instead of #suspend)"
22080
379aa47c41ad #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22076
diff changeset
  1180
23036
e11ebfd9362c #UI_ENHANCEMENT by cg
Claus Gittinger <cg@exept.de>
parents: 22238
diff changeset
  1181
    <resource: #skipInDebuggersWalkBack>
e11ebfd9362c #UI_ENHANCEMENT by cg
Claus Gittinger <cg@exept.de>
parents: 22238
diff changeset
  1182
22155
9ba2e6b6034c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22093
diff changeset
  1183
    |activeProcess timeoutOccurred wasBlocked timeoutBlock endTime dueTime currentDelta maxMilliseconds|
16903
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1184
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1185
    count > 0 ifTrue:[
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1186
        ^ self
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1187
    ].
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1188
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1189
    wasBlocked := OperatingSystem blockInterrupts.
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1190
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1191
    count <= 0 ifTrue:[
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1192
        "with zero-timeout, this is a poll"
24359
13a53955244e #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24358
diff changeset
  1193
        milliSecondsOrNil == 0 ifTrue:[
16903
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1194
            wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1195
            ^ nil
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1196
        ].
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1197
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1198
        activeProcess := Processor activeProcess.
20263
9f3ddb907b77 #OTHER by mawalch
mawalch
parents: 20116
diff changeset
  1199
        timeoutOccurred := false.
16903
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1200
24359
13a53955244e #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24358
diff changeset
  1201
        milliSecondsOrNil notNil ifTrue:[
16903
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1202
            "Wait with timeout: calculate the end-time"
22155
9ba2e6b6034c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22093
diff changeset
  1203
            maxMilliseconds := SmallInteger maxVal // 4.
24359
13a53955244e #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24358
diff changeset
  1204
            currentDelta := milliSecondsOrNil.
22155
9ba2e6b6034c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22093
diff changeset
  1205
            currentDelta > maxMilliseconds ifTrue:[
9ba2e6b6034c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22093
diff changeset
  1206
                "NOTE: the microsecondTime is increasing monotonically,
9ba2e6b6034c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22093
diff changeset
  1207
                       while millisecondTime is wrapping at 16r1fffffff.
9ba2e6b6034c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22093
diff changeset
  1208
                       So use the microsecondTime to check when we are finished"
9ba2e6b6034c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22093
diff changeset
  1209
                dueTime := OperatingSystem getMicrosecondTime + (currentDelta * 1000).
9ba2e6b6034c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22093
diff changeset
  1210
                currentDelta := maxMilliseconds.
9ba2e6b6034c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22093
diff changeset
  1211
            ].
9ba2e6b6034c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22093
diff changeset
  1212
            endTime := OperatingSystem 
9ba2e6b6034c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22093
diff changeset
  1213
                        millisecondTimeAdd:OperatingSystem getMillisecondTime 
9ba2e6b6034c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22093
diff changeset
  1214
                        and:currentDelta.
16903
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1215
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1216
            timeoutBlock := [
20263
9f3ddb907b77 #OTHER by mawalch
mawalch
parents: 20116
diff changeset
  1217
                    timeoutOccurred := true.
16903
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1218
                    timeoutBlock:= nil.
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1219
                    Processor resume:activeProcess.
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1220
                ].
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1221
            Processor addTimedBlock:timeoutBlock for:activeProcess atMilliseconds:endTime.
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1222
        ].
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1223
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1224
        "
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1225
         need a while-loop here, since more than one process may
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1226
         wait for it and another one may also wake up.
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1227
         Thus, the count is not always non-zero after returning from
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1228
         suspend.
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1229
        "
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1230
        [
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1231
            self addWaitingProcess:activeProcess.
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1232
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1233
            "
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1234
             for some more descriptive info in processMonitor ...
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1235
             ... set the state to #wait (instead of #suspend)
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1236
            "
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1237
            [
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1238
                "sleep until resumed..."
22080
379aa47c41ad #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22076
diff changeset
  1239
                activeProcess suspendWithState:newStateSymbol.
16903
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1240
            ] ifCurtailed:[
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1241
                "interrupts are not blocked when entered through Processor>>#interruptActive"
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1242
                OperatingSystem blockInterrupts.
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1243
                timeoutBlock notNil ifTrue:[
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1244
                    Processor removeTimedBlock:timeoutBlock.
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1245
                    timeoutBlock := nil.
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1246
                ].
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1247
                self removeWaitingProcess:activeProcess.
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1248
                wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1249
            ].
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1250
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1251
            self removeWaitingProcess:activeProcess.
20263
9f3ddb907b77 #OTHER by mawalch
mawalch
parents: 20116
diff changeset
  1252
            timeoutOccurred ifTrue:[
22155
9ba2e6b6034c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22093
diff changeset
  1253
                (dueTime notNil
9ba2e6b6034c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22093
diff changeset
  1254
                 and:[(currentDelta := dueTime - OperatingSystem getMicrosecondTime) > 0]) ifTrue:[
9ba2e6b6034c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22093
diff changeset
  1255
                    "there is still some time left"
9ba2e6b6034c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22093
diff changeset
  1256
                    timeoutOccurred := false.
9ba2e6b6034c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22093
diff changeset
  1257
                    currentDelta := (currentDelta // 1000) min:maxMilliseconds.
9ba2e6b6034c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22093
diff changeset
  1258
                    endTime := OperatingSystem 
9ba2e6b6034c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22093
diff changeset
  1259
                                millisecondTimeAdd:OperatingSystem getMillisecondTime 
9ba2e6b6034c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22093
diff changeset
  1260
                                and:currentDelta.
9ba2e6b6034c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22093
diff changeset
  1261
                    Processor addTimedBlock:timeoutBlock for:activeProcess atMilliseconds:endTime.
9ba2e6b6034c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22093
diff changeset
  1262
                ] ifFalse:[
9ba2e6b6034c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22093
diff changeset
  1263
                    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
9ba2e6b6034c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22093
diff changeset
  1264
                    ^ nil
9ba2e6b6034c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22093
diff changeset
  1265
                ].
16903
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1266
            ].
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1267
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1268
            count <= 0
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1269
        ] whileTrue.
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1270
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1271
        timeoutBlock notNil ifTrue:[
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1272
            Processor removeTimedBlock:timeoutBlock.
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1273
            timeoutBlock := nil.
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1274
        ].
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1275
    ].
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1276
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1277
    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1278
    ^ self
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1279
22080
379aa47c41ad #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22076
diff changeset
  1280
    "Created: / 24-07-2017 / 21:53:23 / cg"
379aa47c41ad #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22076
diff changeset
  1281
    "Modified: / 24-07-2017 / 23:06:00 / cg"
22155
9ba2e6b6034c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22093
diff changeset
  1282
    "Modified: / 02-08-2017 / 14:16:08 / stefan"
23036
e11ebfd9362c #UI_ENHANCEMENT by cg
Claus Gittinger <cg@exept.de>
parents: 22238
diff changeset
  1283
    "Modified: / 30-05-2018 / 13:57:11 / Claus Gittinger"
24359
13a53955244e #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24358
diff changeset
  1284
    "Modified (comment): / 25-06-2019 / 14:24:18 / Claus Gittinger"
16903
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1285
!
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1286
19192
62dba7ef04eb #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19189
diff changeset
  1287
waitWithTimeout:secondsOrNilOrTimeDuration
12697
c70a0ec49f43 comment/format in: #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents: 12589
diff changeset
  1288
    "wait for the semaphore, but abort the wait after some time (seconds).
2690
f79d685e06af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2407
diff changeset
  1289
     return the receiver if the semaphore triggered normal, nil if we return
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
  1290
     due to a timeout.
24358
8b8dae901519 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 24351
diff changeset
  1291
     
8b8dae901519 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 24351
diff changeset
  1292
     The argument may be a time duration or the number of seconds as integer
8b8dae901519 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 24351
diff changeset
  1293
     or float (i.e. use 0.1 for a 100ms timeout).
2690
f79d685e06af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2407
diff changeset
  1294
     With zero timeout, this can be used to poll a semaphore (returning
f79d685e06af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2407
diff changeset
  1295
     the receiver if the semaphore is available, nil if not).
4902
caf73c9821c5 Allow nil as argument to #waitWithTimeout: (no timeout) (stefan)
Claus Gittinger <cg@exept.de>
parents: 4633
diff changeset
  1296
     However, polling is not the intended use of semaphores, though.
24358
8b8dae901519 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 24351
diff changeset
  1297
     If the argument is nil, wait without timeout (forever)."
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
  1298
23036
e11ebfd9362c #UI_ENHANCEMENT by cg
Claus Gittinger <cg@exept.de>
parents: 22238
diff changeset
  1299
    <resource: #skipInDebuggersWalkBack>
e11ebfd9362c #UI_ENHANCEMENT by cg
Claus Gittinger <cg@exept.de>
parents: 22238
diff changeset
  1300
6061
9e266783d5c2 care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents: 5713
diff changeset
  1301
    |millis|
9e266783d5c2 care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents: 5713
diff changeset
  1302
19192
62dba7ef04eb #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19189
diff changeset
  1303
    secondsOrNilOrTimeDuration notNil ifTrue:[
22076
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
  1304
        secondsOrNilOrTimeDuration isNumber ifTrue:[
23231
dd75479fdb78 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 23036
diff changeset
  1305
            millis := (secondsOrNilOrTimeDuration * 1000) asInteger.
22076
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
  1306
        ] ifFalse:[
23231
dd75479fdb78 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 23036
diff changeset
  1307
            "a TimeDuration"
dd75479fdb78 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 23036
diff changeset
  1308
            millis := secondsOrNilOrTimeDuration asTruncatedMilliseconds.
22076
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
  1309
        ].
6061
9e266783d5c2 care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents: 5713
diff changeset
  1310
    ].
22076
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
  1311
22080
379aa47c41ad #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22076
diff changeset
  1312
    ^ self waitWithTimeoutMs:millis state:#wait.
22076
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
  1313
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
  1314
    "Modified: / 21-02-2017 / 14:49:08 / stefan"
22080
379aa47c41ad #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22076
diff changeset
  1315
    "Modified: / 24-07-2017 / 21:15:39 / cg"
23036
e11ebfd9362c #UI_ENHANCEMENT by cg
Claus Gittinger <cg@exept.de>
parents: 22238
diff changeset
  1316
    "Modified: / 30-05-2018 / 13:57:33 / Claus Gittinger"
23231
dd75479fdb78 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 23036
diff changeset
  1317
    "Modified: / 24-07-2018 / 16:28:36 / Stefan Vogel"
24358
8b8dae901519 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 24351
diff changeset
  1318
    "Modified (comment): / 25-06-2019 / 14:21:43 / Claus Gittinger"
6061
9e266783d5c2 care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents: 5713
diff changeset
  1319
!
9e266783d5c2 care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents: 5713
diff changeset
  1320
24359
13a53955244e #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24358
diff changeset
  1321
waitWithTimeoutMs:milliSecondsOrNil
6061
9e266783d5c2 care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents: 5713
diff changeset
  1322
    "wait for the semaphore, but abort the wait after some time.
9e266783d5c2 care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents: 5713
diff changeset
  1323
     return the receiver if the semaphore triggered normal, nil if we return
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
  1324
     due to a timeout.
6061
9e266783d5c2 care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents: 5713
diff changeset
  1325
     With zero timeout, this can be used to poll a semaphore (returning
9e266783d5c2 care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents: 5713
diff changeset
  1326
     the receiver if the semaphore is available, nil if not).
9e266783d5c2 care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents: 5713
diff changeset
  1327
     However, polling is not the intended use of semaphores, though.
9e266783d5c2 care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents: 5713
diff changeset
  1328
     If milliSeconds is nil, wait without timeout."
9e266783d5c2 care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents: 5713
diff changeset
  1329
24359
13a53955244e #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24358
diff changeset
  1330
    ^ self waitWithTimeoutMs:milliSecondsOrNil state:#wait
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
  1331
22076
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
  1332
    "Modified: / 21-02-2017 / 15:21:30 / stefan"
22080
379aa47c41ad #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22076
diff changeset
  1333
    "Modified: / 24-07-2017 / 21:15:25 / cg"
24359
13a53955244e #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24358
diff changeset
  1334
    "Modified (format): / 25-06-2019 / 14:26:12 / Claus Gittinger"
16631
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1335
!
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1336
24359
13a53955244e #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24358
diff changeset
  1337
waitWithTimeoutMs:milliSecondsOrNil state:waitStateSymbol
16631
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1338
    "wait for the semaphore, but abort the wait after some time.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1339
     return the receiver if the semaphore triggered normal, nil if we return
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1340
     due to a timeout.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1341
     With zero timeout, this can be used to poll a semaphore (returning
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1342
     the receiver if the semaphore is available, nil if not).
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1343
     However, polling is not the intended use of semaphores, though.
24359
13a53955244e #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24358
diff changeset
  1344
     If milliSecondsOrNil is nil, wait without timeout.
16631
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1345
22076
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
  1346
     THIS IS A COPY of #waitWithTimeoutMs - the only difference is setting waitStateSymbol.
16631
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1347
     waitStateSymbol is the state the process is set to while waiting - normally #wait."
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1348
22155
9ba2e6b6034c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22093
diff changeset
  1349
    |activeProcess timeoutOccurred wasBlocked timeoutBlock endTime currentDelta dueTime maxMilliseconds|
16631
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1350
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1351
    wasBlocked := OperatingSystem blockInterrupts.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1352
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1353
    count <= 0 ifTrue:[
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1354
        "with zero-timeout, this is a poll"
24359
13a53955244e #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24358
diff changeset
  1355
        milliSecondsOrNil == 0 ifTrue:[
16631
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1356
            wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1357
            ^ nil
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1358
        ].
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1359
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1360
        activeProcess := Processor activeProcess.
20263
9f3ddb907b77 #OTHER by mawalch
mawalch
parents: 20116
diff changeset
  1361
        timeoutOccurred := false.
16631
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1362
24359
13a53955244e #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24358
diff changeset
  1363
        milliSecondsOrNil notNil ifTrue:[
16631
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1364
            "Wait with timeout: calculate the end-time"
22155
9ba2e6b6034c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22093
diff changeset
  1365
            maxMilliseconds := SmallInteger maxVal // 4.
24359
13a53955244e #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24358
diff changeset
  1366
            currentDelta := milliSecondsOrNil.
22155
9ba2e6b6034c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22093
diff changeset
  1367
            currentDelta > maxMilliseconds ifTrue:[
9ba2e6b6034c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22093
diff changeset
  1368
                "NOTE: the microsecondTime is increasing monotonically,
9ba2e6b6034c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22093
diff changeset
  1369
                       while millisecondTime is wrapping at 16r1fffffff.
9ba2e6b6034c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22093
diff changeset
  1370
                       So use the microsecondTime to check when we are finished"
9ba2e6b6034c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22093
diff changeset
  1371
                dueTime := OperatingSystem getMicrosecondTime + (currentDelta * 1000).
9ba2e6b6034c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22093
diff changeset
  1372
                currentDelta := maxMilliseconds.
9ba2e6b6034c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22093
diff changeset
  1373
            ].
22076
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
  1374
            endTime := OperatingSystem 
22155
9ba2e6b6034c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22093
diff changeset
  1375
                        millisecondTimeAdd:OperatingSystem getMillisecondTime 
9ba2e6b6034c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22093
diff changeset
  1376
                        and:currentDelta.
16631
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1377
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1378
            timeoutBlock := [
20263
9f3ddb907b77 #OTHER by mawalch
mawalch
parents: 20116
diff changeset
  1379
                    timeoutOccurred := true.
16631
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1380
                    timeoutBlock:= nil.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1381
                    Processor resume:activeProcess.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1382
                ].
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1383
            Processor addTimedBlock:timeoutBlock for:activeProcess atMilliseconds:endTime.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1384
        ].
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1385
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1386
        "
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1387
         need a while-loop here, since more than one process may
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1388
         wait for it and another one may also wake up.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1389
         Thus, the count is not always non-zero after returning from
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1390
         suspend.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1391
        "
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1392
        [
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1393
            self addWaitingProcess:activeProcess.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1394
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1395
            "
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1396
             for some more descriptive info in processMonitor ...
22076
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
  1397
             ... set the state to waitStateSymbol (instead of #suspend)
16631
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1398
            "
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1399
            [
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1400
                "sleep until resumed..."
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1401
                activeProcess suspendWithState:waitStateSymbol.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1402
            ] ifCurtailed:[
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1403
                "interrupts are not blocked when entered through Processor>>#interruptActive"
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1404
                OperatingSystem blockInterrupts.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1405
                timeoutBlock notNil ifTrue:[
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1406
                    Processor removeTimedBlock:timeoutBlock.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1407
                    timeoutBlock := nil.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1408
                ].
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1409
                self removeWaitingProcess:activeProcess.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1410
                wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1411
            ].
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1412
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1413
            self removeWaitingProcess:activeProcess.
20263
9f3ddb907b77 #OTHER by mawalch
mawalch
parents: 20116
diff changeset
  1414
            timeoutOccurred ifTrue:[
22155
9ba2e6b6034c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22093
diff changeset
  1415
                (dueTime notNil
9ba2e6b6034c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22093
diff changeset
  1416
                 and:[(currentDelta := dueTime - OperatingSystem getMicrosecondTime) > 0]) ifTrue:[
9ba2e6b6034c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22093
diff changeset
  1417
                    "there is still some time left"
9ba2e6b6034c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22093
diff changeset
  1418
                    timeoutOccurred := false.
9ba2e6b6034c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22093
diff changeset
  1419
                    currentDelta := (currentDelta // 1000) min:maxMilliseconds.
9ba2e6b6034c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22093
diff changeset
  1420
                    endTime := OperatingSystem 
9ba2e6b6034c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22093
diff changeset
  1421
                                millisecondTimeAdd:OperatingSystem getMillisecondTime 
9ba2e6b6034c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22093
diff changeset
  1422
                                and:currentDelta.
9ba2e6b6034c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22093
diff changeset
  1423
                    Processor addTimedBlock:timeoutBlock for:activeProcess atMilliseconds:endTime.
9ba2e6b6034c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22093
diff changeset
  1424
                ] ifFalse:[
9ba2e6b6034c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22093
diff changeset
  1425
                    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
9ba2e6b6034c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22093
diff changeset
  1426
                    ^ nil
9ba2e6b6034c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22093
diff changeset
  1427
                ].
16631
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1428
            ].
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1429
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1430
            count <= 0
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1431
        ] whileTrue.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1432
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1433
        timeoutBlock notNil ifTrue:[
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1434
            Processor removeTimedBlock:timeoutBlock.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1435
            timeoutBlock := nil.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1436
        ].
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1437
    ].
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1438
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1439
    "if we come here, we have acquired the semaphore"
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1440
    count := count - 1.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1441
    count == 0 ifTrue:[
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1442
        lastOwnerId := Processor activeProcessId.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1443
    ].
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1444
    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1445
    ^ self
22076
a0ca167d3248 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21556
diff changeset
  1446
22080
379aa47c41ad #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22076
diff changeset
  1447
    "Modified: / 24-07-2017 / 23:04:57 / cg"
22155
9ba2e6b6034c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22093
diff changeset
  1448
    "Modified: / 02-08-2017 / 14:16:48 / stefan"
24359
13a53955244e #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24358
diff changeset
  1449
    "Modified (comment): / 25-06-2019 / 14:23:42 / Claus Gittinger"
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
  1450
! !
77
6c38ca59927f *** empty log message ***
claus
parents: 69
diff changeset
  1451
1812
a048a98aa299 kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
  1452
!Semaphore class methodsFor:'documentation'!
183
7899f380e77d signalIf
claus
parents: 159
diff changeset
  1453
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
  1454
version
19189
48f29d2c0455 #DOCUMENTATION
matilk
parents: 16903
diff changeset
  1455
    ^ '$Header$'
12567
eb78ade59632 added: #waitTimeoutSeconds:
Stefan Vogel <sv@exept.de>
parents: 11281
diff changeset
  1456
!
eb78ade59632 added: #waitTimeoutSeconds:
Stefan Vogel <sv@exept.de>
parents: 11281
diff changeset
  1457
eb78ade59632 added: #waitTimeoutSeconds:
Stefan Vogel <sv@exept.de>
parents: 11281
diff changeset
  1458
version_CVS
19189
48f29d2c0455 #DOCUMENTATION
matilk
parents: 16903
diff changeset
  1459
    ^ '$Header$'
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1460
! !
14763
db01620704a8 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 14537
diff changeset
  1461