SharedQueue.st
author Claus Gittinger <cg@exept.de>
Tue, 25 Jun 2019 14:28:51 +0200
changeset 5050 44fa8672d102
parent 5049 0d48cb487e0c
child 5142 2e028025c748
permissions -rw-r--r--
#DOCUMENTATION by cg class: SharedQueue comment/format in: #next #nextWithTimeout:
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4739
fef931c4b934 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4675
diff changeset
     1
"{ Encoding: utf8 }"
fef931c4b934 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4675
diff changeset
     2
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
     3
"
1cf8d1747859 Initial revision
claus
parents:
diff changeset
     4
 COPYRIGHT (c) 1993 by Claus Gittinger
68
6650e0d50a1a *** empty log message ***
claus
parents: 34
diff changeset
     5
	      All Rights Reserved
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
     6
1cf8d1747859 Initial revision
claus
parents:
diff changeset
     7
 This software is furnished under a license and may be used
1cf8d1747859 Initial revision
claus
parents:
diff changeset
     8
 only in accordance with the terms of that license and with the
1cf8d1747859 Initial revision
claus
parents:
diff changeset
     9
 inclusion of the above copyright notice.   This software may not
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    10
 be provided or otherwise made available to, or used by, any
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    11
 other person.  No title to or ownership of the software is
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    12
 hereby transferred.
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    13
"
916
63fddba933d6 Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents: 845
diff changeset
    14
"{ Package: 'stx:libbasic2' }"
63fddba933d6 Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents: 845
diff changeset
    15
3858
baba8974a90d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3435
diff changeset
    16
"{ NameSpace: Smalltalk }"
baba8974a90d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3435
diff changeset
    17
6
claus
parents: 3
diff changeset
    18
Queue subclass:#SharedQueue
917
df608391baa5 Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents: 916
diff changeset
    19
	instanceVariableNames:'dataAvailable spaceAvailable accessLock'
156
109b1c9342b2 access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents: 141
diff changeset
    20
	classVariableNames:''
109b1c9342b2 access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents: 141
diff changeset
    21
	poolDictionaries:''
109b1c9342b2 access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents: 141
diff changeset
    22
	category:'Kernel-Processes'
141
2804943fc6f0 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
    23
!
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    24
469
9b4318b56d9d commentary
Claus Gittinger <cg@exept.de>
parents: 396
diff changeset
    25
!SharedQueue class methodsFor:'documentation'!
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    26
30
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    27
copyright
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    28
"
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    29
 COPYRIGHT (c) 1993 by Claus Gittinger
68
6650e0d50a1a *** empty log message ***
claus
parents: 34
diff changeset
    30
	      All Rights Reserved
30
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    31
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    32
 This software is furnished under a license and may be used
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    33
 only in accordance with the terms of that license and with the
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    34
 inclusion of the above copyright notice.   This software may not
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    35
 be provided or otherwise made available to, or used by, any
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    36
 other person.  No title to or ownership of the software is
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    37
 hereby transferred.
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    38
"
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    39
!
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    40
30
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    41
documentation
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    42
"
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    43
    SharedQueues provide a safe mechanism for processes to communicate.
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    44
    They are basically Queues, with added secure access to the internals,
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    45
    allowing use from multiple processes (i.e. the access methods use
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    46
    critical regions to protect against confusion due to a process
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    47
    switch within a modification).
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    48
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    49
    Also, sharedQueues can be used for synchronization, since a reading
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    50
    process will be blocked when attempting to read an empty queue, while
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    51
    a writer will be blocked when attempting to write into a full queue.
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    52
    For nonBlocking read, use #isEmpty; for nonBlocking write, use #isFull.
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    53
4063
4494f18b967d #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4034
diff changeset
    54
    Be warned:
4739
fef931c4b934 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4675
diff changeset
    55
        if a reader process wants to add elements to the sharedqueue in its
fef931c4b934 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4675
diff changeset
    56
        read-loop, it may block, if the queue is full, leading to a deadlock.
fef931c4b934 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4675
diff changeset
    57
        The reason is that the sharedQueues size is fixed, and any write is blocked
4063
4494f18b967d #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4034
diff changeset
    58
        if the queue is full.
4739
fef931c4b934 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4675
diff changeset
    59
        For this situations, please use an UnlimitedSharedQueue, which grows in that
4063
4494f18b967d #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4034
diff changeset
    60
        particular situation.
4494f18b967d #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4034
diff changeset
    61
        
30
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    62
    See samples in doc/coding.
