author | Claus Gittinger <cg@exept.de> |
Wed, 15 Feb 2017 21:46:40 +0100 | |
changeset 4325 | ccba8c047f87 |
parent 3707 | 473883f651d6 |
child 3716 | be22b8b2f524 |
child 4338 | 5b5732ca1f29 |
permissions | -rw-r--r-- |
2573 | 1 |
" |
2575 | 2 |
COPYRIGHT (c) 2010 by Jan Vrany, SWING Research Group. CTU in Prague |
2587 | 3 |
All Rights Reserved |
2573 | 4 |
|
2575 | 5 |
Permission is hereby granted, free of charge, to any person |
6 |
obtaining a copy of this software and associated documentation |
|
7 |
files (the 'Software'), to deal in the Software without |
|
8 |
restriction, including without limitation the rights to use, |
|
9 |
copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
10 |
copies of the Software, and to permit persons to whom the |
|
11 |
Software is furnished to do so, subject to the following |
|
12 |
conditions: |
|
13 |
||
14 |
The above copyright notice and this permission notice shall be |
|
15 |
included in all copies or substantial portions of the Software. |
|
16 |
||
17 |
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, |
|
18 |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
|
19 |
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
|
20 |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
|
21 |
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
|
22 |
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
|
23 |
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
|
24 |
OTHER DEALINGS IN THE SOFTWARE. |
|
2573 | 25 |
" |
2587 | 26 |
"{ Package: 'stx:libbasic2' }" |
2573 | 27 |
|
3496
15ea7983056e
Cleanup: introduced AbstractBackgroundJob, made all other jobs subclass of it.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3225
diff
changeset
|
28 |
"{ NameSpace: Smalltalk }" |
15ea7983056e
Cleanup: introduced AbstractBackgroundJob, made all other jobs subclass of it.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3225
diff
changeset
|
29 |
|
15ea7983056e
Cleanup: introduced AbstractBackgroundJob, made all other jobs subclass of it.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3225
diff
changeset
|
30 |
AbstractBackgroundJob subclass:#BackgroundQueueProcessingJob |
2696 | 31 |
instanceVariableNames:'queue queueAccessLock queueProcessedSema' |
2573 | 32 |
classVariableNames:'' |
33 |
poolDictionaries:'' |
|
2591 | 34 |
category:'System-Support' |
2573 | 35 |
! |
36 |
||
37 |
!BackgroundQueueProcessingJob class methodsFor:'documentation'! |
|
38 |
||
39 |
copyright |
|
40 |
" |
|
2575 | 41 |
COPYRIGHT (c) 2010 by Jan Vrany, SWING Research Group. CTU in Prague |
2587 | 42 |
All Rights Reserved |
2573 | 43 |
|
2575 | 44 |
Permission is hereby granted, free of charge, to any person |
45 |
obtaining a copy of this software and associated documentation |
|
46 |
files (the 'Software'), to deal in the Software without |
|
47 |
restriction, including without limitation the rights to use, |
|
48 |
copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
49 |
copies of the Software, and to permit persons to whom the |
|
50 |
Software is furnished to do so, subject to the following |
|
51 |
conditions: |
|
52 |
||
53 |
The above copyright notice and this permission notice shall be |
|
54 |
included in all copies or substantial portions of the Software. |
|
55 |
||
56 |
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, |
|
57 |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
|
58 |
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
|
59 |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
|
60 |
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
|
61 |
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
|
62 |
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
|
63 |
OTHER DEALINGS IN THE SOFTWARE. |
|
2573 | 64 |
" |
2601
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
65 |
! |
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
66 |
|
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
67 |
documentation |
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
68 |
" |
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
69 |
A BackgroundQueueProcessingJob is a specialized form |
2612 | 70 |
of a BackgroundJob for background processing of a queue. |
2601
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
71 |
Each item in the queue (added by sending #add: item) |
2612 | 72 |
is processed (using the instance variable job). |
2601
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
73 |
|
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
74 |
Implementation notes: |
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
75 |
The thread is running only if there is at least one item |
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
76 |
to process. When the queue is empty, the thread teminates. |
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
77 |
It is started again when a new item is added to the queue. |
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
78 |
|
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
79 |
[author:] |
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
80 |
Jan Vrany <jan.vrany@fit.cvut.cz> |
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
81 |
|
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
82 |
[instance variables:] |
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
83 |
queue <OrderedCollection> the queue of items to be procesed |
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
84 |
queueAccessLock<RecursionLock> a lock used to synchronize |
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
85 |
access to the queue |
2696 | 86 |
queueProcessedSema<Semaphore> a semaphore signaled when the queue is |
87 |
processed (and therefore empty). |
|
2601
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
88 |
[class variables:] |
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
89 |
|
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
90 |
[see also:] |
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
91 |
BackgroundJob |
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
92 |
|
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
93 |
" |
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
94 |
! |
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
95 |
|
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
96 |
examples |
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
97 |
" |
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
98 |
| job text | |
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
99 |
job := BackgroundQueueProcessingJob named: 'example job' on:[:text| |
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
100 |
Delay waitForSeconds: 3. |
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
101 |
Transcript showCR:'One guy said: ', text |
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
102 |
]. |
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
103 |
job add:'Hello world'. |
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
104 |
Delay waitForSeconds: 5. |
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
105 |
job add:'Ahoj Svete!!'. |
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
106 |
job restart. |
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
107 |
Delay waitForSeconds: 1. |
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
108 |
job add:'Haya, looks like proper queue, you should see all greetings'. |
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
109 |
|
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
110 |
" |
2573 | 111 |
! ! |
112 |
||
3225
a10937a309bb
class: BackgroundQueueProcessingJob
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3048
diff
changeset
|
113 |
!BackgroundQueueProcessingJob methodsFor:'accessing-private'! |
a10937a309bb
class: BackgroundQueueProcessingJob
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3048
diff
changeset
|
114 |
|
a10937a309bb
class: BackgroundQueueProcessingJob
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3048
diff
changeset
|
115 |
queue |
a10937a309bb
class: BackgroundQueueProcessingJob
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3048
diff
changeset
|
116 |
"Returns the internal queue." |
a10937a309bb
class: BackgroundQueueProcessingJob
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3048
diff
changeset
|
117 |
|
a10937a309bb
class: BackgroundQueueProcessingJob
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3048
diff
changeset
|
118 |
^ queue |
a10937a309bb
class: BackgroundQueueProcessingJob
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3048
diff
changeset
|
119 |
|
a10937a309bb
class: BackgroundQueueProcessingJob
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3048
diff
changeset
|
120 |
"Modified (format): / 16-04-2014 / 15:11:19 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
a10937a309bb
class: BackgroundQueueProcessingJob
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3048
diff
changeset
|
121 |
! |
a10937a309bb
class: BackgroundQueueProcessingJob
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3048
diff
changeset
|
122 |
|
a10937a309bb
class: BackgroundQueueProcessingJob
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3048
diff
changeset
|
123 |
queueAccessLock |
a10937a309bb
class: BackgroundQueueProcessingJob
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3048
diff
changeset
|
124 |
"Returns the internal lock used to protect internal queue" |
a10937a309bb
class: BackgroundQueueProcessingJob
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3048
diff
changeset
|
125 |
|
a10937a309bb
class: BackgroundQueueProcessingJob
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3048
diff
changeset
|
126 |
^ queueAccessLock |
a10937a309bb
class: BackgroundQueueProcessingJob
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3048
diff
changeset
|
127 |
|
a10937a309bb
class: BackgroundQueueProcessingJob
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3048
diff
changeset
|
128 |
"Modified (comment): / 16-04-2014 / 15:11:41 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
a10937a309bb
class: BackgroundQueueProcessingJob
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3048
diff
changeset
|
129 |
! ! |
a10937a309bb
class: BackgroundQueueProcessingJob
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3048
diff
changeset
|
130 |
|
2573 | 131 |
!BackgroundQueueProcessingJob methodsFor:'adding & removing'! |
132 |
||
133 |
add: object |
|
2637
41e0362d5631
support for nil objects in job's queue
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2612
diff
changeset
|
134 |
|
41e0362d5631
support for nil objects in job's queue
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2612
diff
changeset
|
135 |
^self add: object at: nil |
41e0362d5631
support for nil objects in job's queue
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2612
diff
changeset
|
136 |
|
41e0362d5631
support for nil objects in job's queue
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2612
diff
changeset
|
137 |
"Created: / 28-04-2011 / 20:40:29 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
41e0362d5631
support for nil objects in job's queue
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2612
diff
changeset
|
138 |
"Modified (format): / 03-08-2011 / 16:43:01 / cg" |
41e0362d5631
support for nil objects in job's queue
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2612
diff
changeset
|
139 |
"Modified: / 07-09-2011 / 12:35:24 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
41e0362d5631
support for nil objects in job's queue
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2612
diff
changeset
|
140 |
! |
41e0362d5631
support for nil objects in job's queue
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2612
diff
changeset
|
141 |
|
41e0362d5631
support for nil objects in job's queue
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2612
diff
changeset
|
142 |
add: object at: index |
2573 | 143 |
queueAccessLock critical:[ |
2637
41e0362d5631
support for nil objects in job's queue
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2612
diff
changeset
|
144 |
(queue includes: object) ifFalse:[ |
41e0362d5631
support for nil objects in job's queue
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2612
diff
changeset
|
145 |
index notNil ifTrue:[ |
41e0362d5631
support for nil objects in job's queue
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2612
diff
changeset
|
146 |
queue add: object at: index |
41e0362d5631
support for nil objects in job's queue
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2612
diff
changeset
|
147 |
] ifFalse:[ |
41e0362d5631
support for nil objects in job's queue
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2612
diff
changeset
|
148 |
queue add: object |
41e0362d5631
support for nil objects in job's queue
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2612
diff
changeset
|
149 |
] |
41e0362d5631
support for nil objects in job's queue
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2612
diff
changeset
|
150 |
]. |
2589 | 151 |
self start |
152 |
]. |
|
2573 | 153 |
|
2589 | 154 |
"Modified (format): / 03-08-2011 / 16:43:01 / cg" |
2637
41e0362d5631
support for nil objects in job's queue
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2612
diff
changeset
|
155 |
"Created: / 07-09-2011 / 12:34:22 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
2573 | 156 |
! ! |
157 |
||
158 |
!BackgroundQueueProcessingJob methodsFor:'initialization'! |
|
159 |
||
160 |
initialize |
|
161 |
"Invoked when a new instance is created." |
|
162 |
||
2584
a6cbe910605d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2575
diff
changeset
|
163 |
super initialize. |
2573 | 164 |
queue := OrderedCollection new. |
165 |
queueAccessLock := RecursionLock new. |
|
2696 | 166 |
queueProcessedSema := Semaphore new. |
2573 | 167 |
|
2696 | 168 |
"Modified: / 06-12-2011 / 15:50:03 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
2573 | 169 |
! ! |
170 |
||
2707
b636f8f594e3
Bug fix in #waitUntilProcessed
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2697
diff
changeset
|
171 |
!BackgroundQueueProcessingJob methodsFor:'private'! |
b636f8f594e3
Bug fix in #waitUntilProcessed
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2697
diff
changeset
|
172 |
|
b636f8f594e3
Bug fix in #waitUntilProcessed
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2697
diff
changeset
|
173 |
setupThread: t priority: p |
b636f8f594e3
Bug fix in #waitUntilProcessed
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2697
diff
changeset
|
174 |
"Sets up worker thread" |
b636f8f594e3
Bug fix in #waitUntilProcessed
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2697
diff
changeset
|
175 |
|
b636f8f594e3
Bug fix in #waitUntilProcessed
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2697
diff
changeset
|
176 |
super setupThread: t priority: p. |
b636f8f594e3
Bug fix in #waitUntilProcessed
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2697
diff
changeset
|
177 |
t addExitAction: [ queueProcessedSema signalForAll ]. |
b636f8f594e3
Bug fix in #waitUntilProcessed
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2697
diff
changeset
|
178 |
|
b636f8f594e3
Bug fix in #waitUntilProcessed
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2697
diff
changeset
|
179 |
"Created: / 20-01-2012 / 16:40:12 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
b636f8f594e3
Bug fix in #waitUntilProcessed
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2697
diff
changeset
|
180 |
! ! |
b636f8f594e3
Bug fix in #waitUntilProcessed
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2697
diff
changeset
|
181 |
|
2573 | 182 |
!BackgroundQueueProcessingJob methodsFor:'processing'! |
183 |
||
184 |
process |
|
3704 | 185 |
[ |
3707 | 186 |
|hasMore item | |
2573 | 187 |
|
3707 | 188 |
hasMore := false. |
2589 | 189 |
queueAccessLock critical:[ |
3707 | 190 |
queue notEmpty ifTrue:[ |
191 |
hasMore := true. |
|
192 |
item := queue removeFirst |
|
193 |
] |
|
2589 | 194 |
]. |
3707 | 195 |
hasMore ifFalse:[^ self]. |
3704 | 196 |
self processItem: item. |
3707 | 197 |
item := nil. |
3704 | 198 |
] loop. |
2573 | 199 |
|
200 |
"Created: / 28-04-2011 / 20:36:35 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
2589 | 201 |
"Modified (format): / 03-08-2011 / 16:43:21 / cg" |
2707
b636f8f594e3
Bug fix in #waitUntilProcessed
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2697
diff
changeset
|
202 |
"Modified: / 20-01-2012 / 16:39:45 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
2573 | 203 |
! |
204 |
||
205 |
processItem: item |
|
206 |
||
207 |
^job value: item |
|
208 |
||
209 |
"Created: / 28-04-2011 / 20:36:51 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
210 |
! ! |
|
211 |
||
2696 | 212 |
!BackgroundQueueProcessingJob methodsFor:'queries'! |
213 |
||
2913
e7c9fce5bd3a
class: BackgroundQueueProcessingJob
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2735
diff
changeset
|
214 |
numberOfItemsInQueue |
2696 | 215 |
"Returns number of items waiting to be processed. |
216 |
May not be accurate as there is no synchronization |
|
217 |
(intentionally, for performance reasons)" |
|
2913
e7c9fce5bd3a
class: BackgroundQueueProcessingJob
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2735
diff
changeset
|
218 |
|
e7c9fce5bd3a
class: BackgroundQueueProcessingJob
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2735
diff
changeset
|
219 |
^ queue size |
2696 | 220 |
|
221 |
"Created: / 06-12-2011 / 16:01:53 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
222 |
! ! |
|
223 |
||
2735
1960c3957377
added: #stopAndRemoveAll
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2707
diff
changeset
|
224 |
!BackgroundQueueProcessingJob methodsFor:'start & stop'! |
1960c3957377
added: #stopAndRemoveAll
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2707
diff
changeset
|
225 |
|
1960c3957377
added: #stopAndRemoveAll
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2707
diff
changeset
|
226 |
stopAndRemoveAll |
1960c3957377
added: #stopAndRemoveAll
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2707
diff
changeset
|
227 |
"Terminates queue processing and remove all pending |
1960c3957377
added: #stopAndRemoveAll
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2707
diff
changeset
|
228 |
items in a queue" |
1960c3957377
added: #stopAndRemoveAll
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2707
diff
changeset
|
229 |
|
1960c3957377
added: #stopAndRemoveAll
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2707
diff
changeset
|
230 |
self stop. |
1960c3957377
added: #stopAndRemoveAll
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2707
diff
changeset
|
231 |
queueAccessLock critical:[ |
1960c3957377
added: #stopAndRemoveAll
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2707
diff
changeset
|
232 |
queue := OrderedCollection new. |
1960c3957377
added: #stopAndRemoveAll
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2707
diff
changeset
|
233 |
] |
1960c3957377
added: #stopAndRemoveAll
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2707
diff
changeset
|
234 |
|
1960c3957377
added: #stopAndRemoveAll
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2707
diff
changeset
|
235 |
"Created: / 09-04-2012 / 17:18:09 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
1960c3957377
added: #stopAndRemoveAll
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2707
diff
changeset
|
236 |
! ! |
1960c3957377
added: #stopAndRemoveAll
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2707
diff
changeset
|
237 |
|
2696 | 238 |
!BackgroundQueueProcessingJob methodsFor:'utilities'! |
239 |
||
240 |
waitUntilProcessed |
|
241 |
"Blocks the receiver until all items from the queue are processed. |
|
242 |
NOTE, that it may block forever if another thread is filling queue |
|
2697
0090025b8aa7
Fix in #waitUntilProcessed
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2696
diff
changeset
|
243 |
fast enough." |
2696 | 244 |
|
2697
0090025b8aa7
Fix in #waitUntilProcessed
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2696
diff
changeset
|
245 |
| shouldWait | |
0090025b8aa7
Fix in #waitUntilProcessed
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2696
diff
changeset
|
246 |
queueAccessLock critical:[ |
2707
b636f8f594e3
Bug fix in #waitUntilProcessed
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2697
diff
changeset
|
247 |
shouldWait := queue notEmpty. |
2697
0090025b8aa7
Fix in #waitUntilProcessed
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2696
diff
changeset
|
248 |
]. |
0090025b8aa7
Fix in #waitUntilProcessed
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2696
diff
changeset
|
249 |
shouldWait ifTrue:[ |
0090025b8aa7
Fix in #waitUntilProcessed
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2696
diff
changeset
|
250 |
self start. |
0090025b8aa7
Fix in #waitUntilProcessed
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2696
diff
changeset
|
251 |
queueProcessedSema wait. |
0090025b8aa7
Fix in #waitUntilProcessed
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2696
diff
changeset
|
252 |
] |
2696 | 253 |
|
254 |
" |
|
255 |
queueProcessedSema signalForAll |
|
256 |
" |
|
257 |
||
258 |
"Created: / 06-12-2011 / 15:58:49 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
259 |
! ! |
|
260 |
||
2573 | 261 |
!BackgroundQueueProcessingJob class methodsFor:'documentation'! |
262 |
||
2591 | 263 |
version |
3704 | 264 |
^ '$Header$' |
2591 | 265 |
! |
266 |
||
2573 | 267 |
version_CVS |
3704 | 268 |
^ '$Header$' |
2573 | 269 |
! |
270 |
||
271 |
version_SVN |
|
3704 | 272 |
^ '$Id$' |
2573 | 273 |
! ! |
2913
e7c9fce5bd3a
class: BackgroundQueueProcessingJob
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2735
diff
changeset
|
274 |