Monitor.st
author Claus Gittinger <cg@exept.de>
Wed, 30 Dec 1998 19:32:45 +0100
changeset 716 242781d82881
parent 710 b17b877ac7b4
child 718 ba2260d7863f
permissions -rw-r--r--
checkin from browser
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
272
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     1
"
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     2
 COPYRIGHT (c) 1996 by Claus Gittinger
599
83af18019cc9 care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
     3
	      All Rights Reserved
272
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     4
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     5
 This software is furnished under a license and may be used
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     6
 only in accordance with the terms of that license and with the
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     8
 be provided or otherwise made available to, or used by, any
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     9
 other person.  No title to or ownership of the software is
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    10
 hereby transferred.
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    11
"
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    12
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    13
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    14
Object subclass:#Monitor
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    15
	instanceVariableNames:'owningProcess sema count'
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    16
	classVariableNames:''
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    17
	poolDictionaries:''
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    18
	category:'Kernel-Processes'
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    19
!
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    20
560
feae8d3ae643 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 485
diff changeset
    21
!Monitor class methodsFor:'documentation'!
272
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    22
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    23
copyright
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    24
"
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    25
 COPYRIGHT (c) 1996 by Claus Gittinger
599
83af18019cc9 care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    26
	      All Rights Reserved
272
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    27
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    28
 This software is furnished under a license and may be used
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    29
 only in accordance with the terms of that license and with the
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    30
 inclusion of the above copyright notice.   This software may not
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    31
 be provided or otherwise made available to, or used by, any
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    32
 other person.  No title to or ownership of the software is
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    33
 hereby transferred.
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    34
"
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    35
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    36
!
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    37
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    38
documentation
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    39
"
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    40
    Monitors - functionality much like RecursionLocks, but not
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    41
    block based.
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    42
    Monitors are not unwind-save (i.e. a return or unwind while a
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    43
    monitor is locked, will lead to a deadlock situation).
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    44
    You have to care for unwinds yourself.
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    45
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    46
    Notice:
599
83af18019cc9 care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    47
	This is an experimental demo class - there is no WARRANTY.
83af18019cc9 care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    48
	Smalltalkers should use Semaphores and RecursionLocks, which
83af18019cc9 care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    49
	are unwind-save.
272
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    50
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    51
    [see also:]
599
83af18019cc9 care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    52
	RecursionLock Semaphore Delay SharedQueue
83af18019cc9 care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    53
	Block
272
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    54
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    55
    [author:]
599
83af18019cc9 care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    56
	Claus Gittinger
272
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    57
"
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    58
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    59
!
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    60
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    61
examples
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    62
"
642
53c27655489b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
    63
						[exBegin]
599
83af18019cc9 care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    64
	|mon p1 p2 p3|
272
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    65
599
83af18019cc9 care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    66
	mon := Monitor new.
272
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    67
599
83af18019cc9 care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    68
	p1 := [
83af18019cc9 care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    69
	     10 timesRepeat:[
83af18019cc9 care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    70
		 Delay waitForSeconds:0.3.
83af18019cc9 care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    71
		 mon enter.
83af18019cc9 care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    72
		 'p1 got it' printNL.
83af18019cc9 care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    73
		 Delay waitForSeconds:0.3.
83af18019cc9 care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    74
		 'p1 leaves' printNL.
83af18019cc9 care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    75
		 mon exit
83af18019cc9 care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    76
	     ]
83af18019cc9 care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    77
	] fork.
272
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    78
599
83af18019cc9 care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    79
	p2 := [
83af18019cc9 care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    80
	     20 timesRepeat:[
83af18019cc9 care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    81
		 Delay waitForSeconds:0.2.
83af18019cc9 care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    82
		 mon enter.
83af18019cc9 care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    83
		 'p2 got it' printNL.
83af18019cc9 care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    84
		 Delay waitForSeconds:0.2.
83af18019cc9 care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    85
		 'p2 leaves' printNL.
83af18019cc9 care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    86
		 mon exit
83af18019cc9 care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    87
	     ]
83af18019cc9 care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    88
	] fork.
