Semaphore.st
author Jan Vrany <jan.vrany@labware.com>
Wed, 03 Feb 2021 11:52:30 +0000
branchjv
changeset 25410 c91ea94445d1
parent 23107 40173e082cbc
child 25422 3b02b0f1f647
permissions -rw-r--r--
Reworked `SimulatedClassPool >> #associationAt:ifAbsent:` to throw an error ...if there's no such class variable. This makes it behave more like dictionary / pool.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     1
"
a27a279701f8 Initial revision
claus
parents:
diff changeset
     2
 COPYRIGHT (c) 1993 by Claus Gittinger
23107
40173e082cbc Copyright updates
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23086
diff changeset
     3
 COPYRIGHT (c) 2017 Jan Vrany
6616
70d54ffebc91 valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents: 6615
diff changeset
     4
	      All Rights Reserved
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     5
a27a279701f8 Initial revision
claus
parents:
diff changeset
     6
 This software is furnished under a license and may be used
a27a279701f8 Initial revision
claus
parents:
diff changeset
     7
 only in accordance with the terms of that license and with the
a27a279701f8 Initial revision
claus
parents:
diff changeset
     8
 inclusion of the above copyright notice.   This software may not
a27a279701f8 Initial revision
claus
parents:
diff changeset
     9
 be provided or otherwise made available to, or used by, any
a27a279701f8 Initial revision
claus
parents:
diff changeset
    10
 other person.  No title to or ownership of the software is
a27a279701f8 Initial revision
claus
parents:
diff changeset
    11
 hereby transferred.
a27a279701f8 Initial revision
claus
parents:
diff changeset
    12
"
5556
1056cc5d6ce0 category change
Claus Gittinger <cg@exept.de>
parents: 5256
diff changeset
    13
"{ Package: 'stx:libbasic' }"
1056cc5d6ce0 category change
Claus Gittinger <cg@exept.de>
parents: 5256
diff changeset
    14
19189
48f29d2c0455 #DOCUMENTATION
matilk
parents: 16903
diff changeset
    15
"{ NameSpace: Smalltalk }"
48f29d2c0455 #DOCUMENTATION
matilk
parents: 16903
diff changeset
    16
375
claus
parents: 362
diff changeset
    17
Object subclass:#Semaphore
19927
b99e02676fb6 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 19192
diff changeset
    18
	instanceVariableNames:'count waitingProcesses lastOwnerId name owner'
6616
70d54ffebc91 valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents: 6615
diff changeset
    19
	classVariableNames:''
70d54ffebc91 valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents: 6615
diff changeset
    20
	poolDictionaries:''
70d54ffebc91 valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents: 6615
diff changeset
    21
	category:'Kernel-Processes'
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    22
!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    23
1812
a048a98aa299 kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
    24
!Semaphore class methodsFor:'documentation'!
69
4564b6328136 *** empty log message ***
claus
parents: 25
diff changeset
    25
88
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    26
copyright
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    27
"
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    28
 COPYRIGHT (c) 1993 by Claus Gittinger
23107
40173e082cbc Copyright updates
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23086
diff changeset
    29
 COPYRIGHT (c) 2017 Jan Vrany
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
23086
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 21249
diff changeset
    80
    NOTE:
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 21249
diff changeset
    81
    You're encouraged to use `RecursionLock` for guarding a critical section.
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 21249
diff changeset
    82
    `RecursionLock` is not only reentrant but also much faster.
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 21249
diff changeset
    83
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
    84
    Hint:
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
    85
	now (Jul2002), Semaphores now allow for a negative count; this allows for
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
    86
	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
    87
	to arrive at a sync-point).
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
    88
	See examples.
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
    89
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
    90
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
    91
    [instance variables:]
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
    92
	count                   <SmallInteger>          the number of waits, that will go through
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
    93
							without blocking.
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
    94
							Incremented on #signal; decremented on #wait.
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
    95
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
    96
	waitingProcesses        <OrderedCollection>     waiting processes - will be served first
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
    97
							come first served when signalled.
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
    98
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
    99
	lastOwnerId             <SmallInteger>          a debugging aid: set when count drops
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   100
							to zero to the current processes id.
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   101
							Helps in finding deadlocks.
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   102
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   103
	name                    <String>                a debugging aid: an optional userFriendly
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   104
							name; helps to identify a semaphore easier.
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   105
1273
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1185
diff changeset
   106
    [see also:]
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   107
	SemaphoreSet RecursionLock Monitor
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   108
	SharedQueue Delay
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   109
	Process ProcessorScheduler
1294
e26bbb61f6b2 documentation
Claus Gittinger <cg@exept.de>
parents: 1273
diff changeset
   110
e26bbb61f6b2 documentation
Claus Gittinger <cg@exept.de>
parents: 1273
diff changeset
   111
    [author:]
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   112
	Claus Gittinger
3392
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
!
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   115
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   116
examples
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   117
"
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   118
    two processes synchronizing on a sema:
20116
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   119
                                                        [exBegin]
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   120
        |sema thread1 thread2|
3392
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   121
20116
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   122
        sema := Semaphore new.
3392
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   123
20116
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   124
        thread1 := [
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   125
                        Transcript showCR:'here is thread 1; now waiting ...'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   126
                        sema wait.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   127
                        Transcript showCR:'here is thread 1 again.'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   128
                   ] newProcess.
3392
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   129
20116
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   130
        thread2 := [
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   131
                        Transcript showCR:'here is thread 2; delaying a bit ...'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   132
                        Delay waitForSeconds:5.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   133
                        Transcript showCR:'here is thread 2 again; now signalling the sema'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   134
                        sema signal.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   135
                        Transcript showCR:'here is thread 2 after the signalling.'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   136
                  ] newProcess.
3392
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   137
20116
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   138
        thread1 priority:7.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   139
        thread2 priority:6.
3392
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   140
20116
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   141
        thread1 resume.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   142
        thread2 resume.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   143
                                                        [exEnd]
3392
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   144
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   145
    semaphore for critical regions:
20116
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   146
                                                        [exBegin]
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   147
        |accessLock|
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   148
20116
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   149
        accessLock := Semaphore forMutualExclusion.
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   150
20116
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   151
        [
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   152
            5 timesRepeat:[
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   153
                Delay waitForSeconds:2.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   154
                accessLock critical:[
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   155
                    Transcript showCR:'thread1 in critical region'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   156
                    Delay waitForSeconds:1.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   157
                    Transcript showCR:'thread1 leaving critical region'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   158
                ].
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   159
            ]
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   160
        ] forkAt:5.
3392
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   161
20116
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   162
        [
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   163
            5 timesRepeat:[
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   164
                Delay waitForSeconds:1.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   165
                accessLock critical:[
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   166
                    Transcript showCR:'thread2 in critical region'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   167
                    Delay waitForSeconds:2.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   168
                    Transcript showCR:'thread2 leaving critical region'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   169
                ].
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   170
            ]
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   171
        ] forkAt:4.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   172
                                                        [exEnd]
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   173
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   174
    a deadlock due to recursive enter of a critical region:
20116
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   175
                                                        [exBegin]
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   176
        |accessLock block|
3392
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   177
20116
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   178
        accessLock := Semaphore forMutualExclusion.
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   179
20116
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   180
        block := [:arg |
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   181
                    Transcript showCR:'about to enter'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   182
                    accessLock critical:[
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   183
                        Transcript showCR:'entered - doing action'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   184
                        arg value
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   185
                    ].
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   186
                    Transcript showCR:'left region'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   187
                 ].
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   188
20116
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   189
        block value:[].                 'this works'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   190
        block value:[block value:[] ].  'this deadlocks'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   191
                                                        [exEnd]
3392
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   192
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   193
    Avoid the deadlock by using a RecursionLock instead:
20116
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   194
                                                        [exBegin]
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   195
        |accessLock block|
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   196
20116
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   197
        accessLock := RecursionLock new.
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   198
20116
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   199
        block := [:arg |
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   200
                    Transcript showCR:'about to enter'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   201
                    accessLock critical:[
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   202
                        Transcript showCR:'entered - doing action'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   203
                        arg value
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   204
                    ].
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   205
                    Transcript showCR:'left region'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   206
                 ].
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   207
20116
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   208
        block value:[].                 'this works'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   209
        block value:[block value:[] ].  'this deadlocks'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   210
                                                        [exEnd]
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   211
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   212
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   213
   Wait for multiple processes to arrive at a sync-point:
20116
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   214
                                                        [exBegin]
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   215
        |syncSema proceedSema thread1 thread2 thread3|
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
        syncSema := Semaphore new.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   218
        syncSema setCount:(1-3).
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   219
        proceedSema := Semaphore new.
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   220
20116
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   221
        thread1 := [
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   222
                        Transcript showCR:'here is thread 1; now busy ...'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   223
                        Delay waitForSeconds:(2 + (Random nextIntegerBetween:2 and:4)).
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   224
                        Transcript showCR:'here is thread 1 again - now syncing.'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   225
                        syncSema signal.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   226
                        Transcript showCR:'thread 1 is waiting for all others...'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   227
                        proceedSema wait.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   228
                        Transcript showCR:'thread 1 done.'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   229
                   ] newProcess.
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   230
20116
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   231
        thread2 := [
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   232
                        Transcript showCR:'here is thread 2; now busy ...'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   233
                        Delay waitForSeconds:(3 + (Random nextIntegerBetween:2 and:4)).
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   234
                        Transcript showCR:'here is thread 2 again - now syncing.'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   235
                        syncSema signal.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   236
                        Transcript showCR:'thread 2 is waiting for all others...'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   237
                        proceedSema wait.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   238
                        Transcript showCR:'thread 2 done.'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   239
                  ] newProcess.
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   240
20116
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   241
        thread3 := [
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   242
                        Transcript showCR:'here is thread 3; now busy ...'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   243
                        Delay waitForSeconds:(4 + (Random nextIntegerBetween:2 and:4)).
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   244
                        Transcript showCR:'here is thread 3 again - now syncing.'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   245
                        syncSema signal.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   246
                        Transcript showCR:'thread 3 is waiting for all others...'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   247
                        proceedSema wait.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   248
                        Transcript showCR:'thread 3 done.'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   249
                  ] newProcess.
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 priority:7.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   252
        thread2 priority:6.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   253
        thread3 priority:9.
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
        thread1 resume.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   256
        thread2 resume.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   257
        thread3 resume.
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   258
20116
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   259
        Transcript showCR:'main thread: now waiting for other threads...'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   260
        syncSema wait.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   261
        Transcript showCR:'main thread: all other threads at syncPoint.'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   262
        Delay waitForSeconds:2.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   263
        Transcript showCR:'main thread: now let them proceed...'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   264
        proceedSema signalForAll.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   265
        Transcript showCR:'main thread: done.'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   266
                                                        [exEnd]
6999
b0f293f2780c comment
ca
parents: 6986
diff changeset
   267
20116
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   268
   waitWithTimeout:0 can also be used to conditionally acquire the semaphore
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   269
   i.e. only acquire it if it is available.
6999
b0f293f2780c comment
ca
parents: 6986
diff changeset
   270
     |s|
b0f293f2780c comment
ca
parents: 6986
diff changeset
   271
b0f293f2780c comment
ca
parents: 6986
diff changeset
   272
     s := Semaphore new.
b0f293f2780c comment
ca
parents: 6986
diff changeset
   273
     [
20116
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   274
          (s waitWithTimeout:0) notNil ifTrue:[
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   275
                Transcript showCR:'process1 got the sema'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   276
                Delay waitForSeconds:1.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   277
                Transcript showCR:'process1 signals sema'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   278
                s signal.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   279
          ] ifFalse:[
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   280
                Transcript showCR:'process1 has NOT got the sema'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   281
          ].
6999
b0f293f2780c comment
ca
parents: 6986
diff changeset
   282
     ] fork.
b0f293f2780c comment
ca
parents: 6986
diff changeset
   283
     [
20116
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   284
          (s waitWithTimeout:0) notNil ifTrue:[
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   285
                Transcript showCR:'process2 got the sema'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   286
                Delay waitForSeconds:1.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   287
                Transcript showCR:'process2 signals sema'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   288
                s signal.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   289
          ] ifFalse:[
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   290
                Transcript showCR:'process2 has NOT got the sema'.
c29d96c1db82 #OTHER by mawalch
mawalch
parents: 19928
diff changeset
   291
          ]
6999
b0f293f2780c comment
ca
parents: 6986
diff changeset
   292
     ] fork.
b0f293f2780c comment
ca
parents: 6986
diff changeset
   293
     s signal.
b0f293f2780c comment
ca
parents: 6986
diff changeset
   294
     Delay waitForSeconds:0.5.
b0f293f2780c comment
ca
parents: 6986
diff changeset
   295
     Transcript showCR:'master waits for sema'.
b0f293f2780c comment
ca
parents: 6986
diff changeset
   296
     s wait.
b0f293f2780c comment
ca
parents: 6986
diff changeset
   297
     Transcript showCR:'master got the sema'.
69
4564b6328136 *** empty log message ***
claus
parents: 25
diff changeset
   298
"
4564b6328136 *** empty log message ***
claus
parents: 25
diff changeset
   299
! !
4564b6328136 *** empty log message ***
claus
parents: 25
diff changeset
   300
1812
a048a98aa299 kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   301
!Semaphore class methodsFor:'instance creation'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   302
14509
2c8531dbf456 added: #cleanup
anwild
parents: 13579
diff changeset
   303
cleanup
2c8531dbf456 added: #cleanup
anwild
parents: 13579
diff changeset
   304
    "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
   305
     Can only (;-?) happen, if a semaphore-holding process was hard terminated
14509
2c8531dbf456 added: #cleanup
anwild
parents: 13579
diff changeset
   306
     (i.e. no ensure handling happened), and semas remain in a bad state."
2c8531dbf456 added: #cleanup
anwild
parents: 13579
diff changeset
   307
2c8531dbf456 added: #cleanup
anwild
parents: 13579
diff changeset
   308
    self allInstancesDo:[:sema |
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   309
        |lastOwner|
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   310
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   311
        (sema count == 0
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   312
         and:[(lastOwner := sema lastOwner) notNil
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   313
         and:[lastOwner isDead]]) ifTrue:[
14509
2c8531dbf456 added: #cleanup
anwild
parents: 13579
diff changeset
   314
            sema signal
2c8531dbf456 added: #cleanup
anwild
parents: 13579
diff changeset
   315
        ]
2c8531dbf456 added: #cleanup
anwild
parents: 13579
diff changeset
   316
    ]
2c8531dbf456 added: #cleanup
anwild
parents: 13579
diff changeset
   317
