SharedQueue.st
author penk
Fri, 19 Nov 2004 11:04:55 +0100
changeset 1494 8f617564556b
parent 1489 88159ab72ba0
child 1498 681b0afc046c
permissions -rw-r--r--
+removeIdentical
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
     1
"
1cf8d1747859 Initial revision
claus
parents:
diff changeset
     2
 COPYRIGHT (c) 1993 by Claus Gittinger
68
6650e0d50a1a *** empty log message ***
claus
parents: 34
diff changeset
     3
	      All Rights Reserved
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
     4
1cf8d1747859 Initial revision
claus
parents:
diff changeset
     5
 This software is furnished under a license and may be used
1cf8d1747859 Initial revision
claus
parents:
diff changeset
     6
 only in accordance with the terms of that license and with the
1cf8d1747859 Initial revision
claus
parents:
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
1cf8d1747859 Initial revision
claus
parents:
diff changeset
     8
 be provided or otherwise made available to, or used by, any
1cf8d1747859 Initial revision
claus
parents:
diff changeset
     9
 other person.  No title to or ownership of the software is
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    10
 hereby transferred.
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    11
"
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    12
916
63fddba933d6 Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents: 845
diff changeset
    13
"{ Package: 'stx:libbasic2' }"
63fddba933d6 Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents: 845
diff changeset
    14
6
claus
parents: 3
diff changeset
    15
Queue subclass:#SharedQueue
917
df608391baa5 Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents: 916
diff changeset
    16
	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
    17
	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
    18
	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
    19
	category:'Kernel-Processes'
141
2804943fc6f0 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
    20
!
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    21
469
9b4318b56d9d commentary
Claus Gittinger <cg@exept.de>
parents: 396
diff changeset
    22
!SharedQueue class methodsFor:'documentation'!
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    23
30
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    24
copyright
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    25
"
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    26
 COPYRIGHT (c) 1993 by Claus Gittinger
68
6650e0d50a1a *** empty log message ***
claus
parents: 34
diff changeset
    27
	      All Rights Reserved
30
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    28
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    29
 This software is furnished under a license and may be used
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    30
 only in accordance with the terms of that license and with the
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    31
 inclusion of the above copyright notice.   This software may not
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    32
 be provided or otherwise made available to, or used by, any
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    33
 other person.  No title to or ownership of the software is
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    34
 hereby transferred.
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    35
"
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    36
!
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    37
30
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    38
documentation
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    39
"
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    40
    SharedQueues provide a safe mechanism for processes to communicate.
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    41
    They are basically Queues, with added secure access to the internals,
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    42
    allowing use from multiple processes (i.e. the access methods use
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    43
    critical regions to protect against confusion due to a process
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    44
    switch within a modification).
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    45
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    46
    Also, sharedQueues can be used for synchronization, since a reading
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    47
    process will be blocked when attempting to read an empty queue, while
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    48
    a writer will be blocked when attempting to write into a full queue.
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    49
    For nonBlocking read, use #isEmpty; for nonBlocking write, use #isFull.
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    50
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    51
    See samples in doc/coding.
251
58fc29adb012 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 156
diff changeset
    52
257
759055770356 documentation
Claus Gittinger <cg@exept.de>
parents: 251
diff changeset
    53
    [author:]
759055770356 documentation
Claus Gittinger <cg@exept.de>
parents: 251
diff changeset
    54
        Claus Gittinger
759055770356 documentation
Claus Gittinger <cg@exept.de>
parents: 251
diff changeset
    55
251
58fc29adb012 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 156
diff changeset
    56
    [see also:]
58fc29adb012 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 156
diff changeset
    57
        Semaphore
58fc29adb012 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 156
diff changeset
    58
        Process
469
9b4318b56d9d commentary
Claus Gittinger <cg@exept.de>
parents: 396
diff changeset
    59
        CodingExamples::SharedQueueExamples
