[Haskell-cafe] Using Atom's task scheduler for existing C code.
Tom Hawkins
tomahawkins at gmail.com
Fri Dec 11 23:04:54 EST 2009
Today we were working on integrating Atom code with some hand-written
C, and one of my colleagues posed the question: Is it possible to use
Atom just for its task scheduler for existing C code? This turns out
to be very simple. It just requires a few combinators built on top of
'action'.
-- Call a C function with type 'void f(void)'.
call :: Name -> Atom ()
call n = action (\ _ -> n ++ "()") []
-- Call a C function in its own rule, atomically.
callAtomic :: Name -> Atom ()
callAtomic n = atom n $ call n
-- Call a C function atomically with a guard condition.
callGuardedAtomic :: E Bool -> Name -> Atom ()
callGuardedAtomic g n = atom n (cond g >> call n)
Then scheduling a collection of tasks becomes easy:
-- 20 millisecond tasks.
period 20 $ do
callAtomic "task1"
callAtomic "task2"
-- 5 millisecond tasks.
period 5 $ do
callAtomic "task3"
callAtomic "task4"
-- 1 millisecond tasks.
period 1 $ do
callGuardedAtomic task5Ready "task5"
callGuardedAtomic task6Ready "task6"
I'll added these 'call' combinators in the next release of Atom.
More information about the Haskell-Cafe
mailing list