Commit acbb989b authored by johnx's avatar johnx Committed by Commit Bot

Introduce Starboard platform

Starboard is the platform abstraction for Cobalt.
This CL introduces all Cobalt changes in src/base/platform.

The review was conducted mostly on:
https://chromium-review.googlesource.com/c/v8/v8/+/2247918

See b/156155426 for background

Tbr: mlippautz@chromium.org
Change-Id: I6cd092304ba6485acd38e82aa2dc4505d7dfb0aa
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2346090
Commit-Queue: John Xu <johnx@google.com>
Reviewed-by: 's avatarMichael Lippautz <mlippautz@chromium.org>
Reviewed-by: 's avatarUlan Degenbaev <ulan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#69530}
parent 89004d8c
......@@ -70,6 +70,7 @@
// V8_OS_POSIX - POSIX compatible (mostly everything except Windows)
// V8_OS_QNX - QNX Neutrino
// V8_OS_SOLARIS - Sun Solaris and OpenSolaris
// V8_OS_STARBOARD - Starboard (platform abstraction for Cobalt)
// V8_OS_AIX - AIX
// V8_OS_WIN - Microsoft Windows
......@@ -93,6 +94,8 @@
#elif defined(__sun)
# define V8_OS_POSIX 1
# define V8_OS_SOLARIS 1
#elif defined(STARBOARD)
# define V8_OS_STARBOARD 1
#elif defined(_AIX)
#define V8_OS_POSIX 1
#define V8_OS_AIX 1
......
......@@ -52,7 +52,8 @@ include_rules = [
"-src/libplatform",
"-include/libplatform",
"+builtins-generated",
"+torque-generated"
"+torque-generated",
"+starboard",
]
specific_include_rules = {
......
......@@ -36,9 +36,25 @@
#include "src/base/base-export.h"
#include "src/base/build_config.h"
#if defined(V8_OS_STARBOARD)
#include "starboard/atomic.h"
#if SB_API_VERSION < 10
#error Your version of Starboard must support SbAtomic8 in order to use V8.
#endif // SB_API_VERSION < 10
#endif // V8_OS_STARBOARD
namespace v8 {
namespace base {
#ifdef V8_OS_STARBOARD
using Atomic8 = SbAtomic8;
using Atomic16 = int16_t;
using Atomic32 = SbAtomic32;
#if SB_IS_64_BIT
using Atomic64 = SbAtomic64;
#endif
#else
using Atomic8 = char;
using Atomic16 = int16_t;
using Atomic32 = int32_t;
......@@ -51,10 +67,15 @@ using Atomic64 = int64_t;
using Atomic64 = intptr_t;
#endif // defined(__ILP32__)
#endif // defined(V8_HOST_ARCH_64_BIT)
#endif // V8_OS_STARBOARD
// Use AtomicWord for a machine-sized pointer. It will use the Atomic32 or
// Atomic64 routines below, depending on your architecture.
#if defined(V8_OS_STARBOARD)
using AtomicWord = SbAtomicPtr;
#else
using AtomicWord = intptr_t;
#endif
// Atomically execute:
// result = *ptr;
......@@ -126,7 +147,7 @@ Atomic64 Acquire_Load(volatile const Atomic64* ptr);
} // namespace base
} // namespace v8
#if defined(V8_OS_WIN)
#if defined(V8_OS_WIN) || defined(V8_OS_STARBOARD)
#include "src/base/atomicops_internals_std.h"
#else
// TODO(ulan): Switch to std version after performance regression with Wheezy
......
......@@ -4,6 +4,10 @@
#include "src/base/cpu.h"
#if defined(STARBOARD)
#include "starboard/cpu_features.h"
#endif
#if V8_LIBC_MSVCRT
#include <intrin.h> // __cpuid()
#endif
......@@ -347,6 +351,54 @@ static bool HasListItem(const char* list, const char* item) {
#endif // V8_HOST_ARCH_ARM || V8_HOST_ARCH_ARM64 ||
// V8_HOST_ARCH_MIPS || V8_HOST_ARCH_MIPS64
#if defined(STARBOARD)
bool CPU::StarboardDetectCPU() {
#if (SB_API_VERSION >= 11)
SbCPUFeatures features;
if (!SbCPUFeaturesGet(&features)) {
return false;
}
architecture_ = features.arm.architecture_generation;
switch (features.architecture) {
case kSbCPUFeaturesArchitectureArm:
case kSbCPUFeaturesArchitectureArm64:
has_neon_ = features.arm.has_neon;
has_thumb2_ = features.arm.has_thumb2;
has_vfp_ = features.arm.has_vfp;
has_vfp3_ = features.arm.has_vfp3;
has_vfp3_d32_ = features.arm.has_vfp3_d32;
has_idiva_ = features.arm.has_idiva;
break;
case kSbCPUFeaturesArchitectureX86:
case kSbCPUFeaturesArchitectureX86_64:
// Following flags are mandatory for V8
has_cmov_ = features.x86.has_cmov;
has_sse2_ = features.x86.has_sse2;
// These flags are optional
has_sse3_ = features.x86.has_sse3;
has_ssse3_ = features.x86.has_ssse3;
has_sse41_ = features.x86.has_sse41;
has_sahf_ = features.x86.has_sahf;
has_avx_ = features.x86.has_avx;
has_fma3_ = features.x86.has_fma3;
has_bmi1_ = features.x86.has_bmi1;
has_bmi2_ = features.x86.has_bmi2;
has_lzcnt_ = features.x86.has_lzcnt;
has_popcnt_ = features.x86.has_popcnt;
break;
default:
return false;
}
return true;
#else // SB_API_VERSION >= 11
return false;
#endif
}
#endif
CPU::CPU()
: stepping_(0),
model_(0),
......@@ -389,6 +441,13 @@ CPU::CPU()
has_non_stop_time_stamp_counter_(false),
has_msa_(false) {
memcpy(vendor_, "Unknown", 8);
#if defined(STARBOARD)
if (StarboardDetectCPU()) {
return;
}
#endif
#if V8_HOST_ARCH_IA32 || V8_HOST_ARCH_X64
int cpu_info[4];
......
......@@ -6,6 +6,8 @@
#ifdef _WIN32
#include <windows.h>
#elif defined(V8_OS_STARBOARD)
#include "starboard/thread.h"
#else
#include <sched.h>
#endif
......@@ -40,6 +42,8 @@ void CallOnceImpl(OnceType* once, std::function<void()> init_func) {
ONCE_STATE_EXECUTING_FUNCTION) {
#ifdef _WIN32
::Sleep(0);
#elif defined(V8_OS_STARBOARD)
SbThreadYield();
#else
sched_yield();
#endif
......
......@@ -159,7 +159,37 @@ bool ConditionVariable::WaitFor(Mutex* mutex, const TimeDelta& rel_time) {
return result != 0;
}
#endif // V8_OS_POSIX
#elif V8_OS_STARBOARD
ConditionVariable::ConditionVariable() {
SbConditionVariableCreate(&native_handle_, nullptr);
}
ConditionVariable::~ConditionVariable() {
SbConditionVariableDestroy(&native_handle_);
}
void ConditionVariable::NotifyOne() {
SbConditionVariableSignal(&native_handle_);
}
void ConditionVariable::NotifyAll() {
SbConditionVariableBroadcast(&native_handle_);
}
void ConditionVariable::Wait(Mutex* mutex) {
SbConditionVariableWait(&native_handle_, &mutex->native_handle());
}
bool ConditionVariable::WaitFor(Mutex* mutex, const TimeDelta& rel_time) {
SbTime microseconds = static_cast<SbTime>(rel_time.InMicroseconds());
SbConditionVariableResult result = SbConditionVariableWaitTimed(
&native_handle_, &mutex->native_handle(), microseconds);
DCHECK(result != kSbConditionVariableFailed);
return result == kSbConditionVariableSignaled;
}
#endif // V8_OS_STARBOARD
} // namespace base
} // namespace v8
......@@ -9,6 +9,10 @@
#include "src/base/lazy-instance.h"
#include "src/base/platform/mutex.h"
#if V8_OS_STARBOARD
#include "starboard/common/condition_variable.h"
#endif
namespace v8 {
namespace base {
......@@ -64,6 +68,8 @@ class V8_BASE_EXPORT ConditionVariable final {
using NativeHandle = pthread_cond_t;
#elif V8_OS_WIN
using NativeHandle = CONDITION_VARIABLE;
#elif V8_OS_STARBOARD
using NativeHandle = SbConditionVariable;
#endif
NativeHandle& native_handle() {
......
......@@ -294,7 +294,42 @@ bool SharedMutex::TryLockExclusive() {
return TryAcquireSRWLockExclusive(&native_handle_);
}
#endif // V8_OS_POSIX
#elif V8_OS_STARBOARD
Mutex::Mutex() { SbMutexCreate(&native_handle_); }
Mutex::~Mutex() { SbMutexDestroy(&native_handle_); }
void Mutex::Lock() { SbMutexAcquire(&native_handle_); }
void Mutex::Unlock() { SbMutexRelease(&native_handle_); }
RecursiveMutex::RecursiveMutex() {}
RecursiveMutex::~RecursiveMutex() {}
void RecursiveMutex::Lock() { native_handle_.Acquire(); }
void RecursiveMutex::Unlock() { native_handle_.Release(); }
bool RecursiveMutex::TryLock() { return native_handle_.AcquireTry(); }
SharedMutex::SharedMutex() = default;
SharedMutex::~SharedMutex() = default;
void SharedMutex::LockShared() { native_handle_.AcquireReadLock(); }
void SharedMutex::LockExclusive() { native_handle_.AcquireWriteLock(); }
void SharedMutex::UnlockShared() { native_handle_.ReleaseReadLock(); }
void SharedMutex::UnlockExclusive() { native_handle_.ReleaseWriteLock(); }
bool SharedMutex::TryLockShared() { return false; }
bool SharedMutex::TryLockExclusive() { return false; }
#endif // V8_OS_STARBOARD
} // namespace base
} // namespace v8
......@@ -16,6 +16,12 @@
#include <pthread.h> // NOLINT
#endif
#if V8_OS_STARBOARD
#include "starboard/common/mutex.h"
#include "starboard/common/recursive_mutex.h"
#include "starboard/common/rwlock.h"
#endif
namespace v8 {
namespace base {
......@@ -58,6 +64,8 @@ class V8_BASE_EXPORT Mutex final {
using NativeHandle = pthread_mutex_t;
#elif V8_OS_WIN
using NativeHandle = SRWLOCK;
#elif V8_OS_STARBOARD
using NativeHandle = SbMutex;
#endif
NativeHandle& native_handle() {
......@@ -159,6 +167,8 @@ class V8_BASE_EXPORT RecursiveMutex final {
using NativeHandle = pthread_mutex_t;
#elif V8_OS_WIN
using NativeHandle = CRITICAL_SECTION;
#elif V8_OS_STARBOARD
using NativeHandle = starboard::RecursiveMutex;
#endif
NativeHandle native_handle_;
......@@ -247,6 +257,8 @@ class V8_BASE_EXPORT SharedMutex final {
using NativeHandle = pthread_rwlock_t;
#elif V8_OS_WIN
using NativeHandle = SRWLOCK;
#elif V8_OS_STARBOARD
using NativeHandle = starboard::RWLock;
#endif
NativeHandle native_handle_;
......
This diff is collapsed.
......@@ -74,6 +74,9 @@ inline intptr_t InternalGetExistingThreadLocal(intptr_t index) {
#elif defined(__APPLE__) && (V8_HOST_ARCH_IA32 || V8_HOST_ARCH_X64)
// tvOS simulator does not use intptr_t as TLS key.
#if !defined(V8_OS_STARBOARD) || !defined(TARGET_OS_SIMULATOR)
#define V8_FAST_TLS_SUPPORTED 1
extern V8_BASE_EXPORT intptr_t kMacTlsBaseOffset;
......@@ -94,6 +97,8 @@ inline intptr_t InternalGetExistingThreadLocal(intptr_t index) {
return result;
}
#endif // !defined(V8_OS_STARBOARD) || !defined(TARGET_OS_SIMULATOR)
#endif
#endif // V8_NO_FAST_TLS
......@@ -323,7 +328,11 @@ inline void EnsureConsoleOutput() {
class V8_BASE_EXPORT Thread {
public:
// Opaque data type for thread-local storage keys.
#if V8_OS_STARBOARD
using LocalStorageKey = SbThreadLocalKey;
#else
using LocalStorageKey = int32_t;
#endif
class Options {
public:
......
......@@ -157,6 +157,21 @@ bool Semaphore::WaitFor(const TimeDelta& rel_time) {
}
}
#elif V8_OS_STARBOARD
Semaphore::Semaphore(int count) : native_handle_(count) { DCHECK_GE(count, 0); }
Semaphore::~Semaphore() {}
void Semaphore::Signal() { native_handle_.Put(); }
void Semaphore::Wait() { native_handle_.Take(); }
bool Semaphore::WaitFor(const TimeDelta& rel_time) {
SbTime microseconds = rel_time.InMicroseconds();
return native_handle_.TakeWait(microseconds);
}
#endif // V8_OS_MACOSX
} // namespace base
......
......@@ -17,6 +17,10 @@
#include <semaphore.h> // NOLINT
#endif
#if V8_OS_STARBOARD
#include "starboard/common/semaphore.h"
#endif
namespace v8 {
namespace base {
......@@ -55,6 +59,8 @@ class V8_BASE_EXPORT Semaphore final {
using NativeHandle = sem_t;
#elif V8_OS_WIN
using NativeHandle = HANDLE;
#elif V8_OS_STARBOARD
using NativeHandle = starboard::Semaphore;
#endif
NativeHandle& native_handle() {
......
......@@ -26,6 +26,10 @@
#include "src/base/logging.h"
#include "src/base/platform/platform.h"
#if V8_OS_STARBOARD
#include "starboard/time.h"
#endif
namespace {
#if V8_OS_MACOSX
......@@ -449,7 +453,13 @@ struct timeval Time::ToTimeval() const {
return tv;
}
#endif // V8_OS_WIN
#elif V8_OS_STARBOARD
Time Time::Now() { return Time(SbTimeToPosix(SbTimeGetNow())); }
Time Time::NowFromSystemTime() { return Now(); }
#endif // V8_OS_STARBOARD
// static
TimeTicks TimeTicks::HighResolutionNow() {
......@@ -717,6 +727,8 @@ TimeTicks TimeTicks::Now() {
ticks = (gethrtime() / Time::kNanosecondsPerMicrosecond);
#elif V8_OS_POSIX
ticks = ClockNow(CLOCK_MONOTONIC);
#elif V8_OS_STARBOARD
ticks = SbTimeGetMonotonicNow();
#else
#error platform does not implement TimeTicks::HighResolutionNow.
#endif // V8_OS_MACOSX
......@@ -740,7 +752,15 @@ bool TimeTicks::IsHighResolution() {
bool ThreadTicks::IsSupported() {
#if (defined(_POSIX_THREAD_CPUTIME) && (_POSIX_THREAD_CPUTIME >= 0)) || \
#if V8_OS_STARBOARD
#if SB_API_VERSION >= 12
return SbTimeIsTimeThreadNowSupported();
#elif SB_HAS(TIME_THREAD_NOW)
return true;
#else
return false;
#endif
#elif(defined(_POSIX_THREAD_CPUTIME) && (_POSIX_THREAD_CPUTIME >= 0)) || \
defined(V8_OS_MACOSX) || defined(V8_OS_ANDROID) || defined(V8_OS_SOLARIS)
return true;
#elif defined(V8_OS_WIN)
......@@ -752,7 +772,17 @@ bool ThreadTicks::IsSupported() {
ThreadTicks ThreadTicks::Now() {
#if V8_OS_MACOSX
#if V8_OS_STARBOARD
#if SB_API_VERSION >= 12
if (SbTimeIsTimeThreadNowSupported())
return ThreadTicks(SbTimeGetMonotonicThreadNow());
UNREACHABLE();
#elif SB_HAS(TIME_THREAD_NOW)
return ThreadTicks(SbTimeGetMonotonicThreadNow());
#else
UNREACHABLE();
#endif
#elif V8_OS_MACOSX
return ThreadTicks(ComputeThreadTicks());
#elif(defined(_POSIX_THREAD_CPUTIME) && (_POSIX_THREAD_CPUTIME >= 0)) || \
defined(V8_OS_ANDROID)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment