[HarfBuzz] Static initialization of C++ classes in harfbuzz

Behdad Esfahbod behdad at behdad.org
Thu Apr 12 06:00:38 PDT 2012


On 01/07/2012 12:40 PM, Petr Kobalíček wrote:
> Hi list,
> 
> While porting harfbuzz to be used with Fog I encountered that the
> static initialization of some C++ classes (global mutex and langs
> list) happened before Fog and this caused runtime error which I'm
> unable to fix without making changes to harfbuzz. Static
> initialization is unpredictable and the behavior is different across
> C++ compilers.
> 
> There is no way how to fix this error without making changes to
> harfbuzz and I fixed it using initialization functions called:
>
>   - hb_init()
>   - hb_shutdown()

I very much want to avoid having hb_init().  This is a hopeless pattern in
system libraries and I don't want to repeat it with HarfBuzz.


> These functions can be called from static class if the static
> initialization behavior is preferred:
> 
>   class HbInitializer
>   {
>     HbInitializer() { hb_init(); }
>     ~HbInitializer() { hb_shutdown(); }
>   } hb_initializer;
> 
> or separately by using my code.
> 
> The question is: Is this good idea to include in harfbuzz or I should
> keep my changes in Fog, making this change internal and not visible to
> the master?

The reason I use static initializers in HarfBuzz is to avoid locking in
hb_shape(), and unless someone suggests a better way to do that, I'm leaning
on keeping the current infrastructure since it seems to be working for
platforms we care about.  My understanding is that it doesn't work for you
because you are also patching harfbuzz to use your own mutex and stuff. (I'm
not sure why you do that BTW, other than "just because I have those implemented").

b


> Because if designed in such way, one can use something like:
> 
> #if !defined(HB_NO_STATIC_INIT)
>   class HbInitializer
>   {
>     HbInitializer() { hb_init(); }
>     ~HbInitializer() { hb_shutdown(); }
>   } hb_initializer;
> #endif /* HB_NO_STATIC_INIT */
> 
> to have complete control of the initialization process.
> 
> Best regards
> Petr Kobalicek
> _______________________________________________
> HarfBuzz mailing list
> HarfBuzz at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/harfbuzz
> 



More information about the HarfBuzz mailing list