2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2014-11-15 05:47:30 +03:00
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_dom_workers_WorkerThread_h__
|
|
|
|
#define mozilla_dom_workers_WorkerThread_h__
|
|
|
|
|
|
|
|
#include "mozilla/Attributes.h"
|
2014-11-17 22:55:37 +03:00
|
|
|
#include "mozilla/CondVar.h"
|
2014-11-15 05:47:30 +03:00
|
|
|
#include "mozilla/DebugOnly.h"
|
|
|
|
#include "nsISupportsImpl.h"
|
2015-10-18 08:24:48 +03:00
|
|
|
#include "mozilla/RefPtr.h"
|
2014-11-15 05:47:30 +03:00
|
|
|
#include "nsThread.h"
|
|
|
|
|
|
|
|
class nsIRunnable;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2018-01-31 10:22:56 +03:00
|
|
|
class WorkerRunnable;
|
2018-01-31 10:24:08 +03:00
|
|
|
class WorkerPrivate;
|
|
|
|
template <class> class WorkerPrivateParent;
|
2018-01-31 10:22:56 +03:00
|
|
|
|
2018-01-31 10:24:59 +03:00
|
|
|
namespace workerinternals {
|
2014-11-17 22:55:37 +03:00
|
|
|
class RuntimeService;
|
2018-01-31 10:20:07 +03:00
|
|
|
}
|
2014-11-15 05:47:30 +03:00
|
|
|
|
2014-11-17 22:55:37 +03:00
|
|
|
// This class lets us restrict the public methods that can be called on
|
|
|
|
// WorkerThread to RuntimeService and WorkerPrivate without letting them gain
|
|
|
|
// full access to private methods (as would happen if they were simply friends).
|
|
|
|
class WorkerThreadFriendKey
|
|
|
|
{
|
2018-01-31 10:24:59 +03:00
|
|
|
friend class workerinternals::RuntimeService;
|
2018-01-31 10:24:08 +03:00
|
|
|
friend class WorkerPrivate;
|
|
|
|
friend class WorkerPrivateParent<WorkerPrivate>;
|
2014-11-17 22:55:37 +03:00
|
|
|
|
|
|
|
WorkerThreadFriendKey();
|
|
|
|
~WorkerThreadFriendKey();
|
|
|
|
};
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class WorkerThread final
|
2014-11-15 05:47:30 +03:00
|
|
|
: public nsThread
|
|
|
|
{
|
|
|
|
class Observer;
|
|
|
|
|
2017-07-07 02:20:23 +03:00
|
|
|
Mutex mLock;
|
2014-11-17 22:55:37 +03:00
|
|
|
CondVar mWorkerPrivateCondVar;
|
|
|
|
|
|
|
|
// Protected by nsThread::mLock.
|
2018-01-31 10:24:08 +03:00
|
|
|
WorkerPrivate* mWorkerPrivate;
|
2014-11-17 22:55:37 +03:00
|
|
|
|
|
|
|
// Only touched on the target thread.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Observer> mObserver;
|
2014-11-15 05:47:30 +03:00
|
|
|
|
2014-11-17 22:55:37 +03:00
|
|
|
// Protected by nsThread::mLock and waited on with mWorkerPrivateCondVar.
|
2015-01-15 00:50:03 +03:00
|
|
|
uint32_t mOtherThreadsDispatchingViaEventTarget;
|
2014-11-17 22:55:37 +03:00
|
|
|
|
2016-02-26 18:52:06 +03:00
|
|
|
#ifdef DEBUG
|
2014-11-15 05:47:30 +03:00
|
|
|
// Protected by nsThread::mLock.
|
2016-02-26 18:52:06 +03:00
|
|
|
bool mAcceptingNonWorkerRunnables;
|
|
|
|
#endif
|
2014-11-15 05:47:30 +03:00
|
|
|
|
|
|
|
public:
|
|
|
|
static already_AddRefed<WorkerThread>
|
2014-11-17 22:55:37 +03:00
|
|
|
Create(const WorkerThreadFriendKey& aKey);
|
2014-11-15 05:47:30 +03:00
|
|
|
|
|
|
|
void
|
2018-01-31 10:20:07 +03:00
|
|
|
SetWorker(const WorkerThreadFriendKey& aKey,
|
2018-01-31 10:24:08 +03:00
|
|
|
WorkerPrivate* aWorkerPrivate);
|
2014-11-15 05:47:30 +03:00
|
|
|
|
2014-11-17 22:55:37 +03:00
|
|
|
nsresult
|
|
|
|
DispatchPrimaryRunnable(const WorkerThreadFriendKey& aKey,
|
2016-06-01 03:04:54 +03:00
|
|
|
already_AddRefed<nsIRunnable> aRunnable);
|
2014-11-15 05:47:30 +03:00
|
|
|
|
2014-11-17 22:55:37 +03:00
|
|
|
nsresult
|
2015-07-10 06:21:46 +03:00
|
|
|
DispatchAnyThread(const WorkerThreadFriendKey& aKey,
|
2018-01-31 10:22:56 +03:00
|
|
|
already_AddRefed<WorkerRunnable> aWorkerRunnable);
|
2014-11-15 05:47:30 +03:00
|
|
|
|
2014-12-17 09:26:15 +03:00
|
|
|
uint32_t
|
|
|
|
RecursionDepth(const WorkerThreadFriendKey& aKey) const;
|
|
|
|
|
2018-03-06 12:19:19 +03:00
|
|
|
PerformanceCounter*
|
|
|
|
GetPerformanceCounter(nsIRunnable* aEvent) override;
|
|
|
|
|
2018-02-12 23:44:40 +03:00
|
|
|
NS_INLINE_DECL_REFCOUNTING_INHERITED(WorkerThread, nsThread)
|
2014-11-15 05:47:30 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
WorkerThread();
|
|
|
|
~WorkerThread();
|
2014-11-17 22:55:37 +03:00
|
|
|
|
|
|
|
// This should only be called by consumers that have an
|
|
|
|
// nsIEventTarget/nsIThread pointer.
|
|
|
|
NS_IMETHOD
|
2016-06-01 03:04:54 +03:00
|
|
|
Dispatch(already_AddRefed<nsIRunnable> aRunnable, uint32_t aFlags) override;
|
2015-07-10 06:21:46 +03:00
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
DispatchFromScript(nsIRunnable* aRunnable, uint32_t aFlags) override;
|
2016-05-13 01:15:43 +03:00
|
|
|
|
|
|
|
NS_IMETHOD
|
2016-06-01 03:04:54 +03:00
|
|
|
DelayedDispatch(already_AddRefed<nsIRunnable>, uint32_t) override;
|
2018-06-19 17:14:06 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
IncrementDispatchCounter();
|
2014-11-15 05:47:30 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_dom_workers_WorkerThread_h__
|