RecursionLock.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Tue, 12 Dec 2017 08:57:23 +0000
branchjv
changeset 23096 36e1c7a471ab
parent 23094 664a6370e264
child 23107 40173e082cbc
permissions -rw-r--r--
Issue 94: make #inflate safe w.r.t inflating an unlocked lock
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
330
claus
parents:
diff changeset
     1
"
claus
parents:
diff changeset
     2
 COPYRIGHT (c) 1995 by Claus Gittinger
341
claus
parents: 330
diff changeset
     3
	      All Rights Reserved
330
claus
parents:
diff changeset
     4
claus
parents:
diff changeset
     5
 This software is furnished under a license and may be used
claus
parents:
diff changeset
     6
 only in accordance with the terms of that license and with the
claus
parents:
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
claus
parents:
diff changeset
     8
 be provided or otherwise made available to, or used by, any
claus
parents:
diff changeset
     9
 other person.  No title to or ownership of the software is
claus
parents:
diff changeset
    10
 hereby transferred.
claus
parents:
diff changeset
    11
"
6552
5ebab8a33da9 Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents: 5735
diff changeset
    12
"{ Package: 'stx:libbasic' }"
5735
3c1f1f115640 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 5104
diff changeset
    13
18748
f7400cf1f488 class: RecursionLock
Stefan Vogel <sv@exept.de>
parents: 16904
diff changeset
    14
"{ NameSpace: Smalltalk }"
f7400cf1f488 class: RecursionLock
Stefan Vogel <sv@exept.de>
parents: 16904
diff changeset
    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
f3c0c579c0d2 commentary
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
    18
	classVariableNames:''
f3c0c579c0d2 commentary
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
    19
	poolDictionaries:''
f3c0c579c0d2 commentary
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
    20
	category:'Kernel-Processes'
330
claus
parents:
diff changeset
    21
!
claus
parents:
diff changeset
    22
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
claus
parents:
diff changeset
    36
!RecursionLock class methodsFor:'documentation'!
claus
parents:
diff changeset
    37
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    38
copyright
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    39
"
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    40
 COPYRIGHT (c) 1995 by Claus Gittinger
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    41
	      All Rights Reserved
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    42
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    43
 This software is furnished under a license and may be used
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    44
 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
    45
 inclusion of the above copyright notice.   This software may not
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    46
 be provided or otherwise made available to, or used by, any
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    47
 other person.  No title to or ownership of the software is
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    48
 hereby transferred.
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    49
"
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    50
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
330
claus
parents:
diff changeset
    53
documentation
claus
parents:
diff changeset
    54
"
23086
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23085
diff changeset
    55
    Like a Semaphore for mutual exclusion, but avoids the deadlock
330
claus
parents:
diff changeset
    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
claus
parents: 330
diff changeset
    58
    is the one which did the original locking.
330
claus
parents:
diff changeset
    59
23086
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23085
diff changeset
    60
    NOTE:
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23085
diff changeset
    61
    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
    62
    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
    63
    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
    64
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23085
diff changeset
    65
    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
    66
    `Semaphore forMutualExclusion` whenever possible.
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23085
diff changeset
    67
3524
b5fe623f0e4d warning in documentation
Claus Gittinger <cg@exept.de>
parents: 3326
diff changeset
    68
    WARNING:
23086
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23085
diff changeset
    69
	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
    70
	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
    71
	(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
    72
	 the #earlyRestart handling does not hurt)
1294
e26bbb61f6b2 documentation
Claus Gittinger <cg@exept.de>
parents: 1273
diff changeset
    73
23086
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23085
diff changeset
    74
    Thinlocks
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23085
diff changeset
    75
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23085
diff changeset
    76
    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
    77
    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
    78
    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
    79
    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
    80
    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
    81
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23085
diff changeset
    82
    [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
    83
        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
    84
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23085
diff changeset
    85
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23085
diff changeset
    86
1294
e26bbb61f6b2 documentation
Claus Gittinger <cg@exept.de>
parents: 1273
diff changeset
    87
    [author:]
23086
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23085
diff changeset
    88
	   Claus Gittinger
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23085
diff changeset
    89
        Jan Vrany (thinlock suppot)
1294
e26bbb61f6b2 documentation
Claus Gittinger <cg@exept.de>
parents: 1273
diff changeset
    90
1273
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1216
diff changeset
    91
    [see also:]
23086
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23085
diff changeset
    92
	   Semaphore
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23085
diff changeset
    93
	   Process ProcessorScheduler
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23085
diff changeset
    94
        AbstractLock
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23085
diff changeset
    95
        thinlocks.h
1273
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1216
diff changeset
    96
"
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1216
diff changeset
    97
!
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1216
diff changeset
    98
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1216
diff changeset
    99
examples
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1216
diff changeset
   100
"
2143
09af9c997961 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   101
  example (good):
6552
5ebab8a33da9 Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents: 5735
diff changeset
   102
									[exBegin]
2143
09af9c997961 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   103
    |lock|
776
f3c0c579c0d2 commentary
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
   104
2143
09af9c997961 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   105
    lock := RecursionLock new.
09af9c997961 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   106
    lock critical:[
6552
5ebab8a33da9 Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents: 5735
diff changeset
   107
	Transcript showCR:'in lock ...'.
5ebab8a33da9 Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents: 5735
diff changeset
   108
	lock critical:[
5ebab8a33da9 Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents: 5735
diff changeset
   109
	    Transcript showCR:'again ...'
5ebab8a33da9 Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents: 5735
diff changeset
   110
	]
2143
09af9c997961 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   111
    ]
6552
5ebab8a33da9 Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents: 5735
diff changeset
   112
									[exEnd]
776
f3c0c579c0d2 commentary
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
   113
2143
09af9c997961 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   114
  in contrast to (wrong example - deadlocks):
6552
5ebab8a33da9 Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents: 5735
diff changeset
   115
									[exBegin]
2143
09af9c997961 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   116
    |lock|
776
f3c0c579c0d2 commentary
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
   117
2143
09af9c997961 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   118
    lock := Semaphore forMutualExclusion.
09af9c997961 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   119
    lock critical:[
6552
5ebab8a33da9 Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents: 5735
diff changeset
   120
	Transcript showCR:'in lock ...'.
5ebab8a33da9 Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents: 5735
diff changeset
   121
	lock critical:[
5ebab8a33da9 Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents: 5735
diff changeset
   122
	    '*** never reached - deadlock because sema is already locked ***'.
5ebab8a33da9 Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents: 5735
diff changeset
   123
	    '    (press CTRL-c and abort in the debugger)'.
5ebab8a33da9 Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents: 5735
diff changeset
   124
	    Transcript showCR:'again ...'
5ebab8a33da9 Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents: 5735
diff changeset
   125
	]
2143
09af9c997961 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   126
    ]
6552
5ebab8a33da9 Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents: 5735
diff changeset
   127
									[exEnd]
330
claus
parents:
diff changeset
   128
"
claus
parents:
diff changeset
   129
! !
claus
parents:
diff changeset
   130
claus
parents:
diff changeset
   131
!RecursionLock class methodsFor:'instance creation'!
claus
parents:
diff changeset
   132
5735
3c1f1f115640 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 5104
diff changeset
   133
forMutualExclusion
18748
f7400cf1f488 class: RecursionLock
Stefan Vogel <sv@exept.de>
parents: 16904
diff changeset
   134
    "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
   135
23082
b75aec4476a4 Issue #94 [4/x]: revamp of `RecursionLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 21249
diff changeset
   136
    ^ self new
23083
c8dcd89b9cf6 Issue #94 [5/x]: introduce an `AbstractLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23082
diff changeset
   137
! !
c8dcd89b9cf6 Issue #94 [5/x]: introduce an `AbstractLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23082
diff changeset
   138
23084
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   139
!RecursionLock methodsFor:'accessing'!
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
count
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   142
    ^ 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
   143
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   144
!
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
owner
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   147
    ^ 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
   148
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   149
! !
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   150
23083
c8dcd89b9cf6 Issue #94 [5/x]: introduce an `AbstractLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23082
diff changeset
   151
!RecursionLock methodsFor:'acquire & release'!
5735
3c1f1f115640 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 5104
diff changeset
   152
23084
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   153
acquireWithTimeoutMs: timeout
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   154
    "
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   155
    Acquire the lock:
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
       * 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
   158
       * 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
   159
       * 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
   160
         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
   161
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   162
    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
   163
    timed out)
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   164
    "
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   165
%{  /* NOCONTEXT */
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   166
#ifdef THINLOCKING
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   167
    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
   168
        return (true);
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   169
    }
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   170
#endif
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
    "/ 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
   173
    "/
23085
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   174
    "/ 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
   175
    "/ 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
   176
    "/ 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
   177
    "/
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   178
    "/ 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
   179
    "/ 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
   180
    "/ 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
   181
    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
   182
    ^ super acquireWithTimeoutMs: timeout
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   183
!
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   184
23083
c8dcd89b9cf6 Issue #94 [5/x]: introduce an `AbstractLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23082
diff changeset
   185
release
c8dcd89b9cf6 Issue #94 [5/x]: introduce an `AbstractLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23082
diff changeset
   186
    "
23084
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   187
    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
   188
    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
   189
    "
23084
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   190
%{  /* NOCONTEXT */
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   191
#ifdef THINLOCKING
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   192
    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
   193
        return (true);
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   194
    }
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   195
#endif
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
    "/ 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
   198
    "/
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   199
    "/ 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
   200
    "/ 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
   201
    "/ 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
   202
    "/
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   203
    "/ 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
   204
    "/ 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
   205
    "/ 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
   206
    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
   207
    super release ifFalse:[
23083
c8dcd89b9cf6 Issue #94 [5/x]: introduce an `AbstractLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23082
diff changeset
   208
        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
   209
    ].
23084
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   210
! !
330
claus
parents:
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
!RecursionLock methodsFor:'initialization'!
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   213
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   214
initialize
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   215
    super initialize.
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   216
    process := 0.
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   217
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   218
    "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
   219
    "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
   220
! !
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   221
23085
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   222
!RecursionLock methodsFor:'printing & storing'!
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   223
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   224
displayOn:aGCOrStream
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   225
    "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
   226
     count for your convenience"
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   227
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   228
    "/ 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
   229
    "/ 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
   230
    (aGCOrStream isStream) ifFalse:[
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   231
        ^ super displayOn:aGCOrStream
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   232
    ].
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   233
    aGCOrStream
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   234
        nextPutAll:self class name;
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   235
        nextPut:$(.
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   236
    sema count printOn:aGCOrStream.
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   237
    aGCOrStream nextPutAll:' name: '.
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   238
    (self name ? 'unnamed') printOn:aGCOrStream.
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   239
    aGCOrStream nextPut:$).
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   240
! !
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   241
23084
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   242
!RecursionLock methodsFor:'private'!
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   243
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   244
inflate
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   245
    "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
   246
     #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
   247
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   248
    Called by:
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
       * #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
   251
         is exceeded (unlikely)
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   252
       * #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
   253
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   254
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
    | processAndCount wasBlocked |
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
    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
   261
    wasBlocked := OperatingSystem blockInterrupts.
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   262
    "/ 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
   263
    "/ 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
   264
    "/ 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
   265
    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
   266
        self processAndCountInto: processAndCount.
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   267
        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
   268
        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
   269
        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
   270
            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
   271
        ].
