CollectingSharedQueueStream.st
author Claus Gittinger <cg@exept.de>
Tue, 25 Jun 2019 14:28:51 +0200
changeset 5050 44fa8672d102
parent 4540 9f49e01f4e9c
child 5162 5e8ae4c499d4
permissions -rw-r--r--
#DOCUMENTATION by cg class: SharedQueue comment/format in: #next #nextWithTimeout:
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4329
a9d435360ebb #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 502
diff changeset
     1
"
a9d435360ebb #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 502
diff changeset
     2
 COPYRIGHT (c) 1997 by Claus Gittinger
a9d435360ebb #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 502
diff changeset
     3
              All Rights Reserved
a9d435360ebb #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 502
diff changeset
     4
a9d435360ebb #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 502
diff changeset
     5
 This software is furnished under a license and may be used
a9d435360ebb #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 502
diff changeset
     6
 only in accordance with the terms of that license and with the
a9d435360ebb #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 502
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
a9d435360ebb #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 502
diff changeset
     8
 be provided or otherwise made available to, or used by, any
a9d435360ebb #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 502
diff changeset
     9
 other person.  No title to or ownership of the software is
a9d435360ebb #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 502
diff changeset
    10
 hereby transferred.
a9d435360ebb #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 502
diff changeset
    11
"
a9d435360ebb #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 502
diff changeset
    12
"{ Package: 'stx:libbasic2' }"
a9d435360ebb #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 502
diff changeset
    13
a9d435360ebb #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 502
diff changeset
    14
"{ NameSpace: Smalltalk }"
a9d435360ebb #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 502
diff changeset
    15
502
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
    16
SequenceableCollection subclass:#CollectingSharedQueueStream
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
    17
	instanceVariableNames:'contents readPosition writePosition dataAvailable closed
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
    18
		finalSizeIfKnown signalChanges'
500
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    19
	classVariableNames:''
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    20
	poolDictionaries:''
501
3299cf782635 category change
Claus Gittinger <cg@exept.de>
parents: 500
diff changeset
    21
	category:'Streams'
500
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    22
!
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    23
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    24
!CollectingSharedQueueStream class methodsFor:'documentation'!
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    25
4329
a9d435360ebb #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 502
diff changeset
    26
copyright
a9d435360ebb #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 502
diff changeset
    27
"
a9d435360ebb #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 502
diff changeset
    28
 COPYRIGHT (c) 1997 by Claus Gittinger
a9d435360ebb #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 502
diff changeset
    29
              All Rights Reserved
a9d435360ebb #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 502
diff changeset
    30
a9d435360ebb #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 502
diff changeset
    31
 This software is furnished under a license and may be used
a9d435360ebb #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 502
diff changeset
    32
 only in accordance with the terms of that license and with the
a9d435360ebb #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 502
diff changeset
    33
 inclusion of the above copyright notice.   This software may not
a9d435360ebb #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 502
diff changeset
    34
 be provided or otherwise made available to, or used by, any
a9d435360ebb #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 502
diff changeset
    35
 other person.  No title to or ownership of the software is
a9d435360ebb #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 502
diff changeset
    36
 hereby transferred.
a9d435360ebb #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 502
diff changeset
    37
"
a9d435360ebb #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 502
diff changeset
    38
!
a9d435360ebb #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 502
diff changeset
    39
500
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    40
documentation
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    41
"
501
3299cf782635 category change
Claus Gittinger <cg@exept.de>
parents: 500
diff changeset
    42
    This class provides a buffering mechanism between a reader and a writer
3299cf782635 category change
Claus Gittinger <cg@exept.de>
parents: 500
diff changeset
    43
    process (i.e. it is much like a sharedQueue), but remembers the data as
3299cf782635 category change
Claus Gittinger <cg@exept.de>
parents: 500
diff changeset
    44
    written by the writer internally, providing indexed access to elements.
3299cf782635 category change
Claus Gittinger <cg@exept.de>
parents: 500
diff changeset
    45
500
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    46
    The reader side may read from it using #next, and possibly access
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    47
    elements via #at:.
501
3299cf782635 category change
Claus Gittinger <cg@exept.de>
parents: 500
diff changeset
    48
    Reading/accessing may start immediately, but will block until enough elements
500
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    49
    have been added by another process, the writer.
501
3299cf782635 category change
Claus Gittinger <cg@exept.de>
parents: 500
diff changeset
    50
500
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    51
    Instances of this class may be useful to start processing on
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    52
    big document/data collection immediately, while the data is still being
501
3299cf782635 category change
Claus Gittinger <cg@exept.de>
parents: 500
diff changeset
    53
    read by another thread; 
3299cf782635 category change
Claus Gittinger <cg@exept.de>
parents: 500
diff changeset
    54
    A concrete application is the HTMLDocumentReader, which is being changed to
3299cf782635 category change
Claus Gittinger <cg@exept.de>
parents: 500
diff changeset
    55
    start processing and displaying the document while the rest is still being read.
500
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    56
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    57
    [author:]
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    58
        Claus Gittinger
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    59
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    60
    [see also:]
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    61
        Stream OrderedCollection SharedQueue
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    62
"
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    63
!
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    64
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    65
examples
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    66
"
502
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
    67
  two processes synchronized much like with a sharedQueue:
500
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    68
                                                        [exBegin]
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    69
    |s reader|
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    70
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    71
    s := CollectingSharedQueueStream new.
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    72
    reader := [
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    73
                [s atEnd] whileFalse:[
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    74
                    Transcript showCR:s next
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    75
                ].
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    76
              ] fork.
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    77
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    78
    1 to:10 do:[:i |
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    79
        Delay waitForSeconds:1.
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    80
        s nextPut:i
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    81
    ].
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    82
                                                        [exEnd]
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    83
502
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
    84
  the writer reads from a (slow) pipe;
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
    85
  the reader sends it to the transcript.
500
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    86
                                                        [exBegin]
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    87
    |pipe s reader|
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    88
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    89
    s := CollectingSharedQueueStream new.
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    90
    reader := [
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    91
                [s atEnd] whileFalse:[
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    92
                    Transcript showCR:s next
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    93
                ].
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    94
              ] forkAt:9.
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    95
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    96
    pipe := PipeStream readingFrom:'ls -lR /usr'.
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    97
    pipe notNil ifTrue:[
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    98
        [pipe atEnd] whileFalse:[
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    99
            pipe readWait.
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   100
            s nextPut:(pipe nextLine).
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   101
        ].
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   102
        pipe close.
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   103
    ].
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   104
    s close
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   105
                                                        [exEnd]
502
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   106
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   107
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   108
  the writer reads from a (slow) pipe;
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   109
  the collection is used in a TextView, which
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   110
  will block whenever lines are to be displayed, which have not
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   111
  yet been read:
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   112
                                                        [exBegin]
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   113
    |view pipe buffer reader|
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   114
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   115
    buffer := CollectingSharedQueueStream new.
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   116
    buffer finalSize:100.
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   117
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   118
    [
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   119
        pipe := PipeStream readingFrom:'ls -lR /usr'.
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   120
        pipe notNil ifTrue:[
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   121
            [pipe atEnd] whileFalse:[
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   122
                pipe readWait.
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   123
                buffer nextPut:(pipe nextLine).
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   124
            ].
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   125
            pipe close.
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   126
        ].
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   127
        buffer changed:#size.
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   128
        buffer close.
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   129
    ] fork.
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   130
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   131
    view := ScrollableView for:TextView.
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   132
    view model:buffer; listMessage:#value; aspectMessage:#value.
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   133
    view open.
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   134
                                                        [exEnd]
500
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   135
"
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   136
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   137
! !
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   138
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   139
!CollectingSharedQueueStream class methodsFor:'instance creation'!
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   140
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   141
new
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   142
    ^ self basicNew initialize
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   143
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   144
    "Created: 5.3.1997 / 14:30:36 / cg"
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   145
! !
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   146
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   147
!CollectingSharedQueueStream methodsFor:'accessing'!
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   148
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   149
at:index
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   150
    "synchronized read - possibly wait for elements up to index
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   151
     being added (by someone else); then return it."
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   152
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   153
    writePosition > index ifTrue:[
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   154
        ^ contents at:index
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   155
    ].
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   156
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   157
    [writePosition <= index] whileTrue:[
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   158
        closed ifTrue:[
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   159
            ^ self subscriptBoundsError:index
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   160
        ].
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   161
        dataAvailable wait.
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   162
    ].
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   163
    ^ contents at:index
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   164
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   165
    "Created: 5.3.1997 / 14:44:41 / cg"
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   166
!
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   167
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   168
next
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   169
    "return the next value in the queue; if there is none,
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   170
     wait 'til something is put into the receiver."
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   171
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   172
    |value|
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   173
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   174
    [readPosition >= writePosition] whileTrue:[
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   175
        closed ifTrue:[
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   176
            ^ nil
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   177
        ].
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   178
        dataAvailable wait
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   179
    ].
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   180
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   181
    value := contents at:readPosition.
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   182
    readPosition := readPosition + 1.
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   183
    ^ value
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   184
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   185
    "Created: 5.3.1997 / 14:28:57 / cg"
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   186
    "Modified: 5.3.1997 / 14:45:54 / cg"
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   187
!
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   188
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   189
nextPut:anObject
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   190
    "append anObject to the queue; if anyone is waiting, tell him"
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   191
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   192
    |value|
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   193
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   194
    contents add:anObject.
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   195
    writePosition := writePosition + 1.
