author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Wed, 03 Aug 2011 22:21:08 +0200 | |
changeset 2601 | 55c6a3887ebf |
parent 2591 | fe84525321f7 |
child 2612 | 922aa3063807 |
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 |
|
28 |
BackgroundJob subclass:#BackgroundQueueProcessingJob |
|
29 |
instanceVariableNames:'queue queueAccessLock' |
|
30 |
classVariableNames:'' |
|
31 |
poolDictionaries:'' |
|
2591 | 32 |
category:'System-Support' |
2573 | 33 |
! |
34 |
||
35 |
!BackgroundQueueProcessingJob class methodsFor:'documentation'! |
|
36 |
||
37 |
copyright |
|
38 |
" |
|
2575 | 39 |
COPYRIGHT (c) 2010 by Jan Vrany, SWING Research Group. CTU in Prague |
2587 | 40 |
All Rights Reserved |
2573 | 41 |
|
2575 | 42 |
Permission is hereby granted, free of charge, to any person |
43 |
obtaining a copy of this software and associated documentation |
|
44 |
files (the 'Software'), to deal in the Software without |
|
45 |
restriction, including without limitation the rights to use, |
|
46 |
copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
47 |
copies of the Software, and to permit persons to whom the |
|
48 |
Software is furnished to do so, subject to the following |
|
49 |
conditions: |
|
50 |
||
51 |
The above copyright notice and this permission notice shall be |
|
52 |
included in all copies or substantial portions of the Software. |
|
53 |
||
54 |
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, |
|
55 |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
|
56 |
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
|
57 |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
|
58 |
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
|
59 |
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
|
60 |
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
|
61 |
OTHER DEALINGS IN THE SOFTWARE. |
|
2573 | 62 |
" |
2601
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
63 |
! |
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
64 |
|
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
65 |
documentation |
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 |
A BackgroundQueueProcessingJob is a specialized form |
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
68 |
of a BackgroundJob for background processing os a queue. |
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
69 |
Each item in the queue (added by sending #add: item) |
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
70 |
is processed (using the ivar job). |
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
71 |
|
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
72 |
Implementation notes: |
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
73 |
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
|
74 |
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
|
75 |
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
|
76 |
|
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
77 |
[author:] |
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
78 |
Jan Vrany <jan.vrany@fit.cvut.cz> |
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
79 |
|
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
80 |
[instance variables:] |
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
81 |
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
|
82 |
queueAccessLock<RecursionLock> a lock used to synchronize |
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
83 |
access to the queue |
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
84 |
|
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
85 |
[class variables:] |
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
86 |
|
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
87 |
[see also:] |
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
88 |
BackgroundJob |
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 |
" |
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
91 |
! |
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 |
examples |
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 |
| job text | |
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
96 |
job := BackgroundQueueProcessingJob named: 'example job' on:[:text| |
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
97 |
Delay waitForSeconds: 3. |
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
98 |
Transcript showCR:'One guy said: ', text |
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
99 |
]. |
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
100 |
job add:'Hello world'. |
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
101 |
Delay waitForSeconds: 5. |
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
102 |
job add:'Ahoj Svete!!'. |
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
103 |
job restart. |
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
104 |
Delay waitForSeconds: 1. |
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
105 |
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
|
106 |
|
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
107 |
" |
2573 | 108 |
! ! |
109 |
||
110 |
!BackgroundQueueProcessingJob methodsFor:'adding & removing'! |
|
111 |
||
112 |
add: object |
|
113 |
"includes: is not synchronized, but should not harm" |
|
114 |
||
115 |
queueAccessLock critical:[ |
|
2589 | 116 |
(queue includes: object) |
117 |
ifFalse:[queue add: object]. |
|
118 |
self start |
|
119 |
]. |
|
2573 | 120 |
|
121 |
"Created: / 28-04-2011 / 20:40:29 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
122 |
"Modified: / 18-05-2011 / 23:30:28 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
2589 | 123 |
"Modified (format): / 03-08-2011 / 16:43:01 / cg" |
2573 | 124 |
! ! |
125 |
||
126 |
!BackgroundQueueProcessingJob methodsFor:'initialization'! |
|
127 |
||
128 |
initialize |
|
129 |
"Invoked when a new instance is created." |
|
130 |
||
2584
a6cbe910605d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2575
diff
changeset
|
131 |
super initialize. |
2573 | 132 |
queue := OrderedCollection new. |
133 |
queueAccessLock := RecursionLock new. |
|
134 |
||
2584
a6cbe910605d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2575
diff
changeset
|
135 |
"Modified: / 29-07-2011 / 10:22:42 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
2573 | 136 |
! ! |
137 |
||
138 |
!BackgroundQueueProcessingJob methodsFor:'processing'! |
|
139 |
||
140 |
process |
|
141 |
||
142 |
| item | |
|
143 |
||
144 |
[ |
|
2589 | 145 |
item := nil. |
146 |
queueAccessLock critical:[ |
|
147 |
queue isEmpty ifFalse:[item := queue removeFirst] |
|
148 |
]. |
|
149 |
item isNil ifFalse:[ |
|
150 |
self processItem: item |
|
151 |
]. |
|
152 |
item notNil. |
|
2573 | 153 |
] whileTrue |
154 |
||
155 |
"Created: / 28-04-2011 / 20:36:35 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
156 |
"Modified: / 18-05-2011 / 23:30:19 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
2589 | 157 |
"Modified (format): / 03-08-2011 / 16:43:21 / cg" |
2573 | 158 |
! |
159 |
||
160 |
processItem: item |
|
161 |
||
162 |
^job value: item |
|
163 |
||
164 |
"Created: / 28-04-2011 / 20:36:51 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
165 |
! ! |
|
166 |
||
167 |
!BackgroundQueueProcessingJob class methodsFor:'documentation'! |
|
168 |
||
2591 | 169 |
version |
2601
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
170 |
^ '$Header: /cvs/stx/stx/libbasic2/BackgroundQueueProcessingJob.st,v 1.7 2011-08-03 20:21:08 vrany Exp $' |
2591 | 171 |
! |
172 |
||
2573 | 173 |
version_CVS |
2601
55c6a3887ebf
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2591
diff
changeset
|
174 |
^ '$Header: /cvs/stx/stx/libbasic2/BackgroundQueueProcessingJob.st,v 1.7 2011-08-03 20:21:08 vrany Exp $' |
2573 | 175 |
! |
176 |
||
177 |
version_SVN |
|
178 |
^ '§Id§' |
|
179 |
! ! |