!
2c8531dbf456 added: #cleanup
anwild
parents: 13579
diff changeset
   318
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   319
forMutualExclusion
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   320
    "create & return a new semaphore which allows exactly one process to
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   321
     wait on it without blocking. This type of semaphore is used
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   322
     for mutual exclusion from critical regions (see #critical:).
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   323
     Also see RecursionLock, to avoid deadlock in case of recursive entered
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   324
     critical regions."
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   325
3600
6c5629eea51d give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents: 3392
diff changeset
   326
    ^ super new setCount:1; name:'criticalRegionSema'
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   327
3600
6c5629eea51d give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents: 3392
diff changeset
   328
    "Modified: / 17.6.1998 / 16:23:09 / cg"
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   329
!
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   330
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   331
new
a27a279701f8 Initial revision
claus
parents:
diff changeset
   332
    "create & return a new semaphore which blocks until a signal is sent"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   333
a27a279701f8 Initial revision
claus
parents:
diff changeset
   334
    ^ super new setCount:0
a27a279701f8 Initial revision
claus
parents:
diff changeset
   335
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   336
a27a279701f8 Initial revision
claus
parents:
diff changeset
   337
new:n
a27a279701f8 Initial revision
claus
parents:
diff changeset
   338
    "create & return a new semaphore which allows n waits before
a27a279701f8 Initial revision
claus
parents:
diff changeset
   339
     blocking"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   340
a27a279701f8 Initial revision
claus
parents:
diff changeset
   341
    ^ super new setCount:n
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   342
! !
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   343
9067
a8b1ac38b89d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8617
diff changeset
   344
!Semaphore methodsFor:'Compatibility-Squeak'!
a8b1ac38b89d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8617
diff changeset
   345
a8b1ac38b89d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8617
diff changeset
   346
isSignaled
a8b1ac38b89d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8617
diff changeset
   347
    ^ self wouldBlock not
12567
eb78ade59632 added: #waitTimeoutSeconds:
Stefan Vogel <sv@exept.de>
parents: 11281
diff changeset
   348
!
eb78ade59632 added: #waitTimeoutSeconds:
Stefan Vogel <sv@exept.de>
parents: 11281
diff changeset
   349
12589
b8964a49b462 added: #waitTimeoutMSecs:
Stefan Vogel <sv@exept.de>
parents: 12567
diff changeset
   350
waitTimeoutMSecs:milliSeconds
b8964a49b462 added: #waitTimeoutMSecs:
Stefan Vogel <sv@exept.de>
parents: 12567
diff changeset
   351
    ^ self waitWithTimeoutMs:milliSeconds
b8964a49b462 added: #waitTimeoutMSecs:
Stefan Vogel <sv@exept.de>
parents: 12567
diff changeset
   352
!
b8964a49b462 added: #waitTimeoutMSecs:
Stefan Vogel <sv@exept.de>
parents: 12567
diff changeset
   353
12567
eb78ade59632 added: #waitTimeoutSeconds:
Stefan Vogel <sv@exept.de>
parents: 11281
diff changeset
   354
waitTimeoutSeconds:seconds
eb78ade59632 added: #waitTimeoutSeconds:
Stefan Vogel <sv@exept.de>
parents: 11281
diff changeset
   355
    ^ self waitWithTimeout:seconds
9067
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
19927
b99e02676fb6 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 19192
diff changeset
   358
!Semaphore methodsFor:'accessing'!
b99e02676fb6 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 19192
diff changeset
   359
b99e02676fb6 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 19192
diff changeset
   360
owner
19928
e2bf8e38979c #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19927
diff changeset
   361
    "an optional reference to someone who owns this semaphore,
e2bf8e38979c #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19927
diff changeset
   362
     typically a shared queue or a windowgroup or similar.
e2bf8e38979c #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19927
diff changeset
   363
     This has no semantic meaning and is only used to support debugging"
23086
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 21249
diff changeset
   364
19927
b99e02676fb6 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 19192
diff changeset
   365
    ^ owner
b99e02676fb6 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 19192
diff changeset
   366
!
b99e02676fb6 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 19192
diff changeset
   367
b99e02676fb6 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 19192
diff changeset
   368
owner:something
19928
e2bf8e38979c #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19927
diff changeset
   369
    "an optional reference to someone who owns this semaphore,
e2bf8e38979c #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19927
diff changeset
   370
     typically a shared queue or a windowgroup or similar.
e2bf8e38979c #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19927
diff changeset
   371
     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
   372
19927
b99e02676fb6 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 19192
diff changeset
   373
    owner := something.
b99e02676fb6 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 19192
diff changeset
   374
! !
b99e02676fb6 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 19192
diff changeset
   375
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   376
!Semaphore methodsFor:'printing & storing'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   377
13355
442f69dcc5b8 added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents: 13152
diff changeset
   378
displayOn:aGCOrStream
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   379
    "return a string to display the receiver - include the
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   380
     count for your convenience"
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   381
13355
442f69dcc5b8 added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents: 13152
diff changeset
   382
    "/ what a kludge - Dolphin and Squeak mean: printOn: a stream;
442f69dcc5b8 added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents: 13152
diff changeset
   383
    "/ ST/X (and some old ST80's) mean: draw-yourself on a GC.
442f69dcc5b8 added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents: 13152
diff changeset
   384
    (aGCOrStream isStream) ifFalse:[
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   385
	^ super displayOn:aGCOrStream
13355
442f69dcc5b8 added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents: 13152
diff changeset
   386
    ].
442f69dcc5b8 added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents: 13152
diff changeset
   387
    aGCOrStream
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   388
	nextPutAll:self class name;
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   389
	nextPut:$(.
13355
442f69dcc5b8 added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents: 13152
diff changeset
   390
    count printOn:aGCOrStream.
442f69dcc5b8 added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents: 13152
diff changeset
   391
    aGCOrStream nextPutAll:' name: '.
442f69dcc5b8 added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents: 13152
diff changeset
   392
    (name ? 'unnamed') printOn:aGCOrStream.
442f69dcc5b8 added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents: 13152
diff changeset
   393
    aGCOrStream nextPut:$).
442f69dcc5b8 added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents: 13152
diff changeset
   394
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   395
2723
0d4c775c14d1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2690
diff changeset
   396
    "Modified: 28.6.1997 / 16:21:09 / cg"
2265
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   397
!
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   398
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   399
name
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   400
    "return the semaphores userFriendly name"
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   401
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   402
    ^ name
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   403
!
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   404
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   405
name:aString
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   406
    "set the semaphores userFriendly name"
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   407
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   408
    name := aString
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   409
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   410
6986
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   411
!Semaphore methodsFor:'private'!
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   412
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   413
addWaitingProcess:aProcess
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   414
    "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
   415
     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
   416
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   417
     NOTE: must be called with blocked interrupts"
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   418
7717
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   419
    waitingProcesses isNil ifTrue:[
21190
138b90edd609 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21115
diff changeset
   420
        "for now - assume that there is probably only one waiter"
138b90edd609 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21115
diff changeset
   421
        waitingProcesses := Array with:aProcess
7717
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   422
    ] ifFalse:[
21190
138b90edd609 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21115
diff changeset
   423
        waitingProcesses isArray ifTrue:[
138b90edd609 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21115
diff changeset
   424
            "add 2 to reserve space for additional waiters"
138b90edd609 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21115
diff changeset
   425
            waitingProcesses := (OrderedCollection new:waitingProcesses size + 2)
138b90edd609 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21115
diff changeset
   426
                                    addAll:waitingProcesses;
138b90edd609 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21115
diff changeset
   427
                                    yourself.
138b90edd609 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21115
diff changeset
   428
        ].
138b90edd609 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21115
diff changeset
   429
        waitingProcesses add:aProcess.
7717
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   430
    ].
6986
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   431
7717
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   432
"/    "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
   433
"/    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
   434
"/
6986
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   435
"/    |priority insertIndex|
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   436
"/
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   437
"/    (waitingProcesses size == 0
6986
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   438
"/     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
   439
"/        waitingProcesses add:aProcess.
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   440
"/        ^ self.
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   441
"/    ].
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   442
"/
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   443
"/    insertIndex := waitingProcesses findFirst:[:process| process priority < priority].
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   444
"/    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
   445
!
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   446
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   447
removeWaitingProcess:aProcess
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   448
    "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
   449
     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
   450
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   451
     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
   452
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   453
    |nWaiting|
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   454
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   455
    nWaiting := waitingProcesses size.
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   456
    nWaiting == 0 ifTrue:[^ self].
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   457
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   458
    nWaiting == 1 ifTrue:[
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   459
	(waitingProcesses at:1) == aProcess ifTrue:[
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   460
	    waitingProcesses := nil.
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   461
	].
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   462
	^ self.
7717
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   463
    ].
8617
5fb5a717deff *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8569
diff changeset
   464
    waitingProcesses removeIdentical:aProcess ifAbsent:[].
11254
a40a7313b9a8 *** empty log message ***
ca
parents: 11218
diff changeset
   465
!
a40a7313b9a8 *** empty log message ***
ca
parents: 11218
diff changeset
   466
a40a7313b9a8 *** empty log message ***
ca
parents: 11218
diff changeset
   467
wakeupWaiters
a40a7313b9a8 *** empty log message ***
ca
parents: 11218
diff changeset
   468
    "remove all waiting processes from the list of waiting processes
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   469
     and resume them.
11254
a40a7313b9a8 *** empty log message ***
ca
parents: 11218
diff changeset
   470
     NOTE: Must be called when known that waitingProcesses is nonNil and
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   471
	   also with blocked interrupts"
11254
a40a7313b9a8 *** empty log message ***
ca
parents: 11218
diff changeset
   472
15635
1b42005ee234 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 15551
diff changeset
   473
    |processes anyDead needsReschedule|
11254
a40a7313b9a8 *** empty log message ***
ca
parents: 11218
diff changeset
   474
13152
78fdc9d96bc3 comment/format in:
Stefan Vogel <sv@exept.de>
parents: 12697
diff changeset
   475
    processes := waitingProcesses.
15635
1b42005ee234 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 15551
diff changeset
   476
"/ do not set to nil - a waiting process may be suspended and will not be resumed by #makeRunnable: ...
13152
78fdc9d96bc3 comment/format in:
Stefan Vogel <sv@exept.de>
parents: 12697
diff changeset
   477
"/    waitingProcesses := nil.
11254
a40a7313b9a8 *** empty log message ***
ca
parents: 11218
diff changeset
   478
14770
87973b889eb8 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 14763
diff changeset
   479
    needsReschedule := false.
14509
2c8531dbf456 added: #cleanup
anwild
parents: 13579
diff changeset
   480
    anyDead := false.
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   481
    processes do:[:eachProcess |
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   482
	(Processor makeRunnable:eachProcess) ifTrue:[
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   483
	    needsReschedule := true.
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   484
	] ifFalse:[
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   485
	    "if process is nil or dead (or for other reasons) makeRunnable returns false.
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   486
	     So check here."
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   487
	    (eachProcess isNil or:[eachProcess isDead]) ifTrue:[
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   488
		"printing to Transcript might not be a good idea while interrupts are blocked"
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   489
		'Semaphore>>wakeupWaiters: removing a dead process: ' infoPrint. eachProcess infoPrintCR.
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   490
		anyDead := true.
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   491
	    ].
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   492
	].
14509
2c8531dbf456 added: #cleanup
anwild
parents: 13579
diff changeset
   493
    ].
2c8531dbf456 added: #cleanup
anwild
parents: 13579
diff changeset
   494
    anyDead ifTrue:[
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   495
	"interrupts are already blocked by sender"
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   496
	waitingProcesses := processes reject:[:p | p isNil or:[p isDead]]
14509
2c8531dbf456 added: #cleanup
anwild
parents: 13579
diff changeset
   497
    ].
2c8531dbf456 added: #cleanup
anwild
parents: 13579
diff changeset
   498
14770
87973b889eb8 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 14763
diff changeset
   499
    ^ needsReschedule.
6986
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   500
! !
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   501
7258
9ccdbee7d1ad method category rename
Claus Gittinger <cg@exept.de>
parents: 6999
diff changeset
   502
!Semaphore methodsFor:'private-accessing'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   503
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   504
clear
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   505
    "clear the semaphores count"
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   506
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   507
    count := 0
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   508
!
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   509
2407
6b0e21391915 added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 2352
diff changeset
   510
initSignals
6b0e21391915 added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 2352
diff changeset
   511
    "set the count of the semaphore to zero.
6b0e21391915 added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 2352
diff changeset
   512
     provided for ST-80 compatibility."
6b0e21391915 added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 2352
diff changeset
   513
6b0e21391915 added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 2352
diff changeset
   514
    count := 0
6b0e21391915 added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 2352
diff changeset
   515
6b0e21391915 added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 2352
diff changeset
   516
    "Created: 17.2.1997 / 11:31:19 / cg"
6b0e21391915 added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 2352
diff changeset
   517
!
6b0e21391915 added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 2352
diff changeset
   518
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   519
setCount:n
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   520
    "set the count of the semaphore;
20426
2ec010b1a559 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 20263
diff changeset
   521
     that's the number of possible waits, without blocking"
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   522
7717
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   523
    waitingProcesses := nil.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   524
    count := n
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   525
2407
6b0e21391915 added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 2352
diff changeset
   526
    "Modified: 17.2.1997 / 11:36:40 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   527
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   528
5256
1209e167d63e category rename
Claus Gittinger <cg@exept.de>
parents: 4902
diff changeset
   529
!Semaphore methodsFor:'queries'!
217
a0400fdbc933 *** empty log message ***
claus
parents: 183
diff changeset
   530
2235
c6a15bd9a33c added access to count
Claus Gittinger <cg@exept.de>
parents: 2234
diff changeset
   531
count
c6a15bd9a33c added access to count
Claus Gittinger <cg@exept.de>
parents: 2234
diff changeset
   532
    "return the number of 'already-counted' trigger events.
20426
2ec010b1a559 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 20263
diff changeset
   533
     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
   534
c6a15bd9a33c added access to count
Claus Gittinger <cg@exept.de>
parents: 2234
diff changeset
   535
    ^ count
c6a15bd9a33c added access to count
Claus Gittinger <cg@exept.de>
parents: 2234
diff changeset
   536
c6a15bd9a33c added access to count
Claus Gittinger <cg@exept.de>
parents: 2234
diff changeset
   537
    "Created: 23.1.1997 / 02:55:58 / cg"
c6a15bd9a33c added access to count
Claus Gittinger <cg@exept.de>
parents: 2234
diff changeset
   538
!
c6a15bd9a33c added access to count
Claus Gittinger <cg@exept.de>
parents: 2234
diff changeset
   539
13579
db80734f9576 changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents: 13355
diff changeset
   540
lastOwner
db80734f9576 changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents: 13355
diff changeset
   541
    "return the last owning process or nil
db80734f9576 changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents: 13355
diff changeset
   542
     (the one which counted to zero).
db80734f9576 changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents: 13355
diff changeset
   543
     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
   544
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   545
    ^ Processor processWithId:lastOwnerId.
13579
db80734f9576 changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents: 13355
diff changeset
   546
db80734f9576 changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents: 13355
diff changeset
   547
    "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
   548
!
db80734f9576 changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents: 13355
diff changeset
   549
2265
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   550
lastOwnerId
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   551
    "return the processId of the last owning process
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   552
     (the one which counted to zero).
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   553
     May be very useful in debugging deadLock situations"
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   554
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   555
    ^ lastOwnerId
2265
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   556
13579
db80734f9576 changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents: 13355
diff changeset
   557
    "Created: / 24-01-1997 / 23:09:33 / cg"
2265
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   558
!
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   559
1329
0a025ca40653 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   560
numberOfWaitingProcesses
0a025ca40653 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   561
    "return the number of processes waiting on the receiver"
0a025ca40653 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   562
0a025ca40653 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   563
    ^ waitingProcesses size
0a025ca40653 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   564
0a025ca40653 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   565
    "Created: 3.5.1996 / 18:06:27 / cg"
0a025ca40653 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   566
!
0a025ca40653 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   567
1577
720f14f90e2c access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents: 1329
diff changeset
   568
waitingProcesses
720f14f90e2c access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents: 1329
diff changeset
   569
    "return the processes waiting on the receiver"
720f14f90e2c access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents: 1329
diff changeset
   570
7717
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   571
    ^ waitingProcesses ? #()
1577
720f14f90e2c access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents: 1329
diff changeset
   572
720f14f90e2c access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents: 1329
diff changeset
   573
    "Created: 18.7.1996 / 20:53:33 / cg"
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   574
! !
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   575
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   576
!Semaphore methodsFor:'semaphoreSet interface'!
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   577
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   578
checkAndAddWaitingProcess:process
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   579
    "interface for SemaphoreSet.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   580
     If the semaphore is available, decrement it and return true.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   581
     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
   582
     and return false.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   583
     ATTENTION: this must be invoked with OperatingSystem-interrupts-blocked.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   584
    "
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   585
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   586
    count > 0 ifTrue:[
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   587
	count := count - 1.
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   588
	count == 0 ifTrue:[
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   589
	    lastOwnerId := Processor activeProcessId.
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   590
	].
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   591
	^ true
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   592
    ].
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   593
    (waitingProcesses notNil and:[(waitingProcesses includesIdentical:process)]) ifFalse:[
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   594
	self addWaitingProcess:process.
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   595
    ].
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   596
    ^ false
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   597
13579
db80734f9576 changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents: 13355
diff changeset
   598
    "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
   599
    "Modified: / 11-08-2011 / 14:36:20 / cg"
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   600
! !
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   601
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   602
!Semaphore methodsFor:'signaling'!
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   603
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   604
signal
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   605
    "waking up (the first) waiter.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   606
     Q: should this be the highest prio waiter ?"
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   607
14770
87973b889eb8 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 14763
diff changeset
   608
    |wasBlocked needsReschedule|
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   609
14770
87973b889eb8 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 14763
diff changeset
   610
    needsReschedule := false.
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   611
    wasBlocked := OperatingSystem blockInterrupts.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   612
    [
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   613
	count := count + 1.
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   614
	waitingProcesses size ~~ 0 ifTrue:[
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   615
	    needsReschedule := self wakeupWaiters.
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   616
	].
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   617
    ] ensure:[
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   618
	wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
14770
87973b889eb8 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 14763
diff changeset
   619
    ].
87973b889eb8 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 14763
diff changeset
   620
    needsReschedule ifTrue:[
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   621
	"now, schedule the highest priority process"
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   622
	Processor reschedule.
14770
87973b889eb8 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 14763
diff changeset
   623
    ].
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   624
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   625
    "Modified: / 4.2.1998 / 21:01:07 / cg"
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   626
!
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   627
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   628
signalForAll
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   629
    "signal the semaphore for all waiters.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   630
     This can be used for process synchronization, if multiple processes are
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   631
     waiting for a common event."
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   632
14770
87973b889eb8 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 14763
diff changeset
   633
    |wasBlocked needsReschedule|
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   634
15635
1b42005ee234 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 15551
diff changeset
   635
    waitingProcesses size ~~ 0 ifTrue:[
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   636
	needsReschedule := false.
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   637
	wasBlocked := OperatingSystem blockInterrupts.
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   638
	[
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   639
	    "first, make them all runnable, but do not schedule
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   640
	     (in case one has higher prio and goes into a wait immediately again.)"
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   641
	    waitingProcesses size ~~ 0 ifTrue:[
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   642
		needsReschedule := self wakeupWaiters.
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   643
		"wakeupWaites may have removed dead processes from waitingProcesses!!"
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   644
		count := count + waitingProcesses size.
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   645
	    ].
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   646
	] ensure:[
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   647
	    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   648
	].
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   649
	needsReschedule ifTrue:[
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   650
	    "now, schedule the highest priority process"
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   651
	    Processor reschedule.
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   652
	].
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   653
    ]
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   654
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   655
    "Modified: / 5.2.1998 / 10:40:26 / cg"
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   656
!
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   657
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   658
signalIf
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   659
    "signal the semaphore, but only if being waited upon.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   660
     This can be used for one-shot semaphores (i.e. not remembering
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   661
     previous signals)"
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   662
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   663
    |wasBlocked|
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   664
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   665
    waitingProcesses notNil ifTrue:[
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   666
	wasBlocked := OperatingSystem blockInterrupts.
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   667
	[
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   668
	    waitingProcesses size ~~ 0 ifTrue:[
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   669
		self signal
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   670
	    ].
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   671
	] ensure:[
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   672
	    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   673
	]
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   674
    ]
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   675
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   676
    "Modified: 28.2.1996 / 21:23:57 / cg"
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   677
!
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
signalOnce
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   680
    "wakeup waiters - but only once.
9462
822bd68c3c45 new: #consume
Stefan Vogel <sv@exept.de>
parents: 9067
diff changeset
   681
     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
   682
14770
87973b889eb8 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 14763
diff changeset
   683
    |wasBlocked needsReschedule|
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   684
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   685
    count <= 0 ifTrue:[
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   686
	needsReschedule := false.
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   687
	wasBlocked := OperatingSystem blockInterrupts.
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   688
	"/ check again - now interrupts are blocked.
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   689
	[
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   690
	    count <= 0 ifTrue:[
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   691
		count := count + 1.
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   692
		count == 1 ifTrue:[
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   693
		    waitingProcesses size ~~ 0 ifTrue:[
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   694
			needsReschedule := self wakeupWaiters.
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   695
		    ].
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   696
		].
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   697
	    ].
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   698
	] ensure:[
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   699
	    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   700
	].
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   701
	needsReschedule ifTrue:[
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   702
	    "now, schedule the highest priority process"
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   703
	    Processor reschedule.
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   704
	].
14770
87973b889eb8 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 14763
diff changeset
   705
    ].
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   706
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   707
    "Modified: 28.2.1996 / 21:24:08 / cg"
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
!Semaphore methodsFor:'testing'!
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   711
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   712
isEmpty
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   713
    "ST80 compatibility - return true if there are no waiters"
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   714
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   715
    ^ waitingProcesses size == 0
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
    "Created: / 3.5.1996 / 18:06:27 / cg"
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   718
    "Modified: / 18.6.1998 / 16:07:38 / cg"
1577
720f14f90e2c access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents: 1329
diff changeset
   719
!
720f14f90e2c access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents: 1329
diff changeset
   720
217
a0400fdbc933 *** empty log message ***
claus
parents: 183
diff changeset
   721
wouldBlock
a0400fdbc933 *** empty log message ***
claus
parents: 183
diff changeset
   722
    "return true, if the receiver would block the activeProcess
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   723
     if a wait was performed. False otherwise.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   724
     Attention: if asked without some global lock (blockedInterrupts),
15551
8ee0431a2f39 class: Semaphore
Claus Gittinger <cg@exept.de>
parents: 14773
diff changeset
   725
     the returned value may be outdated right away."
217
a0400fdbc933 *** empty log message ***
claus
parents: 183
diff changeset
   726
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   727
    ^ count <= 0
217
a0400fdbc933 *** empty log message ***
claus
parents: 183
diff changeset
   728
! !
a0400fdbc933 *** empty log message ***
claus
parents: 183
diff changeset
   729
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   730
!Semaphore methodsFor:'waiting'!
5713
993805688c7f added #clear
Claus Gittinger <cg@exept.de>
parents: 5556
diff changeset
   731
9462
822bd68c3c45 new: #consume
Stefan Vogel <sv@exept.de>
parents: 9067
diff changeset
   732
consume
822bd68c3c45 new: #consume
Stefan Vogel <sv@exept.de>
parents: 9067
diff changeset
   733
    "consume the resource without waiting.
822bd68c3c45 new: #consume
Stefan Vogel <sv@exept.de>
parents: 9067
diff changeset
   734
     This works even if the count is 0 (count may become negative).
822bd68c3c45 new: #consume
Stefan Vogel <sv@exept.de>
parents: 9067
diff changeset
   735
     Answer the new count afterwards"
822bd68c3c45 new: #consume
Stefan Vogel <sv@exept.de>
parents: 9067
diff changeset
   736
822bd68c3c45 new: #consume
Stefan Vogel <sv@exept.de>
parents: 9067
diff changeset
   737
    ^ self consume:1
822bd68c3c45 new: #consume
Stefan Vogel <sv@exept.de>
parents: 9067
diff changeset
   738
!
822bd68c3c45 new: #consume
Stefan Vogel <sv@exept.de>
parents: 9067
diff changeset
   739
822bd68c3c45 new: #consume
Stefan Vogel <sv@exept.de>
parents: 9067
diff changeset
   740
consume:n
822bd68c3c45 new: #consume
Stefan Vogel <sv@exept.de>
parents: 9067
diff changeset
   741
    "consume the resource n times without waiting.
822bd68c3c45 new: #consume
Stefan Vogel <sv@exept.de>
parents: 9067
diff changeset
   742
     This works even if the count is 0 (count may become negative).
822bd68c3c45 new: #consume
Stefan Vogel <sv@exept.de>
parents: 9067
diff changeset
   743
     Answer the new count afterwards"
822bd68c3c45 new: #consume
Stefan Vogel <sv@exept.de>
parents: 9067
diff changeset
   744
822bd68c3c45 new: #consume
Stefan Vogel <sv@exept.de>
parents: 9067
diff changeset
   745
    |wasBlocked|
822bd68c3c45 new: #consume
Stefan Vogel <sv@exept.de>
parents: 9067
diff changeset
   746
822bd68c3c45 new: #consume
Stefan Vogel <sv@exept.de>
parents: 9067
diff changeset
   747
    wasBlocked := OperatingSystem blockInterrupts.
822bd68c3c45 new: #consume
Stefan Vogel <sv@exept.de>
parents: 9067
diff changeset
   748
    count := count - n.
822bd68c3c45 new: #consume
Stefan Vogel <sv@exept.de>
parents: 9067
diff changeset
   749
    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
822bd68c3c45 new: #consume
Stefan Vogel <sv@exept.de>
parents: 9067
diff changeset
   750
822bd68c3c45 new: #consume
Stefan Vogel <sv@exept.de>
parents: 9067
diff changeset
   751
    ^ count
822bd68c3c45 new: #consume
Stefan Vogel <sv@exept.de>
parents: 9067
diff changeset
   752
!
822bd68c3c45 new: #consume
Stefan Vogel <sv@exept.de>
parents: 9067
diff changeset
   753
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   754
critical:aBlock
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   755
    "evaluate aBlock as a critical region; the receiver must be
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   756
     created using Semaphore>>forMutualExclusion"
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   757
14773
47a664dbd54b class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 14772
diff changeset
   758
    |retVal gotSema wasBlocked needsReschedule|
7717
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   759
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   760
    "/ inlined common case - ugly kludge but helpful
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   761
    wasBlocked := OperatingSystem blockInterrupts.
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   762
    count > 0 ifTrue:[
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   763
	count := count - 1.
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   764
	count == 0 ifTrue:[
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   765
	    lastOwnerId := Processor activeProcessId.
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   766
	].
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   767
	wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
7717
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   768
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   769
	retVal := aBlock ifCurtailed:[ self signal ].
7717
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   770
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   771
	OperatingSystem blockInterrupts.
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   772
	needsReschedule := false.
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   773
	[
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   774
	    count := count + 1.
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   775
	    waitingProcesses size ~~ 0 ifTrue:[
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   776
		needsReschedule := self wakeupWaiters.
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   777
	    ].
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   778
	] ensure:[
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   779
	    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   780
	].
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   781
	needsReschedule ifTrue:[
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   782
	    "now, schedule the highest priority process"
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   783
	    Processor reschedule.
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   784
	].
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   785
	^ retVal.
7717
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   786
    ].
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   787
    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   788
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   789
    [
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   790
	gotSema := self wait.
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   791
	retVal := aBlock value.
6616
70d54ffebc91 valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents: 6615
diff changeset
   792
    ] ifCurtailed:[
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   793
	"/ be careful - the unwind may occur both while waiting
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   794
	"/ AND while evaluating the block.
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   795
	gotSema notNil ifTrue:[self signal].
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   796
    ].
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   797
    self signal.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   798
    ^ retVal
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   799
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   800
    "
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   801
      the example below is stupid (it should use a SharedQueue,
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   802
      or at least a Queue with critical regions).
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   803
      Anyhow, it demonstrates how two processes lock each other
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   804
      from accessing coll at the same time
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   805
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   806
     |sema coll|
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   807
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   808
     sema := Semaphore forMutualExclusion.
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   809
     coll := OrderedCollection new:10.
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   810
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   811
     [
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   812
	1 to:1000 do:[:i |
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   813
	    sema critical:[
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   814
		coll addLast:i.
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   815
		(Delay forSeconds:0.1) wait.
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   816
	    ]
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   817
	]
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   818
     ] forkAt:4.
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   819
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   820
     [
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   821
	1 to:1000 do:[:i |
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   822
	    sema critical:[
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   823
		coll removeFirst.
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   824
		(Delay forSeconds:0.1) wait.
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   825
	    ]
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   826
	]
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   827
     ] forkAt:4.
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   828
    "
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
   829
13579
db80734f9576 changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents: 13355
diff changeset
   830
    "Modified: / 16-04-1996 / 10:00:46 / stefan"
db80734f9576 changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents: 13355
diff changeset
   831
    "Modified: / 11-08-2011 / 14:36:30 / cg"
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   832
!
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   833
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   834
wait
a27a279701f8 Initial revision
claus
parents:
diff changeset
   835
    "wait for the semaphore"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   836
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   837
    |activeProcess wasBlocked|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   838
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   839
    wasBlocked := OperatingSystem blockInterrupts.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   840
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   841
    count <= 0 ifTrue:[
16631
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
   842
        activeProcess := Processor activeProcess.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
   843
        "
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
   844
         need a while-loop here, since more than one process may
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
   845
         wait for it and another one may also wake up.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
   846
         Thus, the count is not always non-zero after returning from
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
   847
         suspend.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
   848
        "
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
   849
        [
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
   850
            self addWaitingProcess:activeProcess.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
   851
            "
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
   852
             for some more descriptive info in processMonitor ...
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
   853
             ... set the state to #wait (instead of #suspend)
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
   854
            "
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
   855
            [
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
   856
                activeProcess suspendWithState:#wait
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
   857
            ] ifCurtailed:[
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
   858
                "interrupts are not blocked when entered through Processor>>#interruptActive"
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
   859
                OperatingSystem blockInterrupts.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
   860
                self removeWaitingProcess:activeProcess.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
   861
                wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
   862
            ].
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
   863
            self removeWaitingProcess:activeProcess.
16286
4fd3aa76f29e class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 15690
diff changeset
   864
16631
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
   865
            count <= 0
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
   866
        ] whileTrue.
93
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
   867
    ].
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
   868
10
claus
parents: 3
diff changeset
   869
    count := count - 1.
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   870
    count == 0 ifTrue:[
16631
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
   871
        lastOwnerId := Processor activeProcessId.
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   872
    ].
93
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
   873
    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
752
0259dd855289 new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
   874
13579
db80734f9576 changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents: 13355
diff changeset
   875
    "Modified: / 13-12-1995 / 13:26:33 / stefan"
db80734f9576 changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents: 13355
diff changeset
   876
    "Modified: / 11-08-2011 / 14:36:43 / cg"
10
claus
parents: 3
diff changeset
   877
!
claus
parents: 3
diff changeset
   878
329
claus
parents: 302
diff changeset
   879
waitUncounted
claus
parents: 302
diff changeset
   880
    "wait for the semaphore; do not consume the resource
claus
parents: 302
diff changeset
   881
     (i.e. do not count down)"
claus
parents: 302
diff changeset
   882
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   883
    |activeProcess wasBlocked|
329
claus
parents: 302
diff changeset
   884
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   885
    count > 0 ifTrue:[
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   886
	^ self
329
claus
parents: 302
diff changeset
   887
    ].
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   888
    activeProcess := Processor activeProcess.
329
claus
parents: 302
diff changeset
   889
claus
parents: 302
diff changeset
   890
    wasBlocked := OperatingSystem blockInterrupts.
claus
parents: 302
diff changeset
   891
    "
claus
parents: 302
diff changeset
   892
     need a while-loop here, since more than one process may
claus
parents: 302
diff changeset
   893
     wait for it and another one may also wake up.
claus
parents: 302
diff changeset
   894
     Thus, the count is not always non-zero after returning from
claus
parents: 302
diff changeset
   895
     suspend.
claus
parents: 302
diff changeset
   896
    "
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   897
    [count <= 0] whileTrue:[
16625
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   898
	self addWaitingProcess:activeProcess.
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   899
	"
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   900
	 for some more descriptive info in processMonitor ...
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   901
	 ... set the state to #wait (instead of #suspend)
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   902
	"
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   903
	[
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   904
	    activeProcess suspendWithState:#wait
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   905
	] ifCurtailed:[
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   906
	    "interrupts are not blocked when entered through Processor>>#interruptActive"
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   907
	    OperatingSystem blockInterrupts.
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   908
	    self removeWaitingProcess:activeProcess.
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   909
	    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   910
	].
09b045c29e25 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16286
diff changeset
   911
	self removeWaitingProcess:activeProcess.
329
claus
parents: 302
diff changeset
   912
    ].
claus
parents: 302
diff changeset
   913
    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
752
0259dd855289 new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
   914
0259dd855289 new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
   915
    "Modified: 13.12.1995 / 13:26:49 / stefan"
2352
9588e545f4a5 use #removeIdentical
Claus Gittinger <cg@exept.de>
parents: 2265
diff changeset
   916
    "Modified: 1.2.1997 / 12:11:41 / cg"
329
claus
parents: 302
diff changeset
   917
!
claus
parents: 302
diff changeset
   918
16903
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   919
waitUncountedWithTimeoutMs:milliSeconds
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   920
    "wait for the semaphore; do not consume the resource
20263
9f3ddb907b77 #OTHER by mawalch
mawalch
parents: 20116
diff changeset
   921
     (i.e. do not count down).
16903
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   922
     Abort the wait after some time.
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   923
     return the receiver if the semaphore triggered normal, nil if we return
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   924
     due to a timeout.
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   925
     With zero timeout, this can be used to poll a semaphore (returning
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   926
     the receiver if the semaphore is available, nil if not).
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   927
     However, polling is not the intended use of semaphores, though.
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   928
     If milliSeconds is nil, wait without timeout."
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   929
20263
9f3ddb907b77 #OTHER by mawalch
mawalch
parents: 20116
diff changeset
   930
    |activeProcess timeoutOccurred wasBlocked timeoutBlock now endTime|
16903
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   931
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   932
    count > 0 ifTrue:[
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   933
        ^ self
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   934
    ].
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   935
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   936
    wasBlocked := OperatingSystem blockInterrupts.
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   937
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   938
    count <= 0 ifTrue:[
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   939
        "with zero-timeout, this is a poll"
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   940
        milliSeconds == 0 ifTrue:[
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   941
            wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   942
            ^ nil
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   943
        ].
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   944
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   945
        activeProcess := Processor activeProcess.
20263
9f3ddb907b77 #OTHER by mawalch
mawalch
parents: 20116
diff changeset
   946
        timeoutOccurred := false.
16903
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   947
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   948
        milliSeconds notNil ifTrue:[
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   949
            "Wait with timeout: calculate the end-time"
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   950
            now := OperatingSystem getMillisecondTime.
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   951
            endTime := OperatingSystem millisecondTimeAdd:now and:milliSeconds.
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   952
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   953
            timeoutBlock := [
20263
9f3ddb907b77 #OTHER by mawalch
mawalch
parents: 20116
diff changeset
   954
                    timeoutOccurred := true.
16903
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   955
                    timeoutBlock:= nil.
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   956
                    Processor resume:activeProcess.
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   957
                ].
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   958
            Processor addTimedBlock:timeoutBlock for:activeProcess atMilliseconds:endTime.
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   959
        ].
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   960
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   961
        "
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   962
         need a while-loop here, since more than one process may
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   963
         wait for it and another one may also wake up.
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   964
         Thus, the count is not always non-zero after returning from
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   965
         suspend.
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   966
        "
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   967
        [
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   968
            self addWaitingProcess:activeProcess.
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   969
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   970
            "
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   971
             for some more descriptive info in processMonitor ...
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   972
             ... set the state to #wait (instead of #suspend)
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   973
            "
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   974
            [
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   975
                "sleep until resumed..."
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   976
                activeProcess suspendWithState:#wait.
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   977
            ] ifCurtailed:[
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   978
                "interrupts are not blocked when entered through Processor>>#interruptActive"
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   979
                OperatingSystem blockInterrupts.
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   980
                timeoutBlock notNil ifTrue:[
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   981
                    Processor removeTimedBlock:timeoutBlock.
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   982
                    timeoutBlock := nil.
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   983
                ].
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   984
                self removeWaitingProcess:activeProcess.
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   985
                wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   986
            ].
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   987
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   988
            self removeWaitingProcess:activeProcess.
20263
9f3ddb907b77 #OTHER by mawalch
mawalch
parents: 20116
diff changeset
   989
            timeoutOccurred ifTrue:[
16903
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   990
                wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   991
                ^ nil
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   992
            ].
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   993
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   994
            count <= 0
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   995
        ] whileTrue.
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   996
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   997
        timeoutBlock notNil ifTrue:[
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   998
            Processor removeTimedBlock:timeoutBlock.
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
   999
            timeoutBlock := nil.
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1000
        ].
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1001
    ].
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1002
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1003
    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1004
    ^ self
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1005
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1006
    "Modified: / 13-12-1995 / 13:27:24 / stefan"
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1007
    "Modified: / 11-08-2011 / 14:37:00 / cg"
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1008
!
b8fc32d0daf3 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16631
diff changeset
  1009
19192
62dba7ef04eb #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19189
diff changeset
  1010
waitWithTimeout:secondsOrNilOrTimeDuration
12697
c70a0ec49f43 comment/format in: #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents: 12589
diff changeset
  1011
    "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
  1012
     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
  1013
     due to a timeout.
926
101239898989 unwind block over wait (in critical)
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
  1014
     The seconds-argument may be a float (i.e. use 0.1 for a 100ms timeout).
2690
f79d685e06af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2407
diff changeset
  1015
     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
  1016
     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
  1017
     However, polling is not the intended use of semaphores, though.
caf73c9821c5 Allow nil as argument to #waitWithTimeout: (no timeout) (stefan)
Claus Gittinger <cg@exept.de>
parents: 4633
diff changeset
  1018
     If seconds is nil, wait without timeout."
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
  1019
6061
9e266783d5c2 care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents: 5713
diff changeset
  1020
    |millis|
9e266783d5c2 care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents: 5713
diff changeset
  1021
19192
62dba7ef04eb #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19189
diff changeset
  1022
    secondsOrNilOrTimeDuration notNil ifTrue:[
21115
c745f080ded2 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 20426
diff changeset
  1023
        "asFloat converts a TimeDuration to a Float"
c745f080ded2 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 20426
diff changeset
  1024
        millis := (secondsOrNilOrTimeDuration asFloat * 1000) asInteger
6061
9e266783d5c2 care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents: 5713
diff changeset
  1025
    ].
9e266783d5c2 care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents: 5713
diff changeset
  1026
    ^ self waitWithTimeoutMs:millis.
9e266783d5c2 care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents: 5713
diff changeset
  1027
!
9e266783d5c2 care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents: 5713
diff changeset
  1028
9e266783d5c2 care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents: 5713
diff changeset
  1029
waitWithTimeoutMs:milliSeconds
9e266783d5c2 care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents: 5713
diff changeset
  1030
    "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
  1031
     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
  1032
     due to a timeout.
6061
9e266783d5c2 care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents: 5713
diff changeset
  1033
     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
  1034
     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
  1035
     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
  1036
     If milliSeconds is nil, wait without timeout."
9e266783d5c2 care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents: 5713
diff changeset
  1037
20263
9f3ddb907b77 #OTHER by mawalch
mawalch
parents: 20116
diff changeset
  1038
    |activeProcess timeoutOccurred wasBlocked timeoutBlock now endTime|
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
  1039
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
  1040
    wasBlocked := OperatingSystem blockInterrupts.
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
  1041
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
  1042
    count <= 0 ifTrue:[
16631
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1043
        "with zero-timeout, this is a poll"
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1044
        milliSeconds == 0 ifTrue:[
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1045
            wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1046
            ^ nil
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1047
        ].
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
  1048
16631
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1049
        activeProcess := Processor activeProcess.
20263
9f3ddb907b77 #OTHER by mawalch
mawalch
parents: 20116
diff changeset
  1050
        timeoutOccurred := false.
302
1f76060d58a4 *** empty log message ***
claus
parents: 269
diff changeset
  1051
16631
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1052
        milliSeconds notNil ifTrue:[
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1053
            "Wait with timeout: calculate the end-time"
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1054
            now := OperatingSystem getMillisecondTime.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1055
            endTime := OperatingSystem millisecondTimeAdd:now and:milliSeconds.
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
  1056
16631
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1057
            timeoutBlock := [
20263
9f3ddb907b77 #OTHER by mawalch
mawalch
parents: 20116
diff changeset
  1058
                    timeoutOccurred := true.
16631
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1059
                    timeoutBlock:= nil.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1060
                    Processor resume:activeProcess.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1061
                ].
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1062
            Processor addTimedBlock:timeoutBlock for:activeProcess atMilliseconds:endTime.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1063
        ].
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
  1064
16631
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1065
        "
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1066
         need a while-loop here, since more than one process may
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1067
         wait for it and another one may also wake up.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1068
         Thus, the count is not always non-zero after returning from
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1069
         suspend.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1070
        "
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1071
        [
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1072
            self addWaitingProcess:activeProcess.
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
  1073
16631
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1074
            "
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1075
             for some more descriptive info in processMonitor ...
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1076
             ... set the state to #wait (instead of #suspend)
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1077
            "
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1078
            [
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1079
                "sleep until resumed..."
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1080
                activeProcess suspendWithState:#wait.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1081
            ] ifCurtailed:[
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1082
                "interrupts are not blocked when entered through Processor>>#interruptActive"
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1083
                OperatingSystem blockInterrupts.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1084
                timeoutBlock notNil ifTrue:[
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1085
                    Processor removeTimedBlock:timeoutBlock.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1086
                    timeoutBlock := nil.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1087
                ].
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1088
                self removeWaitingProcess:activeProcess.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1089
                wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1090
            ].
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
  1091
16631
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1092
            self removeWaitingProcess:activeProcess.
20263
9f3ddb907b77 #OTHER by mawalch
mawalch
parents: 20116
diff changeset
  1093
            timeoutOccurred ifTrue:[
16631
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1094
                wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1095
                ^ nil
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1096
            ].
16286
4fd3aa76f29e class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 15690
diff changeset
  1097
16631
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1098
            count <= 0
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1099
        ] whileTrue.
4902
caf73c9821c5 Allow nil as argument to #waitWithTimeout: (no timeout) (stefan)
Claus Gittinger <cg@exept.de>
parents: 4633
diff changeset
  1100
16631
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1101
        timeoutBlock notNil ifTrue:[
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1102
            Processor removeTimedBlock:timeoutBlock.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1103
            timeoutBlock := nil.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1104
        ].
4902
caf73c9821c5 Allow nil as argument to #waitWithTimeout: (no timeout) (stefan)
Claus Gittinger <cg@exept.de>
parents: 4633
diff changeset
  1105
    ].
2690
f79d685e06af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2407
diff changeset
  1106
15635
1b42005ee234 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 15551
diff changeset
  1107
    "if we come here, we have acquired the semaphore"
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
  1108
    count := count - 1.
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
  1109
    count == 0 ifTrue:[
16631
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1110
        lastOwnerId := Processor activeProcessId.
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
  1111
    ].
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
  1112
    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
770
402958905760 protocol of waitWithTImeOut: changed
Claus Gittinger <cg@exept.de>
parents: 769
diff changeset
  1113
    ^ self
752
0259dd855289 new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
  1114
13579
db80734f9576 changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents: 13355
diff changeset
  1115
    "Modified: / 13-12-1995 / 13:27:24 / stefan"
db80734f9576 changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents: 13355
diff changeset
  1116
    "Modified: / 11-08-2011 / 14:37:00 / cg"
16631
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1117
!
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1118
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1119
waitWithTimeoutMs:milliSeconds state:waitStateSymbol
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1120
    "wait for the semaphore, but abort the wait after some time.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1121
     return the receiver if the semaphore triggered normal, nil if we return
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1122
     due to a timeout.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1123
     With zero timeout, this can be used to poll a semaphore (returning
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1124
     the receiver if the semaphore is available, nil if not).
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1125
     However, polling is not the intended use of semaphores, though.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1126
     If milliSeconds is nil, wait without timeout.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1127
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1128
     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
  1129
20263
9f3ddb907b77 #OTHER by mawalch
mawalch
parents: 20116
diff changeset
  1130
    |activeProcess timeoutOccurred wasBlocked timeoutBlock now endTime|
16631
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1131
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1132
    wasBlocked := OperatingSystem blockInterrupts.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1133
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1134
    count <= 0 ifTrue:[
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1135
        "with zero-timeout, this is a poll"
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1136
        milliSeconds == 0 ifTrue:[
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1137
            wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1138
            ^ nil
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1139
        ].
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1140
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1141
        activeProcess := Processor activeProcess.
20263
9f3ddb907b77 #OTHER by mawalch
mawalch
parents: 20116
diff changeset
  1142
        timeoutOccurred := false.
16631
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1143
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1144
        milliSeconds notNil ifTrue:[
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1145
            "Wait with timeout: calculate the end-time"
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1146
            now := OperatingSystem getMillisecondTime.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1147
            endTime := OperatingSystem millisecondTimeAdd:now and:milliSeconds.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1148
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1149
            timeoutBlock := [
20263
9f3ddb907b77 #OTHER by mawalch
mawalch
parents: 20116
diff changeset
  1150
                    timeoutOccurred := true.
16631
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1151
                    timeoutBlock:= nil.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1152
                    Processor resume:activeProcess.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1153
                ].
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1154
            Processor addTimedBlock:timeoutBlock for:activeProcess atMilliseconds:endTime.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1155
        ].
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1156
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1157
        "
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1158
         need a while-loop here, since more than one process may
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1159
         wait for it and another one may also wake up.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1160
         Thus, the count is not always non-zero after returning from
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1161
         suspend.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1162
        "
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1163
        [
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1164
            self addWaitingProcess:activeProcess.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1165
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1166
            "
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1167
             for some more descriptive info in processMonitor ...
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1168
             ... set the state to #wait (instead of #suspend)
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1169
            "
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1170
            [
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1171
                "sleep until resumed..."
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1172
                activeProcess suspendWithState:waitStateSymbol.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1173
            ] ifCurtailed:[
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1174
                "interrupts are not blocked when entered through Processor>>#interruptActive"
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1175
                OperatingSystem blockInterrupts.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1176
                timeoutBlock notNil ifTrue:[
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1177
                    Processor removeTimedBlock:timeoutBlock.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1178
                    timeoutBlock := nil.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1179
                ].
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1180
                self removeWaitingProcess:activeProcess.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1181
                wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1182
            ].
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1183
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1184
            self removeWaitingProcess:activeProcess.
20263
9f3ddb907b77 #OTHER by mawalch
mawalch
parents: 20116
diff changeset
  1185
            timeoutOccurred ifTrue:[
16631
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1186
                wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1187
                ^ nil
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1188
            ].
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1189
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1190
            count <= 0
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1191
        ] whileTrue.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1192
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1193
        timeoutBlock notNil ifTrue:[
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1194
            Processor removeTimedBlock:timeoutBlock.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1195
            timeoutBlock := nil.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1196
        ].
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1197
    ].
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1198
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1199
    "if we come here, we have acquired the semaphore"
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1200
    count := count - 1.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1201
    count == 0 ifTrue:[
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1202
        lastOwnerId := Processor activeProcessId.
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1203
    ].
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1204
    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
ba77908696d0 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 16625
diff changeset
  1205
    ^ self
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
  1206
! !
77
6c38ca59927f *** empty log message ***
claus
parents: 69
diff changeset
  1207
1812
a048a98aa299 kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
  1208
!Semaphore class methodsFor:'documentation'!
183
7899f380e77d signalIf
claus
parents: 159
diff changeset
  1209
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
  1210
version
19189
48f29d2c0455 #DOCUMENTATION
matilk
parents: 16903
diff changeset
  1211
    ^ '$Header$'
12567
eb78ade59632 added: #waitTimeoutSeconds:
Stefan Vogel <sv@exept.de>
parents: 11281
diff changeset
  1212
!
eb78ade59632 added: #waitTimeoutSeconds:
Stefan Vogel <sv@exept.de>
parents: 11281
diff changeset
  1213
eb78ade59632 added: #waitTimeoutSeconds:
Stefan Vogel <sv@exept.de>
parents: 11281
diff changeset
  1214
version_CVS
19189
48f29d2c0455 #DOCUMENTATION
matilk
parents: 16903
diff changeset
  1215
    ^ '$Header$'
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1216
! !
14763
db01620704a8 class: Semaphore
Stefan Vogel <sv@exept.de>
parents: 14537
diff changeset
  1217