Semaphore.st
author Claus Gittinger <cg@exept.de>
Tue, 07 Jan 1997 12:02:05 +0100
changeset 2072 e84dbf5e5424
parent 1812 a048a98aa299
child 2142 d4a21668c699
permissions -rw-r--r--
removed package-change info message
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
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
    14
	instanceVariableNames:'count waitingProcesses'
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.
93
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
    45
    There are also semaphores for mutual access to a critical region
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
    46
    (Semaphore>>forMutualExclusion and Semaphore>>critical:).
88
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    47
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
    48
    You can also attach semaphores to external events (such as I/O arrival or
183
7899f380e77d signalIf
claus
parents: 159
diff changeset
    49
    timer events. This is done by telling the Processor to signal the semaphore
7899f380e77d signalIf
claus
parents: 159
diff changeset
    50
    under some condition.
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
    51
    See 'Processor>>signal:afterSeconds:', 'Processor>>signal:onInput:' etc.
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
    52
88
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    53
    See examples in doc/coding.
1273
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1185
diff changeset
    54
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1185
diff changeset
    55
    [see also:]
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1185
diff changeset
    56
        SemaphoreSet RecursionLock
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1185
diff changeset
    57
        SharedQueue Delay 
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1185
diff changeset
    58
        Process ProcessorScheduler
1294
e26bbb61f6b2 documentation
Claus Gittinger <cg@exept.de>
parents: 1273
diff changeset
    59
e26bbb61f6b2 documentation
Claus Gittinger <cg@exept.de>
parents: 1273
diff changeset
    60
    [author:]
e26bbb61f6b2 documentation
Claus Gittinger <cg@exept.de>
parents: 1273
diff changeset
    61
        Claus Gittinger
69
4564b6328136 *** empty log message ***
claus
parents: 25
diff changeset
    62
"
4564b6328136 *** empty log message ***
claus
parents: 25
diff changeset
    63
! !
4564b6328136 *** empty log message ***
claus
parents: 25
diff changeset
    64
1812
a048a98aa299 kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
    65
!Semaphore class methodsFor:'instance creation'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    66
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    67
forMutualExclusion
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    68
    "create & return a new semaphore which allows exactly one process to
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    69
     wait on it without blocking"
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    70
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    71
    ^ super new setCount:1
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    72
!
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    73
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    74
new
a27a279701f8 Initial revision
claus
parents:
diff changeset
    75
    "create & return a new semaphore which blocks until a signal is sent"
a27a279701f8 Initial revision
claus
parents:
diff changeset
    76
a27a279701f8 Initial revision
claus
parents:
diff changeset
    77
    ^ super new setCount:0
a27a279701f8 Initial revision
claus
parents:
diff changeset
    78
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    79
a27a279701f8 Initial revision
claus
parents:
diff changeset
    80
new:n
a27a279701f8 Initial revision
claus
parents:
diff changeset
    81
    "create & return a new semaphore which allows n waits before
a27a279701f8 Initial revision
claus
parents:
diff changeset
    82
     blocking"
a27a279701f8 Initial revision
claus
parents:
diff changeset
    83
a27a279701f8 Initial revision
claus
parents:
diff changeset
    84
    ^ super new setCount:n
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    85
! !
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    86
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
    87
!Semaphore methodsFor:'friend-class interface'!
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
    88
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
    89
checkAndRegisterProcess:process
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
    90
    "
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
    91
     interface for SemaphoreSet.
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
    92
     If the semaphore is available, decrement it and return true.
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
    93
     Otherwise register our process to be wakened up once the semaphore is available.
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
    94
    "
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
    95
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
    96
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
    97
    "
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
    98
     this works only since interrupts are only serviced at 
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
    99
     message send and method-return time ....
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   100
     If you add a message send into the ifTrue:-block, things will
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   101
     go mad ... (especially be careful when adding a debugPrint-here)
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   102
    "
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   103
    count ~~ 0 ifTrue:[
926
101239898989 unwind block over wait (in critical)
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   104
	count := count - 1.
101239898989 unwind block over wait (in critical)
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   105
	^ true
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   106
    ].
1122
251ea6ec6c61 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1031
diff changeset
   107
    (waitingProcesses identityIndexOf:process) == 0 ifTrue:[
926
101239898989 unwind block over wait (in critical)
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   108
	waitingProcesses add:process.
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   109
    ].
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   110
    ^ false
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
    "Modified: 14.12.1995 / 10:32:17 / stefan"
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   113
!
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   114
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   115
unregisterProcess:process
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   116
    "
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   117
     interface for SemaphoreSet.
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   118
     Unregister our process from the Semaphore
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   119
    "
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   120
769
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   121
    waitingProcesses remove:process ifAbsent:[].
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   122
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   123
    "Created: 14.12.1995 / 10:31:50 / stefan"
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   124
! !
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   125
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   126
!Semaphore methodsFor:'printing & storing'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   127
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   128
displayString
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   129
    ^ self class name , '(' , count printString , ')'
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   130
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   131
a27a279701f8 Initial revision
claus
parents:
diff changeset
   132
!Semaphore methodsFor:'private accessing'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   133
a27a279701f8 Initial revision
claus
parents:
diff changeset
   134
setCount:n
375
claus
parents: 362
diff changeset
   135
    waitingProcesses := OrderedCollection new:3.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   136
    count := n
a27a279701f8 Initial revision
claus
parents:
diff changeset
   137
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   138
217
a0400fdbc933 *** empty log message ***
claus
parents: 183
diff changeset
   139
!Semaphore methodsFor:'queries '!
a0400fdbc933 *** empty log message ***
claus
parents: 183
diff changeset
   140
1329
0a025ca40653 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   141
numberOfWaitingProcesses
0a025ca40653 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   142
    "return the number of processes waiting on the receiver"
0a025ca40653 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   143
0a025ca40653 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   144
    ^ waitingProcesses size
0a025ca40653 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   145
0a025ca40653 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   146
    "Created: 3.5.1996 / 18:06:27 / cg"
0a025ca40653 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   147
!
0a025ca40653 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   148
1577
720f14f90e2c access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents: 1329
diff changeset
   149
waitingProcesses
720f14f90e2c access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents: 1329
diff changeset
   150
    "return the processes waiting on the receiver"
720f14f90e2c access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents: 1329
diff changeset
   151
720f14f90e2c access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents: 1329
diff changeset
   152
    ^ waitingProcesses
720f14f90e2c access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents: 1329
diff changeset
   153
720f14f90e2c access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents: 1329
diff changeset
   154
    "Created: 18.7.1996 / 20:53:33 / cg"
720f14f90e2c access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents: 1329
diff changeset
   155
!
720f14f90e2c access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents: 1329
diff changeset
   156
217
a0400fdbc933 *** empty log message ***
claus
parents: 183
diff changeset
   157
wouldBlock
a0400fdbc933 *** empty log message ***
claus
parents: 183
diff changeset
   158
    "return true, if the receiver would block the activeProcess
a0400fdbc933 *** empty log message ***
claus
parents: 183
diff changeset
   159
     if a wait was performed. False otherwise."
a0400fdbc933 *** empty log message ***
claus
parents: 183
diff changeset
   160
a0400fdbc933 *** empty log message ***
claus
parents: 183
diff changeset
   161
    ^ count == 0
a0400fdbc933 *** empty log message ***
claus
parents: 183
diff changeset
   162
! !
a0400fdbc933 *** empty log message ***
claus
parents: 183
diff changeset
   163
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   164
!Semaphore methodsFor:'wait & signal'!
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   165
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   166
critical:aBlock
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   167
    "evaluate aBlock as a critical region; the receiver must be
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   168
     created using Semaphore>>forMutualExclusion"
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   169
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
   170
    |retVal gotSema|
926
101239898989 unwind block over wait (in critical)
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   171
101239898989 unwind block over wait (in critical)
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   172
    [
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
   173
        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
   174
        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
   175
    ] 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
   176
        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
   177
    ].
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
   178
    self signal.
