smalltalk
/
osmo-st-core
Archived
1
0
Fork 0

dispatcher: Introduce a dispatcher than can dispatch things...

When dispatching everything in the same context we avoid all kind
of issues with locking. If we use a truely multi threaded VM we could
probably use multiple dispatchers.
This commit is contained in:
Holger Hans Peter Freyther 2011-06-28 19:50:31 +02:00
parent 6595b815c9
commit 3386942ce2
4 changed files with 76 additions and 5 deletions

61
Dispatcher.st Normal file
View File

@ -0,0 +1,61 @@
"
(C) 2011 by Holger Hans Peter Freyther
All Rights Reserved
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"
Object subclass: Dispatcher [
| queue dispatch quit |
<category: 'OSMO-Core'>
<comment: 'I run tasks from the same context.'>
Dispatcher class >> instance [
<category: 'singleton'>
^ Smalltalk at: #OsmoDispatcher ifAbsentPut: [Dispatcher new].
]
Dispatcher class >> new [
<category: 'private'>
^ super new
initialize;
addToBeFinalized;
yourself
]
initialize [
<category: 'private'>
quit := false.
queue := SharedQueue new.
dispatch := [[quit] whileFalse: [self dispatch]] fork.
]
dispatchBlock: aBlock [
<category: 'insert'>
queue nextPut: aBlock.
]
dispatch [
| block |
block := queue next.
block on: Error do: [:error |
error logException: 'dispatch failed on "%1".' % {block} area: #core.
]
]
]
Eval [
Dispatcher instance.
]

View File

@ -30,3 +30,16 @@ Osmo.LogArea subclass: LogAreaTimer [
]
]
]
Osmo.LogArea subclass: LogAreaCore [
LogAreaCore class [
areaName [ ^ #core ]
areaDescription [ ^ 'Core related things' ]
default [
^ self new
enabled: true;
minLevel: Osmo.LogLevel debug;
yourself
]
]
]

View File

@ -143,11 +143,7 @@ bit difficult to do this race free.'>
copy do: [:each |
each timeout > now ifTrue: [^true].
sem critical: [queue remove: each].
[
each fire.
] on: Error do: [:e |
e logException: 'Execution of timer failed: %1' % {e tag} area: #timer.
]
OsmoDispatcher dispatchBlock: [each fire].
].
]
]

View File

@ -4,6 +4,7 @@
<prereq>OsmoLogging</prereq>
<filein>LogArea.st</filein>
<filein>Dispatcher.st</filein>
<filein>Timer.st</filein>
<test>