gecko-dev/xpcom/threads/nsIThread.idl
Bill McCloskey 9edd615af7 Bug 1382922 - Refactor event queue to allow multiple implementations (r=erahm)
This patch refactors the nsThread event queue to clean it up and to make it easier to restructure. The fundamental concepts are as follows:

Each nsThread will have a pointer to a refcounted SynchronizedEventQueue. A SynchronizedEQ takes care of doing the locking and condition variable work when posting and popping events. For the actual storage of events, it delegates to an AbstractEventQueue data structure. It keeps a UniquePtr to the AbstractEventQueue that it uses for storage.

Both SynchronizedEQ and AbstractEventQueue are abstract classes. There is only one concrete implementation of SynchronizedEQ in this patch, which is called ThreadEventQueue. ThreadEventQueue uses locks and condition variables to post and pop events the same way nsThread does. It also encapsulates the functionality that DOM workers need to implement their special event loops (PushEventQueue and PopEventQueue). In later Quantum DOM work, I plan to have another SynchronizedEQ implementation for the main thread, called SchedulerEventQueue. It will have special code for the cooperatively scheduling threads in Quantum DOM.

There are two concrete implementations of AbstractEventQueue in this patch: EventQueue and PrioritizedEventQueue. EventQueue replaces the old nsEventQueue. The other AbstractEventQueue implementation is PrioritizedEventQueue, which uses multiple queues for different event priorities.

The final major piece here is ThreadEventTarget, which splits some of the code for posting events out of nsThread. Eventually, my plan is for multiple cooperatively scheduled nsThreads to be able to share a ThreadEventTarget. In this patch, though, each nsThread has its own ThreadEventTarget. The class's purpose is just to collect some related code together.

One final note: I tried to avoid virtual dispatch overhead as much as possible. Calls to SynchronizedEQ methods do use virtual dispatch, since I plan to use different implementations for different threads with Quantum DOM. But all the calls to EventQueue methods should be non-virtual. Although the methods are declared virtual, all the classes used are final and the concrete classes involved should all be known through templatization.

MozReview-Commit-ID: 9Evtr9oIJvx
2017-08-16 20:55:43 -07:00

157 lines
5.9 KiB
Plaintext

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsISerialEventTarget.idl"
%{C++
#include "mozilla/AlreadyAddRefed.h"
%}
[ptr] native PRThread(PRThread);
native nsIEventTargetPtr(nsIEventTarget*);
native nsISerialEventTargetPtr(nsISerialEventTarget*);
/**
* This interface provides a high-level abstraction for an operating system
* thread.
*
* Threads have a built-in event queue, and a thread is an event target that
* can receive nsIRunnable objects (events) to be processed on the thread.
*
* See nsIThreadManager for the API used to create and locate threads.
*/
[builtinclass, scriptable, uuid(5801d193-29d1-4964-a6b7-70eb697ddf2b)]
interface nsIThread : nsISerialEventTarget
{
/**
* @returns
* The NSPR thread object corresponding to this nsIThread.
*/
[noscript] readonly attribute PRThread PRThread;
/**
* @returns
* Whether or not this thread may call into JS. Used in the profiler
* to avoid some unnecessary locking.
*/
[noscript] attribute boolean CanInvokeJS;
/**
* Shutdown the thread. This method prevents further dispatch of events to
* the thread, and it causes any pending events to run to completion before
* the thread joins (see PR_JoinThread) with the current thread. During this
* method call, events for the current thread may be processed.
*
* This method MAY NOT be executed from the thread itself. Instead, it is
* meant to be executed from another thread (usually the thread that created
* this thread or the main application thread). When this function returns,
* the thread will be shutdown, and it will no longer be possible to dispatch
* events to the thread.
*
* @throws NS_ERROR_UNEXPECTED
* Indicates that this method was erroneously called when this thread was
* the current thread, that this thread was not created with a call to
* nsIThreadManager::NewThread, or if this method was called more than once
* on the thread object.
*/
void shutdown();
/**
* This method may be called to determine if there are any events ready to be
* processed. It may only be called when this thread is the current thread.
*
* Because events may be added to this thread by another thread, a "false"
* result does not mean that this thread has no pending events. It only
* means that there were no pending events when this method was called.
*
* @returns
* A boolean value that if "true" indicates that this thread has one or
* more pending events.
*
* @throws NS_ERROR_UNEXPECTED
* Indicates that this method was erroneously called when this thread was
* not the current thread.
*/
boolean hasPendingEvents();
/**
* Process the next event. If there are no pending events, then this method
* may wait -- depending on the value of the mayWait parameter -- until an
* event is dispatched to this thread. This method is re-entrant but may
* only be called if this thread is the current thread.
*
* @param mayWait
* A boolean parameter that if "true" indicates that the method may block
* the calling thread to wait for a pending event.
*
* @returns
* A boolean value that if "true" indicates that an event was processed.
*
* @throws NS_ERROR_UNEXPECTED
* Indicates that this method was erroneously called when this thread was
* not the current thread.
*/
boolean processNextEvent(in boolean mayWait);
/**
* Shutdown the thread asynchronously. This method immediately prevents
* further dispatch of events to the thread, and it causes any pending events
* to run to completion before this thread joins with the current thread.
*
* UNLIKE shutdown() this does not process events on the current thread.
* Instead it merely ensures that the current thread continues running until
* this thread has shut down.
*
* This method MAY NOT be executed from the thread itself. Instead, it is
* meant to be executed from another thread (usually the thread that created
* this thread or the main application thread). When this function returns,
* the thread will continue running until it exhausts its event queue.
*
* @throws NS_ERROR_UNEXPECTED
* Indicates that this method was erroneously called when this thread was
* the current thread, that this thread was not created with a call to
* nsIThreadManager::NewThread, or if this method was called more than once
* on the thread object.
*/
void asyncShutdown();
/**
* Dispatch an event to the thread's idle queue. This function may be called
* from any thread, and it may be called re-entrantly.
*
* @param event
* The alreadyAddRefed<> event to dispatch.
* NOTE that the event will be leaked if it fails to dispatch.
*
* @throws NS_ERROR_INVALID_ARG
* Indicates that event is null.
* @throws NS_ERROR_UNEXPECTED
* Indicates that the thread is shutting down and has finished processing
* events, so this event would never run and has not been dispatched.
*/
[noscript] void idleDispatch(in alreadyAddRefed_nsIRunnable event);
/**
* Use this attribute to dispatch runnables to the thread. Eventually, the
* eventTarget attribute will be the only way to dispatch events to a
* thread--nsIThread will no longer inherit from nsIEventTarget.
*/
readonly attribute nsIEventTarget eventTarget;
/**
* A fast C++ getter for the eventTarget.
*/
[noscript,notxpcom] nsIEventTargetPtr EventTarget();
/**
* A fast C++ getter for the eventTarget. It asserts that the thread's event
* target is an nsISerialEventTarget and then returns it.
*/
[noscript,notxpcom] nsISerialEventTargetPtr SerialEventTarget();
};