Commit Graph

532 Commits

Author SHA1 Message Date
Xidorn Quan
e6088a6eb9 Bug 1186745 part 2 - Move nsThreadSyncDispatch class to its own header file. r=froydnj 2015-09-29 09:28:22 +10:00
Xidorn Quan
8290934aa6 Bug 1186745 part 1 - Add LeakRefPtr for pointer leaking by default. r=froydnj
This class can be used instead of raw pointer for a sound leaking-by-default
behavior. Also it could take advantage of move semantic check in the future.
2015-09-29 09:28:22 +10:00
James Cheng
d9b4b77dd6 Bug 1206598 - Use universal reference to reduce the redundant copy. r=nfroyd 2015-09-20 23:30:00 +02:00
Chris Peterson
b30743a530 Bug 1207031 - Suppress -Wshadow warnings from google-breakpad headers in xpcom/threads. r=froydnj 2015-09-21 22:41:52 -07:00
Nathan Froyd
fa54535e01 Bug 1202497 - follow-up - fix static analysis bustage; r=me 2015-09-22 19:25:37 -04:00
Nathan Froyd
6f8bd8b8e5 Bug 1202497 - part 7 - make nsEventQueue use external locking; r=gerald
We want to ensure that nsThread's use of nsEventQueue uses locking done
in nsThread instead of nsEventQueue, for efficiency's sake: we only need
to lock once in nsThread, rather than the current situation of locking
in nsThread and additionally in nsEventQueue.  With the current
structure of nsEventQueue, that would mean that nsThread should be using
a Monitor internally, rather than a Mutex.

Which would be well and good, except that DOM workers use nsThread's
mutex to protect their own, internal CondVar.  Switching nsThread to use
a Monitor would mean that either:

- DOM workers drop their internal CondVar in favor of nsThread's
  Monitor-owned CondVar.  This change seems unlikely to work out well,
  because now the Monitor-owned CondVar is performing double duty:
  tracking availability of events in nsThread's event queue and
  additionally whatever DOM workers were using a CondVar for.  Having a
  single CondVar track two things in such a fashion is for Experts Only.

- DOM workers grow their own Mutex to protect their own CondVar.  Adding
  a mutex like this would change locking in subtle ways and seems
  unlikely to lead to success.

Using a Monitor in nsThread is therefore untenable, and we would like to
retain the current Mutex that lives in nsThread.  Therefore, we need to
have nsEventQueue manage its own condition variable and push the
required (Mutex) locking to the client of nsEventQueue.  This scheme
also seems more fitting: external clients merely need synchronized
access to the event queue; the details of managing notifications about
events in the event queue should be left up to the event queue itself.

Doing so also forces us to merge nsEventQueueBase and nsEventQueue:
there's no way to have nsEventQueueBase require an externally-defined
Mutex and then have nsEventQueue subclass nsEventQueueBase and provide
its own Mutex to the superclass.  C++ initialization rules (and the way
things like CondVar are constructed) simply forbid it.  But that's OK,
because we want a world where nsEventQueue is externally locked anyway,
so there's no reason to have separate classes here.

One casualty of this work is removing ChaosMode support from
nsEventQueue.  nsEventQueue had support to delay placing events into the
queue, theoretically giving other threads the chance to put events there
first.  Unfortunately, since the thread would have been holding a lock
(as is evident from the MutexAutoLock& parameter required), sleeping in
PutEvent accomplishes nothing but delaying the thread from getting
useful work done.  We should support this, but it's complicated to
figure out how to reasonably support this right now.

A wrinkle in this overall pleasant refactoring is that nsThreadPool's
threads wait for limited amounts of time for new events to be placed in
the event queue, so that they can shut themselves down if no new events
are appearing.  Setting limits on the number of threads also needs to be
able to wake up all threads, so threads can shut themselves down if
necessary.

Unfortunately, with the transition to nsEventQueue managing its own
condition variable, there's no way for nsThreadPool to perform these
functions, since there's no Monitor to wait on.  Therefore, we add a
private API for accessing the condition variable and performing the
tasks nsThreadPool needs.

