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