30
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    60
"
845
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    61
!
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    62
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    63
examples
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    64
"
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    65
    |queues readers writers seqNumber accessLock accessLock2
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    66
     numbersStillToReceive|
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    67
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    68
    seqNumber := 1.
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    69
    accessLock := Semaphore forMutualExclusion.
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    70
    accessLock2 := Semaphore forMutualExclusion.
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    71
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    72
    numbersStillToReceive := BooleanArray new:100000 withAll:true.
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    73
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    74
    queues := (1 to:10) collect:[:i | SharedQueue new].
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    75
    readers := (1 to:10) collect:[:i |
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    76
                                    [   |num|
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    77
                                        10000 timesRepeat:[
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    78
                                            num := (queues at:i) next.
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    79
                                            accessLock2 critical:[
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    80
                                                (numbersStillToReceive at:num) ifFalse:[
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    81
                                                    self halt:(num printString , ' received twice')
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    82
                                                ] ifTrue:[
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    83
                                                    numbersStillToReceive at:num put:false.
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    84
                                                ].
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    85
                                            ].
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    86
                                            'num printCR.'.
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    87
                                        ].
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    88
                                    ] fork
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    89
                                 ].
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    90
    writers := (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
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    93
                                        10000 timesRepeat:[
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    94
                                            accessLock critical:[
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    95
                                                num := seqNumber.
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    96
                                                seqNumber := seqNumber + 1.
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    97
                                            ].
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    98
                                            (queues at:i) nextPut:num.
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
                                    ] fork
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   101
                                 ].
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
    readers do:[:aReader | aReader waitUntilTerminated].
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
    ' any left ? '.
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   106
    (numbersStillToReceive includes:true) ifTrue:[
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   107
        self halt:'oops - not all numbers received'
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   108
    ]
39e962f58eb3 comment & test-example
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   109
"
30
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
   110
! !
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   111
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   112
!SharedQueue methodsFor:'accessing'!
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   113
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   114
next
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   115
    "return the next value in the queue; if it its empty, wait 'til
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   116
     something is put into the receiver.
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   117
     When the datum has been removed, signal space-availability to
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   118
     writers"
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   119
916
63fddba933d6 Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents: 845
diff changeset
   120
    |value|
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   121
916
63fddba933d6 Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents: 845
diff changeset
   122
    dataAvailable wait.
931
60a44b4aa743 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 917
diff changeset
   123
    accessLock critical:[
60a44b4aa743 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 917
diff changeset
   124
        value := super next.
60a44b4aa743 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 917
diff changeset
   125
    ].
916
63fddba933d6 Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents: 845
diff changeset
   126
    spaceAvailable signal.
63fddba933d6 Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents: 845
diff changeset
   127
917
df608391baa5 Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents: 916
diff changeset
   128
    ^ value.
df608391baa5 Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents: 916
diff changeset
   129
!
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   130
967
e087e94555aa added #nextIfEmpty:
Claus Gittinger <cg@exept.de>
parents: 931
diff changeset
   131
nextIfEmpty:exceptionValue
e087e94555aa added #nextIfEmpty:
Claus Gittinger <cg@exept.de>
parents: 931
diff changeset
   132
    "return the next value in the queue; if it its empty do not wait, but return
e087e94555aa added #nextIfEmpty:
Claus Gittinger <cg@exept.de>
parents: 931
diff changeset
   133
     the value of exceptionValue.
e087e94555aa added #nextIfEmpty:
Claus Gittinger <cg@exept.de>
parents: 931
diff changeset
   134
     When a datum has been removed, signal space-availability to writers"
e087e94555aa added #nextIfEmpty:
Claus Gittinger <cg@exept.de>
parents: 931
diff changeset
   135
e087e94555aa added #nextIfEmpty:
Claus Gittinger <cg@exept.de>
parents: 931
diff changeset
   136
    |value anyRemoved|
e087e94555aa added #nextIfEmpty:
Claus Gittinger <cg@exept.de>
parents: 931
diff changeset
   137
