[Haskell-cafe] Current situation regarding global IORefs

Robert Dockins robdockins at fastmail.fm
Fri Apr 21 10:17:44 EDT 2006


On Apr 21, 2006, at 9:56 AM, Brian Hulley wrote:

> Hi -
> I've run into the global mutable state problem described in http:// 
> www.haskell.org/hawiki/GlobalMutableState
> Since the page was last edited in March last year, I'm wondering if  
> there have been any developments or further thoughts on how to  
> safely create top level IORefs since they are absolutely essential  
> for the library I'm writing.
>
> For my library, which implements a GUI, I have a Manager module  
> which keeps track of which control currently has the keyboard focus  
> etc, and I don't want to have to pass round the state of the  
> manager to every control since this would be monstrously  
> inconvenient and a total waste of space/time, so at the moment I'm  
> reduced to:
>
>     module Manager where
>     keyboard :: IORef (Maybe Control)
>     {-# NOINLINE keyboard #-}
>     keyboard = unsafePerformIO $ newIORef Nothing
>
> The problem is that I don't know if this is guaranteed to be  
> completely safe for all Haskell compilers or even for all future  
> versions of ghc (?)

RE: the technique itself, you should also compile the module with - 
fno-cse.

RE: the design, Isn't that bit of state local to a dialog/window/ 
control group or something?  I understand that top level state is a  
problem in general that needs some sort of solution, but I'm not sure  
it's the right hammer here....

As far as I know, the only recent developments in this area are a  
rumor from the Simons that they are working on some sort of thread- 
local state which (under some sets of design decisions) can fill the  
needs of top level state.  If you press them, they might be willing  
to give some details about this.


Rob Dockins

Speak softly and drive a Sherman tank.
Laugh hard; it's a long way to the bank.
           -- TMBG



More information about the Haskell-Cafe mailing list