Prior to all the previous patches, placing items in an nsThread's event
queue required three lock/unlock pairs: one for nsThread's Mutex, one to
enter nsEventQueue's ReentrantMonitor, and one to exit nsEventQueue's
ReentrantMonitor.  The upshot of all this work is that we now only
require one lock/unlock pair in nsThread itself, as things should be.
2015-09-20 05:13:09 -04:00
Nathan Froyd
4f4449fa7a Bug 1202497 - part 6 - make the locking requirements of nsEventQueue explicit; r=gerald
Like the previous patch, this patch is a no-op change in terms of
functionality.  It does, however, pave part of the way for forcing
clients of nsEventQueue to provide their own locking.
2015-09-21 04:34:51 -04:00
Nathan Froyd
0cd3cf3372 Bug 1202497 - part 5 - make the locking requirements of nsChainedEventQueue explicit; r=gerald
This patch is a no-op in terms of functionality.  It ensures that we're
always holding nsThread's mutex when we touch mEvents, as dictated by
the comments.  Putting this addition into its own patch will help make
the change to having nsEventQueue by guarded by a Mutex, rather than a
Monitor, somewhat clearer.
2015-09-20 04:59:56 -04:00
Nathan Froyd
9291612a7d Bug 1202497 - part 4 - lock around call to nsChainedEventQueue::HasPendingEvent; r=gerald
This is another case of an access to mEvents not being protected by
mLock.  Future patches will make this locking requirement explicit in
nsChainedEventQueue, so we won't have problems like this.  (Since
nsEventQueue has its own locking at this point, this omission didn't
matter much, but the omission will most certainly matter later.)
2015-09-20 04:47:10 -04:00
Nathan Froyd
844d0071cb Bug 1202497 - part 3 - remove nsThread::GetEvent; r=gerald
GetEvent was only called from one place, so it wasn't terribly useful as
an abstraction.  It also broke the invariant that we protect accesses to
mEvents with mLock, as documented in nsThread.h.  While upcoming patches
could have just updated GetEvent to do the necessary locking on its own,
it seemed just as easy to make the locking requirements at the callsite,
as will be done for other accesses to mEvents.
2015-09-20 04:44:51 -04:00
Nathan Froyd
e6d7c9cfe6 Bug 1202497 - part 2 - remove ReentrantMonitor specializations for nsEventQueueBase; r=gerald
Now that nsEventQueue no longer depends on ReentrantMonitors, we can get
rid of these unused specializations.
2015-09-20 04:17:05 -04:00
Nathan Froyd
e37dae4a02 Bug 1202497 - part 1 - switch nsEventQueue to use a non-reentrant Monitor; r=gerald
nsEventQueue's monitor does not require re-entrancy now that the monitor
is not externally visible.  Since ReentrantMonitors require two separate
mutex lock/unlock pairs (one on entry, and one on exit), this cuts the
amount of locking nsEventQueue's methods do by half.
2015-09-14 21:52:08 -04:00
Nicholas Nethercote
76df328230 Bug 1205941 - Make TimerFirings logging output post-processible with fix_linux_stack.py. r=glandium. 2015-09-21 17:13:51 -07:00
Nathan Froyd
037b5ceb0a Bug 1121216 - disable BackgroundHangMonitor for TSan builds; r=jchen 2015-08-04 00:27:59 -04:00
Jim Chen
0fb099680a Bug 1196381 - Eliminate breakpad dependency in ThreadStackHelper; r=nfroyd r=snorp
The breakpad dependency in ThreadStackHelper is preventing us from
upgrading our in-tree copy to a newer version (bug 1069556). This patch
gets rid of that dependency. This makes native stack frames not work
for BHR, but because of the ftp.m.o decommissioning, native
symbolication was already broken and naive stack frames already don't
work, so we don't really lose anything from this patch.

Eventually we want to make ThreadStackHelper use other means of
unwinding, such as LUL for Linux

I added | #if 0 | around the code to fill the thread context, but left
the code in because I think we'll evenually want to reuse some of that
code.
2015-09-18 09:17:10 -04:00
Nicholas Nethercote
a3897fb534 Bug 1203427 (part 6) - Add link to MDN docs about TimerFirings logging. r=me.
DONTBUILD because comment-only change.
2015-09-16 21:49:24 -07:00
Nicholas Nethercote
09b4a9e997 Bug 1203427 (part 5) - Add logging of timer firings. r=froydnj. 2015-09-10 00:50:51 -07:00
Nicholas Nethercote
c65f0f318a Bug 1203427 (part 4) - Remove trailing whitespace from nsITimer.idl. r=froydnj.
IGNORE IDL because whitespace only changes.
2015-09-14 15:57:17 -07:00
Nicholas Nethercote
6a282d633a Bug 1203427 (part 3) - Change order of InitCommon() arguments. r=froydnj.
This makes the order of |aDelay| and |aType| match those of the InitWith*()
functions.

I've made this change because the inconsistency tripped me up during the
development of part 4.
2015-09-14 15:57:17 -07:00
Kyle Huey
2943734a64 Bug 1200922: Add the ability to shut down a thread asynchronously. r=froydnj 2015-09-14 18:24:43 -07:00
Nathan Froyd
9963101cf1 Bug 1202828 - use nsEventQueue::HasPendingEvent in nsThread.cpp; r=mccr8
nsEventQueue's HasPending event is defined to simply:

  return GetEvent(false, nullptr);