e087e94555aa added #nextIfEmpty:
Claus Gittinger <cg@exept.de>
parents: 931
diff changeset
   138
    accessLock critical:[
e087e94555aa added #nextIfEmpty:
Claus Gittinger <cg@exept.de>
parents: 931
diff changeset
   139
        self isEmpty ifTrue:[
e087e94555aa added #nextIfEmpty:
Claus Gittinger <cg@exept.de>
parents: 931
diff changeset
   140
            value := exceptionValue value
e087e94555aa added #nextIfEmpty:
Claus Gittinger <cg@exept.de>
parents: 931
diff changeset
   141
        ] ifFalse:[
e087e94555aa added #nextIfEmpty:
Claus Gittinger <cg@exept.de>
parents: 931
diff changeset
   142
            value := super next.
e087e94555aa added #nextIfEmpty:
Claus Gittinger <cg@exept.de>
parents: 931
diff changeset
   143
            anyRemoved := true.
e087e94555aa added #nextIfEmpty:
Claus Gittinger <cg@exept.de>
parents: 931
diff changeset
   144
        ]
e087e94555aa added #nextIfEmpty:
Claus Gittinger <cg@exept.de>
parents: 931
diff changeset
   145
    ].
e087e94555aa added #nextIfEmpty:
Claus Gittinger <cg@exept.de>
parents: 931
diff changeset
   146
    anyRemoved == true ifTrue:[spaceAvailable signal].
e087e94555aa added #nextIfEmpty:
Claus Gittinger <cg@exept.de>
parents: 931
diff changeset
   147
e087e94555aa added #nextIfEmpty:
Claus Gittinger <cg@exept.de>
parents: 931
diff changeset
   148
    ^ value.
e087e94555aa added #nextIfEmpty:
Claus Gittinger <cg@exept.de>
parents: 931
diff changeset
   149
!
e087e94555aa added #nextIfEmpty:
Claus Gittinger <cg@exept.de>
parents: 931
diff changeset
   150
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   151
nextPut:anObject
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   152
    "enter anObject into the queue; wait for available space, if
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   153
     the queue is full. After the put, signal availablity of a datum
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   154
     to readers."
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   155
916
63fddba933d6 Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents: 845
diff changeset
   156
    spaceAvailable wait.
931
60a44b4aa743 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 917
diff changeset
   157
    accessLock critical:[
60a44b4aa743 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 917
diff changeset
   158
        super nextPut:anObject.
60a44b4aa743 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 917
diff changeset
   159
    ].
916
63fddba933d6 Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents: 845
diff changeset
   160
    dataAvailable signal.
917
df608391baa5 Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents: 916
diff changeset
   161
    ^ anObject.
df608391baa5 Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents: 916
diff changeset
   162
!
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
   163
1124
f653d6a20abd +nextPutFirst:
Claus Gittinger <cg@exept.de>
parents: 967
diff changeset
   164
nextPutFirst:anObject
f653d6a20abd +nextPutFirst:
Claus Gittinger <cg@exept.de>
parents: 967
diff changeset
   165
    spaceAvailable wait.
f653d6a20abd +nextPutFirst:
Claus Gittinger <cg@exept.de>
parents: 967
diff changeset
   166
    accessLock critical:[
f653d6a20abd +nextPutFirst:
Claus Gittinger <cg@exept.de>
parents: 967
diff changeset
   167
        super nextPutFirst:anObject.
f653d6a20abd +nextPutFirst:
Claus Gittinger <cg@exept.de>
parents: 967
diff changeset
   168
    ].
f653d6a20abd +nextPutFirst:
Claus Gittinger <cg@exept.de>
parents: 967
diff changeset
   169
    dataAvailable signal.
f653d6a20abd +nextPutFirst:
Claus Gittinger <cg@exept.de>
parents: 967
diff changeset
   170
    ^ anObject.
f653d6a20abd +nextPutFirst:
Claus Gittinger <cg@exept.de>
parents: 967
diff changeset
   171
!
f653d6a20abd +nextPutFirst:
Claus Gittinger <cg@exept.de>
parents: 967
diff changeset
   172
647
f937d30a8afc remove all elements in the queue:
ca
parents: 487
diff changeset
   173
removeAll
f937d30a8afc remove all elements in the queue:
ca
parents: 487
diff changeset
   174
    "remove all elements in the queue; do not wait, but
f937d30a8afc remove all elements in the queue:
ca
parents: 487
diff changeset
   175
     synchronize access to the queue.
f937d30a8afc remove all elements in the queue:
ca
parents: 487
diff changeset
   176
     If the queue was full before, signal space-availability to writers.
f937d30a8afc remove all elements in the queue:
ca
parents: 487
diff changeset
   177
     This can be used to flush queues in multi-process applications,
f937d30a8afc remove all elements in the queue:
ca
parents: 487
diff changeset
   178
     when cleanup is required."