502
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   196
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   197
    finalSizeIfKnown isNil ifTrue:[
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   198
        signalChanges ifTrue:[
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   199
            self changed:#size with:nil
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   200
        ]
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   201
    ].
500
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   202
    dataAvailable signal
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   203
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   204
    "Created: 5.3.1997 / 14:33:44 / cg"
502
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   205
    "Modified: 5.3.1997 / 15:42:33 / cg"
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   206
! !
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   207
4540
9f49e01f4e9c #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4329
diff changeset
   208
!CollectingSharedQueueStream methodsFor:'accessing-special'!
502
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   209
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   210
close
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   211
    "signal the end of input; to be used by the writer"
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   212
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   213
    closed := true.
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   214
    dataAvailable signal
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   215
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   216
    "Modified: 5.3.1997 / 14:45:11 / cg"
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   217
!
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   218
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   219
finalSize:aNumber
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   220
    "can be used by the writer, if the final size is known in
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   221
     advance."
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   222
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   223
    finalSizeIfKnown := aNumber.
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   224
    signalChanges ifTrue:[
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   225
        self changed:#size
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   226
    ].
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   227
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   228
    "Created: 5.3.1997 / 15:36:07 / cg"
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   229
    "Modified: 5.3.1997 / 15:57:24 / cg"
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   230
!
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   231
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   232
signalChanges:aBoolean
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   233
    "controls if I should send out size-changeMessages when new elements arrive"
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   234
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   235
    signalChanges := aBoolean.
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   236
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   237
    "Created: 5.3.1997 / 15:40:57 / cg"
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   238
! !
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   239
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   240
!CollectingSharedQueueStream methodsFor:'dummy converting'!
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   241
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   242
asStringCollection
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   243
     ^ self
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   244
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   245
    "Created: 5.3.1997 / 16:02:57 / cg"
500
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   246
! !
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   247
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   248
!CollectingSharedQueueStream methodsFor:'initialization'!
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   249
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   250
initialize
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   251
    readPosition := writePosition := 1.
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   252
    dataAvailable := Semaphore new.
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   253
    contents := OrderedCollection new.
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   254
    closed := false.
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   255
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   256
    "Modified: 5.3.1997 / 14:34:55 / cg"
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   257
! !
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   258
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   259
!CollectingSharedQueueStream methodsFor:'queries'!
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   260
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   261
atEnd
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   262
    closed ifFalse:[^ false].
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   263
    ^ readPosition >= writePosition
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   264
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   265
    "Modified: 5.3.1997 / 14:41:04 / cg"
502
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   266
!
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   267
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   268
currentSize
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   269
    ^ contents size
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   270
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   271
    "Modified: 5.3.1997 / 15:56:36 / cg"
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   272
!
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   273
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   274
size
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   275
    closed ifTrue:[^ contents size].
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   276
    finalSizeIfKnown notNil ifTrue:[^ finalSizeIfKnown].
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   277
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   278
    "/ must wait until closed
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   279
    [closed] whileFalse:[
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   280
        dataAvailable wait
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   281
    ].
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   282
    closed ifTrue:[^ contents size].
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   283
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   284
    "Created: 5.3.1997 / 15:35:29 / cg"
1d4a576e4955 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 501
diff changeset
   285
    "Modified: 5.3.1997 / 15:57:08 / cg"
500
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   286
! !
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   287
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   288
!CollectingSharedQueueStream class methodsFor:'documentation'!
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   289
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   290
version
4329
a9d435360ebb #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 502
diff changeset
   291
    ^ '$Header$'
500
f1df3a589fb4 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   292
! !
4329
a9d435360ebb #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 502
diff changeset
   293