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-23 13:47:50 +00:00
|
|
|
#if defined(__DragonFly__) || defined(__FreeBSD__) || defined(__OpenBSD__)
|
|
|
|
#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-23 13:47:50 +00:00
|
|
|
#if defined(__linux__)
|
|
|
|
#include <sys/prctl.h> // for prctl
|
|
|
|
#endif
|
|
|
|
#if defined(__APPLE__) || defined(__DragonFly__) || defined(__FreeBSD__) || \
|
|
|
|
defined(__NetBSD__) || defined(__OpenBSD__)
|
|
|
|
#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
|
|
|
|
|
2011-11-21 15:01:52 +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"
|
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
|
|
|
|
|
|
|
// Maximum size of the virtual memory. 0 means there is no artificial
|
|
|
|
// limit.
|
|
|
|
|
|
|
|
intptr_t OS::MaxVirtualMemory() {
|
|
|
|
struct rlimit limit;
|
|
|
|
int result = getrlimit(RLIMIT_DATA, &limit);
|
|
|
|
if (result != 0) return 0;
|
|
|
|
return limit.rlim_cur;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-02 14:08:12 +00:00
|
|
|
intptr_t OS::CommitPageSize() {
|
2012-01-05 10:07:33 +00:00
|
|
|
static intptr_t page_size = getpagesize();
|
|
|
|
return page_size;
|
2011-12-02 14:08:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-23 09:59:14 +00:00
|
|
|
void OS::Free(void* address, const size_t size) {
|
|
|
|
// TODO(1240712): munmap has a return value which is ignored here.
|
|
|
|
int result = munmap(address, size);
|
|
|
|
USE(result);
|
|
|
|
ASSERT(result == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-02 13:34:52 +00:00
|
|
|
// Get rid of writable permission on code allocations.
|
|
|
|
void OS::ProtectCode(void* address, const size_t size) {
|
2013-07-23 09:59:14 +00:00
|
|
|
#if defined(__CYGWIN__)
|
|
|
|
DWORD old_protect;
|
|
|
|
VirtualProtect(address, size, PAGE_EXECUTE_READ, &old_protect);
|
|
|
|
#elif defined(__native_client__)
|
2013-07-18 12:18:35 +00:00
|
|
|
// The Native Client port of V8 uses an interpreter, so
|
|
|
|
// code pages don't need PROT_EXEC.
|
|
|
|
mprotect(address, size, PROT_READ);
|
|
|
|
#else
|
2011-08-02 13:34:52 +00:00
|
|
|
mprotect(address, size, PROT_READ | PROT_EXEC);
|
2013-07-18 12:18:35 +00:00
|
|
|
#endif
|
2011-08-02 13:34:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-20 08:06:43 +00:00
|
|
|
// Create guard pages.
|
|
|
|
void OS::Guard(void* address, const size_t size) {
|
2013-07-23 09:59:14 +00:00
|
|
|
#if defined(__CYGWIN__)
|
|
|
|
DWORD oldprotect;
|
|
|
|
VirtualProtect(address, size, PAGE_READONLY | PAGE_GUARD, &oldprotect);
|
|
|
|
#else
|
2011-07-20 08:06:43 +00:00
|
|
|
mprotect(address, size, PROT_NONE);
|
2013-07-23 09:59:14 +00:00
|
|
|
#endif
|
2011-07-20 08:06:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-10 14:17:42 +00:00
|
|
|
void* OS::GetRandomMmapAddr() {
|
2013-04-09 15:11:45 +00:00
|
|
|
#if defined(__native_client__)
|
|
|
|
// TODO(bradchen): restore randomization once Native Client gets
|
|
|
|
// smarter about using mmap address hints.
|
|
|
|
// See http://code.google.com/p/nativeclient/issues/3341
|
|
|
|
return NULL;
|
|
|
|
#endif
|
2011-10-10 14:17:42 +00:00
|
|
|
Isolate* isolate = Isolate::UncheckedCurrent();
|
|
|
|
// Note that the current isolate isn't set up in a call path via
|
|
|
|
// CpuFeatures::Probe. We don't care about randomization in this case because
|
|
|
|
// the code page is immediately freed.
|
|
|
|
if (isolate != NULL) {
|
2013-06-28 15:34:48 +00:00
|
|
|
#if V8_TARGET_ARCH_X64
|
2011-10-10 14:17:42 +00:00
|
|
|
uint64_t rnd1 = V8::RandomPrivate(isolate);
|
|
|
|
uint64_t rnd2 = V8::RandomPrivate(isolate);
|
|
|
|
uint64_t raw_addr = (rnd1 << 32) ^ rnd2;
|
|
|
|
// Currently available CPUs have 48 bits of virtual addressing. Truncate
|
|
|
|
// the hint address to 46 bits to give the kernel a fighting chance of
|
|
|
|
// fulfilling our placement request.
|
|
|
|
raw_addr &= V8_UINT64_C(0x3ffffffff000);
|
|
|
|
#else
|
|
|
|
uint32_t raw_addr = V8::RandomPrivate(isolate);
|
2013-07-25 08:06:13 +00:00
|
|
|
|
|
|
|
raw_addr &= 0x3ffff000;
|
|
|
|
|
|
|
|
# ifdef __sun
|
|
|
|
// For our Solaris/illumos mmap hint, we pick a random address in the bottom
|
|
|
|
// half of the top half of the address space (that is, the third quarter).
|
|
|
|
// Because we do not MAP_FIXED, this will be treated only as a hint -- the
|
|
|
|
// system will not fail to mmap() because something else happens to already
|
|
|
|
// be mapped at our random address. We deliberately set the hint high enough
|
|
|
|
// to get well above the system's break (that is, the heap); Solaris and
|
|
|
|
// illumos will try the hint and if that fails allocate as if there were
|
|
|
|
// no hint at all. The high hint prevents the break from getting hemmed in
|
|
|
|
// at low values, ceding half of the address space to the system heap.
|
|
|
|
raw_addr += 0x80000000;
|
|
|
|
# else
|
2011-10-10 14:17:42 +00:00
|
|
|
// The range 0x20000000 - 0x60000000 is relatively unpopulated across a
|
|
|
|
// variety of ASLR modes (PAE kernel, NX compat mode, etc) and on macos
|
|
|
|
// 10.6 and 10.7.
|
|
|
|
raw_addr += 0x20000000;
|
2013-07-25 08:06:13 +00:00
|
|
|
# endif
|
2011-10-10 14:17:42 +00:00
|
|
|
#endif
|
|
|
|
return reinterpret_cast<void*>(raw_addr);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-23 09:59:14 +00:00
|
|
|
size_t OS::AllocateAlignment() {
|
|
|
|
return getpagesize();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void OS::Sleep(int milliseconds) {
|
|
|
|
useconds_t ms = static_cast<useconds_t>(milliseconds);
|
|
|
|
usleep(1000 * ms);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int OS::NumberOfCores() {
|
|
|
|
return sysconf(_SC_NPROCESSORS_ONLN);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
#if defined(__native_client__)
|
|
|
|
asm("hlt");
|
|
|
|
#else
|
|
|
|
asm("int $3");
|
|
|
|
#endif // __native_client__
|
|
|
|
#elif V8_HOST_ARCH_X64
|
|
|
|
asm("int $3");
|
|
|
|
#else
|
|
|
|
#error Unsupported host architecture.
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-23 09:18:19 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Math functions
|
|
|
|
|
|
|
|
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() {
|
|
|
|
struct timeval tv;
|
|
|
|
if (gettimeofday(&tv, NULL) < 0) return 0.0;
|
|
|
|
return (static_cast<double>(tv.tv_sec) * 1000) +
|
|
|
|
(static_cast<double>(tv.tv_usec) / 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int64_t OS::Ticks() {
|
|
|
|
// gettimeofday has microsecond resolution.
|
|
|
|
struct timeval tv;
|
|
|
|
if (gettimeofday(&tv, NULL) < 0)
|
|
|
|
return 0;
|
|
|
|
return (static_cast<int64_t>(tv.tv_sec) * 1000000) + tv.tv_usec;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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) {
|
2011-03-17 07:34:37 +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) {
|
2011-03-17 07:34:37 +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) {
|
2011-03-17 07:34:37 +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-10 15:32:39 +00:00
|
|
|
#elif defined(V8_HOST_ARCH_ARM)
|
|
|
|
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-10 15:32:39 +00:00
|
|
|
#elif defined(V8_HOST_ARCH_ARM)
|
|
|
|
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) {
|
|
|
|
#if defined(__DragonFly__) || defined(__FreeBSD__) || defined(__OpenBSD__)
|
2013-07-25 08:00:32 +00:00
|
|
|
pthread_set_name_np(pthread_self(), name);
|
2013-07-23 13:47:50 +00:00
|
|
|
#elif defined(__NetBSD__)
|
|
|
|
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-23 13:47:50 +00:00
|
|
|
#elif defined(__APPLE__)
|
|
|
|
// 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.
|
|
|
|
#if !defined(__native_client__)
|
|
|
|
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) {
|
|
|
|
#if defined(__CYGWIN__)
|
|
|
|
// 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) {
|
|
|
|
#if defined(__CYGWIN__)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class POSIXMutex : public Mutex {
|
|
|
|
public:
|
|
|
|
POSIXMutex() {
|
|
|
|
pthread_mutexattr_t attr;
|
|
|
|
memset(&attr, 0, sizeof(attr));
|
|
|
|
int result = pthread_mutexattr_init(&attr);
|
|
|
|
ASSERT(result == 0);
|
|
|
|
result = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
|
|
|
|
ASSERT(result == 0);
|
|
|
|
result = pthread_mutex_init(&mutex_, &attr);
|
|
|
|
ASSERT(result == 0);
|
|
|
|
result = pthread_mutexattr_destroy(&attr);
|
|
|
|
ASSERT(result == 0);
|
|
|
|
USE(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~POSIXMutex() { pthread_mutex_destroy(&mutex_); }
|
|
|
|
|
|
|
|
virtual int Lock() { return pthread_mutex_lock(&mutex_); }
|
|
|
|
|
|
|
|
virtual int Unlock() { return pthread_mutex_unlock(&mutex_); }
|
|
|
|
|
|
|
|
virtual bool TryLock() {
|
|
|
|
int result = pthread_mutex_trylock(&mutex_);
|
|
|
|
// Return false if the lock is busy and locking failed.
|
|
|
|
if (result == EBUSY) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
ASSERT(result == 0); // Verify no other errors.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
pthread_mutex_t mutex_; // Pthread mutex for POSIX platforms.
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Mutex* OS::CreateMutex() {
|
|
|
|
return new POSIXMutex();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-24 06:32:00 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// POSIX socket support.
|
|
|
|
//
|
|
|
|
|
|
|
|
class POSIXSocket : public Socket {
|
|
|
|
public:
|
|
|
|
explicit POSIXSocket() {
|
|
|
|
// Create the socket.
|
|
|
|
socket_ = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
2010-12-17 12:31:42 +00:00
|
|
|
if (IsValid()) {
|
|
|
|
// Allow rapid reuse.
|
|
|
|
static const int kOn = 1;
|
|
|
|
int ret = setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR,
|
|
|
|
&kOn, sizeof(kOn));
|
|
|
|
ASSERT(ret == 0);
|
|
|
|
USE(ret);
|
|
|
|
}
|
2009-03-24 06:32:00 +00:00
|
|
|
}
|
|
|
|
explicit POSIXSocket(int socket): socket_(socket) { }
|
|
|
|
virtual ~POSIXSocket() { Shutdown(); }
|
|
|
|
|
|
|
|
// Server initialization.
|
|
|
|
bool Bind(const int port);
|
|
|
|
bool Listen(int backlog) const;
|
|
|
|
Socket* Accept() const;
|
|
|
|
|
|
|
|
// Client initialization.
|
|
|
|
bool Connect(const char* host, const char* port);
|
|
|
|
|
|
|
|
// Shutdown socket for both read and write.
|
|
|
|
bool Shutdown();
|
|
|
|
|
|
|
|
// Data Transimission
|
|
|
|
int Send(const char* data, int len) const;
|
|
|
|
int Receive(char* data, int len) const;
|
|
|
|
|
|
|
|
bool SetReuseAddress(bool reuse_address);
|
|
|
|
|
|
|
|
bool IsValid() const { return socket_ != -1; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
int socket_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
bool POSIXSocket::Bind(const int port) {
|
|
|
|
if (!IsValid()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
sockaddr_in addr;
|
|
|
|
memset(&addr, 0, sizeof(addr));
|
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
|
|
|
addr.sin_port = htons(port);
|
|
|
|
int status = bind(socket_,
|
2010-08-12 11:43:10 +00:00
|
|
|
BitCast<struct sockaddr *>(&addr),
|
2009-03-24 06:32:00 +00:00
|
|
|
sizeof(addr));
|
|
|
|
return status == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool POSIXSocket::Listen(int backlog) const {
|
|
|
|
if (!IsValid()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int status = listen(socket_, backlog);
|
|
|
|
return status == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Socket* POSIXSocket::Accept() const {
|
|
|
|
if (!IsValid()) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-05-21 12:58:48 +00:00
|
|
|
int socket;
|
|
|
|
do {
|
|
|
|
socket = accept(socket_, NULL, NULL);
|
|
|
|
} while (socket == -1 && errno == EINTR);
|
|
|
|
|
2009-03-24 06:32:00 +00:00
|
|
|
if (socket == -1) {
|
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
return new POSIXSocket(socket);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool POSIXSocket::Connect(const char* host, const char* port) {
|
|
|
|
if (!IsValid()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup host and port.
|
|
|
|
struct addrinfo *result = NULL;
|
|
|
|
struct addrinfo hints;
|
|
|
|
memset(&hints, 0, sizeof(addrinfo));
|
|
|
|
hints.ai_family = AF_INET;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
hints.ai_protocol = IPPROTO_TCP;
|
|
|
|
int status = getaddrinfo(host, port, &hints, &result);
|
|
|
|
if (status != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connect.
|
2012-05-21 12:58:48 +00:00
|
|
|
do {
|
|
|
|
status = connect(socket_, result->ai_addr, result->ai_addrlen);
|
|
|
|
} while (status == -1 && errno == EINTR);
|
2009-03-24 06:32:00 +00:00
|
|
|
freeaddrinfo(result);
|
|
|
|
return status == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool POSIXSocket::Shutdown() {
|
|
|
|
if (IsValid()) {
|
|
|
|
// Shutdown socket for both read and write.
|
|
|
|
int status = shutdown(socket_, SHUT_RDWR);
|
|
|
|
close(socket_);
|
|
|
|
socket_ = -1;
|
|
|
|
return status == 0;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int POSIXSocket::Send(const char* data, int len) const {
|
2012-05-21 10:02:49 +00:00
|
|
|
if (len <= 0) return 0;
|
2012-05-21 12:58:48 +00:00
|
|
|
int written = 0;
|
|
|
|
while (written < len) {
|
|
|
|
int status = send(socket_, data + written, len - written, 0);
|
|
|
|
if (status == 0) {
|
|
|
|
break;
|
|
|
|
} else if (status > 0) {
|
|
|
|
written += status;
|
|
|
|
} else if (errno != EINTR) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return written;
|
2009-03-24 06:32:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int POSIXSocket::Receive(char* data, int len) const {
|
2012-05-21 10:02:49 +00:00
|
|
|
if (len <= 0) return 0;
|
2012-05-21 12:58:48 +00:00
|
|
|
int status;
|
|
|
|
do {
|
|
|
|
status = recv(socket_, data, len, 0);
|
|
|
|
} while (status == -1 && errno == EINTR);
|
2012-05-21 10:02:49 +00:00
|
|
|
return (status < 0) ? 0 : status;
|
2009-03-24 06:32:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool POSIXSocket::SetReuseAddress(bool reuse_address) {
|
|
|
|
int on = reuse_address ? 1 : 0;
|
|
|
|
int status = setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
|
|
|
|
return status == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-13 13:09:52 +00:00
|
|
|
bool Socket::SetUp() {
|
2009-03-24 06:32:00 +00:00
|
|
|
// Nothing to do on POSIX.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int Socket::LastError() {
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint16_t Socket::HToN(uint16_t value) {
|
|
|
|
return htons(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint16_t Socket::NToH(uint16_t value) {
|
|
|
|
return ntohs(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint32_t Socket::HToN(uint32_t value) {
|
|
|
|
return htonl(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint32_t Socket::NToH(uint32_t value) {
|
|
|
|
return ntohl(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Socket* OS::CreateSocket() {
|
|
|
|
return new POSIXSocket();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} } // namespace v8::internal
|