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

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

Issue 6706018: Fix fast TLS support on Mac. (Closed)
Patch Set: Addressing review comment Created 9 years, 8 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
« no previous file with comments | « no previous file | src/platform-tls-mac.h » ('j') | src/platform-tls-mac.h » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. 1 // Copyright 2006-2008 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 30 matching lines...) Expand all
41 #include <semaphore.h> 41 #include <semaphore.h>
42 #include <signal.h> 42 #include <signal.h>
43 #include <libkern/OSAtomic.h> 43 #include <libkern/OSAtomic.h>
44 #include <mach/mach.h> 44 #include <mach/mach.h>
45 #include <mach/semaphore.h> 45 #include <mach/semaphore.h>
46 #include <mach/task.h> 46 #include <mach/task.h>
47 #include <mach/vm_statistics.h> 47 #include <mach/vm_statistics.h>
48 #include <sys/time.h> 48 #include <sys/time.h>
49 #include <sys/resource.h> 49 #include <sys/resource.h>
50 #include <sys/types.h> 50 #include <sys/types.h>
51 #include <sys/sysctl.h>
51 #include <stdarg.h> 52 #include <stdarg.h>
52 #include <stdlib.h> 53 #include <stdlib.h>
54 #include <string.h>
53 #include <errno.h> 55 #include <errno.h>
54 56
55 #undef MAP_TYPE 57 #undef MAP_TYPE
56 58
57 #include "v8.h" 59 #include "v8.h"
58 60
59 #include "platform.h" 61 #include "platform.h"
60 #include "vm-state-inl.h" 62 #include "vm-state-inl.h"
61 63
62 // Manually define these here as weak imports, rather than including execinfo.h. 64 // Manually define these here as weak imports, rather than including execinfo.h.
(...skipping 437 matching lines...) Expand 10 before | Expand all | Expand 10 after
500 pthread_create(&thread_handle_data()->thread_, attr_ptr, ThreadEntry, this); 502 pthread_create(&thread_handle_data()->thread_, attr_ptr, ThreadEntry, this);
501 ASSERT(IsValid()); 503 ASSERT(IsValid());
502 } 504 }
503 505
504 506
505 void Thread::Join() { 507 void Thread::Join() {
506 pthread_join(thread_handle_data()->thread_, NULL); 508 pthread_join(thread_handle_data()->thread_, NULL);
507 } 509 }
508 510
509 511
512 #ifdef V8_FAST_TLS_SUPPORTED
513
514 static Atomic32 tls_base_offset_initialized = 0;
515 intptr_t kMacTlsBaseOffset = 0;
fschneider 2011/04/04 07:39:26 not sure: Can you guarantee that writes to kMacTls
Vitaly Repeshko 2011/04/04 21:15:43 Good question. Technically to prevent the offset f
516
517 // It's safe to do the initialization more that once, but it has to be
518 // done at least once.
519 static void InitializeTlsBaseOffset() {
520 const size_t kBufferSize = 128;
521 char buffer[kBufferSize];
522 size_t buffer_size = kBufferSize;
523 int ctl_name[] = { CTL_KERN , KERN_OSRELEASE };
524 if (sysctl(ctl_name, 2, buffer, &buffer_size, NULL, 0) != 0) {
525 V8_Fatal(__FILE__, __LINE__, "V8 failed to get kernel version");
526 }
527 // The buffer now contains a string of the form XX.YY.ZZ, where
528 // XX is the major kernel version component.
529 // Make sure the buffer is 0-terminated.
530 buffer[kBufferSize - 1] = '\0';
531 char* period_pos = strchr(buffer, '.');
532 *period_pos = '\0';
533 int kernel_version_major =
534 static_cast<int>(strtol(buffer, NULL, 10)); // NOLINT
535 // The constants below are taken from pthreads.s from the XNU kernel
536 // sources archive at www.opensource.apple.com.
537 if (kernel_version_major < 11) {
538 // 8.x.x (Tiger), 9.x.x (Leopard), 10.x.x (Snow Leopard) have the
539 // same offsets.
540 #if defined(V8_HOST_ARCH_IA32)
541 kMacTlsBaseOffset = 0x48;
542 #else
543 kMacTlsBaseOffset = 0x60;
544 #endif
545 } else {
546 // 11.x.x (Lion) changed the offset.
Mark Mentovai 2011/04/01 16:37:16 Perhaps Avi would be satisfied if you had a test h
Avi (use Gerrit) 2011/04/01 16:41:52 Yes, that would make me happy.
Vitaly Repeshko 2011/04/01 16:47:55 Guys, do you want this in addition to CheckFastTls
547 kMacTlsBaseOffset = 0;
548 }
549
550 Release_Store(&tls_base_offset_initialized, 1);
551 }
552
553 static void CheckFastTls(Thread::LocalStorageKey key) {
554 void* expected = reinterpret_cast<void*>(0x1234CAFE);
555 Thread::SetThreadLocal(key, expected);
556 void* actual = Thread::GetExistingThreadLocal(key);
557 if (expected != actual) {
558 V8_Fatal(__FILE__, __LINE__,
559 "V8 failed to initialize fast TLS on current kernel");
560 }
561 Thread::SetThreadLocal(key, NULL);
562 }
563
564 #endif // V8_FAST_TLS_SUPPORTED
565
566
510 Thread::LocalStorageKey Thread::CreateThreadLocalKey() { 567 Thread::LocalStorageKey Thread::CreateThreadLocalKey() {
568 #ifdef V8_FAST_TLS_SUPPORTED
569 bool check_fast_tls = false;
570 if (tls_base_offset_initialized == 0) {
571 check_fast_tls = true;
572 InitializeTlsBaseOffset();
573 }
574 #endif
511 pthread_key_t key; 575 pthread_key_t key;
512 int result = pthread_key_create(&key, NULL); 576 int result = pthread_key_create(&key, NULL);
513 USE(result); 577 USE(result);
514 ASSERT(result == 0); 578 ASSERT(result == 0);
515 return static_cast<LocalStorageKey>(key); 579 LocalStorageKey typed_key = static_cast<LocalStorageKey>(key);
580 #ifdef V8_FAST_TLS_SUPPORTED
581 // If we just initialized fast TLS support, make sure it works.
582 if (check_fast_tls) CheckFastTls(typed_key);
583 #endif
584 return typed_key;
516 } 585 }
517 586
518 587
519 void Thread::DeleteThreadLocalKey(LocalStorageKey key) { 588 void Thread::DeleteThreadLocalKey(LocalStorageKey key) {
520 pthread_key_t pthread_key = static_cast<pthread_key_t>(key); 589 pthread_key_t pthread_key = static_cast<pthread_key_t>(key);
521 int result = pthread_key_delete(pthread_key); 590 int result = pthread_key_delete(pthread_key);
522 USE(result); 591 USE(result);
523 ASSERT(result == 0); 592 ASSERT(result == 0);
524 } 593 }
525 594
(...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after
787 856
788 void Sampler::Stop() { 857 void Sampler::Stop() {
789 ASSERT(IsActive()); 858 ASSERT(IsActive());
790 SamplerThread::RemoveActiveSampler(this); 859 SamplerThread::RemoveActiveSampler(this);
791 SetActive(false); 860 SetActive(false);
792 } 861 }
793 862
794 #endif // ENABLE_LOGGING_AND_PROFILING 863 #endif // ENABLE_LOGGING_AND_PROFILING
795 864
796 } } // namespace v8::internal 865 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « no previous file | src/platform-tls-mac.h » ('j') | src/platform-tls-mac.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698