Open Bug 1637071 Opened 6 years ago Updated 5 months ago

Restrict `moz_task::ThreadPtrHolder` to main-thread only

Categories

(Core :: XPCOM, task)

task

Tracking

()

People

(Reporter: lina, Unassigned)

Details

(Keywords: good-first-bug)

Back when bug 1482608 added ThreadPtrHolder for Rust, I had its constructor call get_current_thread, so that it could be bound to the thread that created it, not just the main thread.

That was important in an early version of the bookmark sync code, but the code that used it is long gone. In fact, creating a ThreadPtrHolder in a runnable on a background thread queue might be a footgun, if that queue schedules its runnables on multiple threads—as bug 1584568, comment 5 hints at.

Since we only ever create ThreadPtrHolders on the main thread (just like its C++ cousin), let's rename it to MainThreadPtrHolder, remove ThreadPtrHolder::owning_thread, and make it main-thread only. It'll match C++, and remove a level of indirection that gets in the way.

The premise still holds. Five years on:

  • ThreadPtrHolder is unchanged at xpcom/rust/moz_task/src/lib.rs:317, still has the owning_thread: RefPtr<nsIThread> field.
  • There is exactly one consumer left: toolkit/components/places/bookmark_sync/src/merger.rs:117,125. It wraps the callback in MergeTask::new, called from the XPCOM-exposed Merge() method (line 73) before dispatch to the async DB thread — i.e. the only live use already matches the "main-thread only" restriction lina proposed.

Scope seems small: rename to MainThreadPtrHolder (matching C++ nsMainThreadPtrHolder), drop owning_thread in favour of a main-thread getter, add a main-thread assert in the constructor, update the one caller.

Keywords: good-first-bug
You need to log in before you can comment on or make changes to this bug.