OperationQueue.st
author Claus Gittinger <cg@exept.de>
Thu, 08 Nov 2007 17:34:59 +0100
changeset 1913 4c2622e92a91
parent 1307 18bf29731638
child 2974 eb6b47cfde84
permissions -rw-r--r--
*** empty log message ***
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1307
18bf29731638 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 956
diff changeset
     1
"
18bf29731638 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 956
diff changeset
     2
 COPYRIGHT (c) 2000 by eXept Software AG
18bf29731638 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 956
diff changeset
     3
              All Rights Reserved
18bf29731638 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 956
diff changeset
     4
18bf29731638 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 956
diff changeset
     5
 This software is furnished under a license and may be used
18bf29731638 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 956
diff changeset
     6
 only in accordance with the terms of that license and with the
18bf29731638 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 956
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
18bf29731638 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 956
diff changeset
     8
 be provided or otherwise made available to, or used by, any
18bf29731638 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 956
diff changeset
     9
 other person.  No title to or ownership of the software is
18bf29731638 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 956
diff changeset
    10
 hereby transferred.
18bf29731638 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 956
diff changeset
    11
"
18bf29731638 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 956
diff changeset
    12
929
6cd3e1443daa initial checkin
martin
parents:
diff changeset
    13
"{ Package: 'stx:libbasic2' }"
6cd3e1443daa initial checkin
martin
parents:
diff changeset
    14
930
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    15
Object subclass:#OperationQueue
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    16
	instanceVariableNames:'queue queueLock consumerProcess consumerProcessPriority'
929
6cd3e1443daa initial checkin
martin
parents:
diff changeset
    17
	classVariableNames:''
6cd3e1443daa initial checkin
martin
parents:
diff changeset
    18
	poolDictionaries:''
6cd3e1443daa initial checkin
martin
parents:
diff changeset
    19
	category:'Kernel-Processes'
6cd3e1443daa initial checkin
martin
parents:
diff changeset
    20
!
6cd3e1443daa initial checkin
martin
parents:
diff changeset
    21
6cd3e1443daa initial checkin
martin
parents:
diff changeset
    22
Object subclass:#OperationInQueue
956
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
    23
	instanceVariableNames:'operation operationPerformedSema operationResult exception'
929
6cd3e1443daa initial checkin
martin
parents:
diff changeset
    24
	classVariableNames:''
6cd3e1443daa initial checkin
martin
parents:
diff changeset
    25
	poolDictionaries:''
6cd3e1443daa initial checkin
martin
parents:
diff changeset
    26
	privateIn:OperationQueue
6cd3e1443daa initial checkin
martin
parents:
diff changeset
    27
!
6cd3e1443daa initial checkin
martin
parents:
diff changeset
    28
930
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    29
!OperationQueue class methodsFor:'documentation'!
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    30
1307
18bf29731638 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 956
diff changeset
    31
copyright
18bf29731638 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 956
diff changeset
    32
"
18bf29731638 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 956
diff changeset
    33
 COPYRIGHT (c) 2000 by eXept Software AG
18bf29731638 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 956
diff changeset
    34
              All Rights Reserved
18bf29731638 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 956
diff changeset
    35
18bf29731638 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 956
diff changeset
    36
 This software is furnished under a license and may be used
18bf29731638 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 956
diff changeset
    37
 only in accordance with the terms of that license and with the
18bf29731638 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 956
diff changeset
    38
 inclusion of the above copyright notice.   This software may not
18bf29731638 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 956
diff changeset
    39
 be provided or otherwise made available to, or used by, any
18bf29731638 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 956
diff changeset
    40
 other person.  No title to or ownership of the software is
18bf29731638 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 956
diff changeset
    41
 hereby transferred.
18bf29731638 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 956
diff changeset
    42
"
18bf29731638 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 956
diff changeset
    43
!
18bf29731638 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 956
diff changeset
    44
