[Haskell-iPhone] Thread model questions

Daniel Peebles pumpkingod at gmail.com
Fri Jun 10 20:32:24 CEST 2011


I haven't looked specifically at ghc-iphone, but as it's just using the GHC
RTS, I'd assume the situation isn't very different from regular GHC. The
deal there is that the GHC runtime gives you green threads, and
forkIO/forkOS start new green threads. These have tiny overhead and are
scheduled cooperatively by the RTS (every time you go to allocate, which is
all the time in your average haskell program, the RTS checks to see if it
needs to switch). IO and stuff is using select behind the scenes (ghc-iphone
is built on an older version of GHC that still used select, iirc). The RTS
uses a thread pool that you specify by passing arguments like +RTS -N3 (for
three threads) to your program.

You can't actually have "bound threads" in GHC, but forkOS does give you the
guarantee that any foreign calls you make from the green thread will be made
from the same OS thread. And that should not be observably different from
ensuring all your code runs on the same OS thread.

So to address your question more directly, the way I'd probably approach
what it seems you want to do: create a new Chan for events you want to
happen from a single thread, then forkOS a reader that reads forever (the
Control.Monad.forever) and runs your foreign functions, then all your other
green haskell threads (forkIO is good enough for them) can write to the Chan
(it's designed to be safe for concurrent access and will block if necessary)
and will thus be talking to the foreign-talking thread.

Hope this helps,
Dan

On Fri, Jun 10, 2011 at 2:12 PM, David Pollak <feeder.of.the.bears at gmail.com
> wrote:

> Howdy,
>
> I've got a question about threading and thread models in ghc-iphone.
>
> My understanding is that Haskell has its own threading model that under the
> covers uses OS threads, but allows for significantly more active threads.
> Is this a correct understanding?  If so, how does the Haskell threading
> model work on the iPhone?  Is it that iOS is close enough to OS X/Darwin
> that it "just works"?
>
> If it "just works", then how does one marshal an event back onto the iOS
> "UI thread" so that the display or draw or whatever command can take place
> on the UI thread?  Is there a convention or recipe for doing so?  Would it
> be possible to put an event on the UI event queue that applies an Haskell
> closure such that the Haskell code that contains the computed value (I'd
> force the computation before marshaling the event to minimize the actual
> delay on the UI thread) then mutates the UI based on the computed value?
>
> Given the 5 second rule about an iOS app getting shut down... if there are
> Haskell threads doing Haskell-y kind of work, is there a way of notifying
> those threads en masse about the shutdown?
>
> Thanks,
>
> David
>
> --
> Lift, the simply functional web framework http://liftweb.net
> Simply Lift http://simply.liftweb.net
> Follow me: http://twitter.com/dpp
> Blog: http://goodstuff.im
>
>
> _______________________________________________
> iPhone mailing list
> iPhone at haskell.org
> http://www.haskell.org/mailman/listinfo/iphone
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/iphone/attachments/20110610/f8f0414a/attachment.htm>


More information about the iPhone mailing list