Closed Bug 1479035 Opened 6 years ago Closed 6 years ago

Cleanup lifetime handling of thread-local nsThread wrappers

Categories

(Core :: XPCOM, enhancement)

enhancement
Not set
normal

Tracking

()

RESOLVED FIXED
mozilla64
Tracking Status
firefox64 --- fixed

People

(Reporter: kmag, Assigned: kmag)

References

Details

Attachments

(3 files)

The lifetime handling of nsThread wrappers is pretty hairy, and got hairier with bug 1476405.
Comment on attachment 8995742 [details]
Bug 1479035: Part 1 - Don't create event queues for stub nsThread wrappers.

https://reviewboard.mozilla.org/r/260106/#review267142


Code analysis found 1 defect in this patch:
 - 1 defect found by clang-tidy

You can run this analysis locally with:
 - `./mach static-analysis check path/to/file.cpp` (C/C++)


If you see a problem in this automated review, please report it here: http://bit.ly/2y9N9Vx


::: dom/storage/StorageDBThread.cpp:497
(Diff revision 1)
>  void
>  StorageDBThread::ThreadFunc(void* aArg)
>  {
> +  {
> +    auto queue = MakeRefPtr<ThreadEventQueue<EventQueue>>(MakeUnique<EventQueue>());
> +    nsCOMPtr<nsIThread> thread =

Error: Unused "kungFuDeathGrip" 'nsCOMPtr<nsIThread>' objects constructed from temporary values are prohibited [clang-tidy: mozilla-kungfu-death-grip]

    nsCOMPtr<nsIThread> thread =
    ^
Blocks: 1479273
Comment on attachment 8995742 [details]
Bug 1479035: Part 1 - Don't create event queues for stub nsThread wrappers.

https://reviewboard.mozilla.org/r/260106/#review267308

This seems good.

::: xpcom/threads/nsThread.h:63
(Diff revision 1)
>  
>    nsThread(NotNull<mozilla::SynchronizedEventQueue*> aQueue,
>             MainThreadFlag aMainThread,
>             uint32_t aStackSize);
>  
> +  nsThread();

I suppose the other constructor is already public, but it would be nice if this and the existing constructor were private. =/  A follow-up, perhaps?

::: xpcom/threads/nsThread.h:187
(Diff revision 1)
>    RefPtr<mozilla::SynchronizedEventQueue> mEvents;
>    RefPtr<mozilla::ThreadEventTarget> mEventTarget;

These could probably use some comments to describe their new discriminatory role in the implementation?

::: xpcom/threads/nsThread.cpp:690
(Diff revision 1)
> +nsThread::nsThread()
> +  : mEvents(nullptr)
> +  , mEventTarget(nullptr)
> +  , mShutdownContext(nullptr)
> +  , mScriptObserver(nullptr)
> +  , mThread(nullptr)
> +  , mStackSize(0)
> +  , mNestedEventLoopDepth(0)
> +  , mCurrentEventLoopDepth(-1)
> +  , mShutdownRequired(false)
> +  , mPriority(PRIORITY_NORMAL)
> +  , mIsMainThread(NOT_MAIN_THREAD)
> +  , mCanInvokeJS(false)
> +  , mCurrentEvent(nullptr)
> +  , mCurrentEventStart(TimeStamp::Now())
> +  , mCurrentPerformanceCounter(nullptr)

Can we convert some of these member variables to member initializers in a follow-up?  Or figure out a nice way to use delegating constructors to minimize the duplication between the two constructors?

::: xpcom/threads/nsThreadManager.cpp:471
(Diff revision 1)
>    // OK, that's fine.  We'll dynamically create one :-)
> -  RefPtr<ThreadEventQueue<EventQueue>> queue =
> +  RefPtr<nsThread> thread = new nsThread();

Should we expand the comment to explicitly note that we assume anybody implicitly creating a thread through this method doesn't actually want an event queue for the thread?
Attachment #8995742 - Flags: review?(nfroyd) → review+
Blocks: 1479873
Comment on attachment 8995743 [details]
Bug 1479035: Part 2 - Get rid of PRThread to nsThread map.

Eric, are you able to review this?  I haven't had the mental space to, but I can review it early next week if you don't have time.
Attachment #8995743 - Flags: review?(nfroyd) → review?(erahm)
Comment on attachment 8995743 [details]
Bug 1479035: Part 2 - Get rid of PRThread to nsThread map.

Sorry I don't have time to get to this before PTO.
Attachment #8995743 - Flags: review?(erahm)
Hi guys, can you please review the patch or point to someone who can? This bug has other dependencies, i'm referring here to Bug 1479273 that has a very high frequency rate. 

Thank you.
Flags: needinfo?(nfroyd)
Flags: needinfo?(erahm)
I'm back, I can review. Kris can you rebase and attach as a splinter review?
Flags: needinfo?(erahm) → needinfo?(kmaglione+bmo)
Flags: needinfo?(nfroyd)
These maps hold strong references which complicate nsThread lifetime handling
considerably, and only have a couple of fringe uses. We have a linked list of
active threads that the thread manager can use for its internal enumeration
purposes, and the external uses are easily done away with, so there doesn't
seem to be much reason to keep the map around.

MozReview-Commit-ID: x7dsj6C4x8
Attachment #9007105 - Flags: review?(erahm)
Comment on attachment 9007105 [details] [diff] [review]
Part 2 - Get rid of PRThread to nsThread map

Review of attachment 9007105 [details] [diff] [review]:
-----------------------------------------------------------------

Thanks, this is a nice cleanup. r=me with minor tweaks.

::: dom/workers/WorkerPrivate.cpp
@@ +5350,5 @@
>  WorkerPrivate::IsOnWorkerThread() const
>  {
>    // This is much more complicated than it needs to be but we can't use mThread
>    // because it must be protected by mMutex and sometimes this method is called
>    // when mMutex is already locked. This method should always work.

I guess we can get rid of the wording about this being complicated :)

::: xpcom/threads/nsThread.cpp
@@ +422,5 @@
> +  return sMaxActiveThreads;
> +}
> +
> +uint32_t nsThread::sActiveThreads;
> +uint32_t nsThread::sMaxActiveThreads;

Please move these towards the beginning of the file with the other nsThread static.

::: xpcom/threads/nsThreadManager.cpp
@@ +319,5 @@
>  
>    {
>      // We gather the threads from the hashtable into a list, so that we avoid
> +    // holding the enumerator lock while calling nsIThread::Shutdown.
> +    nsTArray<RefPtr<nsThread>> threads;

Can you change the name of this to something like `threadsToShutdown` as it's no longer a full list of the threads.

::: xpcom/threads/nsThreadManager.h
@@ +83,5 @@
>                               bool aCheckingShutdown);
>  
>    static void ReleaseThread(void* aData);
>  
>    nsRefPtrHashtable<nsPtrHashKey<PRThread>, nsThread> mThreadsByPRThread;

We should remove this, right? Also we can get rid of the nsRefPtrHashtable.h include as well.
Attachment #9007105 - Flags: review?(erahm) → review+
Kris, mind finishing the patches so we can get them landed? It would stop all the failures as dependent on bug 1479273. Thanks
This seems to be missing checkin-needed, can i land it?
Flags: needinfo?(erahm)
(In reply to Andreea Pavel [:apavel] from comment #12)
> This seems to be missing checkin-needed, can i land it?

There are a few minor review comments to address before landing.
Flags: needinfo?(erahm)
Thanks Eric :)
The IPC thread is setting an observer [1] and then asserting because there's no event queue. This is fallout from part 1 of the patch. I think we need some way for the ipc thread to opt in to having an event queue.

[1] https://searchfox.org/mozilla-central/rev/ce57be88b8aa2ad03ace1b9684cd6c361be5109f/ipc/glue/MessagePump.cpp#392
(In reply to Eric Rahm [:erahm] from comment #19)
> The IPC thread is setting an observer [1] and then asserting because there's
> no event queue. This is fallout from part 1 of the patch. I think we need
> some way for the ipc thread to opt in to having an event queue.

There already is a way. I special-cased one of the event loop types, but there's another that's apparently used on Windows.
Flags: needinfo?(kmaglione+bmo)
https://hg.mozilla.org/mozilla-central/rev/cf6b1314df05
https://hg.mozilla.org/mozilla-central/rev/6164a5ced4f7
Status: NEW → RESOLVED
Closed: 6 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla64
Depends on: 1504356
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: