RecursionLock.st
author Jan Vrany <jan.vrany@labware.com>
Thu, 25 Mar 2021 20:30:03 +0000
branchjv
changeset 25411 248600ba8fd9
parent 23107 40173e082cbc
child 25415 4ea1fe7c363f
permissions -rw-r--r--
Fix unlikely but possible race in `WeakValueDictionary` It may happen that value in `valueArray` could have been already collected by the GC but #clearDeadSlots have not yet been called. When this happened, `#at:ifAbsentPut:` returned tombstone rather than updating the dictionary with value from block. This commit fixes this by checking whether `valueArray` contain the tombstone and if so, clearing up the dead slots and restarting the operation. HTH.
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
23107
40173e082cbc Copyright updates
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23096
diff changeset
     3
 COPYRIGHT (c) 2017 Jan Vrany
341
claus
parents: 330
diff changeset
     4
	      All Rights Reserved
330
claus
parents:
diff changeset
     5
claus
parents:
diff changeset
     6
 This software is furnished under a license and may be used
claus
parents:
diff changeset
     7
 only in accordance with the terms of that license and with the
claus
parents:
diff changeset
     8
 inclusion of the above copyright notice.   This software may not
claus
parents:
diff changeset
     9
 be provided or otherwise made available to, or used by, any
claus
parents:
diff changeset
    10
 other person.  No title to or ownership of the software is
claus
parents:
diff changeset
    11
 hereby transferred.
claus
parents:
diff changeset
    12
"
6552
5ebab8a33da9 Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents: 5735
diff changeset
    13
"{ Package: 'stx:libbasic' }"
5735
3c1f1f115640 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 5104
diff changeset
    14
18748
f7400cf1f488 class: RecursionLock
Stefan Vogel <sv@exept.de>
parents: 16904
diff changeset
    15
"{ NameSpace: Smalltalk }"
f7400cf1f488 class: RecursionLock
Stefan Vogel <sv@exept.de>
parents: 16904
diff changeset
    16
23083
c8dcd89b9cf6 Issue #94 [5/x]: introduce an `AbstractLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23082
diff changeset
    17
AbstractLock subclass:#RecursionLock
c8dcd89b9cf6 Issue #94 [5/x]: introduce an `AbstractLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23082
diff changeset
    18
	instanceVariableNames:''
776
f3c0c579c0d2 commentary
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
    19
	classVariableNames:''
f3c0c579c0d2 commentary
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
    20
	poolDictionaries:''
f3c0c579c0d2 commentary
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
    21
	category:'Kernel-Processes'
330
claus
parents:
diff changeset
    22
!
claus
parents:
diff changeset
    23
23084
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
    24
!RecursionLock primitiveDefinitions!
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
    25
%{
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
    26
#define THINLOCKING
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
    27
#ifdef THINLOCKING
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
    28
# include <thinlocks.h>
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
    29
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
    30
    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
    31
}
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
    32
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
    33
#endif
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
! !
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
    36
330
claus
parents:
diff changeset
    37
!RecursionLock class methodsFor:'documentation'!
claus
parents:
diff changeset
    38
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    39
copyright
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    40
"
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    41
 COPYRIGHT (c) 1995 by Claus Gittinger
23107
40173e082cbc Copyright updates
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23096
diff changeset
    42
 COPYRIGHT (c) 2017 Jan Vrany
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    43
	      All Rights Reserved
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    44
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    45
 This software is furnished under a license and may be used
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    46
 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
    47
 inclusion of the above copyright notice.   This software may not
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    48
 be provided or otherwise made available to, or used by, any
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    49
 other person.  No title to or ownership of the software is
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    50
 hereby transferred.
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    51
"
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    52
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    53
!
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    54
330
claus
parents:
diff changeset
    55
documentation
claus
parents:
diff changeset
    56
"
23086
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23085
diff changeset
    57
    Like a Semaphore for mutual exclusion, but avoids the deadlock