272
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    89
599
83af18019cc9 care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    90
	p3 := [
83af18019cc9 care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    91
	     30 timesRepeat:[
83af18019cc9 care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    92
		 Delay waitForSeconds:0.1.
83af18019cc9 care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    93
		 mon enter.
83af18019cc9 care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    94
		 'p3 got it' printNL.
83af18019cc9 care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    95
		 Delay waitForSeconds:0.1.
83af18019cc9 care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    96
		 'p3 leaves' printNL.
83af18019cc9 care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    97
		 mon exit
83af18019cc9 care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    98
	     ]
83af18019cc9 care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    99
	] fork.
642
53c27655489b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   100
						[exEnd]
272
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   101
"
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   102
! !
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   103
560
feae8d3ae643 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 485
diff changeset
   104
!Monitor class methodsFor:'instance creation'!
272
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   105
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   106
new
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   107
    ^ self basicNew initialize
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   108
! !
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   109
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   110
!Monitor methodsFor:'enter & leave'!
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   111
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   112
enter
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   113
    "enter the monitor"
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   114
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   115
    |thisProcess wasBlocked|
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   116
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   117
    thisProcess := Processor activeProcess.
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   118
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   119
    "
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   120
     this works only since interrupts are only serviced at 
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   121
     message send and method-return time ....
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   122
     If you add a message send into the ifTrue:-block, things will
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   123
     go mad ... (especially be careful when adding a debugPrint-here)
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   124
    "
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   125
    owningProcess isNil ifTrue:[
699
01af80ac55c5 dont halt when entering a monitor owned by a dead process
Claus Gittinger <cg@exept.de>
parents: 642
diff changeset
   126
        count := 1.
01af80ac55c5 dont halt when entering a monitor owned by a dead process
Claus Gittinger <cg@exept.de>
parents: 642
diff changeset
   127
        owningProcess := thisProcess.
01af80ac55c5 dont halt when entering a monitor owned by a dead process
Claus Gittinger <cg@exept.de>
parents: 642
diff changeset
   128
        ^ self
272
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   129
    ].
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   130
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   131
    owningProcess == thisProcess ifTrue:[
699
01af80ac55c5 dont halt when entering a monitor owned by a dead process
Claus Gittinger <cg@exept.de>
parents: 642
diff changeset
   132
        count := count + 1.
01af80ac55c5 dont halt when entering a monitor owned by a dead process
Claus Gittinger <cg@exept.de>
parents: 642
diff changeset
   133
        ^ self
599
83af18019cc9 care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   134
    ].