930
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    45
documentation
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    46
"
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    47
    An operationQueue allows operations (i.e. actions) to be evaluated in
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    48
    a first-come-first-serve queue like fashion. A single consumer process
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    49
    waits for orders to be enqueued and evaluates them (as they come).
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    50
    Producer processes which want to have some operation performed may
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    51
    enqueue these. Producers are suspended, until the operation is finished.
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    52
    If multiple orders which compare equal are enqueued by multiple producers,
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    53
    the operation is only evaluated once, and both producers will wake up when
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    54
    the operation has finished.
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    55
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    56
    [author:]
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    57
        Martin Walser
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    58
        Claus Gittinger
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    59
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    60
    [see also:]
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    61
        SharedQueue
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    62
        Queue
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    63
"
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    64
!
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    65
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    66
examples
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    67
"
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    68
    two orders; done sequentially:
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    69
                                                                                        [exBegin]
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    70
        |opQ|
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    71
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    72
        opQ := OperationQueue new.
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    73
        opQ scheduleOperation:[ Transcript showCR:'hello world-1'].
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    74
        opQ scheduleOperation:[ Transcript showCR:'hello world-2'].
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    75
                                                                                        [exBegin]
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    76
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    77
    only 10 outputs (same operation entered by multiple producers):
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    78
                                                                                        [exBegin]
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    79
        |p1 p2 p3 opQ order|
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    80
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    81
        opQ := OperationQueue new.
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    82
        order := [ Transcript showCR:'hello world '].
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    83
        p1 := [
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    84
                1 to:10 do:[:i |
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    85
                    opQ scheduleOperation:order.
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    86
                ]
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    87
              ] fork.
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    88
        p2 := [
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    89
                1 to:10 do:[:i |
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    90
                    opQ scheduleOperation:order.
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    91
                ]
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    92
              ] fork.
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    93
        p3 := [
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    94
                1 to:10 do:[:i |
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    95
                    opQ scheduleOperation:order.
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    96
                ]
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    97
              ] fork.
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    98
                                                                                        [exBegin]
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    99
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   100
"
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   101
! !
929
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   102
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   103
!OperationQueue class methodsFor:'instance creation'!
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   104
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   105
new
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   106
    ^ super new initializeLock
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   107
!
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   108
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   109
new:n
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   110
    ^ (super new:n) initializeLock
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   111
! !
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   112
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   113
!OperationQueue methodsFor:'accessing'!
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   114
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   115
consumerProcess
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   116
    "return the value of the instance variable 'consumerProcess' (automatically generated)"
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   117
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   118
    ^ consumerProcess
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   119
!
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   120
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   121
consumerProcessPriority
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   122
    "return the value of the instance variable 'consumerProcessPriority' (automatically generated)"
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   123
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   124
    ^ consumerProcessPriority
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   125
!
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   126
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   127
consumerProcessPriority:something
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   128
    "set the value of the instance variable 'consumerProcessPriority' (automatically generated)"
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   129
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   130
    consumerProcessPriority := something.
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   131
! !
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   132
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   133
!OperationQueue methodsFor:'consumer'!
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   134
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   135
fetchNextOperationAndExecute
930
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   136
    "dequeue the next order, evaluate it and wake up waiters"
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   137
929
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   138
    |opInQ theOperation rslt|
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   139
930
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   140
    opInQ := queue next.
929
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   141
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   142
    theOperation := opInQ operation.
937
ca7e6563ada5 Exception handling
martin
parents: 935
diff changeset
   143
    Object abortSignal handle:[:ex |
956
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   144
        Transcript showCR:'operation aborted'.
937
ca7e6563ada5 Exception handling
martin
parents: 935
diff changeset
   145
        opInQ operationResult:nil.
ca7e6563ada5 Exception handling
martin
parents: 935
diff changeset
   146
    ] do:[
ca7e6563ada5 Exception handling
martin
parents: 935
diff changeset
   147
        Object errorSignal handle:[:ex |
956
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   148
            |ex2|
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   149
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   150
            Transcript showCR:'operation error: ', ex errorString.
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   151
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   152
            ex2 := ex shallowCopy.
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   153
            ex2 suspendedContext:(self copyContextChain:ex suspendedContext).
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   154
            opInQ exception:ex2.
937
ca7e6563ada5 Exception handling
martin
parents: 935
diff changeset
   155
            opInQ operationResult:nil.
ca7e6563ada5 Exception handling
martin
parents: 935
diff changeset
   156
        ] do:[
ca7e6563ada5 Exception handling
martin
parents: 935
diff changeset
   157
            rslt := theOperation value.
ca7e6563ada5 Exception handling
martin
parents: 935
diff changeset
   158
        ].
ca7e6563ada5 Exception handling
martin
parents: 935
diff changeset
   159
    ].