330
claus
parents:
diff changeset
    58
    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
    59
    I.e. allows reentering the critical region IFF the current process
341
claus
parents: 330
diff changeset
    60
    is the one which did the original locking.
330
claus
parents:
diff changeset
    61
23086
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23085
diff changeset
    62
    NOTE:
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23085
diff changeset
    63
    The recursion lock is not only reentrant (same process may enter the
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23085
diff changeset
    64
    critical section multiple times) but also much faster than using
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23085
diff changeset
    65
    semaphore (`lock := Semaphore forMutualExclusion. lock critical:[...]`)
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23085
diff changeset
    66
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23085
diff changeset
    67
    Therefore you're encouraged to use `RecursonLock` rather than
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23085
diff changeset
    68
    `Semaphore forMutualExclusion` whenever possible.
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23085
diff changeset
    69
3524
b5fe623f0e4d warning in documentation
Claus Gittinger <cg@exept.de>
parents: 3326
diff changeset
    70
    WARNING:
23086
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23085
diff changeset
    71
	For now, recursionLocks are not unlocked when an image is
6552
5ebab8a33da9 Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents: 5735
diff changeset
    72
	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
    73
	(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
    74
	 the #earlyRestart handling does not hurt)
1294
e26bbb61f6b2 documentation
Claus Gittinger <cg@exept.de>
parents: 1273
diff changeset
    75
23086
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23085
diff changeset
    76
    Thinlocks
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23085
diff changeset
    77
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23085
diff changeset
    78
    RecursionLocks uses `thinlocks`[1] to optimize locking in the common
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23085
diff changeset
    79
    case - this makes it much faster.
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23085
diff changeset
    80
    The `lockword` is stored in `process` instvar - when a `process` instvar
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23085
diff changeset
    81
    contains a small integer, recursion lock is `thin`, value of `count` instvas
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23085
diff changeset
    82
    is invalid (out of sync).
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23085
diff changeset
    83
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23085
diff changeset
    84
    [1]: David F. Bacon, Ravi Konuru, Chet Murthy, Mauricio Serrano:
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23085
diff changeset
    85
        Thin locks: featherweight synchronization for Java, ACM SIGPLAN 1998
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23085
diff changeset
    86
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23085
diff changeset
    87
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23085
diff changeset
    88
1294
e26bbb61f6b2 documentation
Claus Gittinger <cg@exept.de>
parents: 1273
diff changeset
    89
    [author:]
23086
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23085
diff changeset
    90
	   Claus Gittinger
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23085
diff changeset
    91
        Jan Vrany (thinlock suppot)
1294
e26bbb61f6b2 documentation
Claus Gittinger <cg@exept.de>
parents: 1273
diff changeset
    92
1273
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1216
diff changeset
    93
    [see also:]
23086
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23085
diff changeset
    94
	   Semaphore
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23085
diff changeset
    95
	   Process ProcessorScheduler
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23085
diff changeset
    96
        AbstractLock
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23085
diff changeset
    97
        thinlocks.h
1273
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1216
diff changeset
    98
"
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1216
diff changeset
    99
!
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1216
diff changeset
   100
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1216
diff changeset
   101
examples
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1216
diff changeset
   102
"
2143
09af9c997961 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   103
  example (good):
6552
5ebab8a33da9 Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents: 5735
diff changeset
   104
									[exBegin]
2143
09af9c997961 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   105
    |lock|
776
f3c0c579c0d2 commentary
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
   106
2143
09af9c997961 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   107
    lock := RecursionLock new.
09af9c997961 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   108
    lock critical:[
6552
5ebab8a33da9 Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents: 5735
diff changeset
   109
	Transcript showCR:'in lock ...'.
5ebab8a33da9 Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents: 5735
diff changeset
   110
	lock critical:[
5ebab8a33da9 Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents: 5735
diff changeset
   111
	    Transcript showCR:'again ...'
5ebab8a33da9 Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents: 5735
diff changeset
   112
	]
2143
09af9c997961 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   113
    ]
6552
5ebab8a33da9 Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents: 5735
diff changeset
   114
									[exEnd]
776
f3c0c579c0d2 commentary
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
   115
2143
09af9c997961 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   116
  in contrast to (wrong example - deadlocks):
6552
5ebab8a33da9 Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents: 5735
diff changeset
   117
									[exBegin]
2143
09af9c997961 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   118
    |lock|
776
f3c0c579c0d2 commentary
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
   119
2143
09af9c997961 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   120
    lock := Semaphore forMutualExclusion.
09af9c997961 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   121
    lock critical:[
6552
5ebab8a33da9 Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents: 5735
diff changeset
   122
	Transcript showCR:'in lock ...'.
5ebab8a33da9 Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents: 5735
diff changeset
   123
	lock critical:[
5ebab8a33da9 Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents: 5735
diff changeset
   124
	    '*** never reached - deadlock because sema is already locked ***'.
5ebab8a33da9 Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents: 5735
diff changeset
   125
	    '    (press CTRL-c and abort in the debugger)'.
5ebab8a33da9 Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents: 5735
diff changeset
   126
	    Transcript showCR:'again ...'
5ebab8a33da9 Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents: 5735
diff changeset
   127
	]
2143
09af9c997961 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   128
    ]
6552
5ebab8a33da9 Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents: 5735
diff changeset
   129
									[exEnd]
330
claus
parents:
diff changeset
   130
"
claus
parents:
diff changeset
   131
! !
claus
parents:
diff changeset
   132
claus
parents:
diff changeset
   133
!RecursionLock class methodsFor:'instance creation'!
claus
parents:
diff changeset
   134
5735
3c1f1f115640 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 5104
diff changeset
   135
forMutualExclusion
18748
f7400cf1f488 class: RecursionLock
Stefan Vogel <sv@exept.de>
parents: 16904
diff changeset
   136
    "same as new, for easy exchangability with regular mutual-exclusion Semaphores."
5735
3c1f1f115640 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 5104
diff changeset
   137
23082
b75aec4476a4 Issue #94 [4/x]: revamp of `RecursionLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 21249
diff changeset
   138
    ^ self new
23083
c8dcd89b9cf6 Issue #94 [5/x]: introduce an `AbstractLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23082
diff changeset
   139
! !
c8dcd89b9cf6 Issue #94 [5/x]: introduce an `AbstractLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23082
diff changeset
   140
23084
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   141
!RecursionLock methodsFor:'accessing'!
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   142
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   143
count
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   144
    ^ 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
   145
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   146
!
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
owner
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   149
    ^ 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
   150
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   151
! !
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   152
23083
c8dcd89b9cf6 Issue #94 [5/x]: introduce an `AbstractLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23082
diff changeset
   153
!RecursionLock methodsFor:'acquire & release'!
5735
3c1f1f115640 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 5104
diff changeset
   154
23084
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   155
acquireWithTimeoutMs: timeout
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   156
    "
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   157
    Acquire the lock:
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   158
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   159
       * 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
   160
       * 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
   161
       * 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
   162
         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
   163
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   164
    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
   165
    timed out)
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   166
    "
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   167
%{  /* NOCONTEXT */
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   168
#ifdef THINLOCKING
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   169
    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
   170
        return (true);
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   171
    }
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   172
#endif
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   173
%}.
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   174
    "/ 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
   175
    "/