251
58fc29adb012 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 156
diff changeset
    63
257
759055770356 documentation
Claus Gittinger <cg@exept.de>
parents: 251
diff changeset
    64
    [author:]
759055770356 documentation
Claus Gittinger <cg@exept.de>
parents: 251
diff changeset
    65
        Claus Gittinger
759055770356 documentation
Claus Gittinger <cg@exept.de>
parents: 251
diff changeset
    66
251
58fc29adb012 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 156
diff changeset
    67
    [see also:]
4063
4494f18b967d #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4034
diff changeset
    68
        SharedCollection
4494f18b967d #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4034
diff changeset
    69
        UnlimitedSharedQueue
4494f18b967d #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4034
diff changeset
    70
        Queue
251
58fc29adb012 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 156
diff changeset
    71
        Semaphore
58fc29adb012 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 156
diff changeset
    72
        Process
469
9b4318b56d9d commentary
Claus Gittinger <cg@exept.de>
parents: 396
diff changeset
    73
        CodingExamples::SharedQueueExamples
30
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    74
"
845
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    75
!
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    76
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    77
examples
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    78
"
4739
fef931c4b934 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4675
diff changeset
    79
                                                                    [exBegin]
845
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    80
    |queues readers writers seqNumber accessLock accessLock2
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    81
     numbersStillToReceive|
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    82
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    83
    seqNumber := 1.
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    84
    accessLock := Semaphore forMutualExclusion.
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    85
    accessLock2 := Semaphore forMutualExclusion.
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    86
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    87
    numbersStillToReceive := BooleanArray new:100000 withAll:true.
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    88
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    89
    queues := (1 to:10) collect:[:i | SharedQueue new].
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    90
    readers := (1 to:10) collect:[:i |
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    91
                                    [   |num|
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    92
                                        10000 timesRepeat:[
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    93
                                            num := (queues at:i) next.
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    94
                                            accessLock2 critical:[
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    95
                                                (numbersStillToReceive at:num) ifFalse:[
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    96
                                                    self halt:(num printString , ' received twice')
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    97
                                                ] ifTrue:[
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    98
                                                    numbersStillToReceive at:num put:false.
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    99
                                                ].
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   100
                                            ].
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   101
                                            'num printCR.'.
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   102
                                        ].
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   103
                                    ] fork
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   104
                                 ].
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   105
    writers := (1 to:10) collect:[:i |
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   106
                                    [   |num|
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   107
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   108
                                        10000 timesRepeat:[
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   109
                                            accessLock critical:[
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   110
                                                num := seqNumber.
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   111
                                                seqNumber := seqNumber + 1.
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   112
                                            ].
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   113
                                            (queues at:i) nextPut:num.
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   114
                                        ]
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   115
                                    ] fork
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   116
                                 ].
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   117
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   118
    readers do:[:aReader | aReader waitUntilTerminated].
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   119
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   120
    ' any left ? '.
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   121
    (numbersStillToReceive includes:true) ifTrue:[
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   122
        self halt:'oops - not all numbers received'
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   123
    ]
4739
fef931c4b934 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4675
diff changeset
   124
                                                                    [exEnd]
fef931c4b934 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4675
diff changeset
   125
fef931c4b934 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4675
diff changeset
   126
   deadlock example:
fef931c4b934 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4675
diff changeset
   127
   here, a read process tries to write !!
fef931c4b934 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4675
diff changeset
   128
                                                                    [exBegin]
fef931c4b934 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4675
diff changeset
   129
    |queue reader writer|
fef931c4b934 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4675
diff changeset
   130
fef931c4b934 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4675
diff changeset
   131
    queue := SharedQueue new:10.
fef931c4b934 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4675
diff changeset
   132
    reader := 
fef931c4b934 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4675
diff changeset
   133
        [   
fef931c4b934 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4675
diff changeset
   134
            |num|
fef931c4b934 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4675
diff changeset
   135
            
fef931c4b934 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4675
diff changeset
   136
            [ (num := queue next) ~~ #EOF] whileTrue:[
fef931c4b934 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4675
diff changeset
   137
                'here is the bad code: writing into the queue !!'.
fef931c4b934 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4675
diff changeset
   138
                num == 30 ifTrue:[
fef931c4b934 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4675
diff changeset
   139
                    Transcript showCR:'xxx'.
fef931c4b934 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4675
diff changeset
   140
                    queue nextPut:'bad1'.
fef931c4b934 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4675
diff changeset
   141
                    queue nextPut:'bad2'.
fef931c4b934 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4675
diff changeset
   142
                ].
fef931c4b934 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4675
diff changeset
   143
                Transcript showCR:num.
fef931c4b934 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4675
diff changeset
   144
                Delay waitForSeconds:0.01.
fef931c4b934 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4675
diff changeset
   145
            ].
fef931c4b934 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4675
diff changeset
   146
        ] fork.
fef931c4b934 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4675
diff changeset
   147
fef931c4b934 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4675
diff changeset
   148
    writer := 
fef931c4b934 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4675
diff changeset
   149
        [   |num|
fef931c4b934 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4675
diff changeset
   150
fef931c4b934 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4675
diff changeset
   151
            1 to:60 do:[:seqNr |
fef931c4b934 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4675
diff changeset
   152
                queue nextPut:seqNr.
fef931c4b934 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4675
diff changeset
   153
            ].
fef931c4b934 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4675
diff changeset
   154
            queue nextPut:#EOF.
fef931c4b934 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4675
diff changeset
   155
        ] fork.
fef931c4b934 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4675
diff changeset
   156
fef931c4b934 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4675
diff changeset
   157
    reader waitUntilTerminated.
fef931c4b934 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4675
diff changeset
   158
    writer waitUntilTerminated.
fef931c4b934 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4675
diff changeset
   159
                                                                        [exEnd]
845
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   160
"
30
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
   161
! !
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   162
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   163
!SharedQueue methodsFor:'accessing'!
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   164
4346
7a4c996ac8f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4068
diff changeset
   165
remove:anElement ifAbsent:exceptionalValue
7a4c996ac8f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4068
diff changeset
   166
    |retVal noSuchElement|
7a4c996ac8f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4068
diff changeset
   167
7a4c996ac8f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4068
diff changeset
   168
    noSuchElement := false.
7a4c996ac8f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4068
diff changeset
   169
    retVal := accessLock critical:[
7a4c996ac8f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4068
diff changeset
   170
        super remove:anElement ifAbsent:[noSuchElement := true]
7a4c996ac8f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4068
diff changeset
   171
    ].
7a4c996ac8f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4068
diff changeset
   172
    noSuchElement ifTrue:[
7a4c996ac8f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4068
diff changeset
   173
        ^ exceptionalValue value.
7a4c996ac8f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4068
diff changeset
   174
    ].
4348
06a5c0f38e6c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4346
diff changeset
   175
    dataAvailable consume.
4346
7a4c996ac8f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4068
diff changeset
   176
    spaceAvailable signal.
7a4c996ac8f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4068
diff changeset
   177
    ^ retVal.
7a4c996ac8f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4068
diff changeset
   178
7a4c996ac8f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4068
diff changeset
   179
    "Created: / 22-02-2017 / 14:53:13 / stefan"
4348
06a5c0f38e6c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4346
diff changeset
   180
    "Modified: / 22-02-2017 / 16:50:49 / stefan"
4346
7a4c996ac8f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4068
diff changeset
   181
!
7a4c996ac8f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4068
diff changeset
   182
647
f937d30a8afc remove all elements in the queue:
ca
parents: 487
diff changeset
   183
removeAll
f937d30a8afc remove all elements in the queue:
ca
parents: 487
diff changeset
   184
    "remove all elements in the queue; do not wait, but
f937d30a8afc remove all elements in the queue:
ca
parents: 487
diff changeset
   185
     synchronize access to the queue.
f937d30a8afc remove all elements in the queue:
ca
parents: 487
diff changeset
   186
     If the queue was full before, signal space-availability to writers.
f937d30a8afc remove all elements in the queue:
ca
parents: 487
diff changeset
   187
     This can be used to flush queues in multi-process applications,
f937d30a8afc remove all elements in the queue:
ca
parents: 487
diff changeset
   188
     when cleanup is required."
f937d30a8afc remove all elements in the queue:
ca
parents: 487
diff changeset
   189
916
63fddba933d6 Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents: 845
diff changeset
   190
    |count|
647
f937d30a8afc remove all elements in the queue:
ca
parents: 487
diff changeset
   191
