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

Side by Side Diff: src/platform-posix.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-openbsd.cc ('k') | src/platform-solaris.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 13 matching lines...) Expand all
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 27
28 // Platform specific code for POSIX goes here. This is not a platform on its 28 // Platform specific code for POSIX goes here. This is not a platform on its
29 // own but contains the parts which are the same across POSIX platforms Linux, 29 // own but contains the parts which are the same across POSIX platforms Linux,
30 // Mac OS, FreeBSD and OpenBSD. 30 // Mac OS, FreeBSD and OpenBSD.
31 31
32 #include "platform-posix.h" 32 #include "platform-posix.h"
33 33
34 #include <dlfcn.h>
34 #include <pthread.h> 35 #include <pthread.h>
36 #if defined(__DragonFly__) || defined(__FreeBSD__) || defined(__OpenBSD__)
37 #include <pthread_np.h> // for pthread_set_name_np
38 #endif
35 #include <sched.h> // for sched_yield 39 #include <sched.h> // for sched_yield
36 #include <unistd.h> 40 #include <unistd.h>
37 #include <errno.h> 41 #include <errno.h>
38 #include <time.h> 42 #include <time.h>
39 43
40 #include <sys/mman.h> 44 #include <sys/mman.h>
41 #include <sys/socket.h> 45 #include <sys/socket.h>
42 #include <sys/resource.h> 46 #include <sys/resource.h>
43 #include <sys/time.h> 47 #include <sys/time.h>
44 #include <sys/types.h> 48 #include <sys/types.h>
45 #include <sys/stat.h> 49 #include <sys/stat.h>
50 #if defined(__linux__)
51 #include <sys/prctl.h> // for prctl
52 #endif
53 #if defined(__APPLE__) || defined(__DragonFly__) || defined(__FreeBSD__) || \
54 defined(__NetBSD__) || defined(__OpenBSD__)
55 #include <sys/sysctl.h> // for sysctl
56 #endif
46 57
47 #include <arpa/inet.h> 58 #include <arpa/inet.h>
48 #include <netinet/in.h> 59 #include <netinet/in.h>
49 #include <netdb.h> 60 #include <netdb.h>
50 61
51 #undef MAP_TYPE 62 #undef MAP_TYPE
52 63
53 #if defined(ANDROID) && !defined(V8_ANDROID_LOG_STDOUT) 64 #if defined(ANDROID) && !defined(V8_ANDROID_LOG_STDOUT)
54 #define LOG_TAG "v8" 65 #define LOG_TAG "v8"
55 #include <android/log.h> 66 #include <android/log.h>
56 #endif 67 #endif
57 68
58 #include "v8.h" 69 #include "v8.h"
59 70
60 #include "codegen.h" 71 #include "codegen.h"
61 #include "platform.h" 72 #include "platform.h"
62 73
63 namespace v8 { 74 namespace v8 {
64 namespace internal { 75 namespace internal {
65 76
77 // 0 is never a valid thread id.
78 static const pthread_t kNoThread = (pthread_t) 0;
79
66 80
67 // Maximum size of the virtual memory. 0 means there is no artificial 81 // Maximum size of the virtual memory. 0 means there is no artificial
68 // limit. 82 // limit.
69 83
70 intptr_t OS::MaxVirtualMemory() { 84 intptr_t OS::MaxVirtualMemory() {
71 struct rlimit limit; 85 struct rlimit limit;
72 int result = getrlimit(RLIMIT_DATA, &limit); 86 int result = getrlimit(RLIMIT_DATA, &limit);
73 if (result != 0) return 0; 87 if (result != 0) return 0;
74 return limit.rlim_cur; 88 return limit.rlim_cur;
75 } 89 }
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
210 return (*fast_##name##_function)(x); \ 224 return (*fast_##name##_function)(x); \
211 } 225 }
212 226
213 UNARY_MATH_FUNCTION(sin, CreateTranscendentalFunction(TranscendentalCache::SIN)) 227 UNARY_MATH_FUNCTION(sin, CreateTranscendentalFunction(TranscendentalCache::SIN))
214 UNARY_MATH_FUNCTION(cos, CreateTranscendentalFunction(TranscendentalCache::COS)) 228 UNARY_MATH_FUNCTION(cos, CreateTranscendentalFunction(TranscendentalCache::COS))
215 UNARY_MATH_FUNCTION(tan, CreateTranscendentalFunction(TranscendentalCache::TAN)) 229 UNARY_MATH_FUNCTION(tan, CreateTranscendentalFunction(TranscendentalCache::TAN))
216 UNARY_MATH_FUNCTION(log, CreateTranscendentalFunction(TranscendentalCache::LOG)) 230 UNARY_MATH_FUNCTION(log, CreateTranscendentalFunction(TranscendentalCache::LOG))
217 UNARY_MATH_FUNCTION(exp, CreateExpFunction()) 231 UNARY_MATH_FUNCTION(exp, CreateExpFunction())
218 UNARY_MATH_FUNCTION(sqrt, CreateSqrtFunction()) 232 UNARY_MATH_FUNCTION(sqrt, CreateSqrtFunction())
219 233
220 #undef MATH_FUNCTION 234 #undef UNARY_MATH_FUNCTION
221 235
222 236
223 void lazily_initialize_fast_exp() { 237 void lazily_initialize_fast_exp() {
224 if (fast_exp_function == NULL) { 238 if (fast_exp_function == NULL) {
225 init_fast_exp_function(); 239 init_fast_exp_function();
226 } 240 }
227 } 241 }
228 242
229 243
230 double OS::nan_value() { 244 double OS::nan_value() {
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after
461 475
462 void OS::StrNCpy(Vector<char> dest, const char* src, size_t n) { 476 void OS::StrNCpy(Vector<char> dest, const char* src, size_t n) {
463 strncpy(dest.start(), src, n); 477 strncpy(dest.start(), src, n);
464 } 478 }
465 479
466 480
467 // ---------------------------------------------------------------------------- 481 // ----------------------------------------------------------------------------
468 // POSIX thread support. 482 // POSIX thread support.
469 // 483 //
470 484
485 class Thread::PlatformData : public Malloced {
486 public:
487 PlatformData() : thread_(kNoThread) {}
488 pthread_t thread_; // Thread handle for pthread.
489 };
490
491 Thread::Thread(const Options& options)
492 : data_(new PlatformData),
493 stack_size_(options.stack_size()),
494 start_semaphore_(NULL) {
495 set_name(options.name());
496 }
497
498
499 Thread::~Thread() {
500 delete data_;
501 }
502
503
504 static void SetThreadName(const char* name) {
505 int result = 0;
506 #if defined(__DragonFly__) || defined(__FreeBSD__) || defined(__OpenBSD__)
507 result = pthread_set_name_np(pthread_self(), name);
508 #elif defined(__NetBSD__)
509 STATIC_ASSERT(Thread::kMaxThreadNameLength <= PTHREAD_MAX_NAMELEN_NP);
510 result = pthread_setname_np(pthread_self(), "%s", name);
511 #elif defined(__APPLE__)
512 // pthread_setname_np is only available in 10.6 or later, so test
513 // for it at runtime.
514 int (*dynamic_pthread_setname_np)(const char*);
515 *reinterpret_cast<void**>(&dynamic_pthread_setname_np) =
516 dlsym(RTLD_DEFAULT, "pthread_setname_np");
517 if (dynamic_pthread_setname_np == NULL)
518 return;
519
520 // Mac OS X does not expose the length limit of the name, so hardcode it.
521 static const int kMaxNameLength = 63;
522 STATIC_ASSERT(Thread::kMaxThreadNameLength <= kMaxNameLength);
523 result = dynamic_pthread_setname_np(name);
524 #elif defined(PR_SET_NAME)
525 result = prctl(PR_SET_NAME,
526 reinterpret_cast<unsigned long>(name), // NOLINT
527 0, 0, 0);
528 #endif
529 ASSERT_EQ(0, result);
530 USE(result);
531 }
532
533
534 static void* ThreadEntry(void* arg) {
535 Thread* thread = reinterpret_cast<Thread*>(arg);
536 // This is also initialized by the first argument to pthread_create() but we
537 // don't know which thread will run first (the original thread or the new
538 // one) so we initialize it here too.
539 thread->data()->thread_ = pthread_self();
540 SetThreadName(thread->name());
541 ASSERT(thread->data()->thread_ != kNoThread);
542 thread->NotifyStartedAndRun();
543 return NULL;
544 }
545
546
547 void Thread::set_name(const char* name) {
548 strncpy(name_, name, sizeof(name_));
549 name_[sizeof(name_) - 1] = '\0';
550 }
551
552
553 void Thread::Start() {
554 int result;
555 pthread_attr_t attr;
556 memset(&attr, 0, sizeof(attr));
557 result = pthread_attr_init(&attr);
558 ASSERT_EQ(0, result);
559 // Native client uses default stack size.
560 #if !defined(__native_client__)
561 if (stack_size_ > 0) {
562 result = pthread_attr_setstacksize(&attr, static_cast<size_t>(stack_size_));
563 ASSERT_EQ(0, result);
564 }
565 #endif
566 result = pthread_create(&data_->thread_, &attr, ThreadEntry, this);
567 ASSERT_EQ(0, result);
568 result = pthread_attr_destroy(&attr);
569 ASSERT_EQ(0, result);
570 ASSERT(data_->thread_ != kNoThread);
571 USE(result);
572 }
573
574
575 void Thread::Join() {
576 pthread_join(data_->thread_, NULL);
577 }
578
579
471 void Thread::YieldCPU() { 580 void Thread::YieldCPU() {
472 sched_yield(); 581 int result = sched_yield();
473 } 582 ASSERT_EQ(0, result);
474 583 USE(result);
475 584 }
585
586
587 static Thread::LocalStorageKey PthreadKeyToLocalKey(pthread_key_t pthread_key) {
588 #if defined(__CYGWIN__)
589 // We need to cast pthread_key_t to Thread::LocalStorageKey in two steps
590 // because pthread_key_t is a pointer type on Cygwin. This will probably not
591 // work on 64-bit platforms, but Cygwin doesn't support 64-bit anyway.
592 STATIC_ASSERT(sizeof(Thread::LocalStorageKey) == sizeof(pthread_key_t));
593 intptr_t ptr_key = reinterpret_cast<intptr_t>(pthread_key);
594 return static_cast<Thread::LocalStorageKey>(ptr_key);
595 #else
596 return static_cast<Thread::LocalStorageKey>(pthread_key);
597 #endif
598 }
599
600
601 static pthread_key_t LocalKeyToPthreadKey(Thread::LocalStorageKey local_key) {
602 #if defined(__CYGWIN__)
603 STATIC_ASSERT(sizeof(Thread::LocalStorageKey) == sizeof(pthread_key_t));
604 intptr_t ptr_key = static_cast<intptr_t>(local_key);
605 return reinterpret_cast<pthread_key_t>(ptr_key);
606 #else
607 return static_cast<pthread_key_t>(local_key);
608 #endif
609 }
610
611
612 #ifdef V8_FAST_TLS_SUPPORTED
613
614 static Atomic32 tls_base_offset_initialized = 0;
615 intptr_t kMacTlsBaseOffset = 0;
616
617 // It's safe to do the initialization more that once, but it has to be
618 // done at least once.
619 static void InitializeTlsBaseOffset() {
620 const size_t kBufferSize = 128;
621 char buffer[kBufferSize];
622 size_t buffer_size = kBufferSize;
623 int ctl_name[] = { CTL_KERN , KERN_OSRELEASE };
624 if (sysctl(ctl_name, 2, buffer, &buffer_size, NULL, 0) != 0) {
625 V8_Fatal(__FILE__, __LINE__, "V8 failed to get kernel version");
626 }
627 // The buffer now contains a string of the form XX.YY.ZZ, where
628 // XX is the major kernel version component.
629 // Make sure the buffer is 0-terminated.
630 buffer[kBufferSize - 1] = '\0';
631 char* period_pos = strchr(buffer, '.');
632 *period_pos = '\0';
633 int kernel_version_major =
634 static_cast<int>(strtol(buffer, NULL, 10)); // NOLINT
635 // The constants below are taken from pthreads.s from the XNU kernel
636 // sources archive at www.opensource.apple.com.
637 if (kernel_version_major < 11) {
638 // 8.x.x (Tiger), 9.x.x (Leopard), 10.x.x (Snow Leopard) have the
639 // same offsets.
640 #if V8_HOST_ARCH_IA32
641 kMacTlsBaseOffset = 0x48;
642 #else
643 kMacTlsBaseOffset = 0x60;
644 #endif
645 } else {
646 // 11.x.x (Lion) changed the offset.
647 kMacTlsBaseOffset = 0;
648 }
649
650 Release_Store(&tls_base_offset_initialized, 1);
651 }
652
653
654 static void CheckFastTls(Thread::LocalStorageKey key) {
655 void* expected = reinterpret_cast<void*>(0x1234CAFE);
656 Thread::SetThreadLocal(key, expected);
657 void* actual = Thread::GetExistingThreadLocal(key);
658 if (expected != actual) {
659 V8_Fatal(__FILE__, __LINE__,
660 "V8 failed to initialize fast TLS on current kernel");
661 }
662 Thread::SetThreadLocal(key, NULL);
663 }
664
665 #endif // V8_FAST_TLS_SUPPORTED
666
667
668 Thread::LocalStorageKey Thread::CreateThreadLocalKey() {
669 #ifdef V8_FAST_TLS_SUPPORTED
670 bool check_fast_tls = false;
671 if (tls_base_offset_initialized == 0) {
672 check_fast_tls = true;
673 InitializeTlsBaseOffset();
674 }
675 #endif
676 pthread_key_t key;
677 int result = pthread_key_create(&key, NULL);
678 ASSERT_EQ(0, result);
679 USE(result);
680 LocalStorageKey local_key = PthreadKeyToLocalKey(key);
681 #ifdef V8_FAST_TLS_SUPPORTED
682 // If we just initialized fast TLS support, make sure it works.
683 if (check_fast_tls) CheckFastTls(local_key);
684 #endif
685 return local_key;
686 }
687
688
689 void Thread::DeleteThreadLocalKey(LocalStorageKey key) {
690 pthread_key_t pthread_key = LocalKeyToPthreadKey(key);
691 int result = pthread_key_delete(pthread_key);
692 ASSERT_EQ(0, result);
693 USE(result);
694 }
695
696
697 void* Thread::GetThreadLocal(LocalStorageKey key) {
698 pthread_key_t pthread_key = LocalKeyToPthreadKey(key);
699 return pthread_getspecific(pthread_key);
700 }
701
702
703 void Thread::SetThreadLocal(LocalStorageKey key, void* value) {
704 pthread_key_t pthread_key = LocalKeyToPthreadKey(key);
705 int result = pthread_setspecific(pthread_key, value);
706 ASSERT_EQ(0, result);
707 USE(result);
708 }
709
710
476 class POSIXMutex : public Mutex { 711 class POSIXMutex : public Mutex {
477 public: 712 public:
478 POSIXMutex() { 713 POSIXMutex() {
479 pthread_mutexattr_t attr; 714 pthread_mutexattr_t attr;
480 memset(&attr, 0, sizeof(attr)); 715 memset(&attr, 0, sizeof(attr));
481 int result = pthread_mutexattr_init(&attr); 716 int result = pthread_mutexattr_init(&attr);
482 ASSERT(result == 0); 717 ASSERT(result == 0);
483 result = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); 718 result = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
484 ASSERT(result == 0); 719 ASSERT(result == 0);
485 result = pthread_mutex_init(&mutex_, &attr); 720 result = pthread_mutex_init(&mutex_, &attr);
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after
707 return ntohl(value); 942 return ntohl(value);
708 } 943 }
709 944
710 945
711 Socket* OS::CreateSocket() { 946 Socket* OS::CreateSocket() {
712 return new POSIXSocket(); 947 return new POSIXSocket();
713 } 948 }
714 949
715 950
716 } } // namespace v8::internal 951 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/platform-openbsd.cc ('k') | src/platform-solaris.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698