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 |
'From Smalltalk/X, Version:2.10.5 on 28-apr-1995 at 12:37:45 pm'!
|
|
15 |
|
|
16 |
Object subclass:#RecursionLock
|
|
17 |
instanceVariableNames:'process sema'
|
|
18 |
classVariableNames:''
|
|
19 |
poolDictionaries:''
|
|
20 |
category:'Kernel-Processes'
|
|
21 |
!
|
|
22 |
|
|
23 |
!RecursionLock class methodsFor:'documentation'!
|
|
24 |
|
|
25 |
documentation
|
|
26 |
"
|
|
27 |
like a Semaphore for mutual exclusion, but avoids the deadlock
|
|
28 |
if a critical region is reentered by the same process again.
|
341
|
29 |
I.e. allows reentering the critical region if the current process
|
|
30 |
is the one which did the original locking.
|
330
|
31 |
|
|
32 |
example:
|
|
33 |
|
341
|
34 |
|lock|
|
330
|
35 |
|
341
|
36 |
lock := RecursionLock new.
|
|
37 |
lock critical:[
|
|
38 |
Transcript showCr:'in lock ...'.
|
|
39 |
lock critical:[
|
|
40 |
Transcript showCr:'again ...'
|
|
41 |
]
|
|
42 |
]
|
330
|
43 |
"
|
|
44 |
|
|
45 |
!
|
|
46 |
|
|
47 |
copyright
|
|
48 |
"
|
|
49 |
COPYRIGHT (c) 1995 by Claus Gittinger
|
341
|
50 |
All Rights Reserved
|
330
|
51 |
|
|
52 |
This software is furnished under a license and may be used
|
|
53 |
only in accordance with the terms of that license and with the
|
|
54 |
inclusion of the above copyright notice. This software may not
|
|
55 |
be provided or otherwise made available to, or used by, any
|
|
56 |
other person. No title to or ownership of the software is
|
|
57 |
hereby transferred.
|
|
58 |
"
|
|
59 |
|
|
60 |
!
|
|
61 |
|
|
62 |
version
|
|
63 |
"
|
381
|
64 |
$Header: /cvs/stx/stx/libbasic/RecursionLock.st,v 1.3 1995-08-10 13:18:35 claus Exp $
|
|
65 |
$Revision: 1.3 $
|
330
|
66 |
"
|
|
67 |
! !
|
|
68 |
|
|
69 |
!RecursionLock class methodsFor:'instance creation'!
|
|
70 |
|
|
71 |
new
|
|
72 |
^ self basicNew initialize
|
|
73 |
|
|
74 |
! !
|
|
75 |
|
|
76 |
!RecursionLock methodsFor:'wait & signal'!
|
|
77 |
|
|
78 |
critical:aBlock
|
|
79 |
"evaluate aBlock as a critical region, but do not block,
|
|
80 |
if this lock is already held by the current process."
|
|
81 |
|
|
82 |
|active|
|
|
83 |
|
|
84 |
active := Processor activeProcess.
|
|
85 |
process == active ifTrue:[
|
341
|
86 |
aBlock value
|
330
|
87 |
] ifFalse:[
|
341
|
88 |
[
|
|
89 |
sema critical:[
|
|
90 |
process := active.
|
|
91 |
aBlock value
|
|
92 |
]
|
|
93 |
] valueNowOrOnUnwindDo:[
|
|
94 |
process := nil
|
|
95 |
]
|
330
|
96 |
].
|
|
97 |
! !
|
|
98 |
|
|
99 |
!RecursionLock methodsFor:'private initialization'!
|
|
100 |
|
|
101 |
initialize
|
|
102 |
sema := Semaphore forMutualExclusion
|
|
103 |
! !
|