author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Thu, 31 Aug 2017 10:17:00 +0100 | |
branch | jv |
changeset 23085 | 9effc2bcf8a6 |
parent 23084 | 0ffb59b273ff |
child 23086 | d3f84ef999e6 |
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 |
" |
|
6552
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
12 |
"{ Package: 'stx:libbasic' }" |
5735 | 13 |
|
18748 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
23083
c8dcd89b9cf6
Issue #94 [5/x]: introduce an `AbstractLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23082
diff
changeset
|
16 |
AbstractLock subclass:#RecursionLock |
c8dcd89b9cf6
Issue #94 [5/x]: introduce an `AbstractLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23082
diff
changeset
|
17 |
instanceVariableNames:'' |
776 | 18 |
classVariableNames:'' |
19 |
poolDictionaries:'' |
|
20 |
category:'Kernel-Processes' |
|
330 | 21 |
! |
22 |
||
23084
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
23 |
!RecursionLock primitiveDefinitions! |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
24 |
%{ |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
25 |
#define THINLOCKING |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
26 |
#ifdef THINLOCKING |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
27 |
# include <thinlocks.h> |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
28 |
static inline unsigned INT* stxGetLockwordPtr(OBJ o) { |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
29 |
return (unsigned INT*)(&__OINST(o, process)); |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
30 |
} |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
31 |
|
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
32 |
#endif |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
33 |
%} |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
34 |
! ! |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
35 |
|
330 | 36 |
!RecursionLock class methodsFor:'documentation'! |
37 |
||
699 | 38 |
copyright |
39 |
" |
|
40 |
COPYRIGHT (c) 1995 by Claus Gittinger |
|
41 |
All Rights Reserved |
|
42 |
||
43 |
This software is furnished under a license and may be used |
|
44 |
only in accordance with the terms of that license and with the |
|
45 |
inclusion of the above copyright notice. This software may not |
|
46 |
be provided or otherwise made available to, or used by, any |
|
47 |
other person. No title to or ownership of the software is |
|
48 |
hereby transferred. |
|
49 |
" |
|
50 |
||
51 |
! |
|
52 |
||
330 | 53 |
documentation |
54 |
" |
|
55 |
like a Semaphore for mutual exclusion, but avoids the deadlock |
|
56 |
if a critical region is reentered by the same process again. |
|
23082
b75aec4476a4
Issue #94 [4/x]: revamp of `RecursionLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21249
diff
changeset
|
57 |
I.e. allows reentering the critical region IFF the current process |
341 | 58 |
is the one which did the original locking. |
330 | 59 |
|
3524 | 60 |
WARNING: |
6552
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
61 |
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
|
62 |
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
|
63 |
(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
|
64 |
the #earlyRestart handling does not hurt) |
1294 | 65 |
|
66 |
[author:] |
|
6552
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
67 |
Claus Gittinger |
1294 | 68 |
|
1273 | 69 |
[see also:] |
6552
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
70 |
Semaphore |
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
71 |
Process ProcessorScheduler |
1273 | 72 |
" |
73 |
! |
|
74 |
||
75 |
examples |
|
76 |
" |
|
2143 | 77 |
example (good): |
6552
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
78 |
[exBegin] |
2143 | 79 |
|lock| |
776 | 80 |
|
2143 | 81 |
lock := RecursionLock new. |
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 |
Transcript showCR:'again ...' |
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
86 |
] |
2143 | 87 |
] |
6552
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
88 |
[exEnd] |
776 | 89 |
|
2143 | 90 |
in contrast to (wrong example - deadlocks): |
6552
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
91 |
[exBegin] |
2143 | 92 |
|lock| |
776 | 93 |
|
2143 | 94 |
lock := Semaphore forMutualExclusion. |
95 |
lock critical:[ |
|
6552
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
96 |
Transcript showCR:'in lock ...'. |
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
97 |
lock critical:[ |
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
98 |
'*** never reached - deadlock because sema is already locked ***'. |
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
99 |
' (press CTRL-c and abort in the debugger)'. |
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
100 |
Transcript showCR:'again ...' |
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
101 |
] |
2143 | 102 |
] |
6552
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
103 |
[exEnd] |
330 | 104 |
" |
105 |
! ! |
|
106 |
||
107 |
!RecursionLock class methodsFor:'instance creation'! |
|
108 |
||
5735 | 109 |
forMutualExclusion |
18748 | 110 |
"same as new, for easy exchangability with regular mutual-exclusion Semaphores." |
5735 | 111 |
|
23082
b75aec4476a4
Issue #94 [4/x]: revamp of `RecursionLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21249
diff
changeset
|
112 |
^ self new |
23083
c8dcd89b9cf6
Issue #94 [5/x]: introduce an `AbstractLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23082
diff
changeset
|
113 |
! ! |
c8dcd89b9cf6
Issue #94 [5/x]: introduce an `AbstractLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23082
diff
changeset
|
114 |
|
23084
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
115 |
!RecursionLock methodsFor:'accessing'! |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
116 |
|
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
117 |
count |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
118 |
^ self processAndCount at: 2. |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
119 |
|
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
120 |
! |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
121 |
|
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
122 |
owner |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
123 |
^ self processAndCount at: 1. |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
124 |
|
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
125 |
! ! |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
126 |
|
23083
c8dcd89b9cf6
Issue #94 [5/x]: introduce an `AbstractLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23082
diff
changeset
|
127 |
!RecursionLock methodsFor:'acquire & release'! |
5735 | 128 |
|
23084
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
129 |
acquireWithTimeoutMs: timeout |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
130 |
" |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
131 |
Acquire the lock: |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
132 |
|
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
133 |
* If the lock is not owned by any process, lock it and return immediately. |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
134 |
* If the lock is already owned by the calling process, return immediately. |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
135 |
* Otherwise, wait until owning process release it (by means of #release) |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
136 |
at most `timeout` milliseconds. If `timeout` is nil, wait forever. |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
137 |
|
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
138 |
Return `true` if the lock has been acquired or `false` if bot (e.g. wait |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
139 |
timed out) |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
140 |
" |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
141 |
%{ /* NOCONTEXT */ |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
142 |
#ifdef THINLOCKING |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
143 |
if ( stxThinLock( stxGetLockwordPtr(self) ) == StxThinlockSuccess ) { |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
144 |
return (true); |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
145 |
} |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
146 |
#endif |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
147 |
%}. |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
148 |
"/ Inflate the lock if it's not yet inflated. |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
149 |
"/ |
23085
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
150 |
"/ Note that #inflate method checks again if it's inflated or not, |
23084
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
151 |
"/ it may haopen some other thread inflated the lock in between the check |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
152 |
"/ here and code in #inflate. |
23085
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
153 |
"/ |
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
154 |
"/ Note that `someobject class == SmallInteger` is handled as a special |
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
155 |
"/ case in stc and compiled as `__isSmallInteger(someobject)` and thus |
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
156 |
"/ very fast - just bitwise and + non-zero test. Don't change. |
23084
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
157 |
process class == SmallInteger ifTrue:[ self inflate ]. |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
158 |
^ super acquireWithTimeoutMs: timeout |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
159 |
! |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
160 |
|
23083
c8dcd89b9cf6
Issue #94 [5/x]: introduce an `AbstractLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23082
diff
changeset
|
161 |
release |
c8dcd89b9cf6
Issue #94 [5/x]: introduce an `AbstractLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23082
diff
changeset
|
162 |
" |
23084
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
163 |
Release the lock. Return true of lock has been released, `false` if |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
164 |
not (because calling process does not own it). |
23083
c8dcd89b9cf6
Issue #94 [5/x]: introduce an `AbstractLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23082
diff
changeset
|
165 |
" |
23084
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
166 |
%{ /* NOCONTEXT */ |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
167 |
#ifdef THINLOCKING |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
168 |
if ( stxThinUnlock( stxGetLockwordPtr(self) ) == StxThinlockSuccess ) { |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
169 |
return (true); |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
170 |
} |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
171 |
#endif |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
172 |
%}. |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
173 |
"/ Inflate the lock if it's not yet inflated. |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
174 |
"/ |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
175 |
"/ Note that #inflate method checks again if it's inflated or not, |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
176 |
"/ it may haopen some other thread inflated the lock in between the check |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
177 |
"/ here and code in #inflate |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
178 |
"/ |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
179 |
"/ Note that `someobject class == SmallInteger` is handled as a special |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
180 |
"/ case in stc and compiled as `__isSmallInteger(someobject)` and thus |
23085
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
181 |
"/ very fast - just bitwise and + non-zero test. Don't change. |
23084
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
182 |
process class == SmallInteger ifTrue:[ self inflate ]. |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
183 |
super release ifFalse:[ |
23083
c8dcd89b9cf6
Issue #94 [5/x]: introduce an `AbstractLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23082
diff
changeset
|
184 |
self error: ('Calling process does not own the lock (caller: %1, owner: %2)' bindWith: Processor activeProcess id with: (process isNil ifTrue:['<no owner>'] ifFalse:[process id])). |
c8dcd89b9cf6
Issue #94 [5/x]: introduce an `AbstractLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23082
diff
changeset
|
185 |
]. |
23084
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
186 |
! ! |
330 | 187 |
|
23084
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
188 |
!RecursionLock methodsFor:'initialization'! |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
189 |
|
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
190 |
initialize |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
191 |
super initialize. |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
192 |
process := 0. |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
193 |
|
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
194 |
"Modified: / 25-01-1997 / 00:19:15 / cg" |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
195 |
"Modified: / 29-08-2017 / 09:53:37 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
196 |
! ! |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
197 |
|
23085
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
198 |
!RecursionLock methodsFor:'printing & storing'! |
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
199 |
|
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
200 |
displayOn:aGCOrStream |
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
201 |
"return a string to display the receiver - include the |
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
202 |
count for your convenience" |
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
203 |
|
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
204 |
"/ what a kludge - Dolphin and Squeak mean: printOn: a stream; |
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
205 |
"/ ST/X (and some old ST80's) mean: draw-yourself on a GC. |
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
206 |
(aGCOrStream isStream) ifFalse:[ |
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
207 |
^ super displayOn:aGCOrStream |
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
208 |
]. |
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
209 |
aGCOrStream |
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
210 |
nextPutAll:self class name; |
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
211 |
nextPut:$(. |
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
212 |
sema count printOn:aGCOrStream. |
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
213 |
aGCOrStream nextPutAll:' name: '. |
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
214 |
(self name ? 'unnamed') printOn:aGCOrStream. |
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
215 |
aGCOrStream nextPut:$). |
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
216 |
! ! |
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
217 |
|
23084
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
218 |
!RecursionLock methodsFor:'private'! |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
219 |
|
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
220 |
inflate |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
221 |
"Inflates (thin) lock (into fat lock). If the lock is already a fat lock, |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
222 |
#inflate is no-op. |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
223 |
|
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
224 |
Called by: |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
225 |
|
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
226 |
* #acquire* in case of contention or if maximum nesting count |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
227 |
is exceeded (unlikely) |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
228 |
* #release in case of contention |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
229 |
|
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
230 |
|
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
231 |
" |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
232 |
|
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
233 |
| processAndCount wasBlocked | |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
234 |
|
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
235 |
|
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
236 |
processAndCount := Array new: 2. |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
237 |
wasBlocked := OperatingSystem blockInterrupts. |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
238 |
"/ Note that `someobject class == SmallInteger` is handled as a special |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
239 |
"/ case in stc and compiled as `__isSmallInteger(someobject)` and thus |
23085
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
240 |
"/ very fast - just bitwise and + non-zero test. Don't change |
23084
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
241 |
process class == SmallInteger ifTrue:[ |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
242 |
self processAndCountInto: processAndCount. |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
243 |
process := processAndCount at: 1. |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
244 |
count := processAndCount at: 2. |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
245 |
sema setCount: 0. |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
246 |
]. |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
247 |
wasBlocked ifFalse:[ OperatingSystem unblockInterrupts ]. |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
248 |
|
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
249 |
! |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
250 |
|
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
251 |
processAndCount |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
252 |
| processAndCount | |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
253 |
|
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
254 |
processAndCount := Array new: 2. |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
255 |
self processAndCountInto: processAndCount. |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
256 |
^ processAndCount |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
257 |
|
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
258 |
! |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
259 |
|
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
260 |
processAndCountInto: anArray |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
261 |
"Fills in `anArray` with owning process and nesting count. |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
262 |
|
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
263 |
Note that by the time this method returns, the data in given array may |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
264 |
be already obsolete. |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
265 |
" |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
266 |
| pid cnt proc | |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
267 |
|
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
268 |
"/ Note that `someobject class == SmallInteger` is handled as a special |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
269 |
"/ case in stc and compiled as `__isSmallInteger(someobject)` and thus |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
270 |
"/ very fast - just bitwise and + non-zero test. Don't change! |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
271 |
process class == SmallInteger ifTrue:[ |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
272 |
%{ |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
273 |
#ifdef THINLOCKING |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
274 |
unsigned INT _pid = stxLockwordGetPid( *stxGetLockwordPtr(self) ); |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
275 |
unsigned INT _cnt = stxLockwordGetCnt( *stxGetLockwordPtr(self) ); |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
276 |
|
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
277 |
if (_pid == INV_PROCESS_ID) { |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
278 |
pid = nil; |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
279 |
cnt = __MKINT(0); |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
280 |
} else { |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
281 |
pid = __MKINT(_pid); |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
282 |
cnt = __MKINT(_cnt); |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
283 |
} |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
284 |
#endif |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
285 |
%}. |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
286 |
pid notNil ifTrue:[ |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
287 |
proc := ObjectMemory processesKnownInVM detect:[:p|p id == pid] ifNone:[nil]. |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
288 |
]. |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
289 |
] ifFalse:[ |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
290 |
proc := process. |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
291 |
cnt := count. |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
292 |
]. |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
293 |
anArray at: 1 put: proc. |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
294 |
anArray at: 2 put: cnt. |
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
295 |
|
330 | 296 |
! ! |
297 |
||
1215 | 298 |
!RecursionLock methodsFor:'queries'! |
299 |
||
4938 | 300 |
numberOfWaitingProcesses |
301 |
"return the number of waiting processes" |
|
302 |
||
303 |
^ sema numberOfWaitingProcesses |
|
304 |
||
305 |
"Created: 18.4.1996 / 17:18:08 / cg" |
|
306 |
! |
|
307 |
||
7428 | 308 |
wouldBlock |
23082
b75aec4476a4
Issue #94 [4/x]: revamp of `RecursionLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21249
diff
changeset
|
309 |
"Check if the resource represented by the receiver is |
15550 | 310 |
already in use by another process. |
311 |
Attention: if asked without some global lock (blockedInterrupts), |
|
312 |
the returned value may be outdated right away." |
|
1215 | 313 |
|
20734 | 314 |
|p| |
23082
b75aec4476a4
Issue #94 [4/x]: revamp of `RecursionLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21249
diff
changeset
|
315 |
|
23084
0ffb59b273ff
Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23083
diff
changeset
|
316 |
^ (p := self owner) notNil and:[Processor activeProcess ~~ p and:[p isDead not]] |
1215 | 317 |
! ! |
318 |
||
16904 | 319 |
!RecursionLock methodsFor:'signaling'! |
320 |
||
23082
b75aec4476a4
Issue #94 [4/x]: revamp of `RecursionLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21249
diff
changeset
|
321 |
signal |
b75aec4476a4
Issue #94 [4/x]: revamp of `RecursionLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21249
diff
changeset
|
322 |
self breakPoint: #jv. |
b75aec4476a4
Issue #94 [4/x]: revamp of `RecursionLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21249
diff
changeset
|
323 |
self release ifFalse:[ |
b75aec4476a4
Issue #94 [4/x]: revamp of `RecursionLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21249
diff
changeset
|
324 |
self error: 'Attempt to release a (recursion) lock by process the does not own it!!' |
b75aec4476a4
Issue #94 [4/x]: revamp of `RecursionLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21249
diff
changeset
|
325 |
] |
b75aec4476a4
Issue #94 [4/x]: revamp of `RecursionLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21249
diff
changeset
|
326 |
|
b75aec4476a4
Issue #94 [4/x]: revamp of `RecursionLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21249
diff
changeset
|
327 |
"Modified: / 25-08-2017 / 08:41:01 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
16904 | 328 |
! ! |
329 |
||
23085
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
330 |
!RecursionLock methodsFor:'synchronized evaluation'! |
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
331 |
|
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
332 |
critical:aBlock |
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
333 |
"Evaluate aBlock as a critical region. Same process may |
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
334 |
enter critical region again, i.e., nesting allowed." |
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
335 |
|
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
336 |
<exception: #unwind> |
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
337 |
|
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
338 |
| acquired retval | |
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
339 |
|
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
340 |
acquired := self acquireWithTimeoutMs: nil. |
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
341 |
acquired == true ifTrue:[ |
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
342 |
retval := aBlock value |
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
343 |
]. |
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
344 |
thisContext unmarkForUnwind. |
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
345 |
acquired == true ifTrue:[ |
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
346 |
self release. |
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
347 |
]. |
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
348 |
^ retval |
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
349 |
|
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
350 |
"Created: / 31-08-2017 / 10:12:48 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
351 |
! ! |
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
352 |
|
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
353 |
!RecursionLock methodsFor:'unwinding'! |
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
354 |
|
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
355 |
unwindHandlerInContext:aContext |
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
356 |
aContext selector == #critical: ifTrue:[ |
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
357 |
| acquired | |
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
358 |
acquired := aContext varAt: 1. |
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
359 |
acquired == true ifTrue:[ |
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
360 |
^ [ aContext varAt: 1 put: nil. self release ] |
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
361 |
] ifFalse:[ |
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
362 |
^ nil. |
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
363 |
]. |
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
364 |
]. |
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
365 |
self shouldNeverBeReached. |
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
366 |
|
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
367 |
"Created: / 31-08-2017 / 10:11:45 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
368 |
! ! |
9effc2bcf8a6
Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23084
diff
changeset
|
369 |
|
8568 | 370 |
!RecursionLock methodsFor:'waiting'! |
330 | 371 |
|
16904 | 372 |
wait |
23082
b75aec4476a4
Issue #94 [4/x]: revamp of `RecursionLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21249
diff
changeset
|
373 |
self breakPoint: #jv. |
b75aec4476a4
Issue #94 [4/x]: revamp of `RecursionLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21249
diff
changeset
|
374 |
self acquire. |
16904 | 375 |
|
23082
b75aec4476a4
Issue #94 [4/x]: revamp of `RecursionLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21249
diff
changeset
|
376 |
"Modified: / 25-08-2017 / 08:40:45 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
330 | 377 |
! ! |
378 |
||
699 | 379 |
!RecursionLock class methodsFor:'documentation'! |
330 | 380 |
|
1216
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
381 |
version |
18748 | 382 |
^ '$Header$' |
12681 | 383 |
! |
384 |
||
385 |
version_CVS |
|
18748 | 386 |
^ '$Header$' |
23082
b75aec4476a4
Issue #94 [4/x]: revamp of `RecursionLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21249
diff
changeset
|
387 |
! |
b75aec4476a4
Issue #94 [4/x]: revamp of `RecursionLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21249
diff
changeset
|
388 |
|
b75aec4476a4
Issue #94 [4/x]: revamp of `RecursionLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21249
diff
changeset
|
389 |
version_HG |
b75aec4476a4
Issue #94 [4/x]: revamp of `RecursionLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21249
diff
changeset
|
390 |
|
b75aec4476a4
Issue #94 [4/x]: revamp of `RecursionLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21249
diff
changeset
|
391 |
^ '$Changeset: <not expanded> $' |
330 | 392 |
! ! |
14697 | 393 |