f937d30a8afc remove all elements in the queue:
ca
parents: 487
diff changeset
   179
916
63fddba933d6 Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents: 845
diff changeset
   180
    |count|
647
f937d30a8afc remove all elements in the queue:
ca
parents: 487
diff changeset
   181
916
63fddba933d6 Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents: 845
diff changeset
   182
    count := 0.
63fddba933d6 Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents: 845
diff changeset
   183
    [(dataAvailable waitWithTimeout:0) notNil] whileTrue:[
63fddba933d6 Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents: 845
diff changeset
   184
        count := count + 1.
63fddba933d6 Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents: 845
diff changeset
   185
    ].    
917
df608391baa5 Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents: 916
diff changeset
   186
    accessLock critical:[
df608391baa5 Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents: 916
diff changeset
   187
        super removeAll.
df608391baa5 Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents: 916
diff changeset
   188
    ].
df608391baa5 Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents: 916
diff changeset
   189
    count timesRepeat:[spaceAvailable signal].
df608391baa5 Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents: 916
diff changeset
   190
!
647
f937d30a8afc remove all elements in the queue:
ca
parents: 487
diff changeset
   191
1494
8f617564556b +removeIdentical
penk
parents: 1489
diff changeset
   192
removeIdentical:anElement ifAbsent:exceptionalValue
8f617564556b +removeIdentical
penk
parents: 1489
diff changeset
   193
    |value noSuchElement|
8f617564556b +removeIdentical
penk
parents: 1489
diff changeset
   194
8f617564556b +removeIdentical
penk
parents: 1489
diff changeset
   195
    noSuchElement := false.
8f617564556b +removeIdentical
penk
parents: 1489
diff changeset
   196
    accessLock critical:[
8f617564556b +removeIdentical
penk
parents: 1489
diff changeset
   197
        value := super removeIdentical:anElement ifAbsent:[noSuchElement := true]
8f617564556b +removeIdentical
penk
parents: 1489
diff changeset
   198
    ].
8f617564556b +removeIdentical
penk
parents: 1489
diff changeset
   199
    noSuchElement ifTrue:[
8f617564556b +removeIdentical
penk
parents: 1489
diff changeset
   200
        ^ exceptionalValue value.
8f617564556b +removeIdentical
penk
parents: 1489
diff changeset
   201
    ].
8f617564556b +removeIdentical
penk
parents: 1489
diff changeset
   202
    ^ value.
8f617564556b +removeIdentical
penk
parents: 1489
diff changeset
   203
!
8f617564556b +removeIdentical
penk
parents: 1489
diff changeset
   204
396
88bd6136ee67 added #removeLast
Claus Gittinger <cg@exept.de>
parents: 257
diff changeset
   205
removeLast
88bd6136ee67 added #removeLast
Claus Gittinger <cg@exept.de>
parents: 257
diff changeset
   206
    "return the last value in the queue; if it its empty, wait 'til
88bd6136ee67 added #removeLast
Claus Gittinger <cg@exept.de>
parents: 257
diff changeset
   207
     something is put into the receiver.
88bd6136ee67 added #removeLast
Claus Gittinger <cg@exept.de>
parents: 257
diff changeset
   208
     When the datum has been removed, signal space-availability to
88bd6136ee67 added #removeLast
Claus Gittinger <cg@exept.de>
parents: 257
diff changeset
   209
     writers"
88bd6136ee67 added #removeLast
Claus Gittinger <cg@exept.de>
parents: 257
diff changeset
   210
916
63fddba933d6 Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents: 845
diff changeset
   211
    |value|
396
88bd6136ee67 added #removeLast
Claus Gittinger <cg@exept.de>
parents: 257
diff changeset
   212
916
63fddba933d6 Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents: 845
diff changeset
   213
    dataAvailable wait.
917
df608391baa5 Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents: 916
diff changeset
   214
    accessLock critical:[
df608391baa5 Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents: 916
diff changeset
   215
        value := super removeLast.
df608391baa5 Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents: 916
diff changeset
   216
    ].
916
63fddba933d6 Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents: 845
diff changeset
   217
    spaceAvailable signal.
396
88bd6136ee67 added #removeLast
Claus Gittinger <cg@exept.de>
parents: 257
diff changeset
   218
