platform-posix.cc 20.6 KB
Newer Older
1
// Copyright 2012 the V8 project authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
// 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,
30
// Mac OS, FreeBSD and OpenBSD.
31

32 33
#include "platform-posix.h"

34
#include <dlfcn.h>
35
#include <pthread.h>
36
#if defined(__DragonFly__) || defined(__FreeBSD__) || defined(__OpenBSD__)
37 38
#include <pthread_np.h>  // for pthread_set_name_np
#endif
39
#include <sched.h>  // for sched_yield
40 41
#include <unistd.h>
#include <errno.h>
42
#include <time.h>
43

44
#include <sys/mman.h>
45
#include <sys/socket.h>
46 47
#include <sys/resource.h>
#include <sys/time.h>
48
#include <sys/types.h>
49
#include <sys/stat.h>
50
#if defined(__linux__)
51
#include <sys/prctl.h>  // for prctl
52 53 54
#endif
#if defined(__APPLE__) || defined(__DragonFly__) || defined(__FreeBSD__) || \
    defined(__NetBSD__) || defined(__OpenBSD__)
55 56
#include <sys/sysctl.h>  // for sysctl
#endif
57 58 59 60 61

#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>

62 63
#undef MAP_TYPE

64
#if defined(ANDROID) && !defined(V8_ANDROID_LOG_STDOUT)
65
#define LOG_TAG "v8"
66
#include <android/log.h>
67 68
#endif

69 70
#include "v8.h"

71
#include "codegen.h"
72 73
#include "platform.h"

74 75
namespace v8 {
namespace internal {
76

77 78 79
// 0 is never a valid thread id.
static const pthread_t kNoThread = (pthread_t) 0;

80

81
uint64_t OS::CpuFeaturesImpliedByPlatform() {
82
#if V8_OS_MACOSX
83 84 85
  // 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;
86
  return (one << SSE2) | (one << CMOV);
87 88 89 90 91 92
#else
  return 0;  // Nothing special about the other systems.
#endif
}


93 94 95 96 97 98 99 100 101 102 103
// 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;
}


104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
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
}


122
intptr_t OS::CommitPageSize() {
123 124
  static intptr_t page_size = getpagesize();
  return page_size;
125 126 127
}


128 129 130 131 132 133 134 135
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);
}


136 137
// Get rid of writable permission on code allocations.
void OS::ProtectCode(void* address, const size_t size) {
138
#if defined(__CYGWIN__)
139 140
  DWORD old_protect;
  VirtualProtect(address, size, PAGE_EXECUTE_READ, &old_protect);
141
#elif defined(__native_client__)
142 143 144 145
  // The Native Client port of V8 uses an interpreter, so
  // code pages don't need PROT_EXEC.
  mprotect(address, size, PROT_READ);
#else
146
  mprotect(address, size, PROT_READ | PROT_EXEC);
147
#endif
148 149 150
}


151 152
// Create guard pages.
void OS::Guard(void* address, const size_t size) {
153
#if defined(__CYGWIN__)
154 155 156
  DWORD oldprotect;
  VirtualProtect(address, size, PAGE_READONLY | PAGE_GUARD, &oldprotect);
#else
157
  mprotect(address, size, PROT_NONE);
158
#endif
159 160 161
}


162
void* OS::GetRandomMmapAddr() {
163
#if defined(__native_client__)
164 165 166 167 168
  // 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
169 170 171 172 173
  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) {
174
#if V8_TARGET_ARCH_X64
175 176 177 178 179 180 181 182 183
    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);
184 185 186

    raw_addr &= 0x3ffff000;

187
# ifdef __sun
188 189 190 191 192 193 194 195 196 197
    // 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;
198
# else
199 200 201 202
    // 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;
203 204
# endif
#endif
205 206 207 208 209 210
    return reinterpret_cast<void*>(raw_addr);
  }
  return NULL;
}


211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
size_t OS::AllocateAlignment() {
  return getpagesize();
}


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
237
#if defined(__native_client__)
238 239 240
  asm("hlt");
#else
  asm("int $3");
241
#endif  // __native_client__
242 243 244 245 246 247 248 249
#elif V8_HOST_ARCH_X64
  asm("int $3");
#else
#error Unsupported host architecture.
#endif
}


250 251 252
// ----------------------------------------------------------------------------
// Math functions

253 254 255 256 257 258
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);
}


259 260 261
double modulo(double x, double y) {
  return fmod(x, y);
}
262

263

264 265
#define UNARY_MATH_FUNCTION(name, generator)             \
static UnaryMathFunction fast_##name##_function = NULL;  \
266 267 268
void init_fast_##name##_function() {                     \
  fast_##name##_function = generator;                    \
}                                                        \
269 270
double fast_##name(double x) {                           \
  return (*fast_##name##_function)(x);                   \
271 272
}

273 274 275 276
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))
277
UNARY_MATH_FUNCTION(exp, CreateExpFunction())
278
UNARY_MATH_FUNCTION(sqrt, CreateSqrtFunction())
279

280
#undef UNARY_MATH_FUNCTION
281 282


283 284 285 286 287 288 289
void lazily_initialize_fast_exp() {
  if (fast_exp_function == NULL) {
    init_fast_exp_function();
  }
}


290 291 292 293 294 295
double OS::nan_value() {
  // NAN from math.h is defined in C99 and not in POSIX.
  return NAN;
}


296 297 298 299 300
int OS::GetCurrentProcessId() {
  return static_cast<int>(getpid());
}


301 302 303 304
// ----------------------------------------------------------------------------
// POSIX date/time support.
//