917
df608391baa5 Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents: 916
diff changeset
   192
    accessLock critical:[
3433
174f21ed6ce8 class: SharedQueue
Stefan Vogel <sv@exept.de>
parents: 2032
diff changeset
   193
        [
174f21ed6ce8 class: SharedQueue
Stefan Vogel <sv@exept.de>
parents: 2032
diff changeset
   194
            count := dataAvailable count.
174f21ed6ce8 class: SharedQueue
Stefan Vogel <sv@exept.de>
parents: 2032
diff changeset
   195
            dataAvailable clear.
174f21ed6ce8 class: SharedQueue
Stefan Vogel <sv@exept.de>
parents: 2032
diff changeset
   196
        ] valueUninterruptably.
917
df608391baa5 Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents: 916
diff changeset
   197
        super removeAll.
df608391baa5 Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents: 916
diff changeset
   198
    ].
3433
174f21ed6ce8 class: SharedQueue
Stefan Vogel <sv@exept.de>
parents: 2032
diff changeset
   199
    count timesRepeat:[spaceAvailable signal]. 
917
df608391baa5 Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents: 916
diff changeset
   200
!
647
f937d30a8afc remove all elements in the queue:
ca
parents: 487
diff changeset
   201
1494
8f617564556b +removeIdentical
penk
parents: 1489
diff changeset
   202
removeIdentical:anElement ifAbsent:exceptionalValue
1502
Claus Gittinger <cg@exept.de>
parents: 1501
diff changeset
   203
    |retVal noSuchElement|
1494
8f617564556b +removeIdentical
penk
parents: 1489
diff changeset
   204
8f617564556b +removeIdentical
penk
parents: 1489
diff changeset
   205
    noSuchElement := false.
4348
06a5c0f38e6c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4346
diff changeset
   206
    accessLock critical:[
06a5c0f38e6c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4346
diff changeset
   207
        retVal := super removeIdentical:anElement ifAbsent:[noSuchElement := true].
06a5c0f38e6c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4346
diff changeset
   208
        noSuchElement ifFalse:[
06a5c0f38e6c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4346
diff changeset
   209
            dataAvailable consume.
06a5c0f38e6c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4346
diff changeset
   210
        ].
1494
8f617564556b +removeIdentical
penk
parents: 1489
diff changeset
   211
    ].
8f617564556b +removeIdentical
penk
parents: 1489
diff changeset
   212
    noSuchElement ifTrue:[
8f617564556b +removeIdentical
penk
parents: 1489
diff changeset
   213
        ^ exceptionalValue value.
8f617564556b +removeIdentical
penk
parents: 1489
diff changeset
   214
    ].
1498
681b0afc046c removeIdentical: ifAbsent:
penk
parents: 1494
diff changeset
   215
    spaceAvailable signal.
1502
Claus Gittinger <cg@exept.de>
parents: 1501
diff changeset
   216
    ^ retVal.
4346
7a4c996ac8f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4068
diff changeset
   217
4348
06a5c0f38e6c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4346
diff changeset
   218
    "Modified: / 22-02-2017 / 17:05:10 / stefan"
1494
8f617564556b +removeIdentical
penk
parents: 1489
diff changeset
   219
!
8f617564556b +removeIdentical
penk
parents: 1489
diff changeset
   220
396
88bd6136ee67 added #removeLast
Claus Gittinger <cg@exept.de>
parents: 257
diff changeset
   221
removeLast
3433
174f21ed6ce8 class: SharedQueue
Stefan Vogel <sv@exept.de>
parents: 2032
diff changeset
   222
    "return the last value in the queue; if it its empty, wait until
396
88bd6136ee67 added #removeLast
Claus Gittinger <cg@exept.de>
parents: 257
diff changeset
   223
     something is put into the receiver.
88bd6136ee67 added #removeLast
Claus Gittinger <cg@exept.de>
parents: 257
diff changeset
   224
     When the datum has been removed, signal space-availability to
88bd6136ee67 added #removeLast
Claus Gittinger <cg@exept.de>
parents: 257
diff changeset
   225
     writers"
88bd6136ee67 added #removeLast
Claus Gittinger <cg@exept.de>
parents: 257
diff changeset
   226
1502
Claus Gittinger <cg@exept.de>
parents: 1501
diff changeset
   227
    |retVal|
396
88bd6136ee67 added #removeLast
Claus Gittinger <cg@exept.de>
parents: 257
diff changeset
   228
916
63fddba933d6 Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents: 845
diff changeset
   229
    dataAvailable wait.
4346
7a4c996ac8f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4068
diff changeset
   230
    retVal := accessLock critical:[super removeLast].
916
63fddba933d6 Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents: 845
diff changeset
   231
    spaceAvailable signal.
396
88bd6136ee67 added #removeLast
Claus Gittinger <cg@exept.de>
parents: 257
diff changeset
   232
