Semaphore.st
author Claus Gittinger <cg@exept.de>
Mon, 17 Feb 1997 11:36:58 +0100
changeset 2407 6b0e21391915
parent 2352 9588e545f4a5
child 2690 f79d685e06af
permissions -rw-r--r--
added #initSignals for ST-80 compatibility.
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
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
     3
	      All Rights Reserved
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     4
a27a279701f8 Initial revision
claus
parents:
diff changeset
     5
 This software is furnished under a license and may be used
a27a279701f8 Initial revision
claus
parents:
diff changeset
     6
 only in accordance with the terms of that license and with the
a27a279701f8 Initial revision
claus
parents:
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
a27a279701f8 Initial revision
claus
parents:
diff changeset
     8
 be provided or otherwise made available to, or used by, any
a27a279701f8 Initial revision
claus
parents:
diff changeset
     9
 other person.  No title to or ownership of the software is
a27a279701f8 Initial revision
claus
parents:
diff changeset
    10
 hereby transferred.
a27a279701f8 Initial revision
claus
parents:
diff changeset
    11
"
a27a279701f8 Initial revision
claus
parents:
diff changeset
    12
375
claus
parents: 362
diff changeset
    13
Object subclass:#Semaphore
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
    14
	instanceVariableNames:'count waitingProcesses lastOwnerID name'
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
    15
	classVariableNames:''
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
    16
	poolDictionaries:''
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
    17
	category:'Kernel-Processes'
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    18
!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    19
1812
a048a98aa299 kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
    20
!Semaphore class methodsFor:'documentation'!
69
4564b6328136 *** empty log message ***
claus
parents: 25
diff changeset
    21
88
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    22
copyright
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    23
"
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    24
 COPYRIGHT (c) 1993 by Claus Gittinger
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
    25
	      All Rights Reserved
88
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    26
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    27
 This software is furnished under a license and may be used
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    28
 only in accordance with the terms of that license and with the
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    29
 inclusion of the above copyright notice.   This software may not
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    30
 be provided or otherwise made available to, or used by, any
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    31
 other person.  No title to or ownership of the software is
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    32
 hereby transferred.
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    33
"
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    34
!
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    35
69
4564b6328136 *** empty log message ***
claus
parents: 25
diff changeset
    36
documentation
4564b6328136 *** empty log message ***
claus
parents: 25
diff changeset
    37
"
4564b6328136 *** empty log message ***
claus
parents: 25
diff changeset
    38
    Semaphores are used to synchronize processes providing a nonBusy wait
4564b6328136 *** empty log message ***
claus
parents: 25
diff changeset
    39
    mechanism. A process can wait for the availability of some resource by
93
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
    40
    performing a Semaphore>>wait, which will suspend the process until the
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
    41
    resource becomes available. Signalling is done by (another process performing) 
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
    42
    Semaphore>>signal.
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
    43
    If the resource has been already available before the wait, no suspending is
69
4564b6328136 *** empty log message ***
claus
parents: 25
diff changeset
    44
    done, but the resource immediately allocated.
2234
21fe6c17fe8d commentary
Claus Gittinger <cg@exept.de>
parents: 2142
diff changeset
    45
93
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
    46
    There are also semaphores for mutual access to a critical region
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
    47
    (Semaphore>>forMutualExclusion and Semaphore>>critical:).
88
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    48
2234
21fe6c17fe8d commentary
Claus Gittinger <cg@exept.de>
parents: 2142
diff changeset
    49
    Additional protocol is provided for oneShot semaphores, 
