[Haskell-cafe] Re: [Haskell] Re: Global Variables and IO
initializers
Robert Dockins
robdockins at fastmail.fm
Mon Nov 8 13:10:00 EST 2004
As a purely practical matter, it seems like the easiest solution (to
this particular use case) is to write a small wrapper initializer in C
which is idempotent, then use FFI to call the wrapper, rather than
calling the initialization directly. This is easy enough to do with a
static local variable:
void doInit()
{
static int doneInit = 0;
if( !doneInit ) {
reallyInit();
doneInit = 1;
}
}
Then your haskell libs can call doInit any number of times, and
reallyInit will be called at most once.
Since your committed to FFI anyway (calling a C lib is the premise), the
wrapper seems a small price to pay. For Haskell-only code, something
else would be nice.
Keith Wansbrough wrote:
> Adrian Hey writes:
>
>
>>The problem is simple enough to restate for anyone who's interested.
>>"Provide a simple reliable mechanism to ensure that in a given
>> program run one particular top level IO operation cannot be executed
>> more than once."
>>
>>
>>>Can you give one concrete example of an "intended application
>>>of oneShot", so that we can either propose a concrete Haskell
>>>implementation of it, or agree that global variables really are necessary.
>>
>>Any C library which requires an explicit initialisation call before anything
>>in that library can be used (common enough IME). Accidental re-initialisation
>>(e.g. by two independent modules/libraries) will destroy any state currently
>>be used by the libraries existing "clients".
>
>
> Great, thanks, that's just what I was hoping for - I now see the
> problem you're trying to address.
>
> --KW 8-)
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
More information about the Haskell-Cafe
mailing list