OperationQueue.st
author Claus Gittinger <cg@exept.de>
Sat, 02 May 2020 21:40:13 +0200
changeset 5476 7355a4b11cb6
parent 4943 e8327124ac63
permissions -rw-r--r--
#FEATURE by cg class: Socket class added: #newTCPclientToHost:port:domain:domainOrder:withTimeout: changed: #newTCPclientToHost:port:domain:withTimeout:
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4943
e8327124ac63 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4368
diff changeset
     1
"{ Encoding: utf8 }"
e8327124ac63 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4368
diff changeset
     2
1307
18bf29731638 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 956
diff changeset
     3
"
18bf29731638 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 956
diff changeset
     4
 COPYRIGHT (c) 2000 by eXept Software AG
18bf29731638 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 956
diff changeset
     5
              All Rights Reserved
18bf29731638 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 956
diff changeset
     6
18bf29731638 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 956
diff changeset
     7
 This software is furnished under a license and may be used
18bf29731638 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 956
diff changeset
     8
 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
     9
 inclusion of the above copyright notice.   This software may not
18bf29731638 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 956
diff changeset
    10
 be provided or otherwise made available to, or used by, any
18bf29731638 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 956
diff changeset
    11
 other person.  No title to or ownership of the software is
18bf29731638 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 956
diff changeset
    12
 hereby transferred.
18bf29731638 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 956
diff changeset
    13
"
929
6cd3e1443daa initial checkin
martin
parents:
diff changeset
    14
"{ Package: 'stx:libbasic2' }"
6cd3e1443daa initial checkin
martin
parents:
diff changeset
    15
4368
6596738636b2 #DOCUMENTATION by mawalch
mawalch
parents: 2994
diff changeset
    16
"{ NameSpace: Smalltalk }"
6596738636b2 #DOCUMENTATION by mawalch
mawalch
parents: 2994
diff changeset
    17
930
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    18
Object subclass:#OperationQueue
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    19
	instanceVariableNames:'queue queueLock consumerProcess consumerProcessPriority'
929
6cd3e1443daa initial checkin
martin
parents:
diff changeset
    20
	classVariableNames:''
6cd3e1443daa initial checkin
martin
parents:
diff changeset
    21
	poolDictionaries:''
6cd3e1443daa initial checkin
martin
parents:
diff changeset
    22
	category:'Kernel-Processes'
6cd3e1443daa initial checkin
martin
parents:
diff changeset
    23
!
6cd3e1443daa initial checkin
martin
parents:
diff changeset
    24
6cd3e1443daa initial checkin
martin
parents:
diff changeset
    25
Object subclass:#OperationInQueue
956
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
    26
	instanceVariableNames:'operation operationPerformedSema operationResult exception'
929
6cd3e1443daa initial checkin
martin
parents:
diff changeset
    27
	classVariableNames:''
6cd3e1443daa initial checkin
martin
parents:
diff changeset
    28
	poolDictionaries:''
6cd3e1443daa initial checkin
martin
parents:
diff changeset
    29
	privateIn:OperationQueue
6cd3e1443daa initial checkin
martin
parents:
diff changeset
    30
!
6cd3e1443daa initial checkin
martin
parents:
diff changeset
    31
930
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    32
!OperationQueue class methodsFor:'documentation'!
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    33
1307
18bf29731638 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 956
diff changeset
    34
copyright
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
 COPYRIGHT (c) 2000 by eXept Software AG
18bf29731638 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 956
diff changeset
    37
              All Rights Reserved
18bf29731638 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 956
diff changeset
    38
18bf29731638 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 956
diff changeset
    39
 This software is furnished under a license and may be used
18bf29731638 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 956
diff changeset
    40
 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
    41
 inclusion of the above copyright notice.   This software may not
18bf29731638 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 956
diff changeset
    42
 be provided or otherwise made available to, or used by, any
18bf29731638 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 956
diff changeset
    43
 other person.  No title to or ownership of the software is
18bf29731638 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 956
diff changeset
    44
 hereby transferred.
18bf29731638 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 956
diff changeset
    45
"
18bf29731638 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 956
diff changeset
    46
!
18bf29731638 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 956
diff changeset
    47
