Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(33)

Side by Side Diff: src/platform-macos.cc

Issue 19490010: Avoid duplication of OS::Thread methods for every POSIX platform. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Tweaks Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/platform-linux.cc ('k') | src/platform-openbsd.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
71 extern char** backtrace_symbols(void* const*, int) 71 extern char** backtrace_symbols(void* const*, int)
72 __attribute__((weak_import)); 72 __attribute__((weak_import));
73 extern void backtrace_symbols_fd(void* const*, int, int) 73 extern void backtrace_symbols_fd(void* const*, int, int)
74 __attribute__((weak_import)); 74 __attribute__((weak_import));
75 } 75 }
76 76
77 77
78 namespace v8 { 78 namespace v8 {
79 namespace internal { 79 namespace internal {
80 80
81 // 0 is never a valid thread id on MacOSX since a pthread_t is
82 // a pointer.
83 static const pthread_t kNoThread = (pthread_t) 0;
84
85 81
86 double ceiling(double x) { 82 double ceiling(double x) {
87 // Correct Mac OS X Leopard 'ceil' behavior. 83 // Correct Mac OS X Leopard 'ceil' behavior.
88 if (-1.0 < x && x < 0.0) { 84 if (-1.0 < x && x < 0.0) {
89 return -0.0; 85 return -0.0;
90 } else { 86 } else {
91 return ceil(x); 87 return ceil(x);
92 } 88 }
93 } 89 }
94 90
(...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after
414 bool VirtualMemory::ReleaseRegion(void* address, size_t size) { 410 bool VirtualMemory::ReleaseRegion(void* address, size_t size) {
415 return munmap(address, size) == 0; 411 return munmap(address, size) == 0;
416 } 412 }
417 413
418 414
419 bool VirtualMemory::HasLazyCommits() { 415 bool VirtualMemory::HasLazyCommits() {
420 return false; 416 return false;
421 } 417 }
422 418
423 419
424 class Thread::PlatformData : public Malloced {
425 public:
426 PlatformData() : thread_(kNoThread) {}
427 pthread_t thread_; // Thread handle for pthread.
428 };
429
430
431 Thread::Thread(const Options& options)
432 : data_(new PlatformData),
433 stack_size_(options.stack_size()),
434 start_semaphore_(NULL) {
435 set_name(options.name());
436 }
437
438
439 Thread::~Thread() {
440 delete data_;
441 }
442
443
444 static void SetThreadName(const char* name) {
445 // pthread_setname_np is only available in 10.6 or later, so test
446 // for it at runtime.
447 int (*dynamic_pthread_setname_np)(const char*);
448 *reinterpret_cast<void**>(&dynamic_pthread_setname_np) =
449 dlsym(RTLD_DEFAULT, "pthread_setname_np");
450 if (!dynamic_pthread_setname_np)
451 return;
452
453 // Mac OS X does not expose the length limit of the name, so hardcode it.
454 static const int kMaxNameLength = 63;
455 USE(kMaxNameLength);
456 ASSERT(Thread::kMaxThreadNameLength <= kMaxNameLength);
457 dynamic_pthread_setname_np(name);
458 }
459
460
461 static void* ThreadEntry(void* arg) {
462 Thread* thread = reinterpret_cast<Thread*>(arg);
463 // This is also initialized by the first argument to pthread_create() but we
464 // don't know which thread will run first (the original thread or the new
465 // one) so we initialize it here too.
466 thread->data()->thread_ = pthread_self();
467 SetThreadName(thread->name());
468 ASSERT(thread->data()->thread_ != kNoThread);
469 thread->NotifyStartedAndRun();
470 return NULL;
471 }
472
473
474 void Thread::set_name(const char* name) {
475 strncpy(name_, name, sizeof(name_));
476 name_[sizeof(name_) - 1] = '\0';
477 }
478
479
480 void Thread::Start() {
481 pthread_attr_t* attr_ptr = NULL;
482 pthread_attr_t attr;
483 if (stack_size_ > 0) {
484 pthread_attr_init(&attr);
485 pthread_attr_setstacksize(&attr, static_cast<size_t>(stack_size_));
486 attr_ptr = &attr;
487 }
488 pthread_create(&data_->thread_, attr_ptr, ThreadEntry, this);
489 ASSERT(data_->thread_ != kNoThread);
490 }
491
492
493 void Thread::Join() {
494 pthread_join(data_->thread_, NULL);
495 }
496
497
498 #ifdef V8_FAST_TLS_SUPPORTED
499
500 static Atomic32 tls_base_offset_initialized = 0;
501 intptr_t kMacTlsBaseOffset = 0;
502
503 // It's safe to do the initialization more that once, but it has to be
504 // done at least once.
505 static void InitializeTlsBaseOffset() {
506 const size_t kBufferSize = 128;
507 char buffer[kBufferSize];
508 size_t buffer_size = kBufferSize;
509 int ctl_name[] = { CTL_KERN , KERN_OSRELEASE };
510 if (sysctl(ctl_name, 2, buffer, &buffer_size, NULL, 0) != 0) {
511 V8_Fatal(__FILE__, __LINE__, "V8 failed to get kernel version");
512 }
513 // The buffer now contains a string of the form XX.YY.ZZ, where
514 // XX is the major kernel version component.
515 // Make sure the buffer is 0-terminated.
516 buffer[kBufferSize - 1] = '\0';
517 char* period_pos = strchr(buffer, '.');
518 *period_pos = '\0';
519 int kernel_version_major =
520 static_cast<int>(strtol(buffer, NULL, 10)); // NOLINT
521 // The constants below are taken from pthreads.s from the XNU kernel
522 // sources archive at www.opensource.apple.com.
523 if (kernel_version_major < 11) {
524 // 8.x.x (Tiger), 9.x.x (Leopard), 10.x.x (Snow Leopard) have the
525 // same offsets.
526 #if V8_HOST_ARCH_IA32
527 kMacTlsBaseOffset = 0x48;
528 #else
529 kMacTlsBaseOffset = 0x60;
530 #endif
531 } else {
532 // 11.x.x (Lion) changed the offset.
533 kMacTlsBaseOffset = 0;
534 }
535
536 Release_Store(&tls_base_offset_initialized, 1);
537 }
538
539
540 static void CheckFastTls(Thread::LocalStorageKey key) {
541 void* expected = reinterpret_cast<void*>(0x1234CAFE);
542 Thread::SetThreadLocal(key, expected);
543 void* actual = Thread::GetExistingThreadLocal(key);
544 if (expected != actual) {
545 V8_Fatal(__FILE__, __LINE__,
546 "V8 failed to initialize fast TLS on current kernel");
547 }
548 Thread::SetThreadLocal(key, NULL);
549 }
550
551 #endif // V8_FAST_TLS_SUPPORTED
552
553
554 Thread::LocalStorageKey Thread::CreateThreadLocalKey() {
555 #ifdef V8_FAST_TLS_SUPPORTED
556 bool check_fast_tls = false;
557 if (tls_base_offset_initialized == 0) {
558 check_fast_tls = true;
559 InitializeTlsBaseOffset();
560 }
561 #endif
562 pthread_key_t key;
563 int result = pthread_key_create(&key, NULL);
564 USE(result);
565 ASSERT(result == 0);
566 LocalStorageKey typed_key = static_cast<LocalStorageKey>(key);
567 #ifdef V8_FAST_TLS_SUPPORTED
568 // If we just initialized fast TLS support, make sure it works.
569 if (check_fast_tls) CheckFastTls(typed_key);
570 #endif
571 return typed_key;
572 }
573
574
575 void Thread::DeleteThreadLocalKey(LocalStorageKey key) {
576 pthread_key_t pthread_key = static_cast<pthread_key_t>(key);
577 int result = pthread_key_delete(pthread_key);
578 USE(result);
579 ASSERT(result == 0);
580 }
581
582
583 void* Thread::GetThreadLocal(LocalStorageKey key) {
584 pthread_key_t pthread_key = static_cast<pthread_key_t>(key);
585 return pthread_getspecific(pthread_key);
586 }
587
588
589 void Thread::SetThreadLocal(LocalStorageKey key, void* value) {
590 pthread_key_t pthread_key = static_cast<pthread_key_t>(key);
591 pthread_setspecific(pthread_key, value);
592 }
593
594
595 class MacOSSemaphore : public Semaphore { 420 class MacOSSemaphore : public Semaphore {
596 public: 421 public:
597 explicit MacOSSemaphore(int count) { 422 explicit MacOSSemaphore(int count) {
598 int r; 423 int r;
599 r = semaphore_create(mach_task_self(), 424 r = semaphore_create(mach_task_self(),
600 &semaphore_, 425 &semaphore_,
601 SYNC_POLICY_FIFO, 426 SYNC_POLICY_FIFO,
602 count); 427 count);
603 ASSERT(r == KERN_SUCCESS); 428 ASSERT(r == KERN_SUCCESS);
604 } 429 }
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
646 limit_mutex = CreateMutex(); 471 limit_mutex = CreateMutex();
647 } 472 }
648 473
649 474
650 void OS::TearDown() { 475 void OS::TearDown() {
651 delete limit_mutex; 476 delete limit_mutex;
652 } 477 }
653 478
654 479
655 } } // namespace v8::internal 480 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/platform-linux.cc ('k') | src/platform-openbsd.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698