21fe6c17fe8d commentary
Claus Gittinger <cg@exept.de>
parents: 2142
diff changeset
    50
    (#signalOnce) and for conditional signalling (#signalIf).
21fe6c17fe8d commentary
Claus Gittinger <cg@exept.de>
parents: 2142
diff changeset
    51
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
    52
    You can also attach semaphores to external events (such as I/O arrival or
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
    53
    timer events). 
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
    54
    This is done by telling the Processor to signal the semaphore 
183
7899f380e77d signalIf
claus
parents: 159
diff changeset
    55
    under some condition.
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
    56
    See 'Processor>>signal:afterSeconds:', 'Processor>>signal:onInput:' etc.
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
    57
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
    58
    See examples in doc/coding (found in the CodingExamples-nameSpace).
1273
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1185
diff changeset
    59
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
    60
    [instance variables:]
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
    61
	count			<SmallInteger>		the number of waits, that will go through
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
    62
							without blocking.
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
    63
							Incremented on #signal; decremented on #wait.
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
    64
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
    65
	waitingProcesses	<OrderedCollection>	waiting processes - will be served first
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
    66
							come first served when signalled.
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
    67
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
    68
	lastOwnerID		<SmallInteger>		a debugging aid: set when count drops
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
    69
							to zero to the current processes id.
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
    70
							Helps in finding deadlocks.
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
    71
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
    72
	name			<String>		a debugging aid: an optional userFriendly
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
    73
							name; helps to identify a semaphore easier.
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
    74
1273
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1185
diff changeset
    75
    [see also:]
2234
21fe6c17fe8d commentary
Claus Gittinger <cg@exept.de>
parents: 2142
diff changeset
    76
        SemaphoreSet RecursionLock Monitor
1273
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1185
diff changeset
    77
        SharedQueue Delay 
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1185
diff changeset
    78
        Process ProcessorScheduler
1294
e26bbb61f6b2 documentation
Claus Gittinger <cg@exept.de>
parents: 1273
diff changeset
    79
e26bbb61f6b2 documentation
Claus Gittinger <cg@exept.de>
parents: 1273
diff changeset
    80
    [author:]
e26bbb61f6b2 documentation
Claus Gittinger <cg@exept.de>
parents: 1273
diff changeset
    81
        Claus Gittinger
69
4564b6328136 *** empty log message ***
claus
parents: 25
diff changeset
    82
"
4564b6328136 *** empty log message ***
claus
parents: 25
diff changeset
    83
! !
4564b6328136 *** empty log message ***
claus
parents: 25
diff changeset
    84
1812
a048a98aa299 kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
    85
!Semaphore class methodsFor:'instance creation'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    86
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    87
forMutualExclusion
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    88
    "create & return a new semaphore which allows exactly one process to
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
    89
     wait on it without blocking. This type of semaphore is used
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
    90
     for mutual exclusion from critical regions (see #critical:)"
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    91
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    92
    ^ super new setCount:1
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
    93
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
    94
    "Modified: 10.1.1997 / 21:44:30 / cg"
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    95
!
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    96
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    97
new
a27a279701f8 Initial revision
claus
parents:
diff changeset
    98
    "create & return a new semaphore which blocks until a signal is sent"
a27a279701f8 Initial revision
claus
parents:
diff changeset
    99
a27a279701f8 Initial revision
claus
parents:
diff changeset
   100
    ^ super new setCount:0
a27a279701f8 Initial revision
claus
parents:
diff changeset
   101
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   102
a27a279701f8 Initial revision
claus
parents:
diff changeset
   103
new:n
a27a279701f8 Initial revision
claus
parents:
diff changeset
   104
    "create & return a new semaphore which allows n waits before
a27a279701f8 Initial revision
claus
parents:
diff changeset
   105
     blocking"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   106
a27a279701f8 Initial revision
claus
parents:
diff changeset
   107
    ^ super new setCount:n
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   108
! !
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   109
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   110
!Semaphore methodsFor:'friend-class interface'!
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   111
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   112
checkAndRegisterProcess:process
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   113
    "interface for SemaphoreSet.
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   114
     If the semaphore is available, decrement it and return true.
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   115
     Otherwise register our process to be wakened up once the semaphore is available
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   116
     and return false..
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   117
    "
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   118
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   119
    "
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   120
     bad ST/X trick (needs change, when multiProcessor support is added):
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   121
     this works only since interrupts are only serviced at 
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   122
     message send and method-return time ....
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   123
     If you add a message send into the ifTrue:-block, things will
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   124
     go mad ... (especially be careful when adding a debugPrint-here)
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   125
    "
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   126
    count ~~ 0 ifTrue:[
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   127
        count := count - 1.
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   128
	count == 0 ifTrue:[
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   129
	    lastOwnerID := Processor activeProcessId.
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   130
	].
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   131
        ^ true
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   132
    ].
1122
251ea6ec6c61 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1031
diff changeset
   133
    (waitingProcesses identityIndexOf:process) == 0 ifTrue:[
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   134
        waitingProcesses add:process.
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   135
    ].
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   136
    ^ false
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   137
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   138
    "Modified: 14.12.1995 / 10:32:17 / stefan"
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   139
    "Modified: 10.1.1997 / 21:42:18 / cg"
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   140
!
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   141
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   142
unregisterProcess:process
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   143
    "interface for SemaphoreSet.
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   144
     Unregister our process from the Semaphore"
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   145
2352
9588e545f4a5 use #removeIdentical
Claus Gittinger <cg@exept.de>
parents: 2265
diff changeset
   146
    waitingProcesses removeIdentical:process ifAbsent:[].
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   147
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   148
    "Created: 14.12.1995 / 10:31:50 / stefan"
2352
9588e545f4a5 use #removeIdentical
Claus Gittinger <cg@exept.de>
parents: 2265
diff changeset
   149
    "Modified: 1.2.1997 / 12:11:22 / cg"
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   150
! !
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   151
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   152
!Semaphore methodsFor:'printing & storing'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   153
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   154
displayString
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   155
    "return a string to display the receiver - include the
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   156
     count for your convenience"
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   157
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   158
    |n|
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   159
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   160
    name isNil ifTrue:[
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   161
	n := 'unnamed'
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   162
    ] ifFalse:[
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   163
	n := name
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   164
    ].
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   165
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   166
    ^ self class name , '(' , count printString , ' name: ' , n , ')'
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   167
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   168
    "Modified: 10.1.1997 / 21:43:04 / cg"
2265
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   169
!
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   170
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   171
name
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   172
    "return the semaphores userFriendly name"
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   173
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   174
    ^ name
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   175
!
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   176
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   177
name:aString
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   178
    "set the semaphores userFriendly name"
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   179
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   180
    name := aString
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   181
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   182
a27a279701f8 Initial revision
claus
parents:
diff changeset
   183
!Semaphore methodsFor:'private accessing'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   184
2407
6b0e21391915 added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 2352
diff changeset
   185
initSignals
6b0e21391915 added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 2352
diff changeset
   186
    "set the count of the semaphore to zero.
6b0e21391915 added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 2352
diff changeset
   187
     provided for ST-80 compatibility."
6b0e21391915 added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 2352
diff changeset
   188
6b0e21391915 added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 2352
diff changeset
   189
    count := 0
6b0e21391915 added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 2352
diff changeset
   190
6b0e21391915 added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 2352
diff changeset
   191
    "Created: 17.2.1997 / 11:31:19 / cg"
6b0e21391915 added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 2352
diff changeset
   192
!
6b0e21391915 added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 2352
diff changeset
   193
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   194
setCount:n
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   195
    "set the count of the semaphore;
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   196
     thats the number of possible waits, without blocking"
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   197
2407
6b0e21391915 added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 2352
diff changeset
   198
    waitingProcesses := OrderedCollection new:2.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   199
    count := n
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   200
2407
6b0e21391915 added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 2352
diff changeset
   201
    "Modified: 17.2.1997 / 11:36:40 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   202
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   203
217
a0400fdbc933 *** empty log message ***
claus
parents: 183
diff changeset
   204
!Semaphore methodsFor:'queries '!
a0400fdbc933 *** empty log message ***
claus
parents: 183
diff changeset
   205
2235
c6a15bd9a33c added access to count
Claus Gittinger <cg@exept.de>
parents: 2234
diff changeset
   206
count
c6a15bd9a33c added access to count
Claus Gittinger <cg@exept.de>
parents: 2234
diff changeset
   207
    "return the number of 'already-counted' trigger events.
c6a15bd9a33c added access to count
Claus Gittinger <cg@exept.de>
parents: 2234
diff changeset
   208
     Thats the number of waits which will succeed without blocking"
c6a15bd9a33c added access to count
Claus Gittinger <cg@exept.de>
parents: 2234
diff changeset
   209
c6a15bd9a33c added access to count
Claus Gittinger <cg@exept.de>
parents: 2234
diff changeset
   210
    ^ count
c6a15bd9a33c added access to count
Claus Gittinger <cg@exept.de>
parents: 2234
diff changeset
   211
c6a15bd9a33c added access to count
Claus Gittinger <cg@exept.de>
parents: 2234
diff changeset
   212
    "Created: 23.1.1997 / 02:55:58 / cg"
c6a15bd9a33c added access to count
Claus Gittinger <cg@exept.de>
parents: 2234
diff changeset
   213
!
c6a15bd9a33c added access to count
Claus Gittinger <cg@exept.de>
parents: 2234
diff changeset
   214
2265
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   215
lastOwnerId
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   216
    "return the processId of the last owning process
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   217
     (the one which counted to zero).
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   218
     May be very useful in debugging deadLock situations"
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   219
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   220
    ^ lastOwnerID
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   221
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   222
    "Created: 24.1.1997 / 23:09:33 / cg"
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   223
!
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   224
1329
0a025ca40653 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   225
numberOfWaitingProcesses
0a025ca40653 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   226
    "return the number of processes waiting on the receiver"
0a025ca40653 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   227
0a025ca40653 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   228
    ^ waitingProcesses size
0a025ca40653 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   229
0a025ca40653 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   230
    "Created: 3.5.1996 / 18:06:27 / cg"
0a025ca40653 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   231
!
0a025ca40653 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   232
1577
720f14f90e2c access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents: 1329
diff changeset
   233
waitingProcesses
720f14f90e2c access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents: 1329
diff changeset
   234
    "return the processes waiting on the receiver"
720f14f90e2c access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents: 1329
diff changeset
   235
720f14f90e2c access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents: 1329
diff changeset
   236
    ^ waitingProcesses
720f14f90e2c access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents: 1329
diff changeset
   237
720f14f90e2c access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents: 1329
diff changeset
   238
    "Created: 18.7.1996 / 20:53:33 / cg"
720f14f90e2c access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents: 1329
diff changeset
   239
!
720f14f90e2c access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents: 1329
diff changeset
   240
217
a0400fdbc933 *** empty log message ***
claus
parents: 183
diff changeset
   241
wouldBlock
a0400fdbc933 *** empty log message ***
claus
parents: 183
diff changeset
   242
    "return true, if the receiver would block the activeProcess
a0400fdbc933 *** empty log message ***
claus
parents: 183
diff changeset
   243
     if a wait was performed. False otherwise."
a0400fdbc933 *** empty log message ***
claus
parents: 183
diff changeset
   244
a0400fdbc933 *** empty log message ***
claus
parents: 183
diff changeset
   245
    ^ count == 0
a0400fdbc933 *** empty log message ***
claus
parents: 183
diff changeset
   246
! !
a0400fdbc933 *** empty log message ***
claus
parents: 183
diff changeset
   247
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   248
!Semaphore methodsFor:'wait & signal'!
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   249
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   250
critical:aBlock
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   251
    "evaluate aBlock as a critical region; the receiver must be
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   252
     created using Semaphore>>forMutualExclusion"
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   253
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
   254
    |retVal gotSema|
926
101239898989 unwind block over wait (in critical)
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   255
101239898989 unwind block over wait (in critical)
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   256
    [
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
   257
        gotSema := self wait.
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
   258
        retVal := aBlock value.
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
   259
    ] valueOnUnwindDo:[
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
   260
        gotSema notNil ifTrue:[self signal].
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
   261
    ].
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
   262
    self signal.
926
101239898989 unwind block over wait (in critical)
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   263
    ^ retVal
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   264
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   265
    "
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   266
      the example below is stupid (it should use a SharedQueue,
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   267
      or at least a Queue with critical regions).
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   268
      Anyhow, it demonstrates how two processes lock each other
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   269
      from accessing coll at the same time
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   270
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   271
     |sema coll|
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   272
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   273
     sema := Semaphore forMutualExclusion.
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   274
     coll := OrderedCollection new:10.
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   275
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   276
     [
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
   277
        1 to:1000 do:[:i |
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
   278
            sema critical:[
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
   279
                coll addLast:i.
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
   280
                (Delay forSeconds:0.1) wait.
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
   281
            ]
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
   282
        ]
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   283
     ] forkAt:4.
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   284
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   285
     [
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
   286
        1 to:1000 do:[:i |
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
   287
            sema critical:[
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
   288
                coll removeFirst.
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
   289
                (Delay forSeconds:0.1) wait.
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
   290
            ]
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
   291
        ]
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   292
     ] forkAt:4.
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   293
    "
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
   294
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
   295
    "Modified: 16.4.1996 / 10:00:46 / stefan"
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   296
!
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   297
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   298
signal
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   299
    "waking up (first) waiter"
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   300
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   301
    |p wasBlocked|
362
claus
parents: 329
diff changeset
   302
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   303
    wasBlocked := OperatingSystem blockInterrupts.
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   304
    [
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   305
        count := count + 1.
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   306
        waitingProcesses notEmpty ifTrue:[
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   307
            p := waitingProcesses removeFirst.
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   308
            p resume.
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   309
        ].
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   310
    ] valueNowOrOnUnwindDo:[
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   311
        wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   312
    ]
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   313
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   314
    "Modified: 28.2.1996 / 21:23:10 / cg"
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   315
!
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   316
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   317
signalForAll
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   318
    "signal the semaphore for all waiters.
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   319
     This can be used for process synchronization, if multiple processes are
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   320
     waiting for a common event."
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   321
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   322
    |wasBlocked|
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   323
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   324
    [waitingProcesses notEmpty] whileTrue:[
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   325
        wasBlocked := OperatingSystem blockInterrupts.
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   326
        [
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   327
            waitingProcesses notEmpty ifTrue:[
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   328
                self signal
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   329
            ].
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   330
        ] valueNowOrOnUnwindDo:[
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   331
            wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   332
        ]
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   333
    ]
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   334
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   335
    "Modified: 28.2.1996 / 21:23:38 / cg"
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   336
!
362
claus
parents: 329
diff changeset
   337
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   338
signalIf
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   339
    "signal the semaphore, but only if being waited upon.
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   340
     This can be used for one-shot semaphores (i.e. not remembering
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   341
     previous signals)"
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   342
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   343
    |wasBlocked|
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   344
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   345
    waitingProcesses notEmpty ifTrue:[
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   346
        wasBlocked := OperatingSystem blockInterrupts.
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   347
        [
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   348
            waitingProcesses notEmpty ifTrue:[
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   349
                self signal
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   350
            ].
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   351
        ] valueNowOrOnUnwindDo:[
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   352
            wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   353
        ]
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   354
    ]
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   355
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   356
    "Modified: 28.2.1996 / 21:23:57 / cg"
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   357
!
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   358
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   359
signalOnce
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   360
    "wakeup waiters - but only once.
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   361
     I.e. if the semaphore has already been signalled, this
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   362
     is ignored."
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   363
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   364
    |wasBlocked|
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   365
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   366
    count == 0 ifTrue:[
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   367
        wasBlocked := OperatingSystem blockInterrupts.
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   368
        [
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   369
            count == 0 ifTrue:[
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   370
                self signal
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   371
            ].
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   372
        ] valueNowOrOnUnwindDo:[
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   373
            wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   374
        ]
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   375
    ]
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   376
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   377
    "Modified: 28.2.1996 / 21:24:08 / cg"
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   378
!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   379
a27a279701f8 Initial revision
claus
parents:
diff changeset
   380
wait
a27a279701f8 Initial revision
claus
parents:
diff changeset
   381
    "wait for the semaphore"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   382
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   383
    |activeProcess wasBlocked|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   384
a27a279701f8 Initial revision
claus
parents:
diff changeset
   385
    "
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   386
     bad ST/X trick (needs change, when multiProcessor support is added):
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   387
     this works only since interrupts are only serviced at 
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   388
     message send and method-return time ....
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   389
     If you add a message send between the compare and the decrement,
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   390
     things will go mad ... (especially be careful when adding a debugPrint-here)
93
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
   391
    "
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
   392
    count ~~ 0 ifTrue:[
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   393
        count := count - 1.
2352
9588e545f4a5 use #removeIdentical
Claus Gittinger <cg@exept.de>
parents: 2265
diff changeset
   394
        count == 0 ifTrue:[
9588e545f4a5 use #removeIdentical
Claus Gittinger <cg@exept.de>
parents: 2265
diff changeset
   395
            lastOwnerID := Processor activeProcessId.
9588e545f4a5 use #removeIdentical
Claus Gittinger <cg@exept.de>
parents: 2265
diff changeset
   396
        ].
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   397
        ^ self
93
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
   398
    ].
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
   399
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   400
    activeProcess := Processor activeProcess.
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   401
302
1f76060d58a4 *** empty log message ***
claus
parents: 269
diff changeset
   402
    wasBlocked := OperatingSystem blockInterrupts.
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   403
    "
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   404
     need a while-loop here, since more than one process may
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   405
     wait for it and another one may also wake up.
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   406
     Thus, the count is not always non-zero after returning from
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   407
     suspend.
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   408
    "
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   409
    [count == 0] whileTrue:[
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   410
        waitingProcesses add:activeProcess.
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   411
        "
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   412
         for some more descriptive info in processMonitor ...
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   413
         ... set the state to #wait (instead of #suspend)
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   414
        "
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   415
        [
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   416
            activeProcess suspendWithState:#wait
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   417
        ] valueOnUnwindDo:[
2352
9588e545f4a5 use #removeIdentical
Claus Gittinger <cg@exept.de>
parents: 2265
diff changeset
   418
            waitingProcesses removeIdentical:activeProcess ifAbsent:[].
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   419
            wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
1812
a048a98aa299 kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   420
        ].
a048a98aa299 kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   421
        
a048a98aa299 kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   422
        count == 0 ifTrue:[
a048a98aa299 kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   423
            "/ care for someone manually resuming me ...
a048a98aa299 kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   424
            "/ being multiple times on waitingProcesses
2352
9588e545f4a5 use #removeIdentical
Claus Gittinger <cg@exept.de>
parents: 2265
diff changeset
   425
            waitingProcesses removeIdentical:activeProcess ifAbsent:[].
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   426
        ]
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   427
    ].
10
claus
parents: 3
diff changeset
   428
    count := count - 1.
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   429
    count == 0 ifTrue:[
2352
9588e545f4a5 use #removeIdentical
Claus Gittinger <cg@exept.de>
parents: 2265
diff changeset
   430
        lastOwnerID := Processor activeProcessId.
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   431
    ].
93
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
   432
    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
752
0259dd855289 new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
   433
0259dd855289 new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
   434
    "Modified: 13.12.1995 / 13:26:33 / stefan"
2352
9588e545f4a5 use #removeIdentical
Claus Gittinger <cg@exept.de>
parents: 2265
diff changeset
   435
    "Modified: 1.2.1997 / 12:11:32 / cg"
10
claus
parents: 3
diff changeset
   436
!
claus
parents: 3
diff changeset
   437
329
claus
parents: 302
diff changeset
   438
waitUncounted
claus
parents: 302
diff changeset
   439
    "wait for the semaphore; do not consume the resource
claus
parents: 302
diff changeset
   440
     (i.e. do not count down)"
claus
parents: 302
diff changeset
   441
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   442
    |activeProcess wasBlocked|
329
claus
parents: 302
diff changeset
   443
claus
parents: 302
diff changeset
   444
    "
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   445
     bad ST/X trick (needs change, when multiProcessor support is added):
329
claus
parents: 302
diff changeset
   446
     this works only since interrupts are only serviced at 
claus
parents: 302
diff changeset
   447
     message send and method-return time ....
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   448
     If you add a message send between the compare and the decrement,
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   449
     things will go mad ... (especially be careful when adding a debugPrint-here)
329
claus
parents: 302
diff changeset
   450
    "
claus
parents: 302
diff changeset
   451
    count ~~ 0 ifTrue:[
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   452
        ^ self
329
claus
parents: 302
diff changeset
   453
    ].
claus
parents: 302
diff changeset
   454
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   455
    activeProcess := Processor activeProcess.
329
claus
parents: 302
diff changeset
   456
claus
parents: 302
diff changeset
   457
    wasBlocked := OperatingSystem blockInterrupts.
claus
parents: 302
diff changeset
   458
    "
claus
parents: 302
diff changeset
   459
     need a while-loop here, since more than one process may
claus
parents: 302
diff changeset
   460
     wait for it and another one may also wake up.
claus
parents: 302
diff changeset
   461
     Thus, the count is not always non-zero after returning from
claus
parents: 302
diff changeset
   462
     suspend.
claus
parents: 302
diff changeset
   463
    "
claus
parents: 302
diff changeset
   464
    [count == 0] whileTrue:[
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   465
        waitingProcesses add:activeProcess.
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   466
        "
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   467
         for some more descriptive info in processMonitor ...
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   468
         ... set the state to #wait (instead of #suspend)
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   469
        "
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   470
        [
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   471
            activeProcess suspendWithState:#wait
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   472
        ] valueOnUnwindDo:[
2352
9588e545f4a5 use #removeIdentical
Claus Gittinger <cg@exept.de>
parents: 2265
diff changeset
   473
            waitingProcesses removeIdentical:activeProcess ifAbsent:[].
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   474
            wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
1812
a048a98aa299 kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   475
        ].
a048a98aa299 kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   476
        count == 0 ifTrue:[
a048a98aa299 kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   477
            "/ care for someone manually resuming me ...
a048a98aa299 kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   478
            "/ being multiple times on waitingProcesses
2352
9588e545f4a5 use #removeIdentical
Claus Gittinger <cg@exept.de>
parents: 2265
diff changeset
   479
            waitingProcesses removeIdentical:activeProcess ifAbsent:[].
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   480
        ]
329
claus
parents: 302
diff changeset
   481
    ].
claus
parents: 302
diff changeset
   482
    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
752
0259dd855289 new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
   483
0259dd855289 new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
   484
    "Modified: 13.12.1995 / 13:26:49 / stefan"
2352
9588e545f4a5 use #removeIdentical
Claus Gittinger <cg@exept.de>
parents: 2265
diff changeset
   485
    "Modified: 1.2.1997 / 12:11:41 / cg"
329
claus
parents: 302
diff changeset
   486
!
claus
parents: 302
diff changeset
   487
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   488
waitWithTimeout:seconds
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   489
    "wait for the semaphore, but abort the wait after some time.
770
402958905760 protocol of waitWithTImeOut: changed
Claus Gittinger <cg@exept.de>
parents: 769
diff changeset
   490
     return the receiver if semaphore triggered normal, nil if we return
926
101239898989 unwind block over wait (in critical)
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   491
     due to a timeout. 
101239898989 unwind block over wait (in critical)
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   492
     The seconds-argument may be a float (i.e. use 0.1 for a 100ms timeout).
101239898989 unwind block over wait (in critical)
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   493
     With zero timeout, this can be used to poll a semaphore 
101239898989 unwind block over wait (in critical)
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   494
     (which is not the intend of semaphores, though)."
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   495
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   496
    |activeProcess timeoutOccured wasBlocked unblock now endTime|
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   497
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   498
    "
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   499
     bad ST/X trick (needs change, when multiProcessor support is added):
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   500
     this works only since interrupts are only serviced at 
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   501
     message send and method-return time ....
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   502
     If you add a message send between the compare and the decrement,
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   503
     things will go mad ... (especially be careful when adding a debugPrint-here)
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   504
    "
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   505
    count ~~ 0 ifTrue:[
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   506
        count := count - 1.
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   507
        count == 0 ifTrue:[
2352
9588e545f4a5 use #removeIdentical
Claus Gittinger <cg@exept.de>
parents: 2265
diff changeset
   508
            lastOwnerID := Processor activeProcessId.
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   509
        ].
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   510
        ^ self
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   511
    ].
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   512
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   513
    "
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   514
     with zero-timeout, this is a poll
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   515
    "
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   516
    seconds = 0 ifTrue:[
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   517
        ^ nil
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   518
    ].
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   519
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   520
    activeProcess := Processor activeProcess.
302
1f76060d58a4 *** empty log message ***
claus
parents: 269
diff changeset
   521
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   522
    wasBlocked := OperatingSystem blockInterrupts.
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   523
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   524
    "
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   525
     calculate the end-time
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   526
    "
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   527
    now := OperatingSystem getMillisecondTime.
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   528
    endTime := OperatingSystem millisecondTimeAdd:now and:(seconds * 1000).
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   529
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   530
    unblock := [timeoutOccured := true. Processor resume:activeProcess].
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   531
    Processor addTimedBlock:unblock for:activeProcess atMilliseconds:endTime.
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   532
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   533
    "
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   534
     need a while-loop here, since more than one process may
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   535
     wait for it and another one may also wake up.
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   536
     Thus, the count is not always non-zero after returning from
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   537
     suspend.
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   538
    "
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   539
    [count == 0] whileTrue:[
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   540
        waitingProcesses add:activeProcess.
302
1f76060d58a4 *** empty log message ***
claus
parents: 269
diff changeset
   541
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   542
        timeoutOccured := false.
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   543
        "
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   544
         for some more descriptive info in processMonitor ...
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   545
         ... set the state to #wait (instead of #suspend)
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   546
        "
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   547
        [
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   548
            activeProcess suspendWithState:#wait.
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   549
        ] valueOnUnwindDo:[
2352
9588e545f4a5 use #removeIdentical
Claus Gittinger <cg@exept.de>
parents: 2265
diff changeset
   550
            waitingProcesses removeIdentical:activeProcess ifAbsent:[].
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   551
            wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   552
        ].
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   553
2352
9588e545f4a5 use #removeIdentical
Claus Gittinger <cg@exept.de>
parents: 2265
diff changeset
   554
        waitingProcesses removeIdentical:activeProcess ifAbsent:[].
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   555
        timeoutOccured ifTrue:[
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   556
            wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   557
            ^ nil
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   558
        ].
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   559
    ].
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   560
    Processor removeTimedBlock:unblock.
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   561
    count := count - 1.
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   562
    count == 0 ifTrue:[
2352
9588e545f4a5 use #removeIdentical
Claus Gittinger <cg@exept.de>
parents: 2265
diff changeset
   563
        lastOwnerID := Processor activeProcessId.
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   564
    ].
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   565
    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
770
402958905760 protocol of waitWithTImeOut: changed
Claus Gittinger <cg@exept.de>
parents: 769
diff changeset
   566
    ^ self
752
0259dd855289 new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
   567
0259dd855289 new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
   568
    "Modified: 13.12.1995 / 13:27:24 / stefan"
2352
9588e545f4a5 use #removeIdentical
Claus Gittinger <cg@exept.de>
parents: 2265
diff changeset
   569
    "Modified: 1.2.1997 / 12:11:47 / cg"
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   570
! !
77
6c38ca59927f *** empty log message ***
claus
parents: 69
diff changeset
   571
1812
a048a98aa299 kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   572
!Semaphore class methodsFor:'documentation'!
183
7899f380e77d signalIf
claus
parents: 159
diff changeset
   573
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   574
version
2407
6b0e21391915 added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 2352
diff changeset
   575
    ^ '$Header: /cvs/stx/stx/libbasic/Semaphore.st,v 1.45 1997-02-17 10:36:58 cg Exp $'
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   576
! !