RecursionLock.st
author Claus Gittinger <cg@exept.de>
Tue, 07 Jan 1997 12:02:05 +0100
changeset 2072 e84dbf5e5424
parent 1422 9a0b792f2953
child 2143 09af9c997961
permissions -rw-r--r--
removed package-change info message
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
"
claus
parents:
diff changeset
    12
claus
parents:
diff changeset
    13
claus
parents:
diff changeset
    14
Object subclass:#RecursionLock
776
f3c0c579c0d2 commentary
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
    15
	instanceVariableNames:'process sema'
f3c0c579c0d2 commentary
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
    16
	classVariableNames:''
f3c0c579c0d2 commentary
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
    17
	poolDictionaries:''
f3c0c579c0d2 commentary
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
    18
	category:'Kernel-Processes'
330
claus
parents:
diff changeset
    19
!
claus
parents:
diff changeset
    20
claus
parents:
diff changeset
    21
!RecursionLock class methodsFor:'documentation'!
claus
parents:
diff changeset
    22
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    23
copyright
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    24
"
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    25
 COPYRIGHT (c) 1995 by Claus Gittinger
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    26
	      All Rights Reserved
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    27
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    28
 This software is furnished under a license and may be used
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    29
 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
    30
 inclusion of the above copyright notice.   This software may not
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    31
 be provided or otherwise made available to, or used by, any
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    32
 other person.  No title to or ownership of the software is
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    33
 hereby transferred.
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    34
"
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    35
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    36
!
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    37
330
claus
parents:
diff changeset
    38
documentation
claus
parents:
diff changeset
    39
"
claus
parents:
diff changeset
    40
    like a Semaphore for mutual exclusion, but avoids the deadlock
claus
parents:
diff changeset
    41
    if a critical region is reentered by the same process again.
341
claus
parents: 330
diff changeset
    42
    I.e. allows reentering the critical region if the current process 
claus
parents: 330
diff changeset
    43
    is the one which did the original locking.
330
claus
parents:
diff changeset
    44
1294
e26bbb61f6b2 documentation
Claus Gittinger <cg@exept.de>
parents: 1273
diff changeset
    45
e26bbb61f6b2 documentation
Claus Gittinger <cg@exept.de>
parents: 1273
diff changeset
    46
    [author:]
e26bbb61f6b2 documentation
Claus Gittinger <cg@exept.de>
parents: 1273
diff changeset
    47
        Claus Gittinger
e26bbb61f6b2 documentation
Claus Gittinger <cg@exept.de>
parents: 1273
diff changeset
    48
1273
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1216
diff changeset
    49
    [see also:]
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1216
diff changeset
    50
        Semaphore
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1216
diff changeset
    51
        Process ProcessorScheduler
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1216
diff changeset
    52
"
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1216
diff changeset
    53
!
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1216
diff changeset
    54
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1216
diff changeset
    55
examples
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1216
diff changeset
    56
"
330
claus
parents:
diff changeset
    57
    example:
1316
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
    58
                                                                        [exBegin]
776
f3c0c579c0d2 commentary
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
    59
        |lock|
f3c0c579c0d2 commentary
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
    60
f3c0c579c0d2 commentary
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
    61
        lock := RecursionLock new.
f3c0c579c0d2 commentary
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
    62
        lock critical:[
1422
9a0b792f2953 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
    63
            Transcript showCR:'in lock ...'.
776
f3c0c579c0d2 commentary
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
    64
            lock critical:[
1422
9a0b792f2953 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
    65
                Transcript showCR:'again ...'
776
f3c0c579c0d2 commentary
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
    66
            ]
f3c0c579c0d2 commentary
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
    67
        ]
1316
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
    68
                                                                        [exEnd]
776
f3c0c579c0d2 commentary
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
    69
f3c0c579c0d2 commentary
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
    70
    in contrast to (wrong example):
1316
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
    71
                                                                        [exBegin]
776
f3c0c579c0d2 commentary
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
    72
        |lock|
f3c0c579c0d2 commentary
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
    73
f3c0c579c0d2 commentary
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
    74
        lock := Semaphore forMutualExclusion.