83af18019cc9 care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   135
83af18019cc9 care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   136
    owningProcess isDead ifTrue:[
699
01af80ac55c5 dont halt when entering a monitor owned by a dead process
Claus Gittinger <cg@exept.de>
parents: 642
diff changeset
   137
        'Java [warning]: entering monitor owned by dead process' infoPrintCR.
01af80ac55c5 dont halt when entering a monitor owned by a dead process
Claus Gittinger <cg@exept.de>
parents: 642
diff changeset
   138
        "/ self halt.
01af80ac55c5 dont halt when entering a monitor owned by a dead process
Claus Gittinger <cg@exept.de>
parents: 642
diff changeset
   139
        owningProcess := thisProcess.
01af80ac55c5 dont halt when entering a monitor owned by a dead process
Claus Gittinger <cg@exept.de>
parents: 642
diff changeset
   140
        count := 1.
01af80ac55c5 dont halt when entering a monitor owned by a dead process
Claus Gittinger <cg@exept.de>
parents: 642
diff changeset
   141
        ^ self
272
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   142
    ].
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   143
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   144
    wasBlocked := OperatingSystem blockInterrupts.
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   145
    [
699
01af80ac55c5 dont halt when entering a monitor owned by a dead process
Claus Gittinger <cg@exept.de>
parents: 642
diff changeset
   146
        [owningProcess notNil] whileTrue:[
01af80ac55c5 dont halt when entering a monitor owned by a dead process
Claus Gittinger <cg@exept.de>
parents: 642
diff changeset
   147
            thisProcess state:#monWait.
709
5a8ddf63e001 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
   148
            (sema waitWithTimeout:10) isNil ifTrue:[
5a8ddf63e001 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
   149
                (owningProcess notNil and:[owningProcess isDead]) ifTrue:[
5a8ddf63e001 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
   150
                    'Monitor [warning]: aquire monitor from dead process' errorPrintCR.
5a8ddf63e001 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
   151
                    owningProcess := nil.
5a8ddf63e001 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
   152
                ]
5a8ddf63e001 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
   153
            ]
699
01af80ac55c5 dont halt when entering a monitor owned by a dead process
Claus Gittinger <cg@exept.de>
parents: 642
diff changeset
   154
        ].
01af80ac55c5 dont halt when entering a monitor owned by a dead process
Claus Gittinger <cg@exept.de>
parents: 642
diff changeset
   155
        count := 1.
01af80ac55c5 dont halt when entering a monitor owned by a dead process
Claus Gittinger <cg@exept.de>
parents: 642
diff changeset
   156
        owningProcess := thisProcess.
716
242781d82881 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 710
diff changeset
   157
    ] valueNowOrOnUnwindDo:[
699
01af80ac55c5 dont halt when entering a monitor owned by a dead process
Claus Gittinger <cg@exept.de>
parents: 642
diff changeset
   158
        wasBlocked ifFalse:[OperatingSystem unblockInterrupts]
272
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   159
    ]
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   160
716
242781d82881 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 710
diff changeset
   161
    "Modified: / 30.12.1998 / 19:32:24 / cg"
272
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   162
!
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   163
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   164
exit
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   165
    "exit the monitor"
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   166
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   167
    |thisProcess|
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   168
405
1541a9ccda16 allow exit if already free
Claus Gittinger <cg@exept.de>
parents: 398
diff changeset
   169
    count == 0 ifTrue:[
599
83af18019cc9 care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   170
	'MONITOR [info]: already left' errorPrintCR.
83af18019cc9 care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   171
	^ self
405
1541a9ccda16 allow exit if already free
Claus Gittinger <cg@exept.de>
parents: 398
diff changeset
   172
    ].
1541a9ccda16 allow exit if already free
Claus Gittinger <cg@exept.de>
parents: 398
diff changeset
   173
272
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   174
    thisProcess := Processor activeProcess.
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   175
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   176
    "
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   177
     this works only since interrupts are only serviced at 
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   178
     message send and method-return time ....
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   179
     If you add a message send into the ifTrue:-block, things will
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   180
     go mad ... (especially be careful when adding a debugPrint-here)
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   181
    "
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   182
    owningProcess ~~ thisProcess ifTrue:[
599
83af18019cc9 care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   183
	self halt:'invalid exit'
272
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   184
    ].
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   185
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   186
    count := count - 1.
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   187
    count ~~ 0 ifTrue:[ ^ self].
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   188
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   189
    owningProcess := nil.
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   190
    sema signal.
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   191
560
feae8d3ae643 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 485
diff changeset
   192
    "Modified: 21.8.1997 / 16:44:17 / cg"
272
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   193
! !
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   194
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   195
!Monitor methodsFor:'initialization'!
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   196
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   197
initialize
485
fe9c2ff10461 added friendly semaphore names
Claus Gittinger <cg@exept.de>
parents: 405
diff changeset
   198
    sema := Semaphore new name:'monitorSema'.
272
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   199
    count := 0.
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   200
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   201
    "Created: 3.5.1996 / 17:24:59 / cg"
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   202
! !
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   203
273
c66a8556b40f added isFree-query
Claus Gittinger <cg@exept.de>
parents: 272
diff changeset
   204