305 306 307 308 309 310 311 312 313 314 315
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() {
316
  return Time::Now().ToJsTime();
317 318 319 320
}


double OS::DaylightSavingsOffset(double time) {
321
  if (std::isnan(time)) return nan_value();
322 323
  time_t tv = static_cast<time_t>(floor(time/msPerSecond));
  struct tm* t = localtime(&tv);
324
  if (NULL == t) return nan_value();
325 326 327 328
  return t->tm_isdst > 0 ? 3600 * msPerSecond : 0;
}


329 330 331 332 333
int OS::GetLastError() {
  return errno;
}


334 335 336 337 338
// ----------------------------------------------------------------------------
// POSIX stdio support.
//

FILE* OS::FOpen(const char* path, const char* mode) {
339 340 341 342 343 344 345 346
  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;
347 348 349
}


350 351 352 353 354
bool OS::Remove(const char* path) {
  return (remove(path) == 0);
}


355 356 357 358 359
FILE* OS::OpenTemporaryFile() {
  return tmpfile();
}


360
const char* const OS::LogFileOpenMode = "w";
361 362


363 364 365 366 367 368 369 370 371
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) {
372
#if defined(ANDROID) && !defined(V8_ANDROID_LOG_STDOUT)
373
  __android_log_vprint(ANDROID_LOG_INFO, LOG_TAG, format, args);
374
#else
375
  vprintf(format, args);
376
#endif
377 378 379
}


380 381 382 383 384 385 386 387 388
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) {
389
#if defined(ANDROID) && !defined(V8_ANDROID_LOG_STDOUT)
390
  __android_log_vprint(ANDROID_LOG_INFO, LOG_TAG, format, args);
391 392 393 394 395 396
#else
  vfprintf(out, format, args);
#endif
}


397 398 399 400 401 402 403 404 405
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) {
406
#if defined(ANDROID) && !defined(V8_ANDROID_LOG_STDOUT)
407
  __android_log_vprint(ANDROID_LOG_ERROR, LOG_TAG, format, args);
408
#else
409
  vfprintf(stderr, format, args);
410
#endif
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
}


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()) {
428 429 430
    // If the length is zero, the assignment fails.
    if (str.length() > 0)
      str[str.length() - 1] = '\0';
431 432 433 434 435 436 437
    return -1;
  } else {
    return n;
  }
}


438
#if V8_TARGET_ARCH_IA32
439 440 441 442
static void MemMoveWrapper(void* dest, const void* src, size_t size) {
  memmove(dest, src, size);
}

443

444 445 446
// Initialize to library version so we can call this at any time during startup.
static OS::MemMoveFunction memmove_function = &MemMoveWrapper;

447
// Defined in codegen-ia32.cc.
448
OS::MemMoveFunction CreateMemMoveFunction();
449

450 451
// Copy memory area. No restrictions.
void OS::MemMove(void* dest, const void* src, size_t size) {
452
  if (size == 0) return;
453 454
  // Note: here we rely on dependent reads being ordered. This is true
  // on all architectures we currently support.
455
  (*memmove_function)(dest, src, size);
456
}
457

458
#elif defined(V8_HOST_ARCH_ARM)
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
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
478

479

480
void OS::PostSetUp() {
481
#if V8_TARGET_ARCH_IA32
482 483 484 485
  OS::MemMoveFunction generated_memmove = CreateMemMoveFunction();
  if (generated_memmove != NULL) {
    memmove_function = generated_memmove;
  }
486
#elif defined(V8_HOST_ARCH_ARM)
487 488 489 490
  OS::memcopy_uint8_function =
      CreateMemCopyUint8Function(&OS::MemCopyUint8Wrapper);
  OS::memcopy_uint16_uint8_function =
      CreateMemCopyUint16Uint8Function(&OS::MemCopyUint16Uint8Wrapper);
491 492 493 494 495
#endif
  init_fast_sin_function();
  init_fast_cos_function();
  init_fast_tan_function();
  init_fast_log_function();
496
  // fast_exp is initialized lazily.
497 498 499
  init_fast_sqrt_function();
}

500

501 502 503 504 505 506 507 508 509 510 511 512 513 514
// ----------------------------------------------------------------------------
// 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);
}


515 516 517 518
// ----------------------------------------------------------------------------
// POSIX thread support.
//

519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
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) {
539
#if defined(__DragonFly__) || defined(__FreeBSD__) || defined(__OpenBSD__)
540
  pthread_set_name_np(pthread_self(), name);
541
#elif defined(__NetBSD__)
542
  STATIC_ASSERT(Thread::kMaxThreadNameLength <= PTHREAD_MAX_NAMELEN_NP);
543
  pthread_setname_np(pthread_self(), "%s", name);
544
#elif defined(__APPLE__)
545 546 547 548 549 550 551 552 553 554 555
  // 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);
556
  dynamic_pthread_setname_np(name);
557
#elif defined(PR_SET_NAME)
558 559 560
  prctl(PR_SET_NAME,
        reinterpret_cast<unsigned long>(name),  // NOLINT
        0, 0, 0);
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
#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.
591
#if !defined(__native_client__)
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
  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);
}


611
void Thread::YieldCPU() {
612 613 614 615 616 617 618
  int result = sched_yield();
  ASSERT_EQ(0, result);
  USE(result);
}


static Thread::LocalStorageKey PthreadKeyToLocalKey(pthread_key_t pthread_key) {
619
#if defined(__CYGWIN__)
620 621 622 623 624 625 626 627 628 629 630 631 632
  // 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) {
633
#if defined(__CYGWIN__)
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
  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);
739 740 741
}


742
} }  // namespace v8::internal