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