Bug 1008453. Add support for navigator.hardwareConcurrency. r=khuey

This commit is contained in:
Luke Wagner 2016-03-16 15:41:38 -04:00
Родитель ecec854b11
Коммит 83bbb15353
14 изменённых файлов: 116 добавлений и 0 удалений

Просмотреть файл

@ -48,6 +48,7 @@
#include "mozilla/dom/Voicemail.h"
#include "mozilla/dom/TVManager.h"
#include "mozilla/dom/VRDevice.h"
#include "mozilla/dom/workers/RuntimeService.h"
#include "mozilla/Hal.h"
#include "nsISiteSpecificUserAgent.h"
#include "mozilla/ClearOnShutdown.h"
@ -750,6 +751,17 @@ Navigator::JavaEnabled(ErrorResult& aRv)
return mimeType && mimeType->GetEnabledPlugin();
}
uint64_t
Navigator::HardwareConcurrency()
{
workers::RuntimeService* rts = workers::RuntimeService::GetOrCreateService();
if (!rts) {
return 1;
}
return rts->ClampedHardwareConcurrency();
}
void
Navigator::RefreshMIMEArray()
{

Просмотреть файл

@ -219,6 +219,7 @@ public:
}
PowerManager* GetMozPower(ErrorResult& aRv);
bool JavaEnabled(ErrorResult& aRv);
uint64_t HardwareConcurrency();
bool TaintEnabled()
{
return false;

Просмотреть файл

@ -326,6 +326,7 @@ skip-if = buildapp == 'mulet' || buildapp == 'b2g' || toolkit == 'android' || e1
[test_innersize_scrollport.html]
[test_messagemanager_targetchain.html]
[test_named_frames.html]
[test_navigator_hardwareConcurrency.html]
[test_navigator_resolve_identity.html]
[test_navigator_language.html]
[test_noAudioNotification.html]

Просмотреть файл

@ -0,0 +1,24 @@
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Test for Navigator.hardwareConcurrency</title>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
<script type="application/javascript">
var x = navigator.hardwareConcurrency;
is(typeof x, "number", "hardwareConcurrency should be a number.");
ok(x > 0, "hardwareConcurrency should be greater than 0.");
</script>
</head>
<body>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
</pre>
</body>
</html>

Просмотреть файл

@ -12,6 +12,7 @@
* http://www.w3.org/2012/sysapps/runtime/#extension-to-the-navigator-interface-1
* https://dvcs.w3.org/hg/gamepad/raw-file/default/gamepad.html#navigator-interface-extension
* http://www.w3.org/TR/beacon/#sec-beacon-method
* https://html.spec.whatwg.org/#navigatorconcurrenthardware
*
* © Copyright 2004-2011 Apple Computer, Inc., Mozilla Foundation, and
* Opera Software ASA. You are granted a license to use, reproduce
@ -29,6 +30,7 @@ Navigator implements NavigatorOnLine;
Navigator implements NavigatorContentUtils;
Navigator implements NavigatorStorageUtils;
Navigator implements NavigatorFeatures;
Navigator implements NavigatorConcurrentHardware;
[NoInterfaceObject, Exposed=(Window,Worker)]
interface NavigatorID {
@ -476,3 +478,8 @@ partial interface Navigator {
DOMRequest mozPay(any jwts);
};
#endif
[NoInterfaceObject, Exposed=(Window,Worker)]
interface NavigatorConcurrentHardware {
readonly attribute unsigned long long hardwareConcurrency;
};

Просмотреть файл

@ -11,3 +11,4 @@ WorkerNavigator implements NavigatorID;
WorkerNavigator implements NavigatorLanguage;
WorkerNavigator implements NavigatorOnLine;
WorkerNavigator implements NavigatorDataStore;
WorkerNavigator implements NavigatorConcurrentHardware;

Просмотреть файл

@ -399,4 +399,13 @@ WorkerNavigator::GetUserAgent(nsString& aUserAgent, ErrorResult& aRv) const
runnable->Dispatch(aRv);
}
uint64_t
WorkerNavigator::HardwareConcurrency() const
{
RuntimeService* rts = RuntimeService::GetService();
MOZ_ASSERT(rts);
return rts->ClampedHardwareConcurrency();
}
END_WORKERS_NAMESPACE

Просмотреть файл

@ -112,6 +112,8 @@ public:
const nsAString& aName,
const nsAString& aOwner,
ErrorResult& aRv);
uint64_t HardwareConcurrency() const;
};
END_WORKERS_NAMESPACE

Просмотреть файл

@ -26,6 +26,7 @@
#include "GeckoProfiler.h"
#include "jsfriendapi.h"
#include "mozilla/ArrayUtils.h"
#include "mozilla/Atomics.h"
#include "mozilla/CycleCollectedJSRuntime.h"
#include "mozilla/Telemetry.h"
#include "mozilla/TimeStamp.h"
@ -68,6 +69,7 @@
#include "WorkerRunnable.h"
#include "WorkerScope.h"
#include "WorkerThread.h"
#include "prsystem.h"
using namespace mozilla;
using namespace mozilla::dom;
@ -2452,6 +2454,28 @@ RuntimeService::SendOfflineStatusChangeEventToAllWorkers(bool aIsOffline)
BROADCAST_ALL_WORKERS(OfflineStatusChangeEvent, aIsOffline);
}
uint32_t
RuntimeService::ClampedHardwareConcurrency() const
{
// This needs to be atomic, because multiple workers, and even mainthread,
// could race to initialize it at once.
static Atomic<uint32_t> clampedHardwareConcurrency;
// No need to loop here: if compareExchange fails, that just means that some
// other worker has initialized numberOfProcessors, so we're good to go.
if (!clampedHardwareConcurrency) {
int32_t numberOfProcessors = PR_GetNumberOfProcessors();
if (numberOfProcessors <= 0) {
numberOfProcessors = 1; // Must be one there somewhere
}
uint32_t clampedValue = std::min(uint32_t(numberOfProcessors),
gMaxWorkersPerDomain);
clampedHardwareConcurrency.compareExchange(0, clampedValue);
}
return clampedHardwareConcurrency;
}
// nsISupports
NS_IMPL_ISUPPORTS(RuntimeService, nsIObserver)

Просмотреть файл

@ -249,6 +249,8 @@ public:
void
SendOfflineStatusChangeEventToAllWorkers(bool aIsOffline);
uint32_t ClampedHardwareConcurrency() const;
private:
RuntimeService();
~RuntimeService();

Просмотреть файл

@ -21,6 +21,7 @@ EXPORTS.mozilla.dom += [
]
EXPORTS.mozilla.dom.workers += [
'RuntimeService.h',
'ServiceWorkerManager.h',
'WorkerDebuggerManager.h',
'Workers.h',

Просмотреть файл

@ -258,3 +258,4 @@ skip-if = (os == "win") || (os == "mac") || toolkit == 'android' #bug 798220
[test_sharedWorker_ports.html]
[test_sharedWorker_lifetime.html]
[test_fileReader.html]
[test_navigator_workers_hardwareConcurrency.html]

Просмотреть файл

@ -15,6 +15,7 @@ var supportedProps = [
"onLine",
"language",
"languages",
"hardwareConcurrency",
];
self.onmessage = function(event) {

Просмотреть файл

@ -0,0 +1,30 @@
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Test for Navigator.hardwareConcurrency</title>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
<script type="application/javascript">
SimpleTest.waitForExplicitFinish();
var script = "postMessage(navigator.hardwareConcurrency)";
var url = URL.createObjectURL(new Blob([script]));
var w = new Worker(url);
w.onmessage = function(e) {
var x = e.data;
is(typeof x, "number", "hardwareConcurrency should be a number.");
ok(x > 0, "hardwareConcurrency should be greater than 0.");
SimpleTest.finish();
}
</script>
</head>
<body>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
</pre>
</body>
</html>