1502
Claus Gittinger <cg@exept.de>
parents: 1501
diff changeset
   233
    ^ retVal.
4346
7a4c996ac8f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4068
diff changeset
   234
4348
06a5c0f38e6c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4346
diff changeset
   235
    "Modified: / 22-02-2017 / 16:50:39 / stefan"
931
60a44b4aa743 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 917
diff changeset
   236
! !
60a44b4aa743 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 917
diff changeset
   237
60a44b4aa743 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 917
diff changeset
   238
!SharedQueue methodsFor:'accessing-internals'!
60a44b4aa743 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 917
diff changeset
   239
1489
88159ab72ba0 access to accessLock
Claus Gittinger <cg@exept.de>
parents: 1124
diff changeset
   240
accessLock
4369
eb28bc87f581 #DOCUMENTATION by mawalch
mawalch
parents: 4348
diff changeset
   241
    "return the critical access-semaphore which is used internally to synchronize access"
1489
88159ab72ba0 access to accessLock
Claus Gittinger <cg@exept.de>
parents: 1124
diff changeset
   242
88159ab72ba0 access to accessLock
Claus Gittinger <cg@exept.de>
parents: 1124
diff changeset
   243
    ^ accessLock
4369
eb28bc87f581 #DOCUMENTATION by mawalch
mawalch
parents: 4348
diff changeset
   244
eb28bc87f581 #DOCUMENTATION by mawalch
mawalch
parents: 4348
diff changeset
   245
    "Modified (comment): / 22-05-2017 / 12:08:49 / mawalch"
1489
88159ab72ba0 access to accessLock
Claus Gittinger <cg@exept.de>
parents: 1124
diff changeset
   246
!
88159ab72ba0 access to accessLock
Claus Gittinger <cg@exept.de>
parents: 1124
diff changeset
   247
931
60a44b4aa743 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 917
diff changeset
   248
readSemaphore
60a44b4aa743 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 917
diff changeset
   249
    "return the semaphore which is signalled when data is available
60a44b4aa743 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 917
diff changeset
   250
     for reading."
60a44b4aa743 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 917
diff changeset
   251
60a44b4aa743 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 917
diff changeset
   252
    ^ dataAvailable
60a44b4aa743 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 917
diff changeset
   253
60a44b4aa743 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 917
diff changeset
   254
    "Modified: 16.12.1995 / 13:47:11 / cg"
60a44b4aa743 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 917
diff changeset
   255
!
60a44b4aa743 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 917
diff changeset
   256
2022
0f7f5bae8bdd readWait
Claus Gittinger <cg@exept.de>
parents: 1933
diff changeset
   257
readWaitWithTimeoutMs:ms
4034
fe1516505f8e #OTHER by mawalch
mawalch
parents: 3858
diff changeset
   258
    "Return true if a timeout occurred (i.e. false, if data is available)."
2032
05ebef21930b comment
fm
parents: 2022
diff changeset
   259
3433
174f21ed6ce8 class: SharedQueue
Stefan Vogel <sv@exept.de>
parents: 2032
diff changeset
   260
    ^ (dataAvailable waitUncountedWithTimeoutMs:ms) isNil.
2022
0f7f5bae8bdd readWait
Claus Gittinger <cg@exept.de>
parents: 1933
diff changeset
   261
!
0f7f5bae8bdd readWait
Claus Gittinger <cg@exept.de>
parents: 1933
diff changeset
   262
4066
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   263
superNextPut:anObject
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   264
    "private; to allow subclasses to call the basic nextPut (w.o. synchronization)"
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   265
4348
06a5c0f38e6c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4346
diff changeset
   266
    super nextPut:anObject.
06a5c0f38e6c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4346
diff changeset
   267
06a5c0f38e6c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4346
diff changeset
   268
    "Modified: / 22-02-2017 / 16:34:11 / stefan"
4066
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   269
!
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   270
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   271
superNextPutFirst:anObject
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   272
    "private; to allow subclasses to call the basic nextPutFirst (w.o. synchronization)"
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   273
4348
06a5c0f38e6c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4346
diff changeset
   274
    super nextPutFirst:anObject.
06a5c0f38e6c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4346
diff changeset
   275
06a5c0f38e6c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4346
diff changeset
   276
    "Modified: / 22-02-2017 / 16:34:16 / stefan"
4066
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   277
!
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   278
931
60a44b4aa743 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 917
diff changeset
   279
withAccessLockedDo:aBlock
60a44b4aa743 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 917
diff changeset
   280
    "evaluate aBlock while access via next/nextPut are blocked."