926
101239898989 unwind block over wait (in critical)
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   179
    ^ retVal
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   180
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   181
    "
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   182
      the example below is stupid (it should use a SharedQueue,
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   183
      or at least a Queue with critical regions).
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   184
      Anyhow, it demonstrates how two processes lock each other
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   185
      from accessing coll at the same time
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   186
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   187
     |sema coll|
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   188
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   189
     sema := Semaphore forMutualExclusion.
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   190
     coll := OrderedCollection new:10.
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   191
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   192
     [
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
   193
        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
   194
            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
   195
                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
   196
                (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
   197
            ]
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
   198
        ]
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   199
     ] forkAt:4.
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   200
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   201
     [
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
   202
        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
   203
            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
   204
                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
   205
                (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
   206
            ]
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
   207
        ]
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   208
     ] forkAt:4.
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   209
    "
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
   210
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
   211
    "Modified: 16.4.1996 / 10:00:46 / stefan"
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   212
!
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   213
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   214
signal
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   215
    "waking up (first) waiter"
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   216
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   217
    |p wasBlocked|
362
claus
parents: 329
diff changeset
   218
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   219
    wasBlocked := OperatingSystem blockInterrupts.
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   220
    [
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   221
        count := count + 1.
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   222
        waitingProcesses notEmpty ifTrue:[
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   223
            p := waitingProcesses removeFirst.
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   224
            p resume.
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   225
        ].
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   226
    ] valueNowOrOnUnwindDo:[
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   227
        wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   228
    ]
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   229
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   230
    "Modified: 28.2.1996 / 21:23:10 / cg"
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   231
!
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   232
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   233
signalForAll
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   234
    "signal the semaphore for all waiters.
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   235
     This can be used for process synchronization, if multiple processes are
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   236
     waiting for a common event."
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   237
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   238
    |wasBlocked|
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   239
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   240
    [waitingProcesses notEmpty] whileTrue:[
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   241
        wasBlocked := OperatingSystem blockInterrupts.
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   242
        [
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   243
            waitingProcesses notEmpty ifTrue:[
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   244
                self signal
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   245
            ].
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   246
        ] valueNowOrOnUnwindDo:[
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   247
            wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   248
        ]
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   249
    ]
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   250
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   251
    "Modified: 28.2.1996 / 21:23:38 / cg"
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   252
!
362
claus
parents: 329
diff changeset
   253
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   254
signalIf
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   255
    "signal the semaphore, but only if being waited upon.
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   256
     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
   257
     previous signals)"
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   258
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   259
    |wasBlocked|
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   260
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   261
    waitingProcesses notEmpty ifTrue:[
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   262
        wasBlocked := OperatingSystem blockInterrupts.
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   263
        [
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   264
            waitingProcesses notEmpty ifTrue:[
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   265
                self signal
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   266
            ].
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   267
        ] valueNowOrOnUnwindDo:[
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   268
            wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   269
        ]
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   270
    ]
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   271
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   272
    "Modified: 28.2.1996 / 21:23:57 / cg"
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   273
!
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   274
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   275
signalOnce
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   276
    "wakeup waiters - but only once.
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   277
     I.e. if the semaphore has already been signalled, this
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   278
     is ignored."
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   279
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   280
    |wasBlocked|
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   281
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   282
    count == 0 ifTrue:[
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   283
        wasBlocked := OperatingSystem blockInterrupts.
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   284
        [
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   285
            count == 0 ifTrue:[
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   286
                self signal
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   287
            ].
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   288
        ] valueNowOrOnUnwindDo:[
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   289
            wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   290
        ]
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   291
    ]
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   292
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   293
    "Modified: 28.2.1996 / 21:24:08 / cg"
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   294
!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   295
a27a279701f8 Initial revision
claus
parents:
diff changeset
   296
wait
a27a279701f8 Initial revision
claus
parents:
diff changeset
   297
    "wait for the semaphore"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   298
93
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
   299
    |current wasBlocked|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   300
a27a279701f8 Initial revision
claus
parents:
diff changeset
   301
    "
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   302
     this works only since interrupts are only serviced at 
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   303
     message send and method-return time ....
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   304
     If you add a message send into the ifTrue:-block, things will
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   305
     go mad ... (especially be careful when adding a debugPrint-here)
93
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
   306
    "
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
   307
    count ~~ 0 ifTrue:[
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   308
        count := count - 1.
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   309
        ^ self
93
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
   310
    ].
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
   311
302
1f76060d58a4 *** empty log message ***
claus
parents: 269
diff changeset
   312
    current := Processor activeProcess.
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   313
302
1f76060d58a4 *** empty log message ***
claus
parents: 269
diff changeset
   314
    wasBlocked := OperatingSystem blockInterrupts.
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   315
    "
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   316
     need a while-loop here, since more than one process may
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   317
     wait for it and another one may also wake up.
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   318
     Thus, the count is not always non-zero after returning from
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   319
     suspend.
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   320
    "
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   321
    [count == 0] whileTrue:[
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   322
        waitingProcesses add:current.
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   323
        "
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   324
         for some more descriptive info in processMonitor ...
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   325
         ... set the state to #wait (instead of #suspend)
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
        [
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   328
            current suspendWithState:#wait
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   329
        ] valueOnUnwindDo:[
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   330
            waitingProcesses remove:current ifAbsent:[].
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   331
            wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
1812
a048a98aa299 kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   332
        ].
a048a98aa299 kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   333
        
a048a98aa299 kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   334
        count == 0 ifTrue:[
a048a98aa299 kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   335
            "/ care for someone manually resuming me ...
a048a98aa299 kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   336
            "/ being multiple times on waitingProcesses
a048a98aa299 kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   337
            waitingProcesses remove:current ifAbsent:[].
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   338
        ]
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   339
    ].
