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 @@ ...@@ -70,6 +70,7 @@
// V8_OS_POSIX - POSIX compatible (mostly everything except Windows) // V8_OS_POSIX - POSIX compatible (mostly everything except Windows)
// V8_OS_QNX - QNX Neutrino // V8_OS_QNX - QNX Neutrino
// V8_OS_SOLARIS - Sun Solaris and OpenSolaris // V8_OS_SOLARIS - Sun Solaris and OpenSolaris
// V8_OS_STARBOARD - Starboard (platform abstraction for Cobalt)
// V8_OS_AIX - AIX // V8_OS_AIX - AIX
// V8_OS_WIN - Microsoft Windows // V8_OS_WIN - Microsoft Windows
...@@ -93,6 +94,8 @@ ...@@ -93,6 +94,8 @@
#elif defined(__sun) #elif defined(__sun)
# define V8_OS_POSIX 1 # define V8_OS_POSIX 1
# define V8_OS_SOLARIS 1 # define V8_OS_SOLARIS 1
#elif defined(STARBOARD)
# define V8_OS_STARBOARD 1
#elif defined(_AIX) #elif defined(_AIX)
#define V8_OS_POSIX 1 #define V8_OS_POSIX 1
#define V8_OS_AIX 1 #define V8_OS_AIX 1
......
...@@ -52,7 +52,8 @@ include_rules = [ ...@@ -52,7 +52,8 @@ include_rules = [
"-src/libplatform", "-src/libplatform",
"-include/libplatform", "-include/libplatform",
"+builtins-generated", "+builtins-generated",
"+torque-generated" "+torque-generated",
"+starboard",
] ]
specific_include_rules = { specific_include_rules = {
......
...@@ -36,9 +36,25 @@ ...@@ -36,9 +36,25 @@
#include "src/base/base-export.h" #include "src/base/base-export.h"
#include "src/base/build_config.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 v8 {
namespace base { 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 Atomic8 = char;
using Atomic16 = int16_t; using Atomic16 = int16_t;
using Atomic32 = int32_t; using Atomic32 = int32_t;
...@@ -51,10 +67,15 @@ using Atomic64 = int64_t; ...@@ -51,10 +67,15 @@ using Atomic64 = int64_t;
using Atomic64 = intptr_t; using Atomic64 = intptr_t;
#endif // defined(__ILP32__) #endif // defined(__ILP32__)
#endif // defined(V8_HOST_ARCH_64_BIT) #endif // defined(V8_HOST_ARCH_64_BIT)
#endif // V8_OS_STARBOARD
// Use AtomicWord for a machine-sized pointer. It will use the Atomic32 or // Use AtomicWord for a machine-sized pointer. It will use the Atomic32 or
// Atomic64 routines below, depending on your architecture. // Atomic64 routines below, depending on your architecture.
#if defined(V8_OS_STARBOARD)
using AtomicWord = SbAtomicPtr;
#else
using AtomicWord = intptr_t; using AtomicWord = intptr_t;
#endif
// Atomically execute: // Atomically execute:
// result = *ptr; // result = *ptr;
...@@ -126,7 +147,7 @@ Atomic64 Acquire_Load(volatile const Atomic64* ptr); ...@@ -126,7 +147,7 @@ Atomic64 Acquire_Load(volatile const Atomic64* ptr);
} // namespace base } // namespace base
} // namespace v8 } // namespace v8
#if defined(V8_OS_WIN) #if defined(V8_OS_WIN) || defined(V8_OS_STARBOARD)
#include "src/base/atomicops_internals_std.h" #include "src/base/atomicops_internals_std.h"
#else #else
// TODO(ulan): Switch to std version after performance regression with Wheezy // TODO(ulan): Switch to std version after performance regression with Wheezy
......
...@@ -4,6 +4,10 @@ ...@@ -4,6 +4,10 @@
#include "src/base/cpu.h" #include "src/base/cpu.h"
#if defined(STARBOARD)
#include "starboard/cpu_features.h"
#endif
#if V8_LIBC_MSVCRT #if V8_LIBC_MSVCRT
#include <intrin.h> // __cpuid() #include <intrin.h> // __cpuid()
#endif #endif
...@@ -347,6 +351,54 @@ static bool HasListItem(const char* list, const char* item) { ...@@ -347,6 +351,54 @@ static bool HasListItem(const char* list, const char* item) {
#endif // V8_HOST_ARCH_ARM || V8_HOST_ARCH_ARM64 || #endif // V8_HOST_ARCH_ARM || V8_HOST_ARCH_ARM64 ||
// V8_HOST_ARCH_MIPS || V8_HOST_ARCH_MIPS64 // 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() CPU::CPU()
: stepping_(0), : stepping_(0),
model_(0), model_(0),
...@@ -389,6 +441,13 @@ CPU::CPU() ...@@ -389,6 +441,13 @@ CPU::CPU()
has_non_stop_time_stamp_counter_(false), has_non_stop_time_stamp_counter_(false),
has_msa_(false) { has_msa_(false) {
memcpy(vendor_, "Unknown", 8); memcpy(vendor_, "Unknown", 8);
#if defined(STARBOARD)
if (StarboardDetectCPU()) {
return;
}
#endif
#if V8_HOST_ARCH_IA32 || V8_HOST_ARCH_X64 #if V8_HOST_ARCH_IA32 || V8_HOST_ARCH_X64
int cpu_info[4]; int cpu_info[4];
......
...@@ -6,6 +6,8 @@ ...@@ -6,6 +6,8 @@
#ifdef _WIN32 #ifdef _WIN32
#include <windows.h> #include <windows.h>
#elif defined(V8_OS_STARBOARD)
#include "starboard/thread.h"
#else #else
#include <sched.h> #include <sched.h>
#endif #endif
...@@ -40,6 +42,8 @@ void CallOnceImpl(OnceType* once, std::function<void()> init_func) { ...@@ -40,6 +42,8 @@ void CallOnceImpl(OnceType* once, std::function<void()> init_func) {
ONCE_STATE_EXECUTING_FUNCTION) { ONCE_STATE_EXECUTING_FUNCTION) {
#ifdef _WIN32 #ifdef _WIN32
::Sleep(0); ::Sleep(0);
#elif defined(V8_OS_STARBOARD)
SbThreadYield();
#else #else
sched_yield(); sched_yield();
#endif #endif
......
...@@ -159,7 +159,37 @@ bool ConditionVariable::WaitFor(Mutex* mutex, const TimeDelta& rel_time) { ...@@ -159,7 +159,37 @@ bool ConditionVariable::WaitFor(Mutex* mutex, const TimeDelta& rel_time) {
return result != 0; 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 base
} // namespace v8 } // namespace v8
...@@ -9,6 +9,10 @@ ...@@ -9,6 +9,10 @@
#include "src/base/lazy-instance.h" #include "src/base/lazy-instance.h"
#include "src/base/platform/mutex.h" #include "src/base/platform/mutex.h"
#if V8_OS_STARBOARD
#include "starboard/common/condition_variable.h"
#endif
namespace v8 { namespace v8 {
namespace base { namespace base {
...@@ -64,6 +68,8 @@ class V8_BASE_EXPORT ConditionVariable final { ...@@ -64,6 +68,8 @@ class V8_BASE_EXPORT ConditionVariable final {
using NativeHandle = pthread_cond_t; using NativeHandle = pthread_cond_t;
#elif V8_OS_WIN #elif V8_OS_WIN
using NativeHandle = CONDITION_VARIABLE; using NativeHandle = CONDITION_VARIABLE;
#elif V8_OS_STARBOARD
using NativeHandle = SbConditionVariable;
#endif #endif
NativeHandle& native_handle() { NativeHandle& native_handle() {
......
...@@ -294,7 +294,42 @@ bool SharedMutex::TryLockExclusive() { ...@@ -294,7 +294,42 @@ bool SharedMutex::TryLockExclusive() {
return TryAcquireSRWLockExclusive(&native_handle_); 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 base
} // namespace v8 } // namespace v8
...@@ -16,6 +16,12 @@ ...@@ -16,6 +16,12 @@
#include <pthread.h> // NOLINT #include <pthread.h> // NOLINT
#endif #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 v8 {
namespace base { namespace base {
...@@ -58,6 +64,8 @@ class V8_BASE_EXPORT Mutex final { ...@@ -58,6 +64,8 @@ class V8_BASE_EXPORT Mutex final {
using NativeHandle = pthread_mutex_t; using NativeHandle = pthread_mutex_t;
#elif V8_OS_WIN #elif V8_OS_WIN
using NativeHandle = SRWLOCK; using NativeHandle = SRWLOCK;
#elif V8_OS_STARBOARD
using NativeHandle = SbMutex;
#endif #endif
NativeHandle& native_handle() { NativeHandle& native_handle() {
...@@ -159,6 +167,8 @@ class V8_BASE_EXPORT RecursiveMutex final { ...@@ -159,6 +167,8 @@ class V8_BASE_EXPORT RecursiveMutex final {
using NativeHandle = pthread_mutex_t; using NativeHandle = pthread_mutex_t;
#elif V8_OS_WIN #elif V8_OS_WIN
using NativeHandle = CRITICAL_SECTION; using NativeHandle = CRITICAL_SECTION;
#elif V8_OS_STARBOARD
using NativeHandle = starboard::RecursiveMutex;
#endif #endif
NativeHandle native_handle_; NativeHandle native_handle_;
...@@ -247,6 +257,8 @@ class V8_BASE_EXPORT SharedMutex final { ...@@ -247,6 +257,8 @@ class V8_BASE_EXPORT SharedMutex final {
using NativeHandle = pthread_rwlock_t; using NativeHandle = pthread_rwlock_t;
#elif V8_OS_WIN #elif V8_OS_WIN
using NativeHandle = SRWLOCK; using NativeHandle = SRWLOCK;
#elif V8_OS_STARBOARD
using NativeHandle = starboard::RWLock;
#endif #endif
NativeHandle native_handle_; NativeHandle native_handle_;
......
This diff is collapsed.
...@@ -74,6 +74,9 @@ inline intptr_t InternalGetExistingThreadLocal(intptr_t index) { ...@@ -74,6 +74,9 @@ inline intptr_t InternalGetExistingThreadLocal(intptr_t index) {
#elif defined(__APPLE__) && (V8_HOST_ARCH_IA32 || V8_HOST_ARCH_X64) #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 #define V8_FAST_TLS_SUPPORTED 1
extern V8_BASE_EXPORT intptr_t kMacTlsBaseOffset; extern V8_BASE_EXPORT intptr_t kMacTlsBaseOffset;
...@@ -94,6 +97,8 @@ inline intptr_t InternalGetExistingThreadLocal(intptr_t index) { ...@@ -94,6 +97,8 @@ inline intptr_t InternalGetExistingThreadLocal(intptr_t index) {
return result; return result;
} }
#endif // !defined(V8_OS_STARBOARD) || !defined(TARGET_OS_SIMULATOR)
#endif #endif
#endif // V8_NO_FAST_TLS #endif // V8_NO_FAST_TLS
...@@ -323,7 +328,11 @@ inline void EnsureConsoleOutput() { ...@@ -323,7 +328,11 @@ inline void EnsureConsoleOutput() {
class V8_BASE_EXPORT Thread { class V8_BASE_EXPORT Thread {
public: public:
// Opaque data type for thread-local storage keys. // Opaque data type for thread-local storage keys.
#if V8_OS_STARBOARD
using LocalStorageKey = SbThreadLocalKey;
#else
using LocalStorageKey = int32_t; using LocalStorageKey = int32_t;
#endif
class Options { class Options {
public: public:
......
...@@ -157,6 +157,21 @@ bool Semaphore::WaitFor(const TimeDelta& rel_time) { ...@@ -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 #endif // V8_OS_MACOSX
} // namespace base } // namespace base
......
...@@ -17,6 +17,10 @@ ...@@ -17,6 +17,10 @@
#include <semaphore.h> // NOLINT #include <semaphore.h> // NOLINT
#endif #endif
#if V8_OS_STARBOARD
#include "starboard/common/semaphore.h"
#endif
namespace v8 { namespace v8 {
namespace base { namespace base {
...@@ -55,6 +59,8 @@ class V8_BASE_EXPORT Semaphore final { ...@@ -55,6 +59,8 @@ class V8_BASE_EXPORT Semaphore final {
using NativeHandle = sem_t; using NativeHandle = sem_t;
#elif V8_OS_WIN #elif V8_OS_WIN
using NativeHandle = HANDLE; using NativeHandle = HANDLE;
#elif V8_OS_STARBOARD
using NativeHandle = starboard::Semaphore;
#endif #endif
NativeHandle& native_handle() { NativeHandle& native_handle() {
......
...@@ -26,6 +26,10 @@ ...@@ -26,6 +26,10 @@
#include "src/base/logging.h" #include "src/base/logging.h"
#include "src/base/platform/platform.h" #include "src/base/platform/platform.h"
#if V8_OS_STARBOARD
#include "starboard/time.h"
#endif
namespace { namespace {
#if V8_OS_MACOSX #if V8_OS_MACOSX
...@@ -449,7 +453,13 @@ struct timeval Time::ToTimeval() const { ...@@ -449,7 +453,13 @@ struct timeval Time::ToTimeval() const {
return tv; 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 // static
TimeTicks TimeTicks::HighResolutionNow() { TimeTicks TimeTicks::HighResolutionNow() {
...@@ -717,6 +727,8 @@ TimeTicks TimeTicks::Now() { ...@@ -717,6 +727,8 @@ TimeTicks TimeTicks::Now() {
ticks = (gethrtime() / Time::kNanosecondsPerMicrosecond); ticks = (gethrtime() / Time::kNanosecondsPerMicrosecond);
#elif V8_OS_POSIX #elif V8_OS_POSIX
ticks = ClockNow(CLOCK_MONOTONIC); ticks = ClockNow(CLOCK_MONOTONIC);
#elif V8_OS_STARBOARD
ticks = SbTimeGetMonotonicNow();
#else #else
#error platform does not implement TimeTicks::HighResolutionNow. #error platform does not implement TimeTicks::HighResolutionNow.
#endif // V8_OS_MACOSX #endif // V8_OS_MACOSX
...@@ -740,7 +752,15 @@ bool TimeTicks::IsHighResolution() { ...@@ -740,7 +752,15 @@ bool TimeTicks::IsHighResolution() {
bool ThreadTicks::IsSupported() { 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) defined(V8_OS_MACOSX) || defined(V8_OS_ANDROID) || defined(V8_OS_SOLARIS)
return true; return true;
#elif defined(V8_OS_WIN) #elif defined(V8_OS_WIN)
...@@ -752,7 +772,17 @@ bool ThreadTicks::IsSupported() { ...@@ -752,7 +772,17 @@ bool ThreadTicks::IsSupported() {
ThreadTicks ThreadTicks::Now() { 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()); return ThreadTicks(ComputeThreadTicks());
#elif(defined(_POSIX_THREAD_CPUTIME) && (_POSIX_THREAD_CPUTIME >= 0)) || \ #elif(defined(_POSIX_THREAD_CPUTIME) && (_POSIX_THREAD_CPUTIME >= 0)) || \
defined(V8_OS_ANDROID) 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