2012-05-21 10:02:49 +00:00
|
|
|
// Copyright 2012 the V8 project authors. All rights reserved.
|
2009-03-24 06:32:00 +00:00
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
// Platform specific code for POSIX goes here. This is not a platform on its
|
|
|
|
// own but contains the parts which are the same across POSIX platforms Linux,
|
2009-12-02 13:38:50 +00:00
|
|
|
// Mac OS, FreeBSD and OpenBSD.
|
2009-03-24 06:32:00 +00:00
|
|
|
|
2012-03-30 14:30:46 +00:00
|
|
|
#include "platform-posix.h"
|
|
|
|
|
2013-07-23 13:47:50 +00:00
|
|
|
#include <dlfcn.h>
|
2013-07-11 11:37:08 +00:00
|
|
|
#include <pthread.h>
|
2013-07-29 12:12:39 +00:00
|
|
|
#if defined(__DragonFly__) || defined(__FreeBSD__) || defined(__OpenBSD__)
|
2013-07-23 13:47:50 +00:00
|
|
|
#include <pthread_np.h> // for pthread_set_name_np
|
|
|
|
#endif
|
2013-07-11 11:37:08 +00:00
|
|
|
#include <sched.h> // for sched_yield
|
2009-03-24 06:32:00 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
2009-03-26 09:28:27 +00:00
|
|
|
#include <time.h>
|
2009-03-24 06:32:00 +00:00
|
|
|
|
2011-07-20 08:06:43 +00:00
|
|
|
#include <sys/mman.h>
|
2009-03-24 06:32:00 +00:00
|
|
|
#include <sys/socket.h>
|
2009-03-26 09:43:14 +00:00
|
|
|
#include <sys/resource.h>
|
|
|
|
#include <sys/time.h>
|
2009-03-24 06:32:00 +00:00
|
|
|
#include <sys/types.h>
|
2011-07-11 15:30:24 +00:00
|
|
|
#include <sys/stat.h>
|
2013-07-29 12:12:39 +00:00
|
|
|
#if defined(__linux__)
|
2013-07-23 13:47:50 +00:00
|
|
|
#include <sys/prctl.h> // for prctl
|
2013-07-29 12:12:39 +00:00
|
|
|
#endif
|
|
|
|
#if defined(__APPLE__) || defined(__DragonFly__) || defined(__FreeBSD__) || \
|
|
|
|
defined(__NetBSD__) || defined(__OpenBSD__)
|
2013-07-23 13:47:50 +00:00
|
|
|
#include <sys/sysctl.h> // for sysctl
|
|
|
|
#endif
|
2009-03-24 06:32:00 +00:00
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
|
2011-07-20 08:06:43 +00:00
|
|
|
#undef MAP_TYPE
|
|
|
|
|
2013-07-29 12:12:39 +00:00
|
|
|
#if defined(ANDROID) && !defined(V8_ANDROID_LOG_STDOUT)
|
2009-07-13 21:22:50 +00:00
|
|
|
#define LOG_TAG "v8"
|
2011-11-08 10:14:03 +00:00
|
|
|
#include <android/log.h>
|
2009-07-13 21:22:50 +00:00
|
|
|
#endif
|
|
|
|
|
2009-03-24 06:32:00 +00:00
|
|
|
#include "v8.h"
|
|
|
|
|
2012-03-05 08:17:16 +00:00
|
|
|
#include "codegen.h"
|
2013-09-10 11:13:55 +00:00
|
|
|
#include "isolate-inl.h"
|
2009-03-24 06:32:00 +00:00
|
|
|
#include "platform.h"
|
|
|
|
|
2009-05-25 10:05:56 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
2009-03-24 06:32:00 +00:00
|
|
|
|
2013-07-23 13:47:50 +00:00
|
|
|
// 0 is never a valid thread id.
|
|
|
|
static const pthread_t kNoThread = (pthread_t) 0;
|
|
|
|
|
2011-06-22 10:13:10 +00:00
|
|
|
|
2013-07-25 09:16:39 +00:00
|
|
|
uint64_t OS::CpuFeaturesImpliedByPlatform() {
|
2013-08-26 09:37:39 +00:00
|
|
|
#if V8_OS_MACOSX
|
2013-07-25 09:16:39 +00:00
|
|
|
// Mac OS X requires all these to install so we can assume they are present.
|
|
|
|
// These constants are defined by the CPUid instructions.
|
|
|
|
const uint64_t one = 1;
|
2013-08-26 09:37:39 +00:00
|
|
|
return (one << SSE2) | (one << CMOV);
|
2013-07-25 09:16:39 +00:00
|
|
|
#else
|
|
|
|
return 0; // Nothing special about the other systems.
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-25 09:49:28 +00:00
|
|
|
int OS::ActivationFrameAlignment() {
|
|
|
|
#if V8_TARGET_ARCH_ARM
|
|
|
|
// On EABI ARM targets this is required for fp correctness in the
|
|
|
|
// runtime system.
|
|
|
|
return 8;
|
|
|
|
#elif V8_TARGET_ARCH_MIPS
|
|
|
|
return 8;
|
|
|
|
#else
|
|
|
|
// Otherwise we just assume 16 byte alignment, i.e.:
|
|
|
|
// - With gcc 4.4 the tree vectorization optimizer can generate code
|
|
|
|
// that requires 16 byte alignment such as movdqa on x86.
|
|
|
|
// - Mac OS X and Solaris (64-bit) activation frames must be 16 byte-aligned;
|
|
|
|
// see "Mac OS X ABI Function Call Guide"
|
|
|
|
return 16;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-23 09:59:14 +00:00
|
|
|
void OS::Sleep(int milliseconds) {
|
|
|
|
useconds_t ms = static_cast<useconds_t>(milliseconds);
|
|
|
|
usleep(1000 * ms);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void OS::Abort() {
|
|
|
|
// Redirect to std abort to signal abnormal program termination.
|
|
|
|
if (FLAG_break_on_abort) {
|
|
|
|
DebugBreak();
|
|
|
|
}
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void OS::DebugBreak() {
|
|
|
|
#if V8_HOST_ARCH_ARM
|
|
|
|
asm("bkpt 0");
|
|
|
|
#elif V8_HOST_ARCH_MIPS
|
|
|
|
asm("break");
|
|
|
|
#elif V8_HOST_ARCH_IA32
|
2013-07-29 12:12:39 +00:00
|
|
|
#if defined(__native_client__)
|
2013-07-23 09:59:14 +00:00
|
|
|
asm("hlt");
|
|
|
|
#else
|
|
|
|
asm("int $3");
|
2013-07-29 12:12:39 +00:00
|
|
|
#endif // __native_client__
|
2013-07-23 09:59:14 +00:00
|
|
|
#elif V8_HOST_ARCH_X64
|
|
|
|
asm("int $3");
|
|
|
|
#else
|
|
|
|
#error Unsupported host architecture.
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-23 09:18:19 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Math functions
|
|
|
|
|
2013-07-25 08:49:52 +00:00
|
|
|
double ceiling(double x) {
|
|
|
|
// Correct buggy 'ceil' on some systems (i.e. FreeBSD, OS X 10.5)
|
|
|
|
return (-1.0 < x && x < 0.0) ? -0.0 : ceil(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-23 09:18:19 +00:00
|
|
|
double modulo(double x, double y) {
|
|
|
|
return fmod(x, y);
|
|
|
|
}
|
2009-03-24 06:32:00 +00:00
|
|
|
|
2009-12-03 09:29:21 +00:00
|
|
|
|
2012-03-12 14:56:04 +00:00
|
|
|
#define UNARY_MATH_FUNCTION(name, generator) \
|
|
|
|
static UnaryMathFunction fast_##name##_function = NULL; \
|
2012-03-12 17:15:13 +00:00
|
|
|
void init_fast_##name##_function() { \
|
|
|
|
fast_##name##_function = generator; \
|
|
|
|
} \
|
2012-03-12 14:56:04 +00:00
|
|
|
double fast_##name(double x) { \
|
|
|
|
return (*fast_##name##_function)(x); \
|
2012-03-05 08:17:16 +00:00
|
|
|
}
|
|
|
|
|
2012-03-12 14:56:04 +00:00
|
|
|
UNARY_MATH_FUNCTION(sin, CreateTranscendentalFunction(TranscendentalCache::SIN))
|
|
|
|
UNARY_MATH_FUNCTION(cos, CreateTranscendentalFunction(TranscendentalCache::COS))
|
|
|
|
UNARY_MATH_FUNCTION(tan, CreateTranscendentalFunction(TranscendentalCache::TAN))
|
|
|
|
UNARY_MATH_FUNCTION(log, CreateTranscendentalFunction(TranscendentalCache::LOG))
|
2012-11-26 13:12:35 +00:00
|
|
|
UNARY_MATH_FUNCTION(exp, CreateExpFunction())
|
2012-03-12 14:56:04 +00:00
|
|
|
UNARY_MATH_FUNCTION(sqrt, CreateSqrtFunction())
|
2012-03-05 08:17:16 +00:00
|
|
|
|
2013-07-23 13:47:50 +00:00
|
|
|
#undef UNARY_MATH_FUNCTION
|
2012-03-05 08:17:16 +00:00
|
|
|
|
|
|
|
|
2012-11-26 13:12:35 +00:00
|
|
|
void lazily_initialize_fast_exp() {
|
|
|
|
if (fast_exp_function == NULL) {
|
|
|
|
init_fast_exp_function();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-03 09:29:21 +00:00
|
|
|
double OS::nan_value() {
|
|
|
|
// NAN from math.h is defined in C99 and not in POSIX.
|
|
|
|
return NAN;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-10 12:52:36 +00:00
|
|
|
int OS::GetCurrentProcessId() {
|
|
|
|
return static_cast<int>(getpid());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 09:28:27 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// POSIX date/time support.
|
|
|
|
//
|
|
|
|
|
2009-03-26 09:43:14 +00:00
|
|
|
int OS::GetUserTime(uint32_t* secs, uint32_t* usecs) {
|
|
|
|
struct rusage usage;
|
|
|
|
|
|
|
|
if (getrusage(RUSAGE_SELF, &usage) < 0) return -1;
|
|
|
|
*secs = usage.ru_utime.tv_sec;
|
|
|
|
*usecs = usage.ru_utime.tv_usec;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
double OS::TimeCurrentMillis() {
|
2013-08-29 09:15:13 +00:00
|
|
|
return Time::Now().ToJsTime();
|
2009-03-26 09:43:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
double OS::DaylightSavingsOffset(double time) {
|
2013-04-19 13:26:47 +00:00
|
|
|
if (std::isnan(time)) return nan_value();
|
2009-03-26 09:43:14 +00:00
|
|
|
time_t tv = static_cast<time_t>(floor(time/msPerSecond));
|
|
|
|
struct tm* t = localtime(&tv);
|
2009-07-31 13:17:59 +00:00
|
|
|
if (NULL == t) return nan_value();
|
2009-03-26 09:43:14 +00:00
|
|
|
return t->tm_isdst > 0 ? 3600 * msPerSecond : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-13 11:11:36 +00:00
|
|
|
int OS::GetLastError() {
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-27 08:38:02 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// POSIX stdio support.
|
|
|
|
//
|
|
|
|
|
|
|
|
FILE* OS::FOpen(const char* path, const char* mode) {
|
2011-07-11 15:30:24 +00:00
|
|
|
FILE* file = fopen(path, mode);
|
|
|
|
if (file == NULL) return NULL;
|
|
|
|
struct stat file_stat;
|
|
|
|
if (fstat(fileno(file), &file_stat) != 0) return NULL;
|
|
|
|
bool is_regular_file = ((file_stat.st_mode & S_IFREG) != 0);
|
|
|
|
if (is_regular_file) return file;
|
|
|
|
fclose(file);
|
|
|
|
return NULL;
|
2009-03-27 08:38:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-19 14:28:15 +00:00
|
|
|
bool OS::Remove(const char* path) {
|
|
|
|
return (remove(path) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-13 11:31:22 +00:00
|
|
|
FILE* OS::OpenTemporaryFile() {
|
|
|
|
return tmpfile();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
const char* const OS::LogFileOpenMode = "w";
|
2009-03-31 09:06:37 +00:00
|
|
|
|
|
|
|
|
2009-03-27 08:38:02 +00:00
|
|
|
void OS::Print(const char* format, ...) {
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
VPrint(format, args);
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void OS::VPrint(const char* format, va_list args) {
|
2013-07-29 12:12:39 +00:00
|
|
|
#if defined(ANDROID) && !defined(V8_ANDROID_LOG_STDOUT)
|
2011-11-08 10:14:03 +00:00
|
|
|
__android_log_vprint(ANDROID_LOG_INFO, LOG_TAG, format, args);
|
2009-07-13 21:22:50 +00:00
|
|
|
#else
|
2009-03-27 08:38:02 +00:00
|
|
|
vprintf(format, args);
|
2009-07-13 21:22:50 +00:00
|
|
|
#endif
|
2009-03-27 08:38:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-20 10:38:19 +00:00
|
|
|
void OS::FPrint(FILE* out, const char* format, ...) {
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
VFPrint(out, format, args);
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void OS::VFPrint(FILE* out, const char* format, va_list args) {
|
2013-07-29 12:12:39 +00:00
|
|
|
#if defined(ANDROID) && !defined(V8_ANDROID_LOG_STDOUT)
|
2011-11-08 10:14:03 +00:00
|
|
|
__android_log_vprint(ANDROID_LOG_INFO, LOG_TAG, format, args);
|
2010-12-20 10:38:19 +00:00
|
|
|
#else
|
|
|
|
vfprintf(out, format, args);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-27 08:38:02 +00:00
|
|
|
void OS::PrintError(const char* format, ...) {
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
VPrintError(format, args);
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void OS::VPrintError(const char* format, va_list args) {
|
2013-07-29 12:12:39 +00:00
|
|
|
#if defined(ANDROID) && !defined(V8_ANDROID_LOG_STDOUT)
|
2011-11-08 10:14:03 +00:00
|
|
|
__android_log_vprint(ANDROID_LOG_ERROR, LOG_TAG, format, args);
|
2009-07-13 21:22:50 +00:00
|
|
|
#else
|
2009-03-27 08:38:02 +00:00
|
|
|
vfprintf(stderr, format, args);
|
2009-07-13 21:22:50 +00:00
|
|
|
#endif
|
2009-03-27 08:38:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int OS::SNPrintF(Vector<char> str, const char* format, ...) {
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
int result = VSNPrintF(str, format, args);
|
|
|
|
va_end(args);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int OS::VSNPrintF(Vector<char> str,
|
|
|
|
const char* format,
|
|
|
|
va_list args) {
|
|
|
|
int n = vsnprintf(str.start(), str.length(), format, args);
|
|
|
|
if (n < 0 || n >= str.length()) {
|
2010-12-20 10:38:19 +00:00
|
|
|
// If the length is zero, the assignment fails.
|
|
|
|
if (str.length() > 0)
|
|
|
|
str[str.length() - 1] = '\0';
|
2009-03-27 08:38:02 +00:00
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-28 15:34:48 +00:00
|
|
|
#if V8_TARGET_ARCH_IA32
|
2013-04-16 12:30:51 +00:00
|
|
|
static void MemMoveWrapper(void* dest, const void* src, size_t size) {
|
|
|
|
memmove(dest, src, size);
|
|
|
|
}
|
|
|
|
|
2013-07-05 09:52:11 +00:00
|
|
|
|
2013-04-16 12:30:51 +00:00
|
|
|
// Initialize to library version so we can call this at any time during startup.
|
|
|
|
static OS::MemMoveFunction memmove_function = &MemMoveWrapper;
|
|
|
|
|
2011-03-30 14:04:26 +00:00
|
|
|
// Defined in codegen-ia32.cc.
|
2013-04-16 12:30:51 +00:00
|
|
|
OS::MemMoveFunction CreateMemMoveFunction();
|
2011-03-30 14:04:26 +00:00
|
|
|
|
2013-04-16 12:30:51 +00:00
|
|
|
// Copy memory area. No restrictions.
|
|
|
|
void OS::MemMove(void* dest, const void* src, size_t size) {
|
2013-04-19 16:38:19 +00:00
|
|
|
if (size == 0) return;
|
2011-04-01 14:46:30 +00:00
|
|
|
// Note: here we rely on dependent reads being ordered. This is true
|
|
|
|
// on all architectures we currently support.
|
2013-04-16 12:30:51 +00:00
|
|
|
(*memmove_function)(dest, src, size);
|
2011-03-30 14:04:26 +00:00
|
|
|
}
|
2013-04-16 12:30:51 +00:00
|
|
|
|
2013-07-29 12:12:39 +00:00
|
|
|
#elif defined(V8_HOST_ARCH_ARM)
|
2013-07-10 15:32:39 +00:00
|
|
|
void OS::MemCopyUint16Uint8Wrapper(uint16_t* dest,
|
|
|
|
const uint8_t* src,
|
|
|
|
size_t chars) {
|
|
|
|
uint16_t *limit = dest + chars;
|
|
|
|
while (dest < limit) {
|
|
|
|
*dest++ = static_cast<uint16_t>(*src++);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
OS::MemCopyUint8Function OS::memcopy_uint8_function = &OS::MemCopyUint8Wrapper;
|
|
|
|
OS::MemCopyUint16Uint8Function OS::memcopy_uint16_uint8_function =
|
|
|
|
&OS::MemCopyUint16Uint8Wrapper;
|
|
|
|
// Defined in codegen-arm.cc.
|
|
|
|
OS::MemCopyUint8Function CreateMemCopyUint8Function(
|
|
|
|
OS::MemCopyUint8Function stub);
|
|
|
|
OS::MemCopyUint16Uint8Function CreateMemCopyUint16Uint8Function(
|
|
|
|
OS::MemCopyUint16Uint8Function stub);
|
|
|
|
#endif
|
2011-03-30 14:04:26 +00:00
|
|
|
|
2012-04-05 14:10:39 +00:00
|
|
|
|
2013-07-23 09:59:14 +00:00
|
|
|
void OS::PostSetUp() {
|
2013-06-28 15:34:48 +00:00
|
|
|
#if V8_TARGET_ARCH_IA32
|
2013-04-16 12:30:51 +00:00
|
|
|
OS::MemMoveFunction generated_memmove = CreateMemMoveFunction();
|
|
|
|
if (generated_memmove != NULL) {
|
|
|
|
memmove_function = generated_memmove;
|
|
|
|
}
|
2013-07-29 12:12:39 +00:00
|
|
|
#elif defined(V8_HOST_ARCH_ARM)
|
2013-07-10 15:32:39 +00:00
|
|
|
OS::memcopy_uint8_function =
|
|
|
|
CreateMemCopyUint8Function(&OS::MemCopyUint8Wrapper);
|
|
|
|
OS::memcopy_uint16_uint8_function =
|
|
|
|
CreateMemCopyUint16Uint8Function(&OS::MemCopyUint16Uint8Wrapper);
|
2012-04-05 14:10:39 +00:00
|
|
|
#endif
|
|
|
|
init_fast_sin_function();
|
|
|
|
init_fast_cos_function();
|
|
|
|
init_fast_tan_function();
|
|
|
|
init_fast_log_function();
|
2012-11-26 13:12:35 +00:00
|
|
|
// fast_exp is initialized lazily.
|
2012-04-05 14:10:39 +00:00
|
|
|
init_fast_sqrt_function();
|
|
|
|
}
|
|
|
|
|
2013-07-05 09:52:11 +00:00
|
|
|
|
2009-03-27 08:38:02 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// POSIX string support.
|
|
|
|
//
|
|
|
|
|
|
|
|
char* OS::StrChr(char* str, int c) {
|
|
|
|
return strchr(str, c);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void OS::StrNCpy(Vector<char> dest, const char* src, size_t n) {
|
|
|
|
strncpy(dest.start(), src, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-11 11:37:08 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// POSIX thread support.
|
|
|
|
//
|
|
|
|
|
2013-07-23 13:47:50 +00:00
|
|
|
class Thread::PlatformData : public Malloced {
|
|
|
|
public:
|
|
|
|
PlatformData() : thread_(kNoThread) {}
|
|
|
|
pthread_t thread_; // Thread handle for pthread.
|
|
|
|
};
|
|
|
|
|
|
|
|
Thread::Thread(const Options& options)
|
|
|
|
: data_(new PlatformData),
|
|
|
|
stack_size_(options.stack_size()),
|
|
|
|
start_semaphore_(NULL) {
|
|
|
|
set_name(options.name());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Thread::~Thread() {
|
|
|
|
delete data_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void SetThreadName(const char* name) {
|
2013-07-29 12:12:39 +00:00
|
|
|
#if defined(__DragonFly__) || defined(__FreeBSD__) || defined(__OpenBSD__)
|
2013-07-25 08:00:32 +00:00
|
|
|
pthread_set_name_np(pthread_self(), name);
|
2013-07-29 12:12:39 +00:00
|
|
|
#elif defined(__NetBSD__)
|
2013-07-23 13:47:50 +00:00
|
|
|
STATIC_ASSERT(Thread::kMaxThreadNameLength <= PTHREAD_MAX_NAMELEN_NP);
|
2013-07-25 08:00:32 +00:00
|
|
|
pthread_setname_np(pthread_self(), "%s", name);
|
2013-07-29 12:12:39 +00:00
|
|
|
#elif defined(__APPLE__)
|
2013-07-23 13:47:50 +00:00
|
|
|
// pthread_setname_np is only available in 10.6 or later, so test
|
|
|
|
// for it at runtime.
|
|
|
|
int (*dynamic_pthread_setname_np)(const char*);
|
|
|
|
*reinterpret_cast<void**>(&dynamic_pthread_setname_np) =
|
|
|
|
dlsym(RTLD_DEFAULT, "pthread_setname_np");
|
|
|
|
if (dynamic_pthread_setname_np == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Mac OS X does not expose the length limit of the name, so hardcode it.
|
|
|
|
static const int kMaxNameLength = 63;
|
|
|
|
STATIC_ASSERT(Thread::kMaxThreadNameLength <= kMaxNameLength);
|
2013-07-25 08:00:32 +00:00
|
|
|
dynamic_pthread_setname_np(name);
|
2013-07-23 13:47:50 +00:00
|
|
|
#elif defined(PR_SET_NAME)
|
2013-07-25 08:00:32 +00:00
|
|
|
prctl(PR_SET_NAME,
|
|
|
|
reinterpret_cast<unsigned long>(name), // NOLINT
|
|
|
|
0, 0, 0);
|
2013-07-23 13:47:50 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void* ThreadEntry(void* arg) {
|
|
|
|
Thread* thread = reinterpret_cast<Thread*>(arg);
|
|
|
|
// This is also initialized by the first argument to pthread_create() but we
|
|
|
|
// don't know which thread will run first (the original thread or the new
|
|
|
|
// one) so we initialize it here too.
|
|
|
|
thread->data()->thread_ = pthread_self();
|
|
|
|
SetThreadName(thread->name());
|
|
|
|
ASSERT(thread->data()->thread_ != kNoThread);
|
|
|
|
thread->NotifyStartedAndRun();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Thread::set_name(const char* name) {
|
|
|
|
strncpy(name_, name, sizeof(name_));
|
|
|
|
name_[sizeof(name_) - 1] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Thread::Start() {
|
|
|
|
int result;
|
|
|
|
pthread_attr_t attr;
|
|
|
|
memset(&attr, 0, sizeof(attr));
|
|
|
|
result = pthread_attr_init(&attr);
|
|
|
|
ASSERT_EQ(0, result);
|
|
|
|
// Native client uses default stack size.
|
2013-07-29 12:12:39 +00:00
|
|
|
#if !defined(__native_client__)
|
2013-07-23 13:47:50 +00:00
|
|
|
if (stack_size_ > 0) {
|
|
|
|
result = pthread_attr_setstacksize(&attr, static_cast<size_t>(stack_size_));
|
|
|
|
ASSERT_EQ(0, result);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
result = pthread_create(&data_->thread_, &attr, ThreadEntry, this);
|
|
|
|
ASSERT_EQ(0, result);
|
|
|
|
result = pthread_attr_destroy(&attr);
|
|
|
|
ASSERT_EQ(0, result);
|
|
|
|
ASSERT(data_->thread_ != kNoThread);
|
|
|
|
USE(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Thread::Join() {
|
|
|
|
pthread_join(data_->thread_, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-11 11:37:08 +00:00
|
|
|
void Thread::YieldCPU() {
|
2013-07-23 13:47:50 +00:00
|
|
|
int result = sched_yield();
|
|
|
|
ASSERT_EQ(0, result);
|
|
|
|
USE(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Thread::LocalStorageKey PthreadKeyToLocalKey(pthread_key_t pthread_key) {
|
2013-07-29 12:12:39 +00:00
|
|
|
#if defined(__CYGWIN__)
|
2013-07-23 13:47:50 +00:00
|
|
|
// We need to cast pthread_key_t to Thread::LocalStorageKey in two steps
|
|
|
|
// because pthread_key_t is a pointer type on Cygwin. This will probably not
|
|
|
|
// work on 64-bit platforms, but Cygwin doesn't support 64-bit anyway.
|
|
|
|
STATIC_ASSERT(sizeof(Thread::LocalStorageKey) == sizeof(pthread_key_t));
|
|
|
|
intptr_t ptr_key = reinterpret_cast<intptr_t>(pthread_key);
|
|
|
|
return static_cast<Thread::LocalStorageKey>(ptr_key);
|
|
|
|
#else
|
|
|
|
return static_cast<Thread::LocalStorageKey>(pthread_key);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static pthread_key_t LocalKeyToPthreadKey(Thread::LocalStorageKey local_key) {
|
2013-07-29 12:12:39 +00:00
|
|
|
#if defined(__CYGWIN__)
|
2013-07-23 13:47:50 +00:00
|
|
|
STATIC_ASSERT(sizeof(Thread::LocalStorageKey) == sizeof(pthread_key_t));
|
|
|
|
intptr_t ptr_key = static_cast<intptr_t>(local_key);
|
|
|
|
return reinterpret_cast<pthread_key_t>(ptr_key);
|
|
|
|
#else
|
|
|
|
return static_cast<pthread_key_t>(local_key);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef V8_FAST_TLS_SUPPORTED
|
|
|
|
|
|
|
|
static Atomic32 tls_base_offset_initialized = 0;
|
|
|
|
intptr_t kMacTlsBaseOffset = 0;
|
|
|
|
|
|
|
|
// It's safe to do the initialization more that once, but it has to be
|
|
|
|
// done at least once.
|
|
|
|
static void InitializeTlsBaseOffset() {
|
|
|
|
const size_t kBufferSize = 128;
|
|
|
|
char buffer[kBufferSize];
|
|
|
|
size_t buffer_size = kBufferSize;
|
|
|
|
int ctl_name[] = { CTL_KERN , KERN_OSRELEASE };
|
|
|
|
if (sysctl(ctl_name, 2, buffer, &buffer_size, NULL, 0) != 0) {
|
|
|
|
V8_Fatal(__FILE__, __LINE__, "V8 failed to get kernel version");
|
|
|
|
}
|
|
|
|
// The buffer now contains a string of the form XX.YY.ZZ, where
|
|
|
|
// XX is the major kernel version component.
|
|
|
|
// Make sure the buffer is 0-terminated.
|
|
|
|
buffer[kBufferSize - 1] = '\0';
|
|
|
|
char* period_pos = strchr(buffer, '.');
|
|
|
|
*period_pos = '\0';
|
|
|
|
int kernel_version_major =
|
|
|
|
static_cast<int>(strtol(buffer, NULL, 10)); // NOLINT
|
|
|
|
// The constants below are taken from pthreads.s from the XNU kernel
|
|
|
|
// sources archive at www.opensource.apple.com.
|
|
|
|
if (kernel_version_major < 11) {
|
|
|
|
// 8.x.x (Tiger), 9.x.x (Leopard), 10.x.x (Snow Leopard) have the
|
|
|
|
// same offsets.
|
|
|
|
#if V8_HOST_ARCH_IA32
|
|
|
|
kMacTlsBaseOffset = 0x48;
|
|
|
|
#else
|
|
|
|
kMacTlsBaseOffset = 0x60;
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
// 11.x.x (Lion) changed the offset.
|
|
|
|
kMacTlsBaseOffset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Release_Store(&tls_base_offset_initialized, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void CheckFastTls(Thread::LocalStorageKey key) {
|
|
|
|
void* expected = reinterpret_cast<void*>(0x1234CAFE);
|
|
|
|
Thread::SetThreadLocal(key, expected);
|
|
|
|
void* actual = Thread::GetExistingThreadLocal(key);
|
|
|
|
if (expected != actual) {
|
|
|
|
V8_Fatal(__FILE__, __LINE__,
|
|
|
|
"V8 failed to initialize fast TLS on current kernel");
|
|
|
|
}
|
|
|
|
Thread::SetThreadLocal(key, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // V8_FAST_TLS_SUPPORTED
|
|
|
|
|
|
|
|
|
|
|
|
Thread::LocalStorageKey Thread::CreateThreadLocalKey() {
|
|
|
|
#ifdef V8_FAST_TLS_SUPPORTED
|
|
|
|
bool check_fast_tls = false;
|
|
|
|
if (tls_base_offset_initialized == 0) {
|
|
|
|
check_fast_tls = true;
|
|
|
|
InitializeTlsBaseOffset();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
pthread_key_t key;
|
|
|
|
int result = pthread_key_create(&key, NULL);
|
|
|
|
ASSERT_EQ(0, result);
|
|
|
|
USE(result);
|
|
|
|
LocalStorageKey local_key = PthreadKeyToLocalKey(key);
|
|
|
|
#ifdef V8_FAST_TLS_SUPPORTED
|
|
|
|
// If we just initialized fast TLS support, make sure it works.
|
|
|
|
if (check_fast_tls) CheckFastTls(local_key);
|
|
|
|
#endif
|
|
|
|
return local_key;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Thread::DeleteThreadLocalKey(LocalStorageKey key) {
|
|
|
|
pthread_key_t pthread_key = LocalKeyToPthreadKey(key);
|
|
|
|
int result = pthread_key_delete(pthread_key);
|
|
|
|
ASSERT_EQ(0, result);
|
|
|
|
USE(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void* Thread::GetThreadLocal(LocalStorageKey key) {
|
|
|
|
pthread_key_t pthread_key = LocalKeyToPthreadKey(key);
|
|
|
|
return pthread_getspecific(pthread_key);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Thread::SetThreadLocal(LocalStorageKey key, void* value) {
|
|
|
|
pthread_key_t pthread_key = LocalKeyToPthreadKey(key);
|
|
|
|
int result = pthread_setspecific(pthread_key, value);
|
|
|
|
ASSERT_EQ(0, result);
|
|
|
|
USE(result);
|
2013-07-11 11:37:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-24 06:32:00 +00:00
|
|
|
} } // namespace v8::internal
|