60a44b4aa743 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 917
diff changeset
   281
60a44b4aa743 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 917
diff changeset
   282
    accessLock critical:aBlock
917
df608391baa5 Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents: 916
diff changeset
   283
!
396
88bd6136ee67 added #removeLast
Claus Gittinger <cg@exept.de>
parents: 257
diff changeset
   284
156
109b1c9342b2 access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents: 141
diff changeset
   285
writeSemaphore
109b1c9342b2 access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents: 141
diff changeset
   286
    "return the semaphore which is signalled when the queue has space
109b1c9342b2 access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents: 141
diff changeset
   287
     for writing."
109b1c9342b2 access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents: 141
diff changeset
   288
109b1c9342b2 access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents: 141
diff changeset
   289
    ^ spaceAvailable
109b1c9342b2 access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents: 141
diff changeset
   290
109b1c9342b2 access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents: 141
diff changeset
   291
    "Modified: 16.12.1995 / 13:47:07 / cg"
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   292
! !
141
2804943fc6f0 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   293
4066
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   294
!SharedQueue methodsFor:'accessing-reading'!
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   295
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   296
next
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   297
    "return the next value in the queue; if it its empty, wait 'til
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   298
     something is put into the receiver.
5050
44fa8672d102 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 5049
diff changeset
   299
     When the datum has been removed, signal space-availability to writers"
4066
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   300
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   301
    |retVal|
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   302
4977
5841e91a657b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 4739
diff changeset
   303
    "/ need a loop here, in case someone else was reading the element in-between the
5841e91a657b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 4739
diff changeset
   304
    "/ wait and the accessLock-critical
5841e91a657b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 4739
diff changeset
   305
    [
5841e91a657b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 4739
diff changeset
   306
        dataAvailable wait.
5841e91a657b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 4739
diff changeset
   307
        retVal := accessLock critical:[super nextOrNil].
5841e91a657b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 4739
diff changeset
   308
    ] doWhile:[retVal isNil].
4066
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   309
    spaceAvailable signal.
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   310
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   311
    ^ retVal.
4346
7a4c996ac8f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4068
diff changeset
   312
7a4c996ac8f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4068
diff changeset
   313
    "Modified: / 22-02-2017 / 14:40:32 / stefan"
5050
44fa8672d102 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 5049
diff changeset
   314
    "Modified (comment): / 25-06-2019 / 14:28:44 / Claus Gittinger"
4066
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   315
!
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   316
4346
7a4c996ac8f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4068
diff changeset
   317
nextIfEmpty:exceptionBlock
4675
9ff59d8c7f46 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4498
diff changeset
   318
    "return the next value in the queue; 
9ff59d8c7f46 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4498
diff changeset
   319
     if it is empty do not wait, but return the value of exceptionBlock.
4066
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   320
     When a datum has been removed, signal space-availability to writers"
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   321
4346
7a4c996ac8f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4068
diff changeset
   322
    |retVal isEmpty|
4066
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   323
4346
7a4c996ac8f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4068
diff changeset
   324
    retVal := accessLock critical:[
7a4c996ac8f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4068
diff changeset
   325
        isEmpty := self isEmpty.
7a4c996ac8f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4068
diff changeset
   326
        isEmpty ifFalse:[
4348
06a5c0f38e6c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4346
diff changeset
   327
            dataAvailable consume.
4346
7a4c996ac8f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4068
diff changeset
   328
            super nextOrNil
4066
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   329
        ].
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   330
    ].
4346
7a4c996ac8f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4068
diff changeset
   331
    isEmpty ifTrue:[
7a4c996ac8f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4068
diff changeset
   332
        ^ exceptionBlock value
7a4c996ac8f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4068
diff changeset
   333
    ].
7a4c996ac8f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4068
diff changeset
   334
    spaceAvailable signal.
7a4c996ac8f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4068
diff changeset
   335
    ^ retVal.
4066
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   336
4348
06a5c0f38e6c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4346
diff changeset
   337
    "Modified: / 22-02-2017 / 17:03:23 / stefan"
4675
9ff59d8c7f46 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4498
diff changeset
   338
    "Modified (comment): / 24-05-2018 / 09:23:48 / Claus Gittinger"
4066
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   339
!
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   340
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   341
nextOrNil
5049
0d48cb487e0c #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4977
diff changeset
   342
    "return the next value in the queue; 
0d48cb487e0c #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4977
diff changeset
   343
     if it is empty do not wait, but return nil.