23085
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   176
    "/ 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
   177
    "/ 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
   178
    "/ 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
   179
    "/
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   180
    "/ 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
   181
    "/ 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
   182
    "/ 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
   183
    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
   184
    ^ super acquireWithTimeoutMs: timeout
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   185
!
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   186
23083
c8dcd89b9cf6 Issue #94 [5/x]: introduce an `AbstractLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23082
diff changeset
   187
release
c8dcd89b9cf6 Issue #94 [5/x]: introduce an `AbstractLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23082
diff changeset
   188
    "
23084
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   189
    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
   190
    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
   191
    "
23084
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   192
%{  /* NOCONTEXT */
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   193
#ifdef THINLOCKING
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   194
    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
   195
        return (true);
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
#endif
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   198
%}.
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   199
    "/ 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
   200
    "/
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   201
    "/ 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
   202
    "/ 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
   203
    "/ 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
   204
    "/
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   205
    "/ 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
   206
    "/ 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
   207
    "/ 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
   208
    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
   209
    super release ifFalse:[
23083
c8dcd89b9cf6 Issue #94 [5/x]: introduce an `AbstractLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23082
diff changeset
   210
        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
   211
    ].
23084
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   212
! !
330
claus
parents:
diff changeset
   213
23084
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   214
!RecursionLock methodsFor:'initialization'!
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   215
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   216
initialize
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   217
    super initialize.
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   218
    process := 0.
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
    "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
   221
    "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
   222
