author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Tue, 19 Jan 2016 07:00:28 +0000 | |
branch | jv |
changeset 3706 | 0db6c0586c81 |
parent 3704 | f19ba96f7722 |
child 3716 | be22b8b2f524 |
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 |
"includes: is not synchronized, but should not harm" |
144 |
||
145 |
queueAccessLock critical:[ |
|
2637
41e0362d5631
support for nil objects in job's queue
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2612
diff
changeset
|
146 |
(queue includes: object) ifFalse:[ |
41e0362d5631
support for nil objects in job's queue
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2612
diff
changeset
|
147 |
index notNil ifTrue:[ |
41e0362d5631
support for nil objects in job's queue
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2612
diff
changeset
|
148 |
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
|
149 |
] ifFalse:[ |
41e0362d5631
support for nil objects in job's queue
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2612
diff
changeset
|
150 |
queue add: object |
41e0362d5631
support for nil objects in job's queue
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2612
diff
changeset
|
151 |
] |
41e0362d5631
support for nil objects in job's queue
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2612
diff
changeset
|
152 |
]. |
2589 | 153 |
self start |
154 |
]. |
|
2573 | 155 |
|
2589 | 156 |
"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
|
157 |
"Created: / 07-09-2011 / 12:34:22 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
2573 | 158 |
! ! |
159 |
||
160 |
!BackgroundQueueProcessingJob methodsFor:'initialization'! |
|
161 |
||
162 |
initialize |
|
163 |
"Invoked when a new instance is created." |
|
164 |
||
2584
a6cbe910605d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2575
diff
changeset
|
165 |
super initialize. |
2573 | 166 |
queue := OrderedCollection new. |
167 |
queueAccessLock := RecursionLock new. |
|
2696 | 168 |
queueProcessedSema := Semaphore new. |
2573 | 169 |
|
2696 | 170 |
"Modified: / 06-12-2011 / 15:50:03 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
2573 | 171 |
! ! |
172 |
||
2707
b636f8f594e3
Bug fix in #waitUntilProcessed
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2697
diff
changeset
|
173 |
!BackgroundQueueProcessingJob methodsFor:'private'! |
b636f8f594e3
Bug fix in #waitUntilProcessed
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2697
diff
changeset
|
174 |
|
b636f8f594e3
Bug fix in #waitUntilProcessed
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2697
diff
changeset
|
175 |
setupThread: t priority: p |
b636f8f594e3
Bug fix in #waitUntilProcessed
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2697
diff
changeset
|
176 |
"Sets up worker thread" |
b636f8f594e3
Bug fix in #waitUntilProcessed
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2697
diff
changeset
|
177 |
|
b636f8f594e3
Bug fix in #waitUntilProcessed
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2697
diff
changeset
|
178 |
super setupThread: t priority: p. |
b636f8f594e3
Bug fix in #waitUntilProcessed
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2697
diff
changeset
|
179 |
t addExitAction: [ queueProcessedSema signalForAll ]. |
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 |
"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
|
182 |
! ! |
b636f8f594e3
Bug fix in #waitUntilProcessed
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2697
diff
changeset
|
183 |
|
2573 | 184 |
!BackgroundQueueProcessingJob methodsFor:'processing'! |
185 |
||
186 |
process |
|
3706
0db6c0586c81
Rolled back change f19ba96f7722
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3704
diff
changeset
|
187 |
|
0db6c0586c81
Rolled back change f19ba96f7722
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3704
diff
changeset
|
188 |
| cookie item | |
0db6c0586c81
Rolled back change f19ba96f7722
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3704
diff
changeset
|
189 |
|
0db6c0586c81
Rolled back change f19ba96f7722
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3704
diff
changeset
|
190 |
cookie := 'cookie'. |
3704 | 191 |
[ |
3706
0db6c0586c81
Rolled back change f19ba96f7722
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3704
diff
changeset
|
192 |
item := cookie. |
2589 | 193 |
queueAccessLock critical:[ |
3048
ee0259ea01c5
class: BackgroundQueueProcessingJob
Claus Gittinger <cg@exept.de>
parents:
2913
diff
changeset
|
194 |
queue notEmpty ifTrue:[item := queue removeFirst] |
2589 | 195 |
]. |
3706
0db6c0586c81
Rolled back change f19ba96f7722
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3704
diff
changeset
|
196 |
item ~~ cookie ifTrue:[ |
0db6c0586c81
Rolled back change f19ba96f7722
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3704
diff
changeset
|
197 |
self processItem: item |
0db6c0586c81
Rolled back change f19ba96f7722
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3704
diff
changeset
|
198 |
]. |
0db6c0586c81
Rolled back change f19ba96f7722
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3704
diff
changeset
|
199 |
item ~~ cookie. |
0db6c0586c81
Rolled back change f19ba96f7722
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3704
diff
changeset
|
200 |
] whileTrue. |
2573 | 201 |
|
202 |
"Created: / 28-04-2011 / 20:36:35 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
2589 | 203 |
"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
|
204 |
"Modified: / 20-01-2012 / 16:39:45 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
2573 | 205 |
! |
206 |
||
207 |
processItem: item |
|
208 |
||
209 |
^job value: item |
|
210 |
||
211 |
"Created: / 28-04-2011 / 20:36:51 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
212 |
! ! |
|
213 |
||
2696 | 214 |
!BackgroundQueueProcessingJob methodsFor:'queries'! |
215 |
||
2913
e7c9fce5bd3a
class: BackgroundQueueProcessingJob
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2735
diff
changeset
|
216 |
numberOfItemsInQueue |
2696 | 217 |
"Returns number of items waiting to be processed. |
218 |
May not be accurate as there is no synchronization |
|
219 |
(intentionally, for performance reasons)" |
|
2913
e7c9fce5bd3a
class: BackgroundQueueProcessingJob
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2735
diff
changeset
|
220 |
|
e7c9fce5bd3a
class: BackgroundQueueProcessingJob
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2735
diff
changeset
|
221 |
^ queue size |
2696 | 222 |
|
223 |
"Created: / 06-12-2011 / 16:01:53 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
224 |
! ! |
|
225 |
||
2735
1960c3957377
added: #stopAndRemoveAll
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2707
diff
changeset
|
226 |
!BackgroundQueueProcessingJob methodsFor:'start & stop'! |
1960c3957377
added: #stopAndRemoveAll
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2707
diff
changeset
|
227 |
|
1960c3957377
added: #stopAndRemoveAll
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2707
diff
changeset
|
228 |
stopAndRemoveAll |
1960c3957377
added: #stopAndRemoveAll
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2707
diff
changeset
|
229 |
"Terminates queue processing and remove all pending |
1960c3957377
added: #stopAndRemoveAll
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2707
diff
changeset
|
230 |
items in a queue" |
1960c3957377
added: #stopAndRemoveAll
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2707
diff
changeset
|
231 |
|
1960c3957377
added: #stopAndRemoveAll
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2707
diff
changeset
|
232 |
self stop. |
1960c3957377
added: #stopAndRemoveAll
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2707
diff
changeset
|
233 |
queueAccessLock critical:[ |
1960c3957377
added: #stopAndRemoveAll
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2707
diff
changeset
|
234 |
queue := OrderedCollection new. |
1960c3957377
added: #stopAndRemoveAll
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2707
diff
changeset
|
235 |
] |
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 |
"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
|
238 |
! ! |
1960c3957377
added: #stopAndRemoveAll
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2707
diff
changeset
|
239 |
|
2696 | 240 |
!BackgroundQueueProcessingJob methodsFor:'utilities'! |
241 |
||
242 |
waitUntilProcessed |
|
243 |
"Blocks the receiver until all items from the queue are processed. |
|
244 |
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
|
245 |
fast enough." |
2696 | 246 |
|
2697
0090025b8aa7
Fix in #waitUntilProcessed
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2696
diff
changeset
|
247 |
| shouldWait | |
0090025b8aa7
Fix in #waitUntilProcessed
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2696
diff
changeset
|
248 |
queueAccessLock critical:[ |
2707
b636f8f594e3
Bug fix in #waitUntilProcessed
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2697
diff
changeset
|
249 |
shouldWait := queue notEmpty. |
2697
0090025b8aa7
Fix in #waitUntilProcessed
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2696
diff
changeset
|
250 |
]. |
0090025b8aa7
Fix in #waitUntilProcessed
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2696
diff
changeset
|
251 |
shouldWait ifTrue:[ |
0090025b8aa7
Fix in #waitUntilProcessed
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2696
diff
changeset
|
252 |
self start. |
0090025b8aa7
Fix in #waitUntilProcessed
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2696
diff
changeset
|
253 |
queueProcessedSema wait. |
0090025b8aa7
Fix in #waitUntilProcessed
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2696
diff
changeset
|
254 |
] |
2696 | 255 |
|
256 |
" |
|
257 |
queueProcessedSema signalForAll |
|
258 |
" |
|
259 |
||
260 |
"Created: / 06-12-2011 / 15:58:49 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
261 |
! ! |
|
262 |
||
2573 | 263 |
!BackgroundQueueProcessingJob class methodsFor:'documentation'! |
264 |
||
2591 | 265 |
version |
3706
0db6c0586c81
Rolled back change f19ba96f7722
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3704
diff
changeset
|
266 |
^ '$Header: /cvs/stx/stx/libbasic2/BackgroundQueueProcessingJob.st,v 1.17 2015-02-21 10:03:53 vrany Exp $' |
2591 | 267 |
! |
268 |
||
2573 | 269 |
version_CVS |
3706
0db6c0586c81
Rolled back change f19ba96f7722
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3704
diff
changeset
|
270 |
^ '$Header: /cvs/stx/stx/libbasic2/BackgroundQueueProcessingJob.st,v 1.17 2015-02-21 10:03:53 vrany Exp $' |
2573 | 271 |
! |
272 |
||
273 |
version_SVN |
|
3706
0db6c0586c81
Rolled back change f19ba96f7722
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3704
diff
changeset
|
274 |
^ '$Id: BackgroundQueueProcessingJob.st,v 1.17 2015-02-21 10:03:53 vrany Exp $' |
2573 | 275 |
! ! |
2913
e7c9fce5bd3a
class: BackgroundQueueProcessingJob
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2735
diff
changeset
|
276 |