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