f3c0c579c0d2 commentary
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
    75
        lock critical:[
1422
9a0b792f2953 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
    76
            Transcript showCR:'in lock ...'.
776
f3c0c579c0d2 commentary
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
    77
            lock critical:[
f3c0c579c0d2 commentary
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
    78
                '*** never reached - deadlock because sema is already locked ***'.
f3c0c579c0d2 commentary
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
    79
                '    (press CTRL-c and abort in the debugger)'.
1422
9a0b792f2953 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
    80
                Transcript showCR:'again ...'
776
f3c0c579c0d2 commentary
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
    81
            ]
f3c0c579c0d2 commentary
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
    82
        ]
1316
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
    83
                                                                        [exEnd]
330
claus
parents:
diff changeset
    84
"
claus
parents:
diff changeset
    85
! !
claus
parents:
diff changeset
    86
claus
parents:
diff changeset
    87
!RecursionLock class methodsFor:'instance creation'!
claus
parents:
diff changeset
    88
claus
parents:
diff changeset
    89
new
claus
parents:
diff changeset
    90
    ^ self basicNew initialize
claus
parents:
diff changeset
    91
claus
parents:
diff changeset
    92
! !
claus
parents:
diff changeset
    93
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    94
!RecursionLock methodsFor:'private initialization'!
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    95
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    96
initialize
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    97
    sema := Semaphore forMutualExclusion
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    98
! !
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    99
1215
43e8e17fd9f5 added #wouldBlock
Claus Gittinger <cg@exept.de>
parents: 1186
diff changeset
   100
!RecursionLock methodsFor:'queries'!
43e8e17fd9f5 added #wouldBlock
Claus Gittinger <cg@exept.de>
parents: 1186
diff changeset
   101
43e8e17fd9f5 added #wouldBlock
Claus Gittinger <cg@exept.de>
parents: 1186
diff changeset
   102
wouldBlock
43e8e17fd9f5 added #wouldBlock
Claus Gittinger <cg@exept.de>
parents: 1186
diff changeset
   103
    "return true, if the receiver would block the activeProcess
43e8e17fd9f5 added #wouldBlock
Claus Gittinger <cg@exept.de>
parents: 1186
diff changeset
   104
     if a wait was performed. False otherwise."
43e8e17fd9f5 added #wouldBlock
Claus Gittinger <cg@exept.de>
parents: 1186
diff changeset
   105
43e8e17fd9f5 added #wouldBlock
Claus Gittinger <cg@exept.de>
parents: 1186
diff changeset
   106
    process == Processor activeProcess ifTrue:[^ false].
43e8e17fd9f5 added #wouldBlock
Claus Gittinger <cg@exept.de>
parents: 1186
diff changeset
   107
    ^ sema wouldBlock
43e8e17fd9f5 added #wouldBlock
Claus Gittinger <cg@exept.de>
parents: 1186
diff changeset
   108
43e8e17fd9f5 added #wouldBlock
Claus Gittinger <cg@exept.de>
parents: 1186
diff changeset
   109
    "Created: 18.4.1996 / 17:18:08 / cg"
43e8e17fd9f5 added #wouldBlock
Claus Gittinger <cg@exept.de>
parents: 1186
diff changeset
   110
! !
43e8e17fd9f5 added #wouldBlock
Claus Gittinger <cg@exept.de>
parents: 1186
diff changeset
   111
330
claus
parents:
diff changeset
   112
!RecursionLock methodsFor:'wait & signal'!
claus
parents:
diff changeset
   113
claus
parents:
diff changeset
   114
critical:aBlock
claus
parents:
diff changeset
   115
    "evaluate aBlock as a critical region, but do not block,
claus
parents:
diff changeset
   116
     if this lock is already held by the current process."
claus
parents:
diff changeset
   117
1216
d7cbc6eb8dd4 critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents: 1215
diff changeset
   118
    |active wasBlocked|
330
claus
parents:
diff changeset
   119
claus
parents:
diff changeset
   120
    active := Processor activeProcess.
