servo / font-kit

A cross-platform font loading library written in Rust
Apache License 2.0
693 stars 104 forks source link

Use static-assertions to check that fonts are indeed Send and Sync #108

Open nox opened 4 years ago

nox commented 4 years ago

See https://github.com/servo/core-foundation-rs/pull/347.

pcwalton commented 4 years ago

I don't think FreeType is thread-safe in the same way though. (It's thread-safe in that the library can be used from multiple threads, but I don't think FT_Face methods take locks.) So if we're going to make fonts Sync we need to audit the methods to make sure non-thread-safe functions take &mut self.

I don't know about DirectWrite.

kaiwk commented 4 years ago

There seems be a thread safe implementation for DirectWrite:

https://github.com/Connicpu/directwrite-rs/blob/64706009b30f4601f1c4b4b6f38653856ba10cdc/src/font/mod.rs#L26

Currently, we are using:

https://github.com/vvuk/dwrote-rs/blob/a75100ca9884880692d2523128a4eb5f279ec9b9/src/font.rs#L15

richard-uk1 commented 4 years ago

The cairo docs specifically say that

You must be careful when using this function in a library or in a threaded application, because freetype's design makes it unsafe to call freetype functions simultaneously from multiple threads, (even if using distinct FT_Face objects).

So freetype is not threadsafe and you need to do your own locking (and know exactly what you have to lock: you might need a global freetype lock, I can't tell from that quote).