author | Stefan Vogel <sv@exept.de> |
Tue, 16 Apr 1996 13:00:34 +0200 | |
changeset 1186 | cf4c57ecabb8 |
parent 776 | f3c0c579c0d2 |
child 1215 | 43e8e17fd9f5 |
permissions | -rw-r--r-- |
330 | 1 |
" |
2 |
COPYRIGHT (c) 1995 by Claus Gittinger |
|
341 | 3 |
All Rights Reserved |
330 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
||
13 |
||
14 |
Object subclass:#RecursionLock |
|
776 | 15 |
instanceVariableNames:'process sema' |
16 |
classVariableNames:'' |
|
17 |
poolDictionaries:'' |
|
18 |
category:'Kernel-Processes' |
|
330 | 19 |
! |
20 |
||
21 |
!RecursionLock class methodsFor:'documentation'! |
|
22 |
||
699 | 23 |
copyright |
24 |
" |
|
25 |
COPYRIGHT (c) 1995 by Claus Gittinger |
|
26 |
All Rights Reserved |
|
27 |
||
28 |
This software is furnished under a license and may be used |
|
29 |
only in accordance with the terms of that license and with the |
|
30 |
inclusion of the above copyright notice. This software may not |
|
31 |
be provided or otherwise made available to, or used by, any |
|
32 |
other person. No title to or ownership of the software is |
|
33 |
hereby transferred. |
|
34 |
" |
|
35 |
||
36 |
! |
|
37 |
||
330 | 38 |
documentation |
39 |
" |
|
40 |
like a Semaphore for mutual exclusion, but avoids the deadlock |
|
41 |
if a critical region is reentered by the same process again. |
|
341 | 42 |
I.e. allows reentering the critical region if the current process |
43 |
is the one which did the original locking. |
|
330 | 44 |
|
45 |
example: |
|
46 |
||
776 | 47 |
|lock| |
48 |
||
49 |
lock := RecursionLock new. |
|
50 |
lock critical:[ |
|
51 |
Transcript showCr:'in lock ...'. |
|
52 |
lock critical:[ |
|
53 |
Transcript showCr:'again ...' |
|
54 |
] |
|
55 |
] |
|
56 |
||
57 |
in contrast to (wrong example): |
|
330 | 58 |
|
776 | 59 |
|lock| |
60 |
||
61 |
lock := Semaphore forMutualExclusion. |
|
62 |
lock critical:[ |
|
63 |
Transcript showCr:'in lock ...'. |
|
64 |
lock critical:[ |
|
65 |
'*** never reached - deadlock because sema is already locked ***'. |
|
66 |
' (press CTRL-c and abort in the debugger)'. |
|
67 |
Transcript showCr:'again ...' |
|
68 |
] |
|
69 |
] |
|
330 | 70 |
" |
71 |
! ! |
|
72 |
||
73 |
!RecursionLock class methodsFor:'instance creation'! |
|
74 |
||
75 |
new |
|
76 |
^ self basicNew initialize |
|
77 |
||
78 |
! ! |
|
79 |
||
699 | 80 |
!RecursionLock methodsFor:'private initialization'! |
81 |
||
82 |
initialize |
|
83 |
sema := Semaphore forMutualExclusion |
|
84 |
! ! |
|
85 |
||
330 | 86 |
!RecursionLock methodsFor:'wait & signal'! |
87 |
||
88 |
critical:aBlock |
|
89 |
"evaluate aBlock as a critical region, but do not block, |
|
90 |
if this lock is already held by the current process." |
|
91 |
||
92 |
|active| |
|
93 |
||
94 |
active := Processor activeProcess. |
|
95 |
process == active ifTrue:[ |
|
1186
cf4c57ecabb8
Fix in critical: don't clear process when we didn't get the semaphore when unwinding.
Stefan Vogel <sv@exept.de>
parents:
776
diff
changeset
|
96 |
aBlock value |
330 | 97 |
] ifFalse:[ |
1186
cf4c57ecabb8
Fix in critical: don't clear process when we didn't get the semaphore when unwinding.
Stefan Vogel <sv@exept.de>
parents:
776
diff
changeset
|
98 |
sema critical:[ |
cf4c57ecabb8
Fix in critical: don't clear process when we didn't get the semaphore when unwinding.
Stefan Vogel <sv@exept.de>
parents:
776
diff
changeset
|
99 |
[ |
cf4c57ecabb8
Fix in critical: don't clear process when we didn't get the semaphore when unwinding.
Stefan Vogel <sv@exept.de>
parents:
776
diff
changeset
|
100 |
process := active. |
cf4c57ecabb8
Fix in critical: don't clear process when we didn't get the semaphore when unwinding.
Stefan Vogel <sv@exept.de>
parents:
776
diff
changeset
|
101 |
aBlock value |
cf4c57ecabb8
Fix in critical: don't clear process when we didn't get the semaphore when unwinding.
Stefan Vogel <sv@exept.de>
parents:
776
diff
changeset
|
102 |
] valueNowOrOnUnwindDo:[ |
cf4c57ecabb8
Fix in critical: don't clear process when we didn't get the semaphore when unwinding.
Stefan Vogel <sv@exept.de>
parents:
776
diff
changeset
|
103 |
process := nil. |
cf4c57ecabb8
Fix in critical: don't clear process when we didn't get the semaphore when unwinding.
Stefan Vogel <sv@exept.de>
parents:
776
diff
changeset
|
104 |
] |
cf4c57ecabb8
Fix in critical: don't clear process when we didn't get the semaphore when unwinding.
Stefan Vogel <sv@exept.de>
parents:
776
diff
changeset
|
105 |
] |
330 | 106 |
]. |
1186
cf4c57ecabb8
Fix in critical: don't clear process when we didn't get the semaphore when unwinding.
Stefan Vogel <sv@exept.de>
parents:
776
diff
changeset
|
107 |
|
cf4c57ecabb8
Fix in critical: don't clear process when we didn't get the semaphore when unwinding.
Stefan Vogel <sv@exept.de>
parents:
776
diff
changeset
|
108 |
"Modified: 13.4.1996 / 00:19:31 / stefan" |
330 | 109 |
! ! |
110 |
||
699 | 111 |
!RecursionLock class methodsFor:'documentation'! |
330 | 112 |
|
699 | 113 |
version |
1186
cf4c57ecabb8
Fix in critical: don't clear process when we didn't get the semaphore when unwinding.
Stefan Vogel <sv@exept.de>
parents:
776
diff
changeset
|
114 |
^ '$Header: /cvs/stx/stx/libbasic/RecursionLock.st,v 1.8 1996-04-16 11:00:34 stefan Exp $' |
330 | 115 |
! ! |