! !
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   223
23085
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   224
!RecursionLock methodsFor:'printing & storing'!
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   225
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   226
displayOn:aGCOrStream
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   227
    "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
   228
     count for your convenience"
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   229
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   230
    "/ 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
   231
    "/ 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
   232
    (aGCOrStream isStream) ifFalse:[
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   233
        ^ super displayOn:aGCOrStream
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   234
    ].
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   235
    aGCOrStream
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   236
        nextPutAll:self class name;
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   237
        nextPut:$(.
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   238
    sema count printOn:aGCOrStream.
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   239
    aGCOrStream nextPutAll:' name: '.
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   240
    (self name ? 'unnamed') printOn:aGCOrStream.
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   241
    aGCOrStream nextPut:$).
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   242
! !
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   243
23084
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   244
!RecursionLock methodsFor:'private'!
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   245
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   246
inflate
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   247
    "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
   248
     #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
   249
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   250
    Called by:
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   251
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   252
       * #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
   253
         is exceeded (unlikely)
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   254
       * #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
   255
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   256
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
    | processAndCount wasBlocked |
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   260
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   261
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   262
    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
   263
    wasBlocked := OperatingSystem blockInterrupts.
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   264
    "/ 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
   265
    "/ 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
   266
    "/ 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
   267
    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
   268
        self processAndCountInto: processAndCount.
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   269
        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
   270
        count   := processAndCount at: 2.
23096
36e1c7a471ab Issue 94: make #inflate safe w.r.t inflating an unlocked lock
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23094
diff changeset
   271
        process notNil ifTrue:[
36e1c7a471ab Issue 94: make #inflate safe w.r.t inflating an unlocked lock
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23094
diff changeset
   272
            sema setCount: 0.
36e1c7a471ab Issue 94: make #inflate safe w.r.t inflating an unlocked lock
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23094
diff changeset
   273
        ].
23084
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   274
    ].
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   275
    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
   276
23096
36e1c7a471ab Issue 94: make #inflate safe w.r.t inflating an unlocked lock
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23094
diff changeset
   277
    "Modified: / 11-12-2017 / 21:40:48 / Jan Vrany <jan.vrany@fit.cvut.cz>"
23084
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   278
!
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   279
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   280
processAndCount
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   281
    | processAndCount |
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   282
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   283
    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
   284
    self processAndCountInto: processAndCount.
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   285
    ^ processAndCount
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   286
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   287
!
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
processAndCountInto: anArray
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   290
    "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
   291
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   292
     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
   293
     be already obsolete.
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   294
    "
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   295
    | pid cnt proc |
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   296
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   297
    "/ 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
   298
    "/ 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
   299
    "/ 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
   300
    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
   301
        %{
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   302
#ifdef THINLOCKING
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   303
        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
   304
        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
   305
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   306
        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
   307
            pid = nil;
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   308
            cnt = __MKINT(0);
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   309
        } else {
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   310
            pid = __MKINT(_pid);
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   311
            cnt = __MKINT(_cnt);
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   312
        }
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   313
#endif
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   314
        %}.
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   315
        pid notNil ifTrue:[
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   316
            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
   317
        ].
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   318
    ] ifFalse:[
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   319
        proc := process.
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   320
        cnt := count.
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   321
    ].
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   322
    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
   323
    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
   324
