[HOpenGL] HOpenGL and --enable-threaded-rts

Alastair Reid reid@cs.utah.edu
18 Jun 2002 13:24:33 +0100

> Yes, my guess is that this will probably have to be done by
> manipulating the thread-local state in the RTS - grab the current
> thread-local state whenever a call is made into Haskell, and
> whenever we run a Haskell thread we have to set the appropriate
> thread-local state.  Perhaps it should be stored in the TSO and
> inherited by child threads too.

> The trouble is that there isn't a single object representing the
> whole thread-local state.  Does OpenGL use pthread_getspecific() and
> pthread_setspecific() to access its thread-local state?

This sounds like a lot of work and a porting nightmare (what do you
mean Linux/Win32/HPUX/... doesn't have thread manipulation function X,
it's available on FreeBSD/Win32/...  What if there are other forms
of thread-local state (e.g., errno)?  What about setjmp/longjmp?). 

The only viable solution I can see is to provide a way to capture the
calling thread (as a first class entity) when you call into Haskell
and to explicitly specify whcih thread to use when you call out from
Haskell.  (Hmmm, sounds like callcc for C :-)) Off the top of my head,
this might look like this:

  foreign export "static capture_thread" mycallback :: HsThread -> Int -> Char

which would provide a C function with this prototype:

  HsChar mycallback(HsInt);

and, on the callout side:

  foreign import "static with_thread" foo :: HsThread -> Float -> IO Double

which would invoke a C function with this type:

   HsDouble foo(HsFloat);

Alastair Reid        reid@cs.utah.edu        http://www.cs.utah.edu/~reid/