0d48cb487e0c #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4977
diff changeset
   344
     When a datum has been removed, signal space-availability to writers"
0d48cb487e0c #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4977
diff changeset
   345
4066
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   346
    ^ self nextIfEmpty:nil
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   347
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   348
    "Created: / 31-05-2007 / 15:09:33 / cg"
5049
0d48cb487e0c #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4977
diff changeset
   349
    "Modified (comment): / 25-06-2019 / 14:22:33 / Claus Gittinger"
4066
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   350
!
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   351
5050
44fa8672d102 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 5049
diff changeset
   352
nextWithTimeout:secondsOrTimeDurationOrNil
4066
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   353
    "return the next value in the queue; if it its empty, wait until
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   354
     something is put into the receiver.
4977
5841e91a657b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 4739
diff changeset
   355
     When the datum has been removed, signal space-availability to writers.
5050
44fa8672d102 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 5049
diff changeset
   356
     Answer nil if a timeout occurs.
44fa8672d102 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 5049
diff changeset
   357
44fa8672d102 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 5049
diff changeset
   358
     The argument may be a time duration or the number of seconds as integer
44fa8672d102 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 5049
diff changeset
   359
     or float (i.e. use 0.1 for a 100ms timeout).
44fa8672d102 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 5049
diff changeset
   360
     With zero timeout, this can be used to poll a semaphore (returning
44fa8672d102 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 5049
diff changeset
   361
     the receiver if the semaphore is available, nil if not).
44fa8672d102 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 5049
diff changeset
   362
     However, polling is not the intended use of semaphores, though.
44fa8672d102 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 5049
diff changeset
   363
     If the argument is nil, wait without timeout (forever)."
4066
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   364
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   365
    |retVal|
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   366
5050
44fa8672d102 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 5049
diff changeset
   367
    (dataAvailable waitWithTimeout:secondsOrTimeDurationOrNil) isNil ifTrue:[
4066
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   368
        ^ nil
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   369
    ].
4346
7a4c996ac8f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4068
diff changeset
   370
    retVal := accessLock critical:[super nextOrNil].
4066
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   371
    spaceAvailable signal.
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   372
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   373
    ^ retVal.
4346
7a4c996ac8f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4068
diff changeset
   374
7a4c996ac8f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4068
diff changeset
   375
    "Modified: / 22-02-2017 / 14:35:09 / stefan"
5050
44fa8672d102 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 5049
diff changeset
   376
    "Modified (format): / 25-06-2019 / 14:28:27 / Claus Gittinger"
4066
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   377
!
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   378
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   379
peek
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   380
    self isEmpty ifTrue:[
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   381
        dataAvailable waitUncounted.
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   382
    ].
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   383
    ^ super peek
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   384
! !
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   385
4739
fef931c4b934 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4675
diff changeset
   386
!SharedQueue methodsFor:'accessing-writing'!
4066
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   387
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   388
nextPut:anObject
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   389
    "enter anObject to the end of the queue; 
4346
7a4c996ac8f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4068
diff changeset
   390
     Wait for available space, if the queue is full. 
4066
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   391
     After the put, signal availablity of a datum to readers."
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   392
4068
cadb70dce850 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4066
diff changeset
   393
    self commonWriteWith:[self superNextPut:anObject].
4346
7a4c996ac8f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4068
diff changeset
   394
7a4c996ac8f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4068
diff changeset
   395
    "Modified (comment): / 22-02-2017 / 15:18:36 / stefan"
4066
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   396
!
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   397
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   398
nextPutFirst:anObject
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   399
    "insert anObject at the beginning of the queue; 
4346
7a4c996ac8f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4068
diff changeset
   400
     Wait for available space, if the queue is full. 
4066
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   401
     After the put, signal availablity of a datum to readers.
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   402
     Insertion at the beginning may be useful to add hi-prio elements (for example, in a job-scheduler)"
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   403
4068
cadb70dce850 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4066
diff changeset
   404
    self commonWriteWith:[self superNextPutFirst:anObject].
4346
7a4c996ac8f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4068
diff changeset
   405
7a4c996ac8f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4068
diff changeset
   406
    "Modified (comment): / 22-02-2017 / 15:18:42 / stefan"
4066
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   407
! !
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   408
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   409
!SharedQueue methodsFor:'enumerating'!
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   410
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   411
do:anObject
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   412
    "evaluate the argument, aBlock for each element in the queue"
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   413
4346
7a4c996ac8f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4068
diff changeset
   414
    ^ accessLock critical:[super do:anObject].