330
claus
parents:
diff changeset
   325
! !
claus
parents:
diff changeset
   326
1215
43e8e17fd9f5 added #wouldBlock
Claus Gittinger <cg@exept.de>
parents: 1186
diff changeset
   327
!RecursionLock methodsFor:'queries'!
43e8e17fd9f5 added #wouldBlock
Claus Gittinger <cg@exept.de>
parents: 1186
diff changeset
   328
4938
d5640f1c8894 Add #numberOfWaitingProcesse
Stefan Vogel <sv@exept.de>
parents: 3670
diff changeset
   329
numberOfWaitingProcesses
d5640f1c8894 Add #numberOfWaitingProcesse
Stefan Vogel <sv@exept.de>
parents: 3670
diff changeset
   330
    "return the number of waiting processes"
d5640f1c8894 Add #numberOfWaitingProcesse
Stefan Vogel <sv@exept.de>
parents: 3670
diff changeset
   331
d5640f1c8894 Add #numberOfWaitingProcesse
Stefan Vogel <sv@exept.de>
parents: 3670
diff changeset
   332
    ^ sema numberOfWaitingProcesses
d5640f1c8894 Add #numberOfWaitingProcesse
Stefan Vogel <sv@exept.de>
parents: 3670
diff changeset
   333
d5640f1c8894 Add #numberOfWaitingProcesse
Stefan Vogel <sv@exept.de>
parents: 3670
diff changeset
   334
    "Created: 18.4.1996 / 17:18:08 / cg"
d5640f1c8894 Add #numberOfWaitingProcesse
Stefan Vogel <sv@exept.de>
parents: 3670
diff changeset
   335
!
d5640f1c8894 Add #numberOfWaitingProcesse
Stefan Vogel <sv@exept.de>
parents: 3670
diff changeset
   336
7428
84f4d9f08f04 owner & wouldBlock
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   337
wouldBlock
23082
b75aec4476a4 Issue #94 [4/x]: revamp of `RecursionLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 21249
diff changeset
   338
    "Check if the resource represented by the receiver is
15550
8036fe8c032d class: RecursionLock
Claus Gittinger <cg@exept.de>
parents: 14779
diff changeset
   339
     already in use by another process.
8036fe8c032d class: RecursionLock
Claus Gittinger <cg@exept.de>
parents: 14779
diff changeset
   340
     Attention: if asked without some global lock (blockedInterrupts),
8036fe8c032d class: RecursionLock
Claus Gittinger <cg@exept.de>
parents: 14779
diff changeset
   341
     the returned value may be outdated right away."
1215
43e8e17fd9f5 added #wouldBlock
Claus Gittinger <cg@exept.de>
parents: 1186
diff changeset
   342
20734
52db398ec2e0 #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 20385
diff changeset
   343
    |p|
23082
b75aec4476a4 Issue #94 [4/x]: revamp of `RecursionLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 21249
diff changeset
   344
23084
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   345
    ^ (p := self owner) notNil and:[Processor activeProcess ~~ p and:[p isDead not]]
1215
43e8e17fd9f5 added #wouldBlock
Claus Gittinger <cg@exept.de>
parents: 1186
diff changeset
   346
! !
43e8e17fd9f5 added #wouldBlock
Claus Gittinger <cg@exept.de>
parents: 1186
diff changeset
   347
16904
838840a0be45 class: RecursionLock
Stefan Vogel <sv@exept.de>
parents: 16048
diff changeset
   348
!RecursionLock methodsFor:'signaling'!
838840a0be45 class: RecursionLock
Stefan Vogel <sv@exept.de>
parents: 16048
diff changeset
   349
23082
b75aec4476a4 Issue #94 [4/x]: revamp of `RecursionLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 21249
diff changeset
   350