23084
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
    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
   274
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
   275
    "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
   276
!
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   277
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   278
processAndCount
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   279
    | processAndCount |
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   280
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   281
    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
   282
    self processAndCountInto: processAndCount.
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   283
    ^ processAndCount
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   284
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
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   287
processAndCountInto: anArray
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   288
    "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
   289
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   290
     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
   291
     be already obsolete.
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
    | pid cnt proc |
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
    "/ 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
   296
    "/ 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
   297
    "/ 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
   298
    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
   299
        %{
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   300
#ifdef THINLOCKING
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   301
        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
   302
        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
   303
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   304
        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
   305
            pid = nil;
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   306
            cnt = __MKINT(0);
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   307
        } else {
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   308
            pid = __MKINT(_pid);
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   309
            cnt = __MKINT(_cnt);
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   310
        }
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   311
#endif
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
        pid notNil ifTrue:[
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   314
            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
   315
        ].
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   316
    ] ifFalse:[
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   317
        proc := process.
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   318
        cnt := count.
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   319
    ].
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   320
    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
   321
    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
   322
330
claus
parents:
diff changeset
   323
! !
claus
parents:
diff changeset
   324
1215
43e8e17fd9f5 added #wouldBlock
Claus Gittinger <cg@exept.de>
parents: 1186
diff changeset
   325
