freetype
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [ft] Clarification about FT_Init_FreeType in a multithreaded environ


From: Ulrich Eckhardt
Subject: Re: [ft] Clarification about FT_Init_FreeType in a multithreaded environment
Date: Wed, 12 Jun 2013 15:18:24 +0200
User-agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130509 Thunderbird/17.0.6

Am 11.06.2013 16:33, schrieb Werner LEMBERG:
The documentation for FT_Init_FreeType says "For multi-threading
applications each thread should have its own FT_Library object." and
I'm wondering how to understand this sentence.  Is this meant as
"don't access FT_Library objects concurrently from different
threads" or does every thread have to create its own library
instance and I can't move objects between threads?  I assume the
former, but I'd like to clarify that first.

Actually, it's the latter.  Calls to FreeType are only thread-safe if
you have one FT_Library object per thread.

When rendering text, I would
1. Lock the library mutex.
2. If the faces is not cached, load it now and store it in the cache.
3. Then, lock the face mutex and release the library mutex.
4. Load the glyphs and render their outlines.
5. Release the face mutex.

I must admit that I'm bad at multi-threading, but I think this doesn't
work in general.  For example, here's a comment from one of the
auto-hinter's (internal) header files, `afloader.h', describing the
AF_ModuleRec structure:

   The autofitter module's (global) data structure to communicate with
   actual fonts.  If necessary, `local' data like the current face, the
   current face's auto-hint data, or the current glyph's parameters
   relevant to auto-hinting are `swapped in'.  Cf. functions like
   `af_loader_reset' and `af_loader_load_g'.

In other words, functions related to FT_Face (temporarily) manipulate
data in the various FreeType font driver, hinting, and renderer
modules, and those modules are contained in FT_Library.

Okay, so I'll definitely drop the two-level approach. Just to confirm, the "autofitter module's (global) data structure" is not really global but attached to the library, or? If it is attached to the library, two library instances are completely separate, and using them from different threads even when using the autofitter will work.


Then, does anything there use thread-local storage or write to global data structures? The reason I'm asking is that I don't understand why I can't use one library from different threads, even if I can guarantee that I'm not using it from different threads *at the same time*. But maybe we're just misunderstanding each other.


Also, what is still not completely clear to me is when I can use
FT_Done_FreeType().  For the faces, it seems that they are
reference-counted, but what about the library itself?  Will an early
call pull the library from under my feet?

Ah, this is indeed missing in the docs.  While `FT_Done_FreeType'
simply calls `FT_Done_Library', which *is* reference-counted, it also
calls `FT_Done_Memory', which is not.

So a program with multiple threads should use `FT_New_Library' and
`FT_Done_Library' (instead of `FT_Init_FreeType' and
`FT_Done_FreeType'), providing its own FT_Memory object.

Okay...? I must admit that I don't fully understand this part of your answer yet, probably because I don't understand FreeType's structure enough. It seems that this also influences my other approaches above, I'll have to verify that.


In any case, many thanks for your help!

Uli

(Please keep me on CC when replying)

**************************************************************************************
Domino Laser GmbH, Fangdieckstra�e 75a, 22547 Hamburg, Deutschland
Gesch�ftsf�hrer: Hans Robert Dapprich, Amtsgericht Hamburg HR B62 932
**************************************************************************************
Visit our website at http://www.dominolaser.com
**************************************************************************************
Diese E-Mail einschlie�lich s�mtlicher Anh�nge ist nur f�r den 
Adressaten bestimmt und kann vertrauliche Informationen enthalten. Bitte 
benachrichtigen Sie den Absender umgehend, falls Sie nicht der beabsichtigte 
Empf�nger sein sollten. Die E-Mail ist in diesem Fall zu l�schen und darf 
weder gelesen, weitergeleitet, ver�ffentlicht oder anderweitig benutzt werden.
E-Mails k�nnen durch Dritte gelesen werden und Viren sowie nichtautorisierte 
�nderungen enthalten. Domino Laser GmbH ist f�r diese Folgen nicht 
verantwortlich.
**************************************************************************************




reply via email to

[Prev in Thread] Current Thread [Next in Thread]