On Wed, Jan 21, 2009 at 11:36 PM, Belka <[email protected]> wrote:
>
> Hi!
>
> Could somebody please share some experience on how to implement daemon
> start
> and stop directives. In theory I need something like this:
> 1. "my_daemon start" - starts my app with an infinite loop of serving
> inside.
> 2. "my_daemon stop" - puts in some TVar a value signalizing, that stop is
> given - infinite loop brakes.


You can abstract this pattern:

-- runs its argument in an infinite loop, and returns an action that stops
the loop
daemon :: IO () -> IO (IO ())
daemon action = do
    stopvar <- atomically $ newTVar False
    let run = do
          stop <- atomically $ readTVar stopvar
          if stop then return () else (action >> run)
    forkIO run
    return (atomically $ writeTVar stopvar True)

TVars are overkill here, actually, an IORef would be just fine, I think.

Luke
_______________________________________________
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to