7a4c996ac8f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4068
diff changeset
   415
7a4c996ac8f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4068
diff changeset
   416
    "Modified: / 22-02-2017 / 14:54:45 / stefan"
7a4c996ac8f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4068
diff changeset
   417
!
4066
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   418
4346
7a4c996ac8f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4068
diff changeset
   419
reverseDo:anObject
7a4c996ac8f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4068
diff changeset
   420
    "evaluate the argument, aBlock for each element in the queue"
7a4c996ac8f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4068
diff changeset
   421
7a4c996ac8f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4068
diff changeset
   422
    ^ accessLock critical:[super reverseDo:anObject].
7a4c996ac8f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4068
diff changeset
   423
7a4c996ac8f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4068
diff changeset
   424
    "Created: / 22-02-2017 / 14:54:22 / stefan"
4066
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   425
! !
405375f4f7de #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4065
diff changeset
   426
141
2804943fc6f0 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   427
!SharedQueue methodsFor:'initialization'!
2804943fc6f0 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   428
2804943fc6f0 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   429
init:size
2804943fc6f0 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   430
    "initialize the receiver for size entries"
2804943fc6f0 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   431
2804943fc6f0 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   432
    super init:size.
4498
28b2bd9675c7 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4369
diff changeset
   433
    dataAvailable := Semaphore name:'shared q-read'.
3858
baba8974a90d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3435
diff changeset
   434
    dataAvailable owner:self.
917
df608391baa5 Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents: 916
diff changeset
   435
    spaceAvailable := (Semaphore new:size) name:'shared q-write'.
3858
baba8974a90d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3435
diff changeset
   436
    spaceAvailable owner:self.
3435
222a54328bc1 class: SharedQueue
Stefan Vogel <sv@exept.de>
parents: 3434
diff changeset
   437
    accessLock := RecursionLock new.
487
0230ee378075 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 485
diff changeset
   438
4498
28b2bd9675c7 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4369
diff changeset
   439
    "Modified: / 09-08-2017 / 11:59:38 / cg"
917
df608391baa5 Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents: 916
diff changeset
   440
! !
141
2804943fc6f0 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   441
4068
cadb70dce850 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4066
diff changeset
   442
!SharedQueue methodsFor:'private'!
cadb70dce850 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4066
diff changeset
   443
cadb70dce850 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4066
diff changeset
   444
commonWriteWith:aBlock
cadb70dce850 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4066
diff changeset
   445
    "common code for nextPut / nextPutFirst; 
4346
7a4c996ac8f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4068
diff changeset
   446
     wait for available space, if the queue is full. 
4068
cadb70dce850 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4066
diff changeset
   447
     After the put, signal availablity of a datum to readers."
cadb70dce850 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4066
diff changeset
   448
cadb70dce850 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4066
diff changeset
   449
    spaceAvailable wait.
cadb70dce850 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4066
diff changeset
   450
    accessLock critical:[
cadb70dce850 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4066
diff changeset
   451
        aBlock value.
cadb70dce850 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4066
diff changeset
   452
        dataAvailable signal.
cadb70dce850 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4066
diff changeset
   453
    ].
4346
7a4c996ac8f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4068
diff changeset
   454
7a4c996ac8f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4068
diff changeset
   455
    "Modified (comment): / 22-02-2017 / 15:18:03 / stefan"
4068
cadb70dce850 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4066
diff changeset
   456
! !
cadb70dce850 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4066
diff changeset
   457
469
9b4318b56d9d commentary
Claus Gittinger <cg@exept.de>
parents: 396
diff changeset
   458
!SharedQueue class methodsFor:'documentation'!
141
2804943fc6f0 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   459
2804943fc6f0 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   460
version
3858
baba8974a90d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3435
diff changeset
   461
    ^ '$Header$'
3434
78ea8416cc8e class: SharedQueue
Stefan Vogel <sv@exept.de>
parents: 3433
diff changeset
   462
!
78ea8416cc8e class: SharedQueue
Stefan Vogel <sv@exept.de>
parents: 3433
diff changeset
   463
78ea8416cc8e class: SharedQueue
Stefan Vogel <sv@exept.de>
parents: 3433
diff changeset
   464
version_CVS
3858
baba8974a90d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3435
diff changeset
   465
    ^ '$Header$'
141
2804943fc6f0 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   466
! !
3433
174f21ed6ce8 class: SharedQueue
Stefan Vogel <sv@exept.de>
parents: 2032
diff changeset
   467