mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-23 02:44:24 +01:00
Merge pull request #8943 from dotCooCoo/fix/font-version-rescan-and-steady-gate
Fonts: a rescan clears the version record after it publishes its catalog
This commit is contained in:
3 files changed
+94
-13
No files matched your search
@@ -374,8 +374,6 @@ impl FontDb {
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn scan_font_dirs(&self) -> usize {
|
||||
// Files may have been installed since: other versions are looked for again.
|
||||
self.version_scanned.write().unwrap_or_else(|e| e.into_inner()).clear();
|
||||
let mut catalog = Catalog::new();
|
||||
let mut aliases: HashMap<String, Vec<Alias>> = HashMap::new();
|
||||
let mut postscript = PostScriptNames::new();
|
||||
@@ -455,6 +453,9 @@ impl FontDb {
|
||||
let _change = self.cataloged.get().is_some().then(crate::test_fonts::font_change);
|
||||
*self.stamp.lock().unwrap_or_else(|e| e.into_inner()) = Some(ScanStamp { paths, modified: modified_at });
|
||||
*self.catalog.write().unwrap_or_else(|e| e.into_inner()) = catalog;
|
||||
// Files may have been installed since: other versions are looked for again, in this
|
||||
// catalog. A version lookup made during the scan read the previous one.
|
||||
self.version_scanned.write().unwrap_or_else(|e| e.into_inner()).clear();
|
||||
*self.aliases.write().unwrap_or_else(|e| e.into_inner()) = aliases;
|
||||
*self.postscript.write().unwrap_or_else(|e| e.into_inner()) = postscript;
|
||||
// Faces loaded from a file this scan did not catalog (the font was removed, or its folder
|
||||
|
||||
@@ -105,12 +105,17 @@ impl Drop for SteadyFonts {
|
||||
|
||||
/// [`SteadyFonts`] for `db`. Catalogs `db`'s installed fonts first: a first scan does not wait for
|
||||
/// any guard, so this thread can wait for one running on another thread while it holds a guard. A
|
||||
/// thread's first guard waits for the font changes under way or waiting.
|
||||
/// thread's first guard waits for a font change under way, and for a waiting change that can start
|
||||
/// now. It does not wait for a change that is waiting for guards other threads hold: the thread
|
||||
/// waiting for this guard may hold one of them.
|
||||
pub fn steady_fonts(db: &crate::FontDb) -> SteadyFonts {
|
||||
db.ensure_catalog();
|
||||
if STEADY.with(std::cell::Cell::get) == 0 {
|
||||
let mut s = steadiness();
|
||||
while !CHANGING.with(std::cell::Cell::get) && (s.changing || s.waiting > 0) {
|
||||
let waits = |s: &Steadiness| !CHANGING.with(std::cell::Cell::get) && (s.changing || (s.waiting > 0 && s.holders == s.holders_waiting));
|
||||
#[cfg(test)]
|
||||
gate_decisions().push((std::thread::current().id(), GateAsk::Guard, waits(&s)));
|
||||
while waits(&s) {
|
||||
s = await_steadiness(s);
|
||||
}
|
||||
s.holders += 1;
|
||||
@@ -150,7 +155,10 @@ pub(crate) fn font_change() -> FontChange {
|
||||
font_changers().push(std::thread::current().id());
|
||||
s.waiting += 1;
|
||||
s.holders_waiting += usize::from(holder);
|
||||
while s.changing || if holder { s.holders > s.holders_waiting } else { s.holders > 0 } {
|
||||
let waits = |s: &Steadiness| s.changing || if holder { s.holders > s.holders_waiting } else { s.holders > 0 };
|
||||
#[cfg(test)]
|
||||
gate_decisions().push((std::thread::current().id(), GateAsk::Change, waits(&s)));
|
||||
while waits(&s) {
|
||||
s = await_steadiness(s);
|
||||
}
|
||||
s.waiting -= 1;
|
||||
@@ -175,6 +183,22 @@ pub(crate) fn font_changers() -> std::sync::MutexGuard<'static, Vec<std::thread:
|
||||
CHANGERS.lock().unwrap_or_else(std::sync::PoisonError::into_inner)
|
||||
}
|
||||
|
||||
/// What a thread asked the gate for: its first [`SteadyFonts`] or a [`FontChange`].
|
||||
#[cfg(test)]
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub(crate) enum GateAsk {
|
||||
Guard,
|
||||
Change,
|
||||
}
|
||||
|
||||
/// Each ask made of the gate, with whether it waited when it was made, recorded under the gate's
|
||||
/// lock ([`steady_fonts`], [`font_change`]).
|
||||
#[cfg(test)]
|
||||
pub(crate) fn gate_decisions() -> std::sync::MutexGuard<'static, Vec<(std::thread::ThreadId, GateAsk, bool)>> {
|
||||
static DECISIONS: std::sync::Mutex<Vec<(std::thread::ThreadId, GateAsk, bool)>> = std::sync::Mutex::new(Vec::new());
|
||||
DECISIONS.lock().unwrap_or_else(std::sync::PoisonError::into_inner)
|
||||
}
|
||||
|
||||
/// The family of [`variable_font`].
|
||||
pub const VARIABLE_FAMILY: &str = "Varitest Sans";
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ use std::path::{Path, PathBuf};
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::fontdb::fonts_outside;
|
||||
use crate::test_fonts::GateAsk;
|
||||
use crate::*;
|
||||
|
||||
const FAMILY: &str = "Sysfont Sans3";
|
||||
@@ -83,10 +84,8 @@ fn steady_fonts_hold_back_other_threads_font_changes() {
|
||||
let _steady = guarded.then(|| crate::test_fonts::steady_fonts(&db));
|
||||
db.add_font(font)
|
||||
});
|
||||
let id = adding.thread().id();
|
||||
while !adding.is_finished() && !crate::test_fonts::font_changers().contains(&id) {
|
||||
std::thread::yield_now();
|
||||
}
|
||||
let waits = first_decision(adding.thread().id(), GateAsk::Change, || adding.is_finished());
|
||||
assert_eq!(waits, Some(true), "guarded {guarded}: the other thread's change waits for the guard");
|
||||
assert_eq!(db.generation(), before, "guarded {guarded}: the other thread's change waits for the guard");
|
||||
let listed = db.families().iter().any(|f| f == crate::test_fonts::VARIABLE_FAMILY);
|
||||
assert!(!listed, "guarded {guarded}: the other thread publishes the font only once the guard is dropped");
|
||||
@@ -99,10 +98,8 @@ fn steady_fonts_hold_back_other_threads_font_changes() {
|
||||
let steady = crate::test_fonts::steady_fonts(&db);
|
||||
std::thread::scope(|scope| {
|
||||
let adding = scope.spawn(|| db.add_font(crate::test_fonts::two_axis_font().unwrap()));
|
||||
let id = adding.thread().id();
|
||||
while !adding.is_finished() && !crate::test_fonts::font_changers().contains(&id) {
|
||||
std::thread::yield_now();
|
||||
}
|
||||
let waits = first_decision(adding.thread().id(), GateAsk::Change, || adding.is_finished());
|
||||
assert_eq!(waits, Some(true), "the other thread's change waits for the guard");
|
||||
let before = db.generation();
|
||||
assert!(db.add_font(crate::test_fonts::variable_font().unwrap()) > 0);
|
||||
let after = db.generation();
|
||||
@@ -114,6 +111,65 @@ fn steady_fonts_hold_back_other_threads_font_changes() {
|
||||
});
|
||||
}
|
||||
|
||||
/// Whether the first `ask` that the thread `id` made of the font gate waited, once the thread has
|
||||
/// made it; `None` when `finished` says the thread ended without making it.
|
||||
fn first_decision(id: std::thread::ThreadId, ask: GateAsk, finished: impl Fn() -> bool) -> Option<bool> {
|
||||
loop {
|
||||
let done = finished();
|
||||
let waits = crate::test_fonts::gate_decisions().iter().find(|(t, a, _)| *t == id && *a == ask).map(|(_, _, waits)| *waits);
|
||||
if waits.is_some() || done {
|
||||
return waits;
|
||||
}
|
||||
std::thread::yield_now();
|
||||
}
|
||||
}
|
||||
|
||||
/// While a thread holds [`test_fonts::steady_fonts`] and a font change on another thread waits for
|
||||
/// it, a third thread's first guard does not wait for that change, which cannot start before the
|
||||
/// first guard is dropped.
|
||||
#[test]
|
||||
fn a_first_guard_does_not_wait_for_a_change_that_waits_for_other_guards() {
|
||||
let db = FontDb::with_font_dirs(vec![]);
|
||||
std::thread::scope(|scope| {
|
||||
let steady = crate::test_fonts::steady_fonts(&db);
|
||||
let adding = scope.spawn(|| db.add_font(crate::test_fonts::variable_font().unwrap()));
|
||||
assert_eq!(first_decision(adding.thread().id(), GateAsk::Change, || adding.is_finished()), Some(true), "the change waits for the guard");
|
||||
let guarding = scope.spawn(|| drop(crate::test_fonts::steady_fonts(&db)));
|
||||
let waits = first_decision(guarding.thread().id(), GateAsk::Guard, || guarding.is_finished());
|
||||
assert_eq!(waits, Some(false), "the new guard does not wait for the change that waits for this thread's guard");
|
||||
guarding.join().unwrap();
|
||||
drop(steady);
|
||||
assert!(adding.join().unwrap() > 0, "the font is new to this database");
|
||||
});
|
||||
}
|
||||
|
||||
/// A version of a family looked up while a rescan of the font folders is under way, before the
|
||||
/// rescan publishes what it found, does not hide a version of that family the rescan found.
|
||||
#[test]
|
||||
fn a_version_looked_up_during_a_rescan_is_found_after_it() {
|
||||
use crate::test_fonts::{TWIN_FAMILY, TWIN_VERSIONS, twin_font};
|
||||
let dir = std::env::temp_dir().join(format!("vc-sysfonts-{}-rescan-versions", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
std::fs::write(dir.join("Older.ttf"), twin_font(false).unwrap()).unwrap();
|
||||
let db = FontDb::with_font_dirs(vec![dir.clone()]);
|
||||
assert_eq!(db.load_system_fonts(), 1);
|
||||
let face = db.face(TWIN_FAMILY, "Regular").expect("the installed face");
|
||||
assert_eq!(face.version, TWIN_VERSIONS[0]);
|
||||
std::fs::write(dir.join("Newer.ttf"), twin_font(true).unwrap()).unwrap();
|
||||
let versions = |db: &FontDb| db.versions(&face).iter().map(|f| f.version.clone()).collect::<Vec<_>>();
|
||||
std::thread::scope(|scope| {
|
||||
let steady = crate::test_fonts::steady_fonts(&db);
|
||||
let rescanning = scope.spawn(|| db.load_system_fonts());
|
||||
let waits = first_decision(rescanning.thread().id(), GateAsk::Change, || rescanning.is_finished());
|
||||
assert_eq!(waits, Some(true), "the rescan waits for the guard before it publishes");
|
||||
assert_eq!(versions(&db), [TWIN_VERSIONS[0]], "before the rescan publishes, only the older file is cataloged");
|
||||
drop(steady);
|
||||
assert_eq!(rescanning.join().unwrap(), 2);
|
||||
});
|
||||
assert_eq!(versions(&db), TWIN_VERSIONS, "the version the rescan found is loaded");
|
||||
std::fs::remove_dir_all(dir).unwrap();
|
||||
}
|
||||
|
||||
/// The first scan of a database's installed fonts does not wait for [`test_fonts::steady_fonts`]
|
||||
/// held for another database, so a thread holding one can catalog a database that another thread
|
||||
/// is scanning.
|
||||
|
||||
Reference in new issue
Block a user