claus
parents:
diff changeset
   121
    process == active ifTrue:[
1186
cf4c57ecabb8 Fix in critical: don't clear process when we didn't get the semaphore when unwinding.
Stefan Vogel <sv@exept.de>
parents: 776
diff changeset
   122
        aBlock value
330
claus
parents:
diff changeset
   123
    ] ifFalse:[
1216
d7cbc6eb8dd4 critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents: 1215
diff changeset
   124
        "/
d7cbc6eb8dd4 critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents: 1215
diff changeset
   125
        "/ sema wait & process := active
d7cbc6eb8dd4 critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents: 1215
diff changeset
   126
        "/ and:
d7cbc6eb8dd4 critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents: 1215
diff changeset
   127
        "/ proces := nil & sema signal
d7cbc6eb8dd4 critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents: 1215
diff changeset
   128
        "/ must both be done atomic
d7cbc6eb8dd4 critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents: 1215
diff changeset
   129
        "/ Scenario:
d7cbc6eb8dd4 critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents: 1215
diff changeset
   130
        "/   ... recLock critical
d7cbc6eb8dd4 critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents: 1215
diff changeset
   131
        "/         got lock
d7cbc6eb8dd4 critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents: 1215
diff changeset
   132
        "/         evaluated
d7cbc6eb8dd4 critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents: 1215
diff changeset
   133
        "/         set process to nil
d7cbc6eb8dd4 critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents: 1215
diff changeset
   134
        "/         -> timer interrupt
d7cbc6eb8dd4 critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents: 1215
diff changeset
   135
        "/              recLock critical in timeOut action
d7cbc6eb8dd4 critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents: 1215
diff changeset
   136
        "/              process isNil
d7cbc6eb8dd4 critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents: 1215
diff changeset
   137
        "/                 sema wait !!!!!! DEADLOCK
d7cbc6eb8dd4 critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents: 1215
diff changeset
   138
        "/
d7cbc6eb8dd4 critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents: 1215
diff changeset
   139
        wasBlocked := OperatingSystem blockInterrupts.
1186
cf4c57ecabb8 Fix in critical: don't clear process when we didn't get the semaphore when unwinding.
Stefan Vogel <sv@exept.de>
parents: 776
diff changeset
   140
        sema critical:[
cf4c57ecabb8 Fix in critical: don't clear process when we didn't get the semaphore when unwinding.
Stefan Vogel <sv@exept.de>
parents: 776
diff changeset
   141
            [
cf4c57ecabb8 Fix in critical: don't clear process when we didn't get the semaphore when unwinding.
Stefan Vogel <sv@exept.de>
parents: 776
diff changeset
   142
                process := active.
1216
d7cbc6eb8dd4 critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents: 1215
diff changeset
   143
                wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
d7cbc6eb8dd4 critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents: 1215
diff changeset
   144
                aBlock value.
d7cbc6eb8dd4 critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents: 1215
diff changeset
   145
                OperatingSystem blockInterrupts.
d7cbc6eb8dd4 critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents: 1215
diff changeset
   146
            ] valueOnUnwindDo:[
1186
cf4c57ecabb8 Fix in critical: don't clear process when we didn't get the semaphore when unwinding.
Stefan Vogel <sv@exept.de>
parents: 776
diff changeset
   147
                process := nil.
cf4c57ecabb8 Fix in critical: don't clear process when we didn't get the semaphore when unwinding.
Stefan Vogel <sv@exept.de>
parents: 776
diff changeset
   148
            ]
1216
d7cbc6eb8dd4 critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents: 1215
diff changeset
   149
        ].
d7cbc6eb8dd4 critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents: 1215
diff changeset
   150
        process := nil.
d7cbc6eb8dd4 critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents: 1215
diff changeset
   151
        wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
330
claus
parents:
diff changeset
   152
    ].
1186
cf4c57ecabb8 Fix in critical: don't clear process when we didn't get the semaphore when unwinding.
Stefan Vogel <sv@exept.de>
parents: 776
diff changeset
   153
cf4c57ecabb8 Fix in critical: don't clear process when we didn't get the semaphore when unwinding.
Stefan Vogel <sv@exept.de>
parents: 776
diff changeset
   154
    "Modified: 13.4.1996 / 00:19:31 / stefan"
1216
d7cbc6eb8dd4 critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents: 1215
diff changeset
   155
    "Modified: 18.4.1996 / 21:09:39 / cg"
330
claus
parents:
diff changeset
   156
! !
claus
parents:
diff changeset
   157
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   158
!RecursionLock class methodsFor:'documentation'!
330
claus
parents:
diff changeset
   159
1216
d7cbc6eb8dd4 critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents: 1215
diff changeset
   160
version
1422
9a0b792f2953 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
   161
    ^ '$Header: /cvs/stx/stx/libbasic/RecursionLock.st,v 1.14 1996-05-18 15:32:43 cg Exp $'
330
claus
parents:
diff changeset
   162
! !