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