!RecursionLock methodsFor:'queries'!
43e8e17fd9f5 added #wouldBlock
Claus Gittinger <cg@exept.de>
parents: 1186
diff changeset
   326
4938
d5640f1c8894 Add #numberOfWaitingProcesse
Stefan Vogel <sv@exept.de>
parents: 3670
diff changeset
   327
numberOfWaitingProcesses
d5640f1c8894 Add #numberOfWaitingProcesse
Stefan Vogel <sv@exept.de>
parents: 3670
diff changeset
   328
    "return the number of waiting processes"
d5640f1c8894 Add #numberOfWaitingProcesse
Stefan Vogel <sv@exept.de>
parents: 3670
diff changeset
   329
d5640f1c8894 Add #numberOfWaitingProcesse
Stefan Vogel <sv@exept.de>
parents: 3670
diff changeset
   330
    ^ sema numberOfWaitingProcesses
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
    "Created: 18.4.1996 / 17:18:08 / cg"
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
7428
84f4d9f08f04 owner & wouldBlock
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   335
wouldBlock
23082
b75aec4476a4 Issue #94 [4/x]: revamp of `RecursionLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 21249
diff changeset
   336
    "Check if the resource represented by the receiver is
15550
8036fe8c032d class: RecursionLock
Claus Gittinger <cg@exept.de>
parents: 14779
diff changeset
   337
     already in use by another process.
8036fe8c032d class: RecursionLock
Claus Gittinger <cg@exept.de>
parents: 14779
diff changeset
   338
     Attention: if asked without some global lock (blockedInterrupts),
8036fe8c032d class: RecursionLock
Claus Gittinger <cg@exept.de>
parents: 14779
diff changeset
   339
     the returned value may be outdated right away."
1215
43e8e17fd9f5 added #wouldBlock
Claus Gittinger <cg@exept.de>
parents: 1186
diff changeset
   340
20734
52db398ec2e0 #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 20385
diff changeset
   341
    |p|
23082
b75aec4476a4 Issue #94 [4/x]: revamp of `RecursionLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 21249
diff changeset
   342
23084
0ffb59b273ff Issue #94 [6/x]: refactor `RecursionLock` to use thinlocks.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23083
diff changeset
   343
    ^ (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
   344
! !
43e8e17fd9f5 added #wouldBlock
Claus Gittinger <cg@exept.de>
parents: 1186
diff changeset
   345
16904
838840a0be45 class: RecursionLock
Stefan Vogel <sv@exept.de>
parents: 16048
diff changeset
   346
!RecursionLock methodsFor:'signaling'!
838840a0be45 class: RecursionLock
Stefan Vogel <sv@exept.de>
parents: 16048
diff changeset
   347
23082
b75aec4476a4 Issue #94 [4/x]: revamp of `RecursionLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 21249
diff changeset
   348
signal
b75aec4476a4 Issue #94 [4/x]: revamp of `RecursionLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 21249
diff changeset
   349
    self breakPoint: #jv.
b75aec4476a4 Issue #94 [4/x]: revamp of `RecursionLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 21249
diff changeset
   350
    self release ifFalse:[
b75aec4476a4 Issue #94 [4/x]: revamp of `RecursionLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 21249
diff changeset
   351
        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
   352
    ]
b75aec4476a4 Issue #94 [4/x]: revamp of `RecursionLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 21249
diff changeset
   353
b75aec4476a4 Issue #94 [4/x]: revamp of `RecursionLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 21249
diff changeset
   354
    "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
   355
! !
838840a0be45 class: RecursionLock
Stefan Vogel <sv@exept.de>
parents: 16048
diff changeset
   356
23085
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   357
!RecursionLock methodsFor:'synchronized evaluation'!
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   358
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   359
critical:aBlock
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   360
    "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
   361
     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
   362
d3f84ef999e6 Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23085
diff changeset
   363
     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
   364
    "
23085
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   365
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   366
    <exception: #unwind>
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   367
23090
58f7ca7bb385 Issue #94: simplified `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23088
diff changeset
   368
    | retval |
23085
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
    self acquireWithTimeoutMs: nil.
58f7ca7bb385 Issue #94: simplified `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23088
diff changeset
   371
    retval := aBlock value.
23085
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   372
    thisContext unmarkForUnwind.
23090
58f7ca7bb385 Issue #94: simplified `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23088
diff changeset
   373
    self release.
23085
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   374
    ^ retval
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   375
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   376
    "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
   377
! !
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   378
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   379
!RecursionLock methodsFor:'unwinding'!
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
unwindHandlerInContext:aContext
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   382
    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
   383
        ^ [
664a6370e264 Issue 94: fixed a bad bug in #critical: unwind mechanism.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23090
diff changeset
   384
            "/ 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
   385
            "/ cases:
664a6370e264 Issue 94: fixed a bad bug in #critical: unwind mechanism.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23090
diff changeset
   386
            "/
664a6370e264 Issue 94: fixed a bad bug in #critical: unwind mechanism.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23090
diff changeset
   387
            "/ 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
   388
            "/ 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
   389
            "/    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
   390
            "/
664a6370e264 Issue 94: fixed a bad bug in #critical: unwind mechanism.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23090
diff changeset
   391
            "/ 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
   392
            "/ 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
   393
            "/ 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
   394
            "/ 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
   395
            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
   396
          ]
23085
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   397
    ].
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   398
    self shouldNeverBeReached.
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
    "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
   401
    "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
   402
! !
9effc2bcf8a6 Issue #94 [7/x]: Optimize `RecursionLock >> #critical:`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 23084
diff changeset
   403
8568
624bfd00371f category change
Stefan Vogel <sv@exept.de>
parents: 7590
diff changeset
   404
!RecursionLock methodsFor:'waiting'!
330
claus
parents:
diff changeset
   405
16904
838840a0be45 class: RecursionLock
Stefan Vogel <sv@exept.de>
parents: 16048
diff changeset
   406
wait
23082
b75aec4476a4 Issue #94 [4/x]: revamp of `RecursionLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 21249
diff changeset
   407
    self breakPoint: #jv.
b75aec4476a4 Issue #94 [4/x]: revamp of `RecursionLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 21249
diff changeset
   408
    self acquire.
16904
838840a0be45 class: RecursionLock
Stefan Vogel <sv@exept.de>
parents: 16048
diff changeset
   409
23082
b75aec4476a4 Issue #94 [4/x]: revamp of `RecursionLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 21249
diff changeset
   410
    "Modified: / 25-08-2017 / 08:40:45 / Jan Vrany <jan.vrany@fit.cvut.cz>"
330
claus
parents:
diff changeset
   411
! !
claus
parents:
diff changeset
   412
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   413
!RecursionLock class methodsFor:'documentation'!
330
claus
parents:
diff changeset
   414
1216
d7cbc6eb8dd4 critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents: 1215
diff changeset
   415
version
18748
f7400cf1f488 class: RecursionLock
Stefan Vogel <sv@exept.de>
parents: 16904
diff changeset
   416
    ^ '$Header$'
12681
Stefan Vogel <sv@exept.de>
parents: 11769
diff changeset
   417
!
Stefan Vogel <sv@exept.de>
parents: 11769
diff changeset
   418
Stefan Vogel <sv@exept.de>
parents: 11769
diff changeset
   419
version_CVS
18748
f7400cf1f488 class: RecursionLock
Stefan Vogel <sv@exept.de>
parents: 16904
diff changeset
   420
    ^ '$Header$'
23082
b75aec4476a4 Issue #94 [4/x]: revamp of `RecursionLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 21249
diff changeset
   421
!
b75aec4476a4 Issue #94 [4/x]: revamp of `RecursionLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 21249
diff changeset
   422
b75aec4476a4 Issue #94 [4/x]: revamp of `RecursionLock`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 21249
diff changeset
   423
version_HG
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
    ^ '$Changeset: <not expanded> $'
330
claus
parents:
diff changeset
   426
! !
14697
e9ef6bbd0507 class: RecursionLock
Claus Gittinger <cg@exept.de>
parents: 13356
diff changeset
   427