917
df608391baa5 Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents: 916
diff changeset
   219
    ^ value.
931
60a44b4aa743 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 917
diff changeset
   220
! !
60a44b4aa743 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 917
diff changeset
   221
60a44b4aa743 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 917
diff changeset
   222
!SharedQueue methodsFor:'accessing-internals'!
60a44b4aa743 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 917
diff changeset
   223
1489
88159ab72ba0 access to accessLock
Claus Gittinger <cg@exept.de>
parents: 1124
diff changeset
   224
accessLock
88159ab72ba0 access to accessLock
Claus Gittinger <cg@exept.de>
parents: 1124
diff changeset
   225
    "return the critical access-semaphore which is used internally to syncronize access"
88159ab72ba0 access to accessLock
Claus Gittinger <cg@exept.de>
parents: 1124
diff changeset
   226
88159ab72ba0 access to accessLock
Claus Gittinger <cg@exept.de>
parents: 1124
diff changeset
   227
    ^ accessLock
88159ab72ba0 access to accessLock
Claus Gittinger <cg@exept.de>
parents: 1124
diff changeset
   228
!
88159ab72ba0 access to accessLock
Claus Gittinger <cg@exept.de>
parents: 1124
diff changeset
   229
931
60a44b4aa743 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 917
diff changeset
   230
readSemaphore
60a44b4aa743 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 917
diff changeset
   231
    "return the semaphore which is signalled when data is available
60a44b4aa743 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 917
diff changeset
   232
     for reading."
60a44b4aa743 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 917
diff changeset
   233
60a44b4aa743 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 917
diff changeset
   234
    ^ dataAvailable
60a44b4aa743 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 917
diff changeset
   235
60a44b4aa743 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 917
diff changeset
   236
    "Modified: 16.12.1995 / 13:47:11 / cg"
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
60a44b4aa743 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 917
diff changeset
   239
withAccessLockedDo:aBlock
60a44b4aa743 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 917
diff changeset
   240
    "evaluate aBlock while access via next/nextPut are blocked."
60a44b4aa743 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 917
diff changeset
   241
60a44b4aa743 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 917
diff changeset
   242
    accessLock critical:aBlock
917
df608391baa5 Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents: 916
diff changeset
   243
!
396
88bd6136ee67 added #removeLast
Claus Gittinger <cg@exept.de>
parents: 257
diff changeset
   244
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
   245
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
   246
    "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
   247
     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
   248
109b1c9342b2 access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents: 141
diff changeset
   249
    ^ 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
   250
109b1c9342b2 access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents: 141
diff changeset
   251
    "Modified: 16.12.1995 / 13:47:07 / cg"
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   252
! !
141
2804943fc6f0 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   253
2804943fc6f0 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   254
!SharedQueue methodsFor:'initialization'!
2804943fc6f0 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   255
2804943fc6f0 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   256
init:size
2804943fc6f0 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   257
    "initialize the receiver for size entries"
2804943fc6f0 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   258
2804943fc6f0 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   259
    super init:size.
487
0230ee378075 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 485
diff changeset
   260
    dataAvailable := Semaphore new name:'shared q-read'.
917
df608391baa5 Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents: 916
diff changeset
   261
    spaceAvailable := (Semaphore new:size) name:'shared q-write'.
931
60a44b4aa743 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 917
diff changeset
   262
    accessLock := RecursionLock new.
487
0230ee378075 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 485
diff changeset
   263
917
df608391baa5 Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents: 916
diff changeset
   264
    "Modified: 25.1.1997 / 00:19:45 / cg"
df608391baa5 Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents: 916
diff changeset
   265
! !
141
2804943fc6f0 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   266
469
9b4318b56d9d commentary
Claus Gittinger <cg@exept.de>
parents: 396
diff changeset
   267
!SharedQueue class methodsFor:'documentation'!
141
2804943fc6f0 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   268
2804943fc6f0 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   269
version
1494
8f617564556b +removeIdentical
penk
parents: 1489
diff changeset
   270
    ^ '$Header: /cvs/stx/stx/libbasic2/SharedQueue.st,v 1.28 2004-11-19 10:04:55 penk Exp $'
141
2804943fc6f0 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   271
! !