signal
b75aec4476a4 Issue #94 [4/x]: revamp of `RecursionLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 21249
diff changeset
   351
    self breakPoint: #jv.
b75aec4476a4 Issue #94 [4/x]: revamp of `RecursionLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 21249
diff changeset
   352
    self release ifFalse:[
b75aec4476a4 Issue #94 [4/x]: revamp of `RecursionLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 21249
diff changeset
   353
        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
   354
    ]
b75aec4476a4 Issue #94 [4/x]: revamp of `RecursionLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 21249
diff changeset
   355
b75aec4476a4 Issue #94 [4/x]: revamp of `RecursionLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 21249
diff changeset
   356
    "Modified: / 25-08-2017 / 08:41:01 / Jan Vrany <jan.vrany@fit.cvut.cz>"
16904
838840a0be45 class: RecursionLock
Stefan Vogel <sv@exept.de>
parents: 16048
diff changeset
   357
! !
838840a0be45 class: RecursionLock
Stefan Vogel <sv@exept.de>
parents: 16048
diff changeset
   358
23085
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   359
!RecursionLock methodsFor:'synchronized evaluation'!
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   360
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   361
critical:aBlock
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   362
    "Evaluate aBlock as a critical region. Same process may
23086
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23085
diff changeset
   363
     enter critical region again, i.e., nesting allowed.
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23085
diff changeset
   364
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23085
diff changeset
   365
     Returns the (return) value of `aBlock`
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23085
diff changeset
   366
    "
23085
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   367
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   368
    <exception: #unwind>
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   369
23090
58f7ca7bb385 Issue #94: simplified `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23088
diff changeset
   370
    | retval |
23085
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   371
23090
58f7ca7bb385 Issue #94: simplified `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23088
diff changeset
   372
    self acquireWithTimeoutMs: nil.
58f7ca7bb385 Issue #94: simplified `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23088
diff changeset
   373
    retval := aBlock value.
23085
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   374
    thisContext unmarkForUnwind.
23090
58f7ca7bb385 Issue #94: simplified `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23088
diff changeset
   375
    self release.
23085
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   376
    ^ retval
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   377
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   378
    "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
   379
! !
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   380
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   381
!RecursionLock methodsFor:'unwinding'!
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   382
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   383
unwindHandlerInContext:aContext
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   384
    aContext selector == #critical: ifTrue:[
23094
664a6370e264 Issue 94: fixed a bad bug in #critical: unwind mechanism.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23090
diff changeset
   385
        ^ [
664a6370e264 Issue 94: fixed a bad bug in #critical: unwind mechanism.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23090
diff changeset
   386
            "/ When unwiding a #critical: frame, there are two (only two?)
664a6370e264 Issue 94: fixed a bad bug in #critical: unwind mechanism.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23090
diff changeset
   387
            "/ cases:
664a6370e264 Issue 94: fixed a bad bug in #critical: unwind mechanism.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23090
diff changeset
   388
            "/
664a6370e264 Issue 94: fixed a bad bug in #critical: unwind mechanism.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23090
diff changeset
   389
            "/ 1. the critical section is executing aand being unwound
664a6370e264 Issue 94: fixed a bad bug in #critical: unwind mechanism.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23090
diff changeset
   390
            "/ 2. the process calling #critical: is waiting inside #acquire...
664a6370e264 Issue 94: fixed a bad bug in #critical: unwind mechanism.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23090
diff changeset
   391
            "/    for some other process to release it.
664a6370e264 Issue 94: fixed a bad bug in #critical: unwind mechanism.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23090
diff changeset
   392
            "/
664a6370e264 Issue 94: fixed a bad bug in #critical: unwind mechanism.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23090
diff changeset
   393
            "/ In the first case we have to call #release, in the second
664a6370e264 Issue 94: fixed a bad bug in #critical: unwind mechanism.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23090
diff changeset
   394
            "/ we MUST NOT since other process owns the lock.
664a6370e264 Issue 94: fixed a bad bug in #critical: unwind mechanism.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23090
diff changeset
   395
            "/ To distinguish, we check whether we own the lock or not
664a6370e264 Issue 94: fixed a bad bug in #critical: unwind mechanism.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23090
diff changeset
   396
            "/ by `self owner == Processor activeProcess`.
664a6370e264 Issue 94: fixed a bad bug in #critical: unwind mechanism.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23090
diff changeset
   397
            self owner == Processor activeProcess ifTrue:[ self release ].
664a6370e264 Issue 94: fixed a bad bug in #critical: unwind mechanism.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23090
diff changeset
   398
          ]