929
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   160
    opInQ operationResult:rslt.
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   161
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   162
    opInQ operationPerformedSema signalForAll.
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   163
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   164
    [
930
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   165
        queue isEmpty ifTrue:[
929
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   166
            self stopConsumerProcess
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   167
        ]
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   168
    ] valueUninterruptably
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   169
!
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   170
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   171
startConsumerProcess
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   172
    [
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   173
        consumerProcess isNil ifTrue:[
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   174
            consumerProcessPriority := consumerProcessPriority ? (Processor userSchedulingPriority).
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   175
            consumerProcess := [
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   176
                [true] whileTrue:[
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   177
                    self fetchNextOperationAndExecute.
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   178
                ].
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   179
            ] newProcess.
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   180
            consumerProcess priority:consumerProcessPriority.
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   181
            consumerProcess name:'Op-Q consumer'.
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   182
            consumerProcess resume.
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   183
        ].
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   184
    ] valueUninterruptably
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   185
!
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   186
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   187
stopConsumerProcess
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   188
    [
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   189
        |p|
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   190
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   191
        (p := consumerProcess) notNil ifTrue:[
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   192
            consumerProcess := nil.
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   193
            p terminate
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   194
        ].
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   195
    ] valueUninterruptably
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   196
! !
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   197
956
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   198
!OperationQueue methodsFor:'debugging support'!
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   199
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   200
copyContextChain:aContext
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   201
    "dequeue the next order, evaluate it and wake up waiters"
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   202
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   203
    |copy|
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   204
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   205
    copy := aContext shallowCopy.
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   206
    aContext sender notNil ifTrue:[
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   207
        copy setSender:(self copyContextChain:aContext sender)
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   208
    ].
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   209
    ^ copy
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   210
!
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   211
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   212
linkContextChain:aConsumerChain
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   213
    "for debugging - concatenate aConsumerChain to my own context chain (to make debugging easier)"
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   214
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   215
    |c|
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   216
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   217
    c := aConsumerChain.
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   218
    [c sender notNil and:[c methodHome selector ~~ #fetchNextOperationAndExecute]] whileTrue:[
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   219
        c := c sender.
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   220
    ].
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   221
    c setSender:(thisContext sender).
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   222
! !
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   223
929
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   224
!OperationQueue methodsFor:'initialization'!
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   225
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   226
initializeLock
930
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   227
    queue := SharedQueue new.
929
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   228
    queueLock := Semaphore forMutualExclusion.
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   229
! !
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   230
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   231
!OperationQueue methodsFor:'producer'!
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   232
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   233
scheduleOperation:anotherOperation
930
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   234
    "enqueue an order (something that understands #value) to the op-queue;
929
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   235
     wait until the operation has performed (#value been sent),
930
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   236
     return the result of the #value send.
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   237
     If a similar order is already in the queue, wait for that one to finish."
929
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   238
935
c4aa5a3080f5 *** empty log message ***
martin
parents: 930
diff changeset
   239
    ^ self scheduleOperation:anotherOperation asynchronous:false
c4aa5a3080f5 *** empty log message ***
martin
parents: 930
diff changeset
   240
!
c4aa5a3080f5 *** empty log message ***
martin
parents: 930
diff changeset
   241
c4aa5a3080f5 *** empty log message ***
martin
parents: 930
diff changeset
   242
scheduleOperation:anotherOperation asynchronous:asynchronous
c4aa5a3080f5 *** empty log message ***
martin
parents: 930
diff changeset
   243
    "enqueue an order (something that understands #value) to the op-queue;
c4aa5a3080f5 *** empty log message ***
martin
parents: 930
diff changeset
   244
     if asynchronous is false, wait until the operation has performed (#value been sent),
c4aa5a3080f5 *** empty log message ***
martin
parents: 930
diff changeset
   245
     return the result of the #value send.
c4aa5a3080f5 *** empty log message ***
martin
parents: 930
diff changeset
   246
     If a similar order is already in the queue, wait for that one to finish.
c4aa5a3080f5 *** empty log message ***
martin
parents: 930
diff changeset
   247
     If asynchronous is true, do not wait (but also: do not return a return value)"
c4aa5a3080f5 *** empty log message ***
martin
parents: 930
diff changeset
   248
956
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   249
    |myOpInQ ex|
929
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   250
930
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   251
    queueLock critical:[
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   252
        "/ operation already in queue ?
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   253
        queue withAccessLockedDo:[
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   254
            myOpInQ := nil.
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   255
            queue do:[:eachOpInQ |
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   256
               anotherOperation = eachOpInQ operation ifTrue:[
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   257
                    myOpInQ := eachOpInQ
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   258
               ]
929
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   259
            ].
930
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   260
            "/ if not, create a new one and enqueue
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   261
            myOpInQ isNil ifTrue:[
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   262
                myOpInQ := OperationInQueue new.
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   263
                myOpInQ operationPerformedSema:(Semaphore new).
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   264
                myOpInQ operation:anotherOperation.
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   265
                [
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   266
                    queue nextPut:myOpInQ.
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   267
                    consumerProcess isNil ifTrue:[
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   268
                        self startConsumerProcess
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   269
                    ].
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   270
                ] valueUninterruptably.
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   271
            ].
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   272
        ].
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   273
    ]. 
929
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   274
935
c4aa5a3080f5 *** empty log message ***
martin
parents: 930
diff changeset
   275
    asynchronous ifTrue:[
c4aa5a3080f5 *** empty log message ***
martin
parents: 930
diff changeset
   276
        ^ nil
c4aa5a3080f5 *** empty log message ***
martin
parents: 930
diff changeset
   277
    ].
c4aa5a3080f5 *** empty log message ***
martin
parents: 930
diff changeset
   278
930
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   279
    "/ wait for the operation to finish
929
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   280
    myOpInQ operationPerformedSema wait.
956
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   281
    (ex := myOpInQ exception) notNil ifTrue:[
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   282
        "/ trick: makes calling chain look as if the error happended here
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   283
        "/ (in reality, it happended in the consumer-process).
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   284
        self linkContextChain:ex suspendedContext.
929
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   285
956
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   286
        ^ ex signal raiseErrorString:('asyncronous operation error:' , ex errorString) in:ex suspendedContext
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   287
    ].
930
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   288
    "/ now, the operation has been performed - return its result
929
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   289
    ^ myOpInQ operationResult
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   290
! !
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   291
930
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   292
!OperationQueue methodsFor:'queries'!
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   293
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   294
size
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   295
    "return the number of operations in the queue"
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   296
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   297
    ^ queue size
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   298
! !
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   299
929
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   300
!OperationQueue::OperationInQueue methodsFor:'accessing'!
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   301
956
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   302
exception
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   303
    "return the value of the instance variable 'exception' (automatically generated)"
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   304
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   305
    ^ exception
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   306
!
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   307
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   308
exception:something
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   309
    "set the value of the instance variable 'exception' (automatically generated)"
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   310
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   311
    exception := something.
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   312
!
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   313
929
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   314
operation
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   315
    "return the value of the instance variable 'operation' (automatically generated)"
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   316
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   317
    ^ operation
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   318
!
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   319
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   320
operation:something
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   321
    "set the value of the instance variable 'operation' (automatically generated)"
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   322
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   323
    operation := something.
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   324
!
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   325
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   326
operationPerformedSema
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   327
    "return the value of the instance variable 'operationPerformedSema' (automatically generated)"
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   328
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   329
    ^ operationPerformedSema
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   330
!
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   331
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   332
operationPerformedSema:something
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   333
    "set the value of the instance variable 'operationPerformedSema' (automatically generated)"
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   334
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   335
    operationPerformedSema := something.
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   336
!
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   337
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   338
operationResult
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   339
    "return the value of the instance variable 'operationResult' (automatically generated)"
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   340
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   341
    ^ operationResult
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   342
!
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   343
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   344
operationResult:something
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   345
    "set the value of the instance variable 'operationResult' (automatically generated)"
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   346
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   347
    operationResult := something.
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   348
! !
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   349
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   350
!OperationQueue class methodsFor:'documentation'!
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   351
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   352
version
1307
18bf29731638 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 956
diff changeset
   353
    ^ '$Header: /cvs/stx/stx/libbasic2/OperationQueue.st,v 1.6 2003-08-29 19:23:43 cg Exp $'
929
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   354
! !