author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Wed, 18 Apr 2012 10:17:12 +0200 | |
changeset 2736 | 5f916a59f5e6 |
parent 2711 | 4e821abc0c2e |
child 2976 | e1da8a03d161 |
permissions | -rw-r--r-- |
2574 | 1 |
" |
2576 | 2 |
COPYRIGHT (c) 2010 by Jan Vrany, SWING Research Group. CTU in Prague |
2587 | 3 |
All Rights Reserved |
2574 | 4 |
|
2576 | 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. |
|
2574 | 25 |
" |
2587 | 26 |
"{ Package: 'stx:libbasic2' }" |
2574 | 27 |
|
28 |
Object subclass:#BackgroundJob |
|
2585
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
29 |
instanceVariableNames:'name job priority thread running' |
2574 | 30 |
classVariableNames:'' |
31 |
poolDictionaries:'' |
|
2590 | 32 |
category:'System-Support' |
2574 | 33 |
! |
34 |
||
35 |
!BackgroundJob class methodsFor:'documentation'! |
|
36 |
||
37 |
copyright |
|
38 |
" |
|
2576 | 39 |
COPYRIGHT (c) 2010 by Jan Vrany, SWING Research Group. CTU in Prague |
2587 | 40 |
All Rights Reserved |
2574 | 41 |
|
2576 | 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. |
|
2574 | 62 |
" |
2590 | 63 |
! |
64 |
||
65 |
documentation |
|
66 |
" |
|
2602
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
67 |
A BackgroundJob is a helper class for any kind of background |
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
68 |
processing. Possible uses include (but not limited to): |
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
69 |
copying files, syntax higlighting, live searching, autosave, etc. |
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
70 |
|
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
71 |
A task to be processed in a background is given to instances |
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
72 |
in a form of a block or a message send. The background task must |
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
73 |
be then started by sending a #start message to the instance of |
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
74 |
the job. A job may be restarted any time by sending #restart or |
2611 | 75 |
terminated by sending #stop. Sending #start to already started |
2602
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
76 |
job does nothing. |
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
77 |
|
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
78 |
Implementation note: |
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
79 |
The the task is actually processed in a separate, exclusive |
2611 | 80 |
worker thread, so an explicit synchronization have to |
2602
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
81 |
be done iff the task accesses possibly shared data. |
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
82 |
The worker exists only iff the job is actually running. When |
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
83 |
the task is finished, worker thread terminates. |
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
84 |
|
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
85 |
[author:] |
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
86 |
Jan Vrany <jan.vrany@fit.cvut.cz> |
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
87 |
|
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
88 |
[instance variables:] |
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
89 |
name <String|nil> A user friendly name of a job, |
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
90 |
useful for identifing job's thread in |
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
91 |
process list. |
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
92 |
job <Block|MessageSend> A task to perform in background. |
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
93 |
priority<Integer> A priority of worker thread. Defaults to |
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
94 |
Processor userBackgroundPriority. |
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
95 |
thread <Process|nil> The worker thread |
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
96 |
running <Boolean> Boolean value indicating whether |
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
97 |
tasks already started or not. |
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
98 |
|
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
99 |
[see also:] |
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
100 |
BackgroundQueueProcessingJob |
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
101 |
Tools::CodeHighlightingService (uses this class) |
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
102 |
|
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
103 |
|
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
104 |
|
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
105 |
" |
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
106 |
! |
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
107 |
|
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
108 |
examples |
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
109 |
" |
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
110 |
| job text | |
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
111 |
job := BackgroundJob named: 'example job' on:[ |
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
112 |
Delay waitForSeconds: 3. |
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
113 |
Transcript showCR:'One guy said: ', text |
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
114 |
]. |
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
115 |
text := 'Hello world'. |
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
116 |
job restart. |
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
117 |
Delay waitForSeconds: 5. |
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
118 |
text := 'Ahoj Svete!!'. |
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
119 |
job restart. |
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
120 |
Delay waitForSeconds: 1. |
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
121 |
text := 'Haya, I''m talking fast, you should not see the czech greeting'. |
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
122 |
job restart. |
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
123 |
|
2590 | 124 |
" |
2574 | 125 |
! ! |
126 |
||
127 |
!BackgroundJob class methodsFor:'instance creation'! |
|
128 |
||
129 |
named: name |
|
130 |
||
131 |
^self new name: name |
|
132 |
||
133 |
"Created: / 28-04-2011 / 20:26:48 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
134 |
! |
|
135 |
||
136 |
named: name on: block |
|
137 |
||
138 |
^self new name: name; job: block |
|
139 |
||
140 |
"Created: / 28-04-2011 / 20:27:52 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
141 |
! |
|
142 |
||
2585
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
143 |
new |
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
144 |
"return an initialized instance" |
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
145 |
|
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
146 |
^ self basicNew initialize. |
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
147 |
! |
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
148 |
|
2574 | 149 |
on: block |
150 |
||
151 |
^self new job: block |
|
152 |
||
153 |
"Created: / 28-04-2011 / 20:30:03 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
154 |
! ! |
|
155 |
||
156 |
!BackgroundJob methodsFor:'accessing'! |
|
157 |
||
158 |
job |
|
159 |
^ job |
|
160 |
! |
|
161 |
||
162 |
job:aBlockOrMessageSend |
|
2602
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
163 |
|
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
164 |
"Sets the job to be done. The job is sent |
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
165 |
#value from the worker thread once started" |
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
166 |
|
2574 | 167 |
job := aBlockOrMessageSend. |
2602
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
168 |
|
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
169 |
"Modified (comment): / 03-08-2011 / 21:02:41 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
2574 | 170 |
! |
171 |
||
172 |
name |
|
173 |
^ name ? '<unnamed background job>' |
|
174 |
||
175 |
"Modified: / 28-04-2011 / 20:29:52 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
176 |
! |
|
177 |
||
178 |
name:aString |
|
179 |
name := aString. |
|
2585
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
180 |
! |
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
181 |
|
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
182 |
priority |
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
183 |
^ priority |
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
184 |
! |
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
185 |
|
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
186 |
priority:anInteger |
2602
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
187 |
"Set the priority of a worker thread" |
2587 | 188 |
|
2585
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
189 |
priority := anInteger. |
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
190 |
|
2588 | 191 |
"Modified (format): / 03-08-2011 / 16:42:45 / cg" |
2602
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
192 |
"Modified (comment): / 03-08-2011 / 21:01:40 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
2736 | 193 |
! |
194 |
||
195 |
thread |
|
196 |
"Return the thread (an instance of Process) that currently |
|
197 |
processes the job or nil. if no processing is currently performed." |
|
198 |
||
199 |
^thread |
|
200 |
||
201 |
"Created: / 18-04-2012 / 10:16:16 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
2585
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
202 |
! ! |
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
203 |
|
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
204 |
!BackgroundJob methodsFor:'initialization'! |
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
205 |
|
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
206 |
initialize |
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
207 |
priority := Processor userBackgroundPriority. |
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
208 |
running := false. |
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
209 |
|
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
210 |
"/ super initialize. -- commented since inherited method does nothing |
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
211 |
|
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
212 |
"Modified: / 29-07-2011 / 10:52:41 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
2588 | 213 |
"Modified (format): / 03-08-2011 / 16:42:51 / cg" |
2574 | 214 |
! ! |
215 |
||
2706
99b71585e8d0
Refactoring to allow custom thread setup in subclasses
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2674
diff
changeset
|
216 |
!BackgroundJob methodsFor:'private'! |
99b71585e8d0
Refactoring to allow custom thread setup in subclasses
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2674
diff
changeset
|
217 |
|
99b71585e8d0
Refactoring to allow custom thread setup in subclasses
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2674
diff
changeset
|
218 |
setupThread: t priority: p |
99b71585e8d0
Refactoring to allow custom thread setup in subclasses
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2674
diff
changeset
|
219 |
"Sets up worker thread" |
99b71585e8d0
Refactoring to allow custom thread setup in subclasses
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2674
diff
changeset
|
220 |
|
99b71585e8d0
Refactoring to allow custom thread setup in subclasses
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2674
diff
changeset
|
221 |
t priority: p. |
99b71585e8d0
Refactoring to allow custom thread setup in subclasses
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2674
diff
changeset
|
222 |
t name: name. |
99b71585e8d0
Refactoring to allow custom thread setup in subclasses
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2674
diff
changeset
|
223 |
|
99b71585e8d0
Refactoring to allow custom thread setup in subclasses
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2674
diff
changeset
|
224 |
"Created: / 20-01-2012 / 16:38:42 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
99b71585e8d0
Refactoring to allow custom thread setup in subclasses
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2674
diff
changeset
|
225 |
! ! |
99b71585e8d0
Refactoring to allow custom thread setup in subclasses
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2674
diff
changeset
|
226 |
|
2574 | 227 |
!BackgroundJob methodsFor:'processing'! |
228 |
||
229 |
process |
|
230 |
"Do the job. May be overriden by subclasses to |
|
231 |
do some more complex operations" |
|
232 |
||
2585
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
233 |
running := true. |
2574 | 234 |
job value |
235 |
||
236 |
"Created: / 28-04-2011 / 20:23:20 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
2588 | 237 |
"Modified (format): / 03-08-2011 / 16:42:42 / cg" |
2574 | 238 |
! ! |
239 |
||
2585
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
240 |
!BackgroundJob methodsFor:'queries'! |
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
241 |
|
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
242 |
running |
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
243 |
"Return true if the job is actually running, i.e., if it |
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
244 |
is actually computing a value, contrary to #scheduled, that |
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
245 |
returns true even if computation actually did not start |
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
246 |
(i.e., thread is created but was not scheduled so far)" |
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
247 |
|
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
248 |
^running |
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
249 |
|
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
250 |
"Created: / 29-07-2011 / 10:23:04 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
2588 | 251 |
"Modified (format): / 03-08-2011 / 16:42:35 / cg" |
2585
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
252 |
! |
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
253 |
|
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
254 |
scheduled |
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
255 |
"Return true, if the job has been already started" |
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
256 |
|
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
257 |
^thread notNil |
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
258 |
|
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
259 |
"Created: / 29-07-2011 / 10:47:43 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
2588 | 260 |
"Modified (format): / 03-08-2011 / 16:42:32 / cg" |
2585
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
261 |
! ! |
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
262 |
|
2574 | 263 |
!BackgroundJob methodsFor:'start & stop'! |
264 |
||
2711 | 265 |
abort |
266 |
"Abort the job, if it is running. Similar to #stop, but raises |
|
267 |
abort operation request so the job has a chance to terminate |
|
268 |
gracefully" |
|
269 |
||
270 |
| t | |
|
271 |
||
272 |
(t := thread) isNil ifTrue:[^self]. |
|
273 |
t interruptWith:[ |
|
274 |
NoHandlerError handle:[:ex | |
|
275 |
ex parameter signal == AbortOperationRequest ifTrue:[ |
|
276 |
ex return. |
|
277 |
]. |
|
278 |
ex reject. |
|
279 |
] do:[ |
|
280 |
AbortOperationRequest raise. |
|
281 |
]. |
|
282 |
]. |
|
283 |
self stop |
|
284 |
||
285 |
"Created: / 09-02-2012 / 19:34:01 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
286 |
! |
|
287 |
||
2574 | 288 |
restart |
2602
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
289 |
|
2585
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
290 |
running ifTrue:[ |
2588 | 291 |
self stop. |
2585
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
292 |
]. |
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
293 |
self start |
2574 | 294 |
|
295 |
"Created: / 28-04-2011 / 20:31:41 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
2588 | 296 |
"Modified (format): / 03-08-2011 / 16:42:39 / cg" |
2602
44d00fea6ee0
Added documentation and examples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2590
diff
changeset
|
297 |
"Modified (comment): / 03-08-2011 / 21:03:21 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
2574 | 298 |
! |
299 |
||
300 |
start |
|
2585
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
301 |
self startWithPriority: priority |
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
302 |
|
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
303 |
"Created: / 28-04-2011 / 20:23:25 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
2590 | 304 |
"Modified (format): / 03-08-2011 / 16:44:36 / cg" |
2585
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
305 |
! |
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
306 |
|
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
307 |
startWithPriority: prio |
2588 | 308 |
| t | |
2585
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
309 |
|
2588 | 310 |
((t := thread) isNil or:[t isDead]) ifTrue:[ |
311 |
thread := [ |
|
312 |
[ |
|
313 |
self process |
|
314 |
] ensure: [ |
|
315 |
running := false. |
|
316 |
thread := nil |
|
317 |
] |
|
318 |
] newProcess. |
|
2706
99b71585e8d0
Refactoring to allow custom thread setup in subclasses
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2674
diff
changeset
|
319 |
self setupThread: thread priority: prio. |
2588 | 320 |
thread resume. |
2574 | 321 |
] |
322 |
||
2585
af78d4b32e4d
- CodeHighlightingService refactored to use
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2576
diff
changeset
|
323 |
"Created: / 29-07-2011 / 11:04:22 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
2588 | 324 |
"Modified (format): / 03-08-2011 / 16:42:19 / cg" |
2706
99b71585e8d0
Refactoring to allow custom thread setup in subclasses
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2674
diff
changeset
|
325 |
"Modified: / 20-01-2012 / 16:39:08 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
2574 | 326 |
! |
327 |
||
328 |
stop |
|
329 |
| t | |
|
330 |
||
2674 | 331 |
(t := thread) isNil ifTrue:[^self]. |
2574 | 332 |
thread := nil. |
333 |
t terminate. |
|
334 |
"/ raise its prio to make it terminate quickly |
|
335 |
t priority:(Processor userSchedulingPriority + 1) |
|
336 |
||
337 |
"Created: / 28-04-2011 / 20:31:18 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
2674 | 338 |
"Modified: / 18-11-2011 / 14:07:28 / cg" |
2574 | 339 |
! ! |
340 |
||
341 |
!BackgroundJob class methodsFor:'documentation'! |
|
342 |
||
2590 | 343 |
version |
2736 | 344 |
^ '$Header: /cvs/stx/stx/libbasic2/BackgroundJob.st,v 1.12 2012-04-18 08:17:12 vrany Exp $' |
2590 | 345 |
! |
346 |
||
2574 | 347 |
version_CVS |
2736 | 348 |
^ '$Header: /cvs/stx/stx/libbasic2/BackgroundJob.st,v 1.12 2012-04-18 08:17:12 vrany Exp $' |
2574 | 349 |
! |
350 |
||
351 |
version_SVN |
|
352 |
^ '§Id§' |
|
353 |
! ! |