Native Threads in the RTS

Alastair Reid alastair@reid-consulting-uk.ltd.uk
26 Nov 2002 11:17:44 +0000


Nicolas Oury <Nicolas.Oury@ens-lyon.fr> writes:

> * I think that, if it is not too much complicated, it could be great
> to put many threads in the OpenGL OS thread. The goal of concurrent
> Haskell was to allow concurrency for expressivity. It would be a
> pity to lose this in part of programs for technical reason. Having
> this possibility would also be a pro this language : Haskell would
> be the only language to have safe multithreaded OpenGL programming.

Note that you can achieve the goal of having multithreaded OpenGL
programming using Wolfgang's proposal.  All you need to do is fork a
worker thread which will make all the OpenGL calls.  Any green thread
wanting to make a call just sends a request to the worker and waits
for a response.  The code would look something like this:

  type Response = ()
  type Job      = IO Response
  type Ch       = (MVar Job, MVar Response) 

  -- the worker thread runs this
  worker :: Ch -> IO ()
  worker ch@(jobs, responses) = do
    j <- readMVar jobs
    a <- j
    writeMVar responses a
    worker ch

  -- green threads call this to get work done
  doit :: Ch -> Job -> IO Response
  doit (jobs, responses) j = do
    writeMVar jobs j
    readMVar response

Things get a little tedious if you want multiple return types.

--
Alastair Reid                 alastair@reid-consulting-uk.ltd.uk  
Reid Consulting (UK) Limited  http://www.reid-consulting-uk.ltd.uk/alastair/