So we can substitute HasPendingEvent for this particular GetEvent call
to make the code clearer.
2015-09-04 20:39:10 -04:00
Randell Jesup
1dd37ea37d Bug 1197152: Alternative to remove all sleep/wake functionality from Timers r=froydnj
CLOSED TREE
2015-09-08 11:34:09 -04:00
Nathan Froyd
fc1aef0cd3 Bug 1202667 - make TaskQueue task running slightly more efficient; r=mccr8
We can transfer the reference out of the queue of runnables instead of
taking a new reference right before we drop the old one.
2015-09-07 20:02:16 -04:00
Nathan Froyd
4ce765e87e Bug 1202667 - make TaskQueue dispatching slightly more efficient; r=mccr8
We're already holding a reference to the Runner prior to dispatching it
to the thread pool; we can pass that reference in rather than requiring
the thread pool to take a new reference to it.
2015-09-07 19:51:54 -04:00
Nathan Froyd
c3d18cd091 Bug 1195767 - part 5 - use signaling instead of broadcast when work items are placed in nsEventQueue; r=gerald
There's no reason to wake up all the threads in a thread pool when one
item gets placed in the queue.  Waking up one will serve the same
purpose and is significantly more efficient for thread pools with large
numbers of threads.
2015-09-03 16:38:18 -04:00
Nathan Froyd
667621dccc Bug 1195767 - part 4 - remove nsEventQueue::GetReentrantMonitor; r=gerald
The last commit eliminated the only client of this method, so we can
remove it now.
2015-09-03 16:37:51 -04:00
Nathan Froyd
17c15d8da0 Bug 1195767 - part 3 - modify nsThreadPool to use a non-reentrant monitor; r=gerald
There's no reason nsThreadPool needs to use a reentrant monitor for
locking its event queue.  Having it use a non-reentrant one should be
slightly more efficient, both in the general operation of the monitor,
and that we're not performing redundant locking in methods like
nsThreadPool::Run.  This change also eliminates the only usage of
nsEventQueue::GetReentrantMonitor.
2015-09-03 15:38:28 -04:00
Nathan Froyd
88405e9aba Bug 1195767 - part 2 - create an nsEventQueueBase templated over the monitor type; r=gerald
Clients of nsEventQueue don't always need fully reentrant monitors.
Let's account for that by having a base class templated on the monitor
type.  This change also opens up the possibility of having the monitor
for the event queue not owned by the event queue itself, but by the
client class, which makes a lot more sense than the current design.
2015-08-28 13:26:17 -04:00
Nathan Froyd
d40d775d1e Bug 1195767 - part 1 - remove nsCOMPtr temporary from nsEventQueue::PutEvent; r=gerald
The comment here suggests that we might AddRef/Release, but we really do
no such thing.  Let's deal with the transfer of ownership directly,
rather than going through nsCOMPtr.  This change makes the code slightly
smaller, and it also makes later refactorings to pull the lock out of
this function easier to do, since we don't have to consider how to hold
the lock within the lifetime of the nsCOMPtr temporary.
2015-08-28 14:19:49 -04:00
Nicholas Nethercote
69d088e45f Bug 1198334 (part 1) - Replace the opt-in FAIL_ON_WARNINGS with the opt-out ALLOW_COMPILER_WARNINGS. r=glandium.
The patch removes 455 occurrences of FAIL_ON_WARNINGS from moz.build files, and
adds 78 instances of ALLOW_COMPILER_WARNINGS. About half of those 78 are in
code we control and which should be removable with a little effort.
2015-08-27 20:44:53 -07:00
Cervantes Yu
aa469abcee Bug 1166207 - Load preload.js in the Nuwa process. r=khuey 2015-08-28 17:57:44 +08:00
Chris Peterson
cc68dfd296 Bug 1197563 - Polyfill __func__ for MSVC 2013 and earlier. r=froydnj 2015-08-20 23:39:18 -07:00
Miko Mynttinen
5415160f70 Bug 1197316 - Remove PR_snprintf calls in xpcom/. r=froydnj 2015-08-22 17:57:52 -07:00
Bobby Holley
a076cdeb03 Bug 1188976 - Improve MozPromise.h comment. r=me DONTBUILD 2015-08-19 17:13:45 -07:00
Bobby Holley
9a4849b006 Bug 1195867 - Hoist StateWatching and StateMirroring into XPCOM. r=froydnj 2015-08-18 15:37:06 -07:00
Bobby Holley
6c899edb65 Bug 1188976 - Hoist MozPromise into xpcom. r=froydnj 2015-08-17 14:54:45 -07:00
Kyle Huey
0a62d4d460 Bug 1179909: Refactor stable state handling. r=smaug
This is motivated by three separate but related problems:

1. Our concept of recursion depth is broken for things that run from AfterProcessNextEvent observers (e.g. Promises). We decrement the recursionDepth counter before firing observers, so a Promise callback running at the lowest event loop depth has a recursion depth of 0 (whereas a regular nsIRunnable would be 1). This is a problem because it's impossible to distinguish a Promise running after a sync XHR's onreadystatechange handler from a top-level event (since the former runs with depth 2 - 1 = 1, and the latter runs with just 1).

2. The nsIThreadObserver mechanism that is used by a lot of code to run "after" the current event is a poor fit for anything that runs script. First, the order the observers fire in is the order they were added, not anything fixed by spec. Additionally, running script can cause the event loop to spin, which is a big source of pain here (bholley has some nasty bug caused by this).

3. We run Promises from different points in the code for workers and main thread. The latter runs from XPConnect's nsIThreadObserver callbacks, while the former runs from a hardcoded call to run Promises in the worker event loop. What workers do is particularly problematic because it means we can't get the right recursion depth no matter what we do to nsThread.

The solve this, this patch does the following:

1. Consolidate some handling of microtasks and all handling of stable state from appshell and WorkerPrivate into CycleCollectedJSRuntime.
2. Make the recursionDepth counter only available to CycleCollectedJSRuntime (and its consumers) and remove it from the nsIThreadInternal and nsIThreadObserver APIs.
3. Adjust the recursionDepth counter so that microtasks run with the recursionDepth of the task they are associated with.
4. Introduce the concept of metastable state to replace appshell's RunBeforeNextEvent. Metastable state is reached after every microtask or task is completed. This provides the semantics that bent and I want for IndexedDB, where transactions autocommit at the end of a microtask and do not "spill" from one microtask into a subsequent microtask. This differs from appshell's RunBeforeNextEvent in two ways:
a) It fires between microtasks, which was the motivation for starting this.
b) It no longer ensures that we're at the same event loop depth in the native event queue. bent decided we don't care about this.
5. Reorder stable state to happen after microtasks such as Promises, per HTML. Right now we call the regular thread observers, including appshell, before the main thread observer (XPConnect), so stable state tasks happen before microtasks.
2015-08-11 06:10:46 -07:00
Bobby Holley
4a12af4a65 Bug 1190495 - Hoist TaskQueue into xpcom. r=froydnj 2015-08-11 08:55:22 -04:00
Alfredo Yang
fa4f511520 Bug 1146086: use promise to Init() in PlatformDecoderModule. r=jya,r=cpearce 2015-08-11 14:09:12 +10:00
Bobby Holley
7ab1ef1472 Bug 1190492 - Hoist AbstractThread and TaskDispatcher to xpcom. r=froydnj 2015-08-07 16:38:35 -07:00
Nicholas Nethercote
6a0cbc501c Bug 1190735 - Remove nsITimer.TYPE_REPEATING_PRECISE. r=froydnj. 2015-08-04 17:30:53 -07:00
Bobby Holley
9f9bae1800 Bug 1191063 - Followup comments. r=me DONTBUILD 2015-08-04 17:36:06 -07:00
Birunthan Mohanathas
e1f0334d06 Bug 1191100 - Remove XPIDL signature comments in .cpp files. r=ehsan
Comment-only so DONTBUILD.
2015-08-04 16:17:36 -07:00
Bobby Holley
da17ea5369 Bug 1190496 - Hoist SharedThreadPool into xpcom. r=froydnj 2015-08-04 14:00:58 -07:00
Byron Campen [:bwc]
dde8d36fb0 Bug 1059572 - Part 2: Make absolutely sure a timer is removed before reinitting it. r=nfroyd 2015-07-28 10:10:54 -05:00
Byron Campen [:bwc]
b3903a6fc7 Bug 1059572 - Part 1: Move PostTimerEvent to TimerThread to allow TimerThread's monitor to protect it. r=nfroyd 2015-07-22 12:39:34 -05:00
Kyle Huey
b8d33919e9 Bug 1185470: Remove 'Get' prefixes from hashtable iterator methods. r=froydnj 2015-07-20 20:21:28 +08:00
Benoit Girard
0d865b0172 Bug 1182516 - Add Chaos Mode environment variable MOZ_CHAOSMODE. r=roc 2015-07-14 17:29:23 -04:00
Birunthan Mohanathas
47ed3a3675 Bug 1182996 - Fix and add missing namespace comments. rs=ehsan
The bulk of this commit was generated by running:

  run-clang-tidy.py \
    -checks='-*,llvm-namespace-comment' \
    -header-filter=^/.../mozilla-central/.* \
    -fix
2015-07-13 08:25:42 -07:00
Nicholas Nethercote
00c571d40d Bug 1181445 (part 12) - Use nsBaseHashTable::Iterator in xpcom/threads/. r=froydnj. 2015-07-09 16:54:59 -07:00