10
claus
parents: 3
diff changeset
   340
    count := count - 1.
93
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
   341
    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
752
0259dd855289 new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
   342
0259dd855289 new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
   343
    "Modified: 13.12.1995 / 13:26:33 / stefan"
1812
a048a98aa299 kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   344
    "Modified: 23.10.1996 / 18:26:23 / cg"
10
claus
parents: 3
diff changeset
   345
!
claus
parents: 3
diff changeset
   346
329
claus
parents: 302
diff changeset
   347
waitUncounted
claus
parents: 302
diff changeset
   348
    "wait for the semaphore; do not consume the resource
claus
parents: 302
diff changeset
   349
     (i.e. do not count down)"
claus
parents: 302
diff changeset
   350
claus
parents: 302
diff changeset
   351
    |current wasBlocked|
claus
parents: 302
diff changeset
   352
claus
parents: 302
diff changeset
   353
    "
claus
parents: 302
diff changeset
   354
     this works only since interrupts are only serviced at 
claus
parents: 302
diff changeset
   355
     message send and method-return time ....
claus
parents: 302
diff changeset
   356
     If you add a message send into the ifTrue:-block, things will
claus
parents: 302
diff changeset
   357
     go mad ... (especially be careful when adding a debugPrint-here)
claus
parents: 302
diff changeset
   358
    "
claus
parents: 302
diff changeset
   359
    count ~~ 0 ifTrue:[
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   360
        ^ self
329
claus
parents: 302
diff changeset
   361
    ].
claus
parents: 302
diff changeset
   362
claus
parents: 302
diff changeset
   363
    current := Processor activeProcess.
claus
parents: 302
diff changeset
   364
claus
parents: 302
diff changeset
   365
    wasBlocked := OperatingSystem blockInterrupts.
claus
parents: 302
diff changeset
   366
    "
claus
parents: 302
diff changeset
   367
     need a while-loop here, since more than one process may
claus
parents: 302
diff changeset
   368
     wait for it and another one may also wake up.
claus
parents: 302
diff changeset
   369
     Thus, the count is not always non-zero after returning from
claus
parents: 302
diff changeset
   370
     suspend.
claus
parents: 302
diff changeset
   371
    "
claus
parents: 302
diff changeset
   372
    [count == 0] whileTrue:[
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   373
        waitingProcesses add:current.
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   374
        "
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   375
         for some more descriptive info in processMonitor ...
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   376
         ... set the state to #wait (instead of #suspend)
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   377
        "
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   378
        [
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   379
            current suspendWithState:#wait
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   380
        ] valueOnUnwindDo:[
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   381
            waitingProcesses remove:current ifAbsent:[].
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   382
            wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
1812
a048a98aa299 kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   383
        ].
a048a98aa299 kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   384
        count == 0 ifTrue:[
a048a98aa299 kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   385
            "/ care for someone manually resuming me ...
a048a98aa299 kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   386
            "/ being multiple times on waitingProcesses
a048a98aa299 kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   387
            waitingProcesses remove:current ifAbsent:[].
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   388
        ]
329
claus
parents: 302
diff changeset
   389
    ].