23085
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   399
    ].
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   400
    self shouldNeverBeReached.
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   401
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   402
    "Created: / 31-08-2017 / 10:11:45 / Jan Vrany <jan.vrany@fit.cvut.cz>"
23094
664a6370e264 Issue 94: fixed a bad bug in #critical: unwind mechanism.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23090
diff changeset
   403
    "Modified (format): / 11-12-2017 / 11:33:47 / Jan Vrany <jan.vrany@fit.cvut.cz>"
23085
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   404
! !
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   405
8568
624bfd00371f category change
Stefan Vogel <sv@exept.de>
parents: 7590
diff changeset
   406
!RecursionLock methodsFor:'waiting'!
330
claus
parents:
diff changeset
   407
16904
838840a0be45 class: RecursionLock
Stefan Vogel <sv@exept.de>
parents: 16048
diff changeset
   408
wait
23082
b75aec4476a4 Issue #94 [4/x]: revamp of `RecursionLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 21249
diff changeset
   409
    self breakPoint: #jv.
b75aec4476a4 Issue #94 [4/x]: revamp of `RecursionLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 21249
diff changeset
   410
    self acquire.
16904
838840a0be45 class: RecursionLock
Stefan Vogel <sv@exept.de>
parents: 16048
diff changeset
   411
23082
b75aec4476a4 Issue #94 [4/x]: revamp of `RecursionLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 21249
diff changeset
   412
    "Modified: / 25-08-2017 / 08:40:45 / Jan Vrany <jan.vrany@fit.cvut.cz>"
330
claus
parents:
diff changeset
   413
! !
claus
parents:
diff changeset
   414
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   415
!RecursionLock class methodsFor:'documentation'!
330
claus
parents:
diff changeset
   416
1216
d7cbc6eb8dd4 critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents: 1215
diff changeset
   417
version
18748
f7400cf1f488 class: RecursionLock
Stefan Vogel <sv@exept.de>
parents: 16904
diff changeset
   418
    ^ '$Header$'
12681
Stefan Vogel <sv@exept.de>
parents: 11769
diff changeset
   419
!
Stefan Vogel <sv@exept.de>
parents: 11769
diff changeset
   420
Stefan Vogel <sv@exept.de>
parents: 11769
diff changeset
   421
version_CVS
18748
f7400cf1f488 class: RecursionLock
Stefan Vogel <sv@exept.de>
parents: 16904
diff changeset
   422
    ^ '$Header$'
23082
b75aec4476a4 Issue #94 [4/x]: revamp of `RecursionLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 21249
diff changeset
   423
!
b75aec4476a4 Issue #94 [4/x]: revamp of `RecursionLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 21249
diff changeset
   424
b75aec4476a4 Issue #94 [4/x]: revamp of `RecursionLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 21249
diff changeset
   425
version_HG
b75aec4476a4 Issue #94 [4/x]: revamp of `RecursionLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 21249
diff changeset
   426
b75aec4476a4 Issue #94 [4/x]: revamp of `RecursionLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 21249
diff changeset
   427
    ^ '$Changeset: <not expanded> $'
330
claus
parents:
diff changeset
   428
! !
14697
e9ef6bbd0507 class: RecursionLock
Claus Gittinger <cg@exept.de>
parents: 13356
diff changeset
   429