930
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    48
documentation
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    49
"
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    50
    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
    51
    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
    52
    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
    53
    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
    54
    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
    55
    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
    56
    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
    57
    the operation has finished.
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
    [author:]
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    60
        Martin Walser
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    61
        Claus Gittinger
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
    [see also:]
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    64
        SharedQueue
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    65
        Queue
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
!
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
examples
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
    two orders; done sequentially:
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    72
                                                                                        [exBegin]
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    73
        |opQ|
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    74
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    75
        opQ := OperationQueue new.
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    76
        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
    77
        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
    78
                                                                                        [exBegin]
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
    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
    81
                                                                                        [exBegin]
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    82
        |p1 p2 p3 opQ order|
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    83
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    84
        opQ := OperationQueue new.
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    85
        order := [ Transcript showCR:'hello world '].
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    86
        p1 := [
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    87
                1 to:10 do:[:i |
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    88
                    opQ scheduleOperation:order.
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    89
                ]
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    90
              ] fork.
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    91
        p2 := [
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    92
                1 to:10 do:[:i |
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    93
                    opQ scheduleOperation:order.
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    94
                ]
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    95
              ] fork.
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    96
        p3 := [
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    97
                1 to:10 do:[:i |
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
    98
                    opQ scheduleOperation:order.
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
              ] fork.
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   101
                                                                                        [exBegin]
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   102
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   103
"
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   104
! !
929
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   105
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   106
!OperationQueue class methodsFor:'instance creation'!
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   107
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   108
new
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   109
    ^ super new 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
new:n
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   113
    ^ (super new:n) initializeLock
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   114
! !
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   115
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   116
!OperationQueue methodsFor:'accessing'!
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
    "return the value of the instance variable 'consumerProcess' (automatically generated)"
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   120
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   121
    ^ consumerProcess
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   122
!
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
    "return the value of the instance variable 'consumerProcessPriority' (automatically generated)"
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   126
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   127
    ^ consumerProcessPriority
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   128
!
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
    "set the value of the instance variable 'consumerProcessPriority' (automatically generated)"
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   132
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   133
    consumerProcessPriority := something.
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   134
! !
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   135
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   136
!OperationQueue methodsFor:'consumer'!
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   137
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   138
fetchNextOperationAndExecute
930
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   139
    "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
   140
929
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   141
    |opInQ theOperation rslt|
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   142
930
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   143
    opInQ := queue next.
929
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   144
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   145
    theOperation := opInQ operation.
2994
81c2f934a7cd exception access
Claus Gittinger <cg@exept.de>
parents: 2975
diff changeset
   146
    AbortOperationRequest handle:[:ex |
956
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   147
        Transcript showCR:'operation aborted'.
937
ca7e6563ada5 Exception handling
martin
parents: 935
diff changeset
   148
        opInQ operationResult:nil.
ca7e6563ada5 Exception handling
martin
parents: 935
diff changeset
   149
    ] do:[
2994
81c2f934a7cd exception access
Claus Gittinger <cg@exept.de>
parents: 2975
diff changeset
   150
        Error handle:[:ex |
956
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   151
            |ex2|
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
            Transcript showCR:'operation error: ', ex errorString.
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   154
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   155
            ex2 := ex shallowCopy.
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   156
            ex2 suspendedContext:(self copyContextChain:ex suspendedContext).
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   157
            opInQ exception:ex2.
937
ca7e6563ada5 Exception handling
martin
parents: 935
diff changeset
   158
            opInQ operationResult:nil.
ca7e6563ada5 Exception handling
martin
parents: 935
diff changeset
   159
        ] do:[
ca7e6563ada5 Exception handling
martin
parents: 935
diff changeset
   160
            rslt := theOperation value.
ca7e6563ada5 Exception handling
martin
parents: 935
diff changeset
   161
        ].
ca7e6563ada5 Exception handling
martin
parents: 935
diff changeset
   162
    ].
929
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   163
    opInQ operationResult:rslt.
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   164
4943
e8327124ac63 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4368
diff changeset
   165
    opInQ operationPerformedSema signal.
929
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   166
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   167
    [
930
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   168
        queue isEmpty ifTrue:[
929
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   169
            self stopConsumerProcess
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   170
        ]
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   171
    ] valueUninterruptably
4943
e8327124ac63 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4368
diff changeset
   172
e8327124ac63 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4368
diff changeset
   173
    "Modified: / 16-05-2019 / 14:52:55 / Stefan Vogel"
929
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   174
!
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   175
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   176
startConsumerProcess
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   177
    [
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   178
        consumerProcess isNil ifTrue:[
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   179
            consumerProcessPriority := consumerProcessPriority ? (Processor userSchedulingPriority).
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   180
            consumerProcess := [
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   181
                [true] whileTrue:[
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   182
                    self fetchNextOperationAndExecute.
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   183
                ].
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   184
            ] newProcess.
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   185
            consumerProcess priority:consumerProcessPriority.
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   186
            consumerProcess name:'Op-Q consumer'.
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   187
            consumerProcess resume.
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   188
        ].
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   189
    ] valueUninterruptably
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   190
!
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   191
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   192
stopConsumerProcess
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   193
    [
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   194
        |p|
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   195
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   196
        (p := consumerProcess) notNil ifTrue:[
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   197
            consumerProcess := nil.
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   198
            p terminate
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   199
        ].
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   200
    ] valueUninterruptably
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   201
! !
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   202
956
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   203
!OperationQueue methodsFor:'debugging support'!
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
copyContextChain:aContext
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   206
    "dequeue the next order, evaluate it and wake up waiters"
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
    copy := aContext shallowCopy.
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   211
    aContext sender notNil ifTrue:[
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   212
        copy setSender:(self copyContextChain:aContext sender)
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
    ^ copy
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
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   217
linkContextChain:aConsumerChain
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   218
    "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
   219
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   220
    |c|
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 := aConsumerChain.
2974
eb6b47cfde84 class: OperationQueue
Claus Gittinger <cg@exept.de>
parents: 1307
diff changeset
   223
    [c sender notNil and:[c methodSelector ~~ #fetchNextOperationAndExecute]] whileTrue:[
956
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   224
        c := c sender.
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   225
    ].
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   226
    c setSender:(thisContext sender).
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   227
! !
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   228
929
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   229
!OperationQueue methodsFor:'initialization'!
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   230
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   231
initializeLock
930
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   232
    queue := SharedQueue new.
929
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   233
    queueLock := Semaphore forMutualExclusion.
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   234
! !
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   235
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   236
!OperationQueue methodsFor:'producer'!
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   237
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   238
scheduleOperation:anotherOperation
930
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   239
    "enqueue an order (something that understands #value) to the op-queue;
929
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   240
     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
   241
     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
   242
     If a similar order is already in the queue, wait for that one to finish."
929
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   243
935
c4aa5a3080f5 *** empty log message ***
martin
parents: 930
diff changeset
   244
    ^ self scheduleOperation:anotherOperation asynchronous:false
c4aa5a3080f5 *** empty log message ***
martin
parents: 930
diff changeset
   245
!
c4aa5a3080f5 *** empty log message ***
martin
parents: 930
diff changeset
   246
c4aa5a3080f5 *** empty log message ***
martin
parents: 930
diff changeset
   247
scheduleOperation:anotherOperation asynchronous:asynchronous
c4aa5a3080f5 *** empty log message ***
martin
parents: 930
diff changeset
   248
    "enqueue an order (something that understands #value) to the op-queue;
c4aa5a3080f5 *** empty log message ***
martin
parents: 930
diff changeset
   249
     if asynchronous is false, wait until the operation has performed (#value been sent),
c4aa5a3080f5 *** empty log message ***
martin
parents: 930
diff changeset
   250
     return the result of the #value send.
c4aa5a3080f5 *** empty log message ***
martin
parents: 930
diff changeset
   251
     If a similar order is already in the queue, wait for that one to finish.
c4aa5a3080f5 *** empty log message ***
martin
parents: 930
diff changeset
   252
     If asynchronous is true, do not wait (but also: do not return a return value)"
c4aa5a3080f5 *** empty log message ***
martin
parents: 930
diff changeset
   253
956
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   254
    |myOpInQ ex|
929
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   255
930
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   256
    queueLock critical:[
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   257
        "/ operation already in queue ?
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   258
        queue withAccessLockedDo:[
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   259
            myOpInQ := nil.
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   260
            queue do:[:eachOpInQ |
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   261
               anotherOperation = eachOpInQ operation ifTrue:[
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   262
                    myOpInQ := eachOpInQ
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   263
               ]
929
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   264
            ].
930
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   265
            "/ 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
   266
            myOpInQ isNil ifTrue:[
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   267
                myOpInQ := OperationInQueue new.
4943
e8327124ac63 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4368
diff changeset
   268
                "/ use an EventSemaphore to protect against races between signal and wait
e8327124ac63 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4368
diff changeset
   269
                myOpInQ operationPerformedSema:(EventSemaphore new).
930
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   270
                myOpInQ operation:anotherOperation.
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
                    queue nextPut:myOpInQ.
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   273
                    consumerProcess isNil ifTrue:[
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   274
                        self startConsumerProcess
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   275
                    ].
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   276
                ] valueUninterruptably.
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   277
            ].
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   278
        ].
4368
6596738636b2 #DOCUMENTATION by mawalch
mawalch
parents: 2994
diff changeset
   279
    ].
929
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   280
935
c4aa5a3080f5 *** empty log message ***
martin
parents: 930
diff changeset
   281
    asynchronous ifTrue:[
c4aa5a3080f5 *** empty log message ***
martin
parents: 930
diff changeset
   282
        ^ nil
c4aa5a3080f5 *** empty log message ***
martin
parents: 930
diff changeset
   283
    ].
c4aa5a3080f5 *** empty log message ***
martin
parents: 930
diff changeset
   284
930
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   285
    "/ wait for the operation to finish
929
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   286
    myOpInQ operationPerformedSema wait.
956
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   287
    (ex := myOpInQ exception) notNil ifTrue:[
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   288
        "/ 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
   289
        "/ (in reality, it happended in the consumer-process).
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   290
        self linkContextChain:ex suspendedContext.
929
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   291
4368
6596738636b2 #DOCUMENTATION by mawalch
mawalch
parents: 2994
diff changeset
   292
        ^ 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
   293
    ].
930
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   294
    "/ now, the operation has been performed - return its result
929
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   295
    ^ myOpInQ operationResult
4368
6596738636b2 #DOCUMENTATION by mawalch
mawalch
parents: 2994
diff changeset
   296
6596738636b2 #DOCUMENTATION by mawalch
mawalch
parents: 2994
diff changeset
   297
    "Modified (format): / 22-05-2017 / 12:08:05 / mawalch"
4943
e8327124ac63 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4368
diff changeset
   298
    "Modified: / 16-05-2019 / 14:51:57 / Stefan Vogel"
929
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   299
! !
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   300
930
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   301
!OperationQueue methodsFor:'queries'!
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   302
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   303
size
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   304
    "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
   305
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   306
    ^ queue size
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   307
! !
f084913b188a rewritten to no longer be a subclass of sharedQueue
Claus Gittinger <cg@exept.de>
parents: 929
diff changeset
   308
929
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   309
!OperationQueue::OperationInQueue methodsFor:'accessing'!
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   310
956
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   311
exception
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   312
    "return 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
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
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   317
exception:something
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   318
    "set the value of the instance variable 'exception' (automatically generated)"
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   319
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   320
    exception := something.
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   321
!
d62d8a6f3ba0 handle error in consumer, pass info to producer,
martin
parents: 937
diff changeset
   322
929
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   323
operation
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   324
    "return 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
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   327
!
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   328
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   329
operation:something
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   330
    "set the value of the instance variable 'operation' (automatically generated)"
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   331
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   332
    operation := something.
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
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   336
    "return 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
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   339
!
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   340
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   341
operationPerformedSema:something
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   342
    "set the value of the instance variable 'operationPerformedSema' (automatically generated)"
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   343
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   344
    operationPerformedSema := something.
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
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   348
    "return 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
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   351
!
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   352
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   353
operationResult:something
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   354
    "set the value of the instance variable 'operationResult' (automatically generated)"
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   355
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   356
    operationResult := something.
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   357
! !
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   358
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   359
!OperationQueue class methodsFor:'documentation'!
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   360
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   361
version
4368
6596738636b2 #DOCUMENTATION by mawalch
mawalch
parents: 2994
diff changeset
   362
    ^ '$Header$'
929
6cd3e1443daa initial checkin
martin
parents:
diff changeset
   363
! !
2974
eb6b47cfde84 class: OperationQueue
Claus Gittinger <cg@exept.de>
parents: 1307
diff changeset
   364