Hi Robert,

As you said, doing (2)

const char * getSingletonGlExtensionDisable()
{
    ScopedLock lock(s_mutex);
    static const char * env = getenv("OSG_GL_EXTENSION_DISABLE");
    return env;
}

instead of (1)

static const char * env = getenv("OSG_GL_EXTENSION_DISABLE");

Ensures that the singleton initialization occurs only when the function
getSingletonGlExtensionDisable is called.

The problem is that solution (1) implies that the initialization is done
in DllMain: when osg.dll is loaded, the singleton will be automatically
initialized, which leads to getenv() being called in DllMain.

Solution (2) delays the initialization until the function getSingleton()
is called. If there is no global singleton left (or more precisely, if
there is no global scoped variable initialization code calling
getSingleton()), then you have the guarantee that the singleton will
only be initialized when the user starts explicitly calling osg
functions/classes.

In other words, solution (2) guarantees that DllMain has long been
executed before any singleton initialization occurs. Which is exactly
want we want.

Regards,

Tanguy


-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Robert
Osfield
Sent: 06 August 2009 13:14
To: OpenSceneGraph Users
Subject: Re: [osg-users] Deadlock when loading osg.dll, singletons are
evil

Hi Tanguy,

On Thu, Aug 6, 2009 at 12:53 PM, Tanguy
Fautre<[email protected]> wrote:
> Unfortunately serializing the access to getenv() is not likely to
solve
> the issue. The problem is not that OSG calls several getenv() in
> parallel, the problem is that getenv() should not be called *at all*
in
> DllMain().
>
> Serializing getenv() in OSG does not guarantee that another C function
> will not be called in another thread unrelated to OSG. In fact, this
is
> exactly what is happening to us.

OK... but as yet I'm still not clear on why you suggestion avoids the
init.  Is it simple that static's in the global scope are getting
initialized automatically, while ones inside methods are only
initialized on first call... So you've moved the static initializer
into the method...

Robert.
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.or
g
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to