claus
parents: 302
diff changeset
   390
    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
752
0259dd855289 new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
   391
0259dd855289 new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
   392
    "Modified: 13.12.1995 / 13:26:49 / stefan"
1812
a048a98aa299 kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   393
    "Modified: 23.10.1996 / 18:26:16 / cg"
329
claus
parents: 302
diff changeset
   394
!
claus
parents: 302
diff changeset
   395
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   396
waitWithTimeout:seconds
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   397
    "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
   398
     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
   399
     due to a timeout. 
101239898989 unwind block over wait (in critical)
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   400
     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
   401
     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
   402
     (which is not the intend of semaphores, though)."
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   403
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   404
    |current timeoutOccured wasBlocked unblock now endTime|
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   405
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   406
    "
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   407
     this works only since interrupts are only serviced at 
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   408
     message send and method-return time ....
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   409
     If you add a message send into the ifTrue:-block, things will
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   410
     go mad ... (especially be careful when adding a debugPrint-here)
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   411
    "
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   412
    count ~~ 0 ifTrue:[
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   413
        count := count - 1.
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   414
        ^ self
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   415
    ].
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   416
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   417
    "
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   418
     with zero-timeout, this is a poll
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   419
    "
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   420
    seconds = 0 ifTrue:[
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   421
        ^ nil
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   422
    ].
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   423
302
1f76060d58a4 *** empty log message ***
claus
parents: 269
diff changeset
   424
    current := Processor activeProcess.
1f76060d58a4 *** empty log message ***
claus
parents: 269
diff changeset
   425
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   426
    wasBlocked := OperatingSystem blockInterrupts.
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   427
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   428
    "
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   429
     calculate the end-time
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   430
    "
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   431
    now := OperatingSystem getMillisecondTime.
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   432
    endTime := OperatingSystem millisecondTimeAdd:now and:(seconds * 1000).
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   433
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   434
    unblock := [timeoutOccured := true. Processor resume:current].
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   435
    Processor addTimedBlock:unblock for:current atMilliseconds:endTime.
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   436
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   437
    "
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   438
     need a while-loop here, since more than one process may
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   439
     wait for it and another one may also wake up.
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   440
     Thus, the count is not always non-zero after returning from
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   441
     suspend.
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   442
    "
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   443
    [count == 0] whileTrue:[
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   444
        waitingProcesses add:current.
302
1f76060d58a4 *** empty log message ***
claus
parents: 269
diff changeset
   445
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   446
        timeoutOccured := false.
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   447
        "
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   448
         for some more descriptive info in processMonitor ...
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   449
         ... set the state to #wait (instead of #suspend)
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   450
        "
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   451
        [
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   452
            current suspendWithState:#wait.
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   453
        ] valueOnUnwindDo:[
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   454
            waitingProcesses remove:current ifAbsent:[].
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   455
            wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   456
        ].
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   457
1812
a048a98aa299 kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   458
        waitingProcesses remove:current ifAbsent:[].
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   459
        timeoutOccured ifTrue:[
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   460
            wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   461
            ^ nil
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   462
        ].
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   463
    ].
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   464
    Processor removeTimedBlock:unblock.
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   465
    count := count - 1.
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   466
    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
770
402958905760 protocol of waitWithTImeOut: changed
Claus Gittinger <cg@exept.de>
parents: 769
diff changeset
   467
    ^ self
752
0259dd855289 new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
   468
0259dd855289 new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
   469
    "Modified: 13.12.1995 / 13:27:24 / stefan"
1812
a048a98aa299 kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   470
    "Modified: 23.10.1996 / 18:27:14 / cg"
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   471
! !
77
6c38ca59927f *** empty log message ***
claus
parents: 69
diff changeset
   472
1812
a048a98aa299 kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   473
!Semaphore class methodsFor:'documentation'!
183
7899f380e77d signalIf
claus
parents: 159
diff changeset
   474
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   475
version
1812
a048a98aa299 kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   476
    ^ '$Header: /cvs/stx/stx/libbasic/Semaphore.st,v 1.38 1996-10-23 17:28:50 cg Exp $'
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   477
! !