!Monitor methodsFor:'queries'!
c66a8556b40f added isFree-query
Claus Gittinger <cg@exept.de>
parents: 272
diff changeset
   205
c66a8556b40f added isFree-query
Claus Gittinger <cg@exept.de>
parents: 272
diff changeset
   206
isFree
c66a8556b40f added isFree-query
Claus Gittinger <cg@exept.de>
parents: 272
diff changeset
   207
    "return true, if the monitor is free
c66a8556b40f added isFree-query
Claus Gittinger <cg@exept.de>
parents: 272
diff changeset
   208
     (i.e. noone waits and count is zero)"
c66a8556b40f added isFree-query
Claus Gittinger <cg@exept.de>
parents: 272
diff changeset
   209
c66a8556b40f added isFree-query
Claus Gittinger <cg@exept.de>
parents: 272
diff changeset
   210
    |wasBlocked ret|
c66a8556b40f added isFree-query
Claus Gittinger <cg@exept.de>
parents: 272
diff changeset
   211
c66a8556b40f added isFree-query
Claus Gittinger <cg@exept.de>
parents: 272
diff changeset
   212
    ret := true.
c66a8556b40f added isFree-query
Claus Gittinger <cg@exept.de>
parents: 272
diff changeset
   213
c66a8556b40f added isFree-query
Claus Gittinger <cg@exept.de>
parents: 272
diff changeset
   214
    wasBlocked := OperatingSystem blockInterrupts.
c66a8556b40f added isFree-query
Claus Gittinger <cg@exept.de>
parents: 272
diff changeset
   215
    owningProcess notNil ifTrue:[
599
83af18019cc9 care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   216
	ret := false
273
c66a8556b40f added isFree-query
Claus Gittinger <cg@exept.de>
parents: 272
diff changeset
   217
    ] ifFalse:[
599
83af18019cc9 care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   218
	sema numberOfWaitingProcesses ~~ 0 ifTrue:[ret := false].
273
c66a8556b40f added isFree-query
Claus Gittinger <cg@exept.de>
parents: 272
diff changeset
   219
    ].
c66a8556b40f added isFree-query
Claus Gittinger <cg@exept.de>
parents: 272
diff changeset
   220
    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
c66a8556b40f added isFree-query
Claus Gittinger <cg@exept.de>
parents: 272
diff changeset
   221
    ^ ret.
c66a8556b40f added isFree-query
Claus Gittinger <cg@exept.de>
parents: 272
diff changeset
   222
c66a8556b40f added isFree-query
Claus Gittinger <cg@exept.de>
parents: 272
diff changeset
   223
    "Created: 3.5.1996 / 18:08:38 / cg"
710
b17b877ac7b4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 709
diff changeset
   224
!
b17b877ac7b4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 709
diff changeset
   225
b17b877ac7b4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 709
diff changeset
   226
owningProcess
b17b877ac7b4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 709
diff changeset
   227
    "return the monitors owner; or nil, if its free"
b17b877ac7b4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 709
diff changeset
   228
b17b877ac7b4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 709
diff changeset
   229
    ^ owningProcess
b17b877ac7b4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 709
diff changeset
   230
b17b877ac7b4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 709
diff changeset
   231
    "Created: / 11.12.1998 / 13:43:39 / cg"
273
c66a8556b40f added isFree-query
Claus Gittinger <cg@exept.de>
parents: 272
diff changeset
   232
! !
c66a8556b40f added isFree-query
Claus Gittinger <cg@exept.de>
parents: 272
diff changeset
   233
560
feae8d3ae643 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 485
diff changeset
   234
!Monitor class methodsFor:'documentation'!
272
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   235
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   236
version
716
242781d82881 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 710
diff changeset
   237
    ^ '$Header: /cvs/stx/stx/libbasic2/Monitor.st,v 1.13 1998-12-30 18:32:45 cg Exp $'
272
32ebd03ddb58 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   238
! !