RecursionLock.st
author Stefan Vogel <sv@exept.de>
Tue, 21 Sep 2004 20:00:49 +0200
changeset 8568 624bfd00371f
parent 7590 e318df34c74e
child 10860 cd924455cfa9
permissions -rw-r--r--
category change
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
330
claus
parents:
diff changeset
     1
"
claus
parents:
diff changeset
     2
 COPYRIGHT (c) 1995 by Claus Gittinger
341
claus
parents: 330
diff changeset
     3
	      All Rights Reserved
330
claus
parents:
diff changeset
     4
claus
parents:
diff changeset
     5
 This software is furnished under a license and may be used
claus
parents:
diff changeset
     6
 only in accordance with the terms of that license and with the
claus
parents:
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
claus
parents:
diff changeset
     8
 be provided or otherwise made available to, or used by, any
claus
parents:
diff changeset
     9
 other person.  No title to or ownership of the software is
claus
parents:
diff changeset
    10
 hereby transferred.
claus
parents:
diff changeset
    11
"
claus
parents:
diff changeset
    12
claus
parents:
diff changeset
    13
6552
5ebab8a33da9 Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents: 5735
diff changeset
    14
"{ Package: 'stx:libbasic' }"
5735
3c1f1f115640 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 5104
diff changeset
    15
330
claus
parents:
diff changeset
    16
Object subclass:#RecursionLock
776
f3c0c579c0d2 commentary
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
    17
	instanceVariableNames:'process sema'
f3c0c579c0d2 commentary
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
    18
	classVariableNames:''
f3c0c579c0d2 commentary
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
    19
	poolDictionaries:''
f3c0c579c0d2 commentary
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
    20
	category:'Kernel-Processes'
330
claus
parents:
diff changeset
    21
!
claus
parents:
diff changeset
    22
claus
parents:
diff changeset
    23
!RecursionLock class methodsFor:'documentation'!
claus
parents:
diff changeset
    24
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    25
copyright
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    26
"
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    27
 COPYRIGHT (c) 1995 by Claus Gittinger
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    28
	      All Rights Reserved
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    29
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    30
 This software is furnished under a license and may be used
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    31
 only in accordance with the terms of that license and with the
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    32
 inclusion of the above copyright notice.   This software may not
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    33
 be provided or otherwise made available to, or used by, any
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    34
 other person.  No title to or ownership of the software is
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    35
 hereby transferred.
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    36
"
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    37
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    38
!
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    39
330
claus
parents:
diff changeset
    40
documentation
claus
parents:
diff changeset
    41
"
claus
parents:
diff changeset
    42
    like a Semaphore for mutual exclusion, but avoids the deadlock
claus
parents:
diff changeset
    43
    if a critical region is reentered by the same process again.
2143
09af9c997961 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
    44
    I.e. allows reentering the critical region IFF the current process 
341
claus
parents: 330
diff changeset
    45
    is the one which did the original locking.
330
claus
parents:
diff changeset
    46
3524
b5fe623f0e4d warning in documentation
Claus Gittinger <cg@exept.de>
parents: 3326
diff changeset
    47
    WARNING:
6552
5ebab8a33da9 Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents: 5735
diff changeset
    48
	for now, recursionLocks are not unlocked when an image is
5ebab8a33da9 Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents: 5735
diff changeset
    49
	restarted. You may have to recreate them to avoid a deadLock.
5ebab8a33da9 Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents: 5735
diff changeset
    50
	(this may change in the future, but recreating a recursionLock in
5ebab8a33da9 Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents: 5735
diff changeset
    51
	 the #earlyRestart handling does not hurt)
1294
e26bbb61f6b2 documentation
Claus Gittinger <cg@exept.de>
parents: 1273
diff changeset
    52
e26bbb61f6b2 documentation
Claus Gittinger <cg@exept.de>
parents: 1273
diff changeset
    53
    [author:]
6552
5ebab8a33da9 Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents: 5735
diff changeset
    54
	Claus Gittinger
1294
e26bbb61f6b2 documentation
Claus Gittinger <cg@exept.de>
parents: 1273
diff changeset
    55
1273
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1216
diff changeset
    56
    [see also:]
6552
5ebab8a33da9 Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents: 5735
diff changeset
    57
	Semaphore
5ebab8a33da9 Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents: 5735
diff changeset
    58
	Process ProcessorScheduler
1273
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1216
diff changeset
    59
"
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1216
diff changeset
    60
!
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1216
diff changeset
    61
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1216
diff changeset
    62
examples
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1216
diff changeset
    63
"
2143
09af9c997961 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
    64
  example (good):
6552
5ebab8a33da9 Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents: 5735
diff changeset
    65
									[exBegin]
2143
09af9c997961 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
    66
    |lock|
776
f3c0c579c0d2 commentary
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
    67
2143
09af9c997961 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
    68
    lock := RecursionLock new.
09af9c997961 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
    69
    lock critical:[
6552
5ebab8a33da9 Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents: 5735
diff changeset
    70
	Transcript showCR:'in lock ...'.
5ebab8a33da9 Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents: 5735
diff changeset
    71
	lock critical:[
5ebab8a33da9 Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents: 5735
diff changeset
    72
	    Transcript showCR:'again ...'
5ebab8a33da9 Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents: 5735
diff changeset
    73
	]
2143
09af9c997961 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
    74
    ]
6552
5ebab8a33da9 Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents: 5735
diff changeset
    75
									[exEnd]
776
f3c0c579c0d2 commentary
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
    76
2143
09af9c997961 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
    77
  in contrast to (wrong example - deadlocks):
6552
5ebab8a33da9 Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents: 5735
diff changeset
    78
									[exBegin]
2143
09af9c997961 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
    79
    |lock|
776
f3c0c579c0d2 commentary
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
    80
2143
09af9c997961 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
    81
    lock := Semaphore forMutualExclusion.
09af9c997961 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
    82
    lock critical:[
6552
5ebab8a33da9 Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents: 5735
diff changeset
    83
	Transcript showCR:'in lock ...'.
5ebab8a33da9 Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents: 5735
diff changeset
    84
	lock critical:[
5ebab8a33da9 Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents: 5735
diff changeset
    85
	    '*** never reached - deadlock because sema is already locked ***'.
5ebab8a33da9 Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents: 5735
diff changeset
    86
	    '    (press CTRL-c and abort in the debugger)'.
5ebab8a33da9 Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents: 5735
diff changeset
    87
	    Transcript showCR:'again ...'
5ebab8a33da9 Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents: 5735
diff changeset
    88
	]
2143
09af9c997961 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
    89
    ]
6552
5ebab8a33da9 Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents: 5735
diff changeset
    90
									[exEnd]
330
claus
parents:
diff changeset
    91
"
claus
parents:
diff changeset
    92
! !
claus
parents:
diff changeset
    93
claus
parents:
diff changeset
    94
!RecursionLock class methodsFor:'instance creation'!
claus
parents:
diff changeset
    95
5735
3c1f1f115640 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 5104
diff changeset
    96
forMutualExclusion
3c1f1f115640 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 5104
diff changeset
    97
    "for easy exchangability with mutual-exclusion Semaphores..."
3c1f1f115640 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 5104
diff changeset
    98
3c1f1f115640 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 5104
diff changeset
    99
    ^ self new 
3c1f1f115640 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 5104
diff changeset
   100
!
3c1f1f115640 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 5104
diff changeset
   101
330
claus
parents:
diff changeset
   102
new
claus
parents:
diff changeset
   103
    ^ self basicNew initialize
claus
parents:
diff changeset
   104
claus
parents:
diff changeset
   105
! !
claus
parents:
diff changeset
   106
2722
f6c7146fced2 added naming protocol
Claus Gittinger <cg@exept.de>
parents: 2266
diff changeset
   107
!RecursionLock methodsFor:'printing & storing'!
f6c7146fced2 added naming protocol
Claus Gittinger <cg@exept.de>
parents: 2266
diff changeset
   108
f6c7146fced2 added naming protocol
Claus Gittinger <cg@exept.de>
parents: 2266
diff changeset
   109
displayString
f6c7146fced2 added naming protocol
Claus Gittinger <cg@exept.de>
parents: 2266
diff changeset
   110
    "return a string to display the receiver - include the
5104
99233ab3f6f5 comments
Claus Gittinger <cg@exept.de>
parents: 4938
diff changeset
   111
     count and user-friendly name for your convenience"
2722
f6c7146fced2 added naming protocol
Claus Gittinger <cg@exept.de>
parents: 2266
diff changeset
   112
f6c7146fced2 added naming protocol
Claus Gittinger <cg@exept.de>
parents: 2266
diff changeset
   113
    ^ self class name , '(' , sema count printString , ' name: ' , (self name ? 'unnamed') , ')'
f6c7146fced2 added naming protocol
Claus Gittinger <cg@exept.de>
parents: 2266
diff changeset
   114
5104
99233ab3f6f5 comments
Claus Gittinger <cg@exept.de>
parents: 4938
diff changeset
   115
    "Created: / 28.6.1997 / 16:20:33 / cg"
99233ab3f6f5 comments
Claus Gittinger <cg@exept.de>
parents: 4938
diff changeset
   116
    "Modified: / 14.12.1999 / 21:04:08 / cg"
2722
f6c7146fced2 added naming protocol
Claus Gittinger <cg@exept.de>
parents: 2266
diff changeset
   117
!
f6c7146fced2 added naming protocol
Claus Gittinger <cg@exept.de>
parents: 2266
diff changeset
   118
f6c7146fced2 added naming protocol
Claus Gittinger <cg@exept.de>
parents: 2266
diff changeset
   119
name
5104
99233ab3f6f5 comments
Claus Gittinger <cg@exept.de>
parents: 4938
diff changeset
   120
    "return the semaphores userFriendly name"
99233ab3f6f5 comments
Claus Gittinger <cg@exept.de>
parents: 4938
diff changeset
   121
2722
f6c7146fced2 added naming protocol
Claus Gittinger <cg@exept.de>
parents: 2266
diff changeset
   122
    ^ sema name
f6c7146fced2 added naming protocol
Claus Gittinger <cg@exept.de>
parents: 2266
diff changeset
   123
5104
99233ab3f6f5 comments
Claus Gittinger <cg@exept.de>
parents: 4938
diff changeset
   124
    "Created: / 28.6.1997 / 16:19:40 / cg"
99233ab3f6f5 comments
Claus Gittinger <cg@exept.de>
parents: 4938
diff changeset
   125
    "Modified: / 14.12.1999 / 21:03:46 / cg"
2722
f6c7146fced2 added naming protocol
Claus Gittinger <cg@exept.de>
parents: 2266
diff changeset
   126
!
f6c7146fced2 added naming protocol
Claus Gittinger <cg@exept.de>
parents: 2266
diff changeset
   127
f6c7146fced2 added naming protocol
Claus Gittinger <cg@exept.de>
parents: 2266
diff changeset
   128
name:aString
5104
99233ab3f6f5 comments
Claus Gittinger <cg@exept.de>
parents: 4938
diff changeset
   129
    "set the semaphores userFriendly name"
99233ab3f6f5 comments
Claus Gittinger <cg@exept.de>
parents: 4938
diff changeset
   130
4938
d5640f1c8894 Add #numberOfWaitingProcesse
Stefan Vogel <sv@exept.de>
parents: 3670
diff changeset
   131
    sema name:aString
2722
f6c7146fced2 added naming protocol
Claus Gittinger <cg@exept.de>
parents: 2266
diff changeset
   132
5104
99233ab3f6f5 comments
Claus Gittinger <cg@exept.de>
parents: 4938
diff changeset
   133
    "Created: / 28.6.1997 / 16:19:47 / cg"
99233ab3f6f5 comments
Claus Gittinger <cg@exept.de>
parents: 4938
diff changeset
   134
    "Modified: / 14.12.1999 / 21:03:52 / cg"
2722
f6c7146fced2 added naming protocol
Claus Gittinger <cg@exept.de>
parents: 2266
diff changeset
   135
! !
f6c7146fced2 added naming protocol
Claus Gittinger <cg@exept.de>
parents: 2266
diff changeset
   136
7258
9ccdbee7d1ad method category rename
Claus Gittinger <cg@exept.de>
parents: 6985
diff changeset
   137
!RecursionLock methodsFor:'private-initialization'!
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   138
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   139
initialize
2266
a94af740c68a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2263
diff changeset
   140
    sema := Semaphore forMutualExclusion name:'recursionLock'
a94af740c68a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2263
diff changeset
   141
a94af740c68a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2263
diff changeset
   142
    "Modified: 25.1.1997 / 00:19:15 / cg"
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   143
! !
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   144
1215
43e8e17fd9f5 added #wouldBlock
Claus Gittinger <cg@exept.de>
parents: 1186
diff changeset
   145
!RecursionLock methodsFor:'queries'!
43e8e17fd9f5 added #wouldBlock
Claus Gittinger <cg@exept.de>
parents: 1186
diff changeset
   146
4938
d5640f1c8894 Add #numberOfWaitingProcesse
Stefan Vogel <sv@exept.de>
parents: 3670
diff changeset
   147
numberOfWaitingProcesses
d5640f1c8894 Add #numberOfWaitingProcesse
Stefan Vogel <sv@exept.de>
parents: 3670
diff changeset
   148
    "return the number of waiting processes"
d5640f1c8894 Add #numberOfWaitingProcesse
Stefan Vogel <sv@exept.de>
parents: 3670
diff changeset
   149
d5640f1c8894 Add #numberOfWaitingProcesse
Stefan Vogel <sv@exept.de>
parents: 3670
diff changeset
   150
    ^ sema numberOfWaitingProcesses
d5640f1c8894 Add #numberOfWaitingProcesse
Stefan Vogel <sv@exept.de>
parents: 3670
diff changeset
   151
d5640f1c8894 Add #numberOfWaitingProcesse
Stefan Vogel <sv@exept.de>
parents: 3670
diff changeset
   152
    "Created: 18.4.1996 / 17:18:08 / cg"
d5640f1c8894 Add #numberOfWaitingProcesse
Stefan Vogel <sv@exept.de>
parents: 3670
diff changeset
   153
!
d5640f1c8894 Add #numberOfWaitingProcesse
Stefan Vogel <sv@exept.de>
parents: 3670
diff changeset
   154
7428
84f4d9f08f04 owner & wouldBlock
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   155
owner
84f4d9f08f04 owner & wouldBlock
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   156
    "return the owning processes (or nil)"
84f4d9f08f04 owner & wouldBlock
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   157
84f4d9f08f04 owner & wouldBlock
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   158
    ^ process
84f4d9f08f04 owner & wouldBlock
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   159
!
1215
43e8e17fd9f5 added #wouldBlock
Claus Gittinger <cg@exept.de>
parents: 1186
diff changeset
   160
7428
84f4d9f08f04 owner & wouldBlock
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   161
wouldBlock
84f4d9f08f04 owner & wouldBlock
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   162
    "Check if the resource represented by the receiver is  
84f4d9f08f04 owner & wouldBlock
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   163
     already in use by another Process."
1215
43e8e17fd9f5 added #wouldBlock
Claus Gittinger <cg@exept.de>
parents: 1186
diff changeset
   164
7428
84f4d9f08f04 owner & wouldBlock
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   165
    ^ process notNil and:[Processor activeProcess ~~ process]
1215
43e8e17fd9f5 added #wouldBlock
Claus Gittinger <cg@exept.de>
parents: 1186
diff changeset
   166
! !
43e8e17fd9f5 added #wouldBlock
Claus Gittinger <cg@exept.de>
parents: 1186
diff changeset
   167
8568
624bfd00371f category change
Stefan Vogel <sv@exept.de>
parents: 7590
diff changeset
   168
!RecursionLock methodsFor:'waiting'!
330
claus
parents:
diff changeset
   169
claus
parents:
diff changeset
   170
critical:aBlock
claus
parents:
diff changeset
   171
    "evaluate aBlock as a critical region, but do not block,
claus
parents:
diff changeset
   172
     if this lock is already held by the current process."
claus
parents:
diff changeset
   173
6985
ca488dd4a56c Save a block in #critical:
Stefan Vogel <sv@exept.de>
parents: 6975
diff changeset
   174
    |active retVal wasBlocked gotSema|
330
claus
parents:
diff changeset
   175
claus
parents:
diff changeset
   176
    active := Processor activeProcess.
claus
parents:
diff changeset
   177
    process == active ifTrue:[
6985
ca488dd4a56c Save a block in #critical:
Stefan Vogel <sv@exept.de>
parents: 6975
diff changeset
   178
        "I have already got the lock"
6603
bf9b8a0d489b Use #ifCurtailed: instead of #valueOnUnwindDo:
Stefan Vogel <sv@exept.de>
parents: 6552
diff changeset
   179
        ^ aBlock value
330
claus
parents:
diff changeset
   180
    ].
3670
91b4524a74b3 avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents: 3524
diff changeset
   181
91b4524a74b3 avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents: 3524
diff changeset
   182
    "/
91b4524a74b3 avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents: 3524
diff changeset
   183
    "/ sema wait & process := active
91b4524a74b3 avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents: 3524
diff changeset
   184
    "/ and:
91b4524a74b3 avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents: 3524
diff changeset
   185
    "/ proces := nil & sema signal
91b4524a74b3 avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents: 3524
diff changeset
   186
    "/ must both be done atomic
91b4524a74b3 avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents: 3524
diff changeset
   187
    "/ Scenario:
91b4524a74b3 avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents: 3524
diff changeset
   188
    "/   ... recLock critical
91b4524a74b3 avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents: 3524
diff changeset
   189
    "/         got lock
91b4524a74b3 avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents: 3524
diff changeset
   190
    "/         evaluated
91b4524a74b3 avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents: 3524
diff changeset
   191
    "/         set process to nil
91b4524a74b3 avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents: 3524
diff changeset
   192
    "/         -> timer interrupt
91b4524a74b3 avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents: 3524
diff changeset
   193
    "/              recLock critical in timeOut action
91b4524a74b3 avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents: 3524
diff changeset
   194
    "/              process isNil
91b4524a74b3 avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents: 3524
diff changeset
   195
    "/                 sema wait !!!!!! DEADLOCK
91b4524a74b3 avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents: 3524
diff changeset
   196
    "/
91b4524a74b3 avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents: 3524
diff changeset
   197
    wasBlocked := OperatingSystem blockInterrupts.
6985
ca488dd4a56c Save a block in #critical:
Stefan Vogel <sv@exept.de>
parents: 6975
diff changeset
   198
    [
ca488dd4a56c Save a block in #critical:
Stefan Vogel <sv@exept.de>
parents: 6975
diff changeset
   199
        gotSema := sema wait.
ca488dd4a56c Save a block in #critical:
Stefan Vogel <sv@exept.de>
parents: 6975
diff changeset
   200
        process := active.
ca488dd4a56c Save a block in #critical:
Stefan Vogel <sv@exept.de>
parents: 6975
diff changeset
   201
        wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
ca488dd4a56c Save a block in #critical:
Stefan Vogel <sv@exept.de>
parents: 6975
diff changeset
   202
        retVal := aBlock value.
ca488dd4a56c Save a block in #critical:
Stefan Vogel <sv@exept.de>
parents: 6975
diff changeset
   203
    ] ifCurtailed:[
ca488dd4a56c Save a block in #critical:
Stefan Vogel <sv@exept.de>
parents: 6975
diff changeset
   204
        "be careful - the unwind may occur both while waiting
ca488dd4a56c Save a block in #critical:
Stefan Vogel <sv@exept.de>
parents: 6975
diff changeset
   205
         AND while evaluating the block"
ca488dd4a56c Save a block in #critical:
Stefan Vogel <sv@exept.de>
parents: 6975
diff changeset
   206
        gotSema notNil ifTrue:[
6603
bf9b8a0d489b Use #ifCurtailed: instead of #valueOnUnwindDo:
Stefan Vogel <sv@exept.de>
parents: 6552
diff changeset
   207
            OperatingSystem blockInterrupts.
bf9b8a0d489b Use #ifCurtailed: instead of #valueOnUnwindDo:
Stefan Vogel <sv@exept.de>
parents: 6552
diff changeset
   208
            process := nil.
6985
ca488dd4a56c Save a block in #critical:
Stefan Vogel <sv@exept.de>
parents: 6975
diff changeset
   209
            sema signal.
6975
480fec265743 FIX RACE CONDITION
Stefan Vogel <sv@exept.de>
parents: 6603
diff changeset
   210
        ].
6985
ca488dd4a56c Save a block in #critical:
Stefan Vogel <sv@exept.de>
parents: 6975
diff changeset
   211
        wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
3670
91b4524a74b3 avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents: 3524
diff changeset
   212
    ].
6985
ca488dd4a56c Save a block in #critical:
Stefan Vogel <sv@exept.de>
parents: 6975
diff changeset
   213
    OperatingSystem blockInterrupts.
ca488dd4a56c Save a block in #critical:
Stefan Vogel <sv@exept.de>
parents: 6975
diff changeset
   214
    process := nil.
ca488dd4a56c Save a block in #critical:
Stefan Vogel <sv@exept.de>
parents: 6975
diff changeset
   215
    sema signal.
3670
91b4524a74b3 avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents: 3524
diff changeset
   216
    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
6985
ca488dd4a56c Save a block in #critical:
Stefan Vogel <sv@exept.de>
parents: 6975
diff changeset
   217
    ^ retVal.
330
claus
parents:
diff changeset
   218
! !
claus
parents:
diff changeset
   219
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   220
!RecursionLock class methodsFor:'documentation'!
330
claus
parents:
diff changeset
   221
1216
d7cbc6eb8dd4 critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents: 1215
diff changeset
   222
version
8568
624bfd00371f category change
Stefan Vogel <sv@exept.de>
parents: 7590
diff changeset
   223
    ^ '$Header: /cvs/stx/stx/libbasic/RecursionLock.st,v 1.32 2004-09-21 18:00:49 stefan Exp $'
330
claus
parents:
diff changeset
   224
! !