OLD | NEW |
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 Loading... |
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 |
| 80 |
| 81 uint64_t OS::CpuFeaturesImpliedByPlatform() { |
| 82 #if defined(__APPLE__) |
| 83 // Mac OS X requires all these to install so we can assume they are present. |
| 84 // These constants are defined by the CPUid instructions. |
| 85 const uint64_t one = 1; |
| 86 return (one << SSE2) | (one << CMOV) | (one << RDTSC) | (one << CPUID); |
| 87 #else |
| 88 return 0; // Nothing special about the other systems. |
| 89 #endif |
| 90 } |
| 91 |
66 | 92 |
67 // Maximum size of the virtual memory. 0 means there is no artificial | 93 // Maximum size of the virtual memory. 0 means there is no artificial |
68 // limit. | 94 // limit. |
69 | 95 |
70 intptr_t OS::MaxVirtualMemory() { | 96 intptr_t OS::MaxVirtualMemory() { |
71 struct rlimit limit; | 97 struct rlimit limit; |
72 int result = getrlimit(RLIMIT_DATA, &limit); | 98 int result = getrlimit(RLIMIT_DATA, &limit); |
73 if (result != 0) return 0; | 99 if (result != 0) return 0; |
74 return limit.rlim_cur; | 100 return limit.rlim_cur; |
75 } | 101 } |
76 | 102 |
77 | 103 |
| 104 int OS::ActivationFrameAlignment() { |
| 105 #if V8_TARGET_ARCH_ARM |
| 106 // On EABI ARM targets this is required for fp correctness in the |
| 107 // runtime system. |
| 108 return 8; |
| 109 #elif V8_TARGET_ARCH_MIPS |
| 110 return 8; |
| 111 #else |
| 112 // Otherwise we just assume 16 byte alignment, i.e.: |
| 113 // - With gcc 4.4 the tree vectorization optimizer can generate code |
| 114 // that requires 16 byte alignment such as movdqa on x86. |
| 115 // - Mac OS X and Solaris (64-bit) activation frames must be 16 byte-aligned; |
| 116 // see "Mac OS X ABI Function Call Guide" |
| 117 return 16; |
| 118 #endif |
| 119 } |
| 120 |
| 121 |
78 intptr_t OS::CommitPageSize() { | 122 intptr_t OS::CommitPageSize() { |
79 static intptr_t page_size = getpagesize(); | 123 static intptr_t page_size = getpagesize(); |
80 return page_size; | 124 return page_size; |
81 } | 125 } |
82 | 126 |
83 | 127 |
84 #ifndef __CYGWIN__ | 128 void OS::Free(void* address, const size_t size) { |
| 129 // TODO(1240712): munmap has a return value which is ignored here. |
| 130 int result = munmap(address, size); |
| 131 USE(result); |
| 132 ASSERT(result == 0); |
| 133 } |
| 134 |
| 135 |
85 // Get rid of writable permission on code allocations. | 136 // Get rid of writable permission on code allocations. |
86 void OS::ProtectCode(void* address, const size_t size) { | 137 void OS::ProtectCode(void* address, const size_t size) { |
87 #if defined(__native_client__) | 138 #if defined(__CYGWIN__) |
| 139 DWORD old_protect; |
| 140 VirtualProtect(address, size, PAGE_EXECUTE_READ, &old_protect); |
| 141 #elif defined(__native_client__) |
88 // The Native Client port of V8 uses an interpreter, so | 142 // The Native Client port of V8 uses an interpreter, so |
89 // code pages don't need PROT_EXEC. | 143 // code pages don't need PROT_EXEC. |
90 mprotect(address, size, PROT_READ); | 144 mprotect(address, size, PROT_READ); |
91 #else | 145 #else |
92 mprotect(address, size, PROT_READ | PROT_EXEC); | 146 mprotect(address, size, PROT_READ | PROT_EXEC); |
93 #endif | 147 #endif |
94 } | 148 } |
95 | 149 |
96 | 150 |
97 // Create guard pages. | 151 // Create guard pages. |
98 void OS::Guard(void* address, const size_t size) { | 152 void OS::Guard(void* address, const size_t size) { |
| 153 #if defined(__CYGWIN__) |
| 154 DWORD oldprotect; |
| 155 VirtualProtect(address, size, PAGE_READONLY | PAGE_GUARD, &oldprotect); |
| 156 #else |
99 mprotect(address, size, PROT_NONE); | 157 mprotect(address, size, PROT_NONE); |
| 158 #endif |
100 } | 159 } |
101 #endif // __CYGWIN__ | |
102 | 160 |
103 | 161 |
104 void* OS::GetRandomMmapAddr() { | 162 void* OS::GetRandomMmapAddr() { |
105 #if defined(__native_client__) | 163 #if defined(__native_client__) |
106 // TODO(bradchen): restore randomization once Native Client gets | 164 // TODO(bradchen): restore randomization once Native Client gets |
107 // smarter about using mmap address hints. | 165 // smarter about using mmap address hints. |
108 // See http://code.google.com/p/nativeclient/issues/3341 | 166 // See http://code.google.com/p/nativeclient/issues/3341 |
109 return NULL; | 167 return NULL; |
110 #endif | 168 #endif |
111 Isolate* isolate = Isolate::UncheckedCurrent(); | 169 Isolate* isolate = Isolate::UncheckedCurrent(); |
112 // Note that the current isolate isn't set up in a call path via | 170 // Note that the current isolate isn't set up in a call path via |
113 // CpuFeatures::Probe. We don't care about randomization in this case because | 171 // CpuFeatures::Probe. We don't care about randomization in this case because |
114 // the code page is immediately freed. | 172 // the code page is immediately freed. |
115 if (isolate != NULL) { | 173 if (isolate != NULL) { |
116 #if V8_TARGET_ARCH_X64 | 174 #if V8_TARGET_ARCH_X64 |
117 uint64_t rnd1 = V8::RandomPrivate(isolate); | 175 uint64_t rnd1 = V8::RandomPrivate(isolate); |
118 uint64_t rnd2 = V8::RandomPrivate(isolate); | 176 uint64_t rnd2 = V8::RandomPrivate(isolate); |
119 uint64_t raw_addr = (rnd1 << 32) ^ rnd2; | 177 uint64_t raw_addr = (rnd1 << 32) ^ rnd2; |
120 // Currently available CPUs have 48 bits of virtual addressing. Truncate | 178 // Currently available CPUs have 48 bits of virtual addressing. Truncate |
121 // the hint address to 46 bits to give the kernel a fighting chance of | 179 // the hint address to 46 bits to give the kernel a fighting chance of |
122 // fulfilling our placement request. | 180 // fulfilling our placement request. |
123 raw_addr &= V8_UINT64_C(0x3ffffffff000); | 181 raw_addr &= V8_UINT64_C(0x3ffffffff000); |
124 #else | 182 #else |
125 uint32_t raw_addr = V8::RandomPrivate(isolate); | 183 uint32_t raw_addr = V8::RandomPrivate(isolate); |
| 184 |
| 185 raw_addr &= 0x3ffff000; |
| 186 |
| 187 # ifdef __sun |
| 188 // For our Solaris/illumos mmap hint, we pick a random address in the bottom |
| 189 // half of the top half of the address space (that is, the third quarter). |
| 190 // Because we do not MAP_FIXED, this will be treated only as a hint -- the |
| 191 // system will not fail to mmap() because something else happens to already |
| 192 // be mapped at our random address. We deliberately set the hint high enough |
| 193 // to get well above the system's break (that is, the heap); Solaris and |
| 194 // illumos will try the hint and if that fails allocate as if there were |
| 195 // no hint at all. The high hint prevents the break from getting hemmed in |
| 196 // at low values, ceding half of the address space to the system heap. |
| 197 raw_addr += 0x80000000; |
| 198 # else |
126 // The range 0x20000000 - 0x60000000 is relatively unpopulated across a | 199 // The range 0x20000000 - 0x60000000 is relatively unpopulated across a |
127 // variety of ASLR modes (PAE kernel, NX compat mode, etc) and on macos | 200 // variety of ASLR modes (PAE kernel, NX compat mode, etc) and on macos |
128 // 10.6 and 10.7. | 201 // 10.6 and 10.7. |
129 raw_addr &= 0x3ffff000; | |
130 raw_addr += 0x20000000; | 202 raw_addr += 0x20000000; |
| 203 # endif |
131 #endif | 204 #endif |
132 return reinterpret_cast<void*>(raw_addr); | 205 return reinterpret_cast<void*>(raw_addr); |
133 } | 206 } |
134 return NULL; | 207 return NULL; |
135 } | 208 } |
136 | 209 |
137 | 210 |
| 211 size_t OS::AllocateAlignment() { |
| 212 return getpagesize(); |
| 213 } |
| 214 |
| 215 |
| 216 void OS::Sleep(int milliseconds) { |
| 217 useconds_t ms = static_cast<useconds_t>(milliseconds); |
| 218 usleep(1000 * ms); |
| 219 } |
| 220 |
| 221 |
| 222 int OS::NumberOfCores() { |
| 223 return sysconf(_SC_NPROCESSORS_ONLN); |
| 224 } |
| 225 |
| 226 |
| 227 void OS::Abort() { |
| 228 // Redirect to std abort to signal abnormal program termination. |
| 229 if (FLAG_break_on_abort) { |
| 230 DebugBreak(); |
| 231 } |
| 232 abort(); |
| 233 } |
| 234 |
| 235 |
| 236 void OS::DebugBreak() { |
| 237 #if V8_HOST_ARCH_ARM |
| 238 asm("bkpt 0"); |
| 239 #elif V8_HOST_ARCH_A64 |
| 240 asm("brk 0"); |
| 241 #elif V8_HOST_ARCH_MIPS |
| 242 asm("break"); |
| 243 #elif V8_HOST_ARCH_IA32 |
| 244 #if defined(__native_client__) |
| 245 asm("hlt"); |
| 246 #else |
| 247 asm("int $3"); |
| 248 #endif // __native_client__ |
| 249 #elif V8_HOST_ARCH_X64 |
| 250 asm("int $3"); |
| 251 #else |
| 252 #error Unsupported host architecture. |
| 253 #endif |
| 254 } |
| 255 |
| 256 |
138 // ---------------------------------------------------------------------------- | 257 // ---------------------------------------------------------------------------- |
139 // Math functions | 258 // Math functions |
140 | 259 |
| 260 double ceiling(double x) { |
| 261 // Correct buggy 'ceil' on some systems (i.e. FreeBSD, OS X 10.5) |
| 262 return (-1.0 < x && x < 0.0) ? -0.0 : ceil(x); |
| 263 } |
| 264 |
| 265 |
141 double modulo(double x, double y) { | 266 double modulo(double x, double y) { |
142 return fmod(x, y); | 267 return fmod(x, y); |
143 } | 268 } |
144 | 269 |
145 | 270 |
146 #define UNARY_MATH_FUNCTION(name, generator) \ | 271 #define UNARY_MATH_FUNCTION(name, generator) \ |
147 static UnaryMathFunction fast_##name##_function = NULL; \ | 272 static UnaryMathFunction fast_##name##_function = NULL; \ |
148 void init_fast_##name##_function() { \ | 273 void init_fast_##name##_function() { \ |
149 fast_##name##_function = generator; \ | 274 fast_##name##_function = generator; \ |
150 } \ | 275 } \ |
151 double fast_##name(double x) { \ | 276 double fast_##name(double x) { \ |
152 return (*fast_##name##_function)(x); \ | 277 return (*fast_##name##_function)(x); \ |
153 } | 278 } |
154 | 279 |
155 UNARY_MATH_FUNCTION(sin, CreateTranscendentalFunction(TranscendentalCache::SIN)) | 280 UNARY_MATH_FUNCTION(sin, CreateTranscendentalFunction(TranscendentalCache::SIN)) |
156 UNARY_MATH_FUNCTION(cos, CreateTranscendentalFunction(TranscendentalCache::COS)) | 281 UNARY_MATH_FUNCTION(cos, CreateTranscendentalFunction(TranscendentalCache::COS)) |
157 UNARY_MATH_FUNCTION(tan, CreateTranscendentalFunction(TranscendentalCache::TAN)) | 282 UNARY_MATH_FUNCTION(tan, CreateTranscendentalFunction(TranscendentalCache::TAN)) |
158 UNARY_MATH_FUNCTION(log, CreateTranscendentalFunction(TranscendentalCache::LOG)) | 283 UNARY_MATH_FUNCTION(log, CreateTranscendentalFunction(TranscendentalCache::LOG)) |
159 UNARY_MATH_FUNCTION(exp, CreateExpFunction()) | 284 UNARY_MATH_FUNCTION(exp, CreateExpFunction()) |
160 UNARY_MATH_FUNCTION(sqrt, CreateSqrtFunction()) | 285 UNARY_MATH_FUNCTION(sqrt, CreateSqrtFunction()) |
161 | 286 |
162 #undef MATH_FUNCTION | 287 #undef UNARY_MATH_FUNCTION |
163 | 288 |
164 | 289 |
165 void lazily_initialize_fast_exp() { | 290 void lazily_initialize_fast_exp() { |
166 if (fast_exp_function == NULL) { | 291 if (fast_exp_function == NULL) { |
167 init_fast_exp_function(); | 292 init_fast_exp_function(); |
168 } | 293 } |
169 } | 294 } |
170 | 295 |
171 | 296 |
172 double OS::nan_value() { | 297 double OS::nan_value() { |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
364 OS::MemCopyUint16Uint8Function OS::memcopy_uint16_uint8_function = | 489 OS::MemCopyUint16Uint8Function OS::memcopy_uint16_uint8_function = |
365 &OS::MemCopyUint16Uint8Wrapper; | 490 &OS::MemCopyUint16Uint8Wrapper; |
366 // Defined in codegen-arm.cc. | 491 // Defined in codegen-arm.cc. |
367 OS::MemCopyUint8Function CreateMemCopyUint8Function( | 492 OS::MemCopyUint8Function CreateMemCopyUint8Function( |
368 OS::MemCopyUint8Function stub); | 493 OS::MemCopyUint8Function stub); |
369 OS::MemCopyUint16Uint8Function CreateMemCopyUint16Uint8Function( | 494 OS::MemCopyUint16Uint8Function CreateMemCopyUint16Uint8Function( |
370 OS::MemCopyUint16Uint8Function stub); | 495 OS::MemCopyUint16Uint8Function stub); |
371 #endif | 496 #endif |
372 | 497 |
373 | 498 |
374 void POSIXPostSetUp() { | 499 void OS::PostSetUp() { |
375 #if V8_TARGET_ARCH_IA32 | 500 #if V8_TARGET_ARCH_IA32 |
376 OS::MemMoveFunction generated_memmove = CreateMemMoveFunction(); | 501 OS::MemMoveFunction generated_memmove = CreateMemMoveFunction(); |
377 if (generated_memmove != NULL) { | 502 if (generated_memmove != NULL) { |
378 memmove_function = generated_memmove; | 503 memmove_function = generated_memmove; |
379 } | 504 } |
380 #elif defined(V8_HOST_ARCH_ARM) | 505 #elif defined(V8_HOST_ARCH_ARM) |
381 OS::memcopy_uint8_function = | 506 OS::memcopy_uint8_function = |
382 CreateMemCopyUint8Function(&OS::MemCopyUint8Wrapper); | 507 CreateMemCopyUint8Function(&OS::MemCopyUint8Wrapper); |
383 OS::memcopy_uint16_uint8_function = | 508 OS::memcopy_uint16_uint8_function = |
384 CreateMemCopyUint16Uint8Function(&OS::MemCopyUint16Uint8Wrapper); | 509 CreateMemCopyUint16Uint8Function(&OS::MemCopyUint16Uint8Wrapper); |
(...skipping 18 matching lines...) Expand all Loading... |
403 | 528 |
404 void OS::StrNCpy(Vector<char> dest, const char* src, size_t n) { | 529 void OS::StrNCpy(Vector<char> dest, const char* src, size_t n) { |
405 strncpy(dest.start(), src, n); | 530 strncpy(dest.start(), src, n); |
406 } | 531 } |
407 | 532 |
408 | 533 |
409 // ---------------------------------------------------------------------------- | 534 // ---------------------------------------------------------------------------- |
410 // POSIX thread support. | 535 // POSIX thread support. |
411 // | 536 // |
412 | 537 |
| 538 class Thread::PlatformData : public Malloced { |
| 539 public: |
| 540 PlatformData() : thread_(kNoThread) {} |
| 541 pthread_t thread_; // Thread handle for pthread. |
| 542 }; |
| 543 |
| 544 Thread::Thread(const Options& options) |
| 545 : data_(new PlatformData), |
| 546 stack_size_(options.stack_size()), |
| 547 start_semaphore_(NULL) { |
| 548 set_name(options.name()); |
| 549 } |
| 550 |
| 551 |
| 552 Thread::~Thread() { |
| 553 delete data_; |
| 554 } |
| 555 |
| 556 |
| 557 static void SetThreadName(const char* name) { |
| 558 #if defined(__DragonFly__) || defined(__FreeBSD__) || defined(__OpenBSD__) |
| 559 pthread_set_name_np(pthread_self(), name); |
| 560 #elif defined(__NetBSD__) |
| 561 STATIC_ASSERT(Thread::kMaxThreadNameLength <= PTHREAD_MAX_NAMELEN_NP); |
| 562 pthread_setname_np(pthread_self(), "%s", name); |
| 563 #elif defined(__APPLE__) |
| 564 // pthread_setname_np is only available in 10.6 or later, so test |
| 565 // for it at runtime. |
| 566 int (*dynamic_pthread_setname_np)(const char*); |
| 567 *reinterpret_cast<void**>(&dynamic_pthread_setname_np) = |
| 568 dlsym(RTLD_DEFAULT, "pthread_setname_np"); |
| 569 if (dynamic_pthread_setname_np == NULL) |
| 570 return; |
| 571 |
| 572 // Mac OS X does not expose the length limit of the name, so hardcode it. |
| 573 static const int kMaxNameLength = 63; |
| 574 STATIC_ASSERT(Thread::kMaxThreadNameLength <= kMaxNameLength); |
| 575 dynamic_pthread_setname_np(name); |
| 576 #elif defined(PR_SET_NAME) |
| 577 prctl(PR_SET_NAME, |
| 578 reinterpret_cast<unsigned long>(name), // NOLINT |
| 579 0, 0, 0); |
| 580 #endif |
| 581 } |
| 582 |
| 583 |
| 584 static void* ThreadEntry(void* arg) { |
| 585 Thread* thread = reinterpret_cast<Thread*>(arg); |
| 586 // This is also initialized by the first argument to pthread_create() but we |
| 587 // don't know which thread will run first (the original thread or the new |
| 588 // one) so we initialize it here too. |
| 589 thread->data()->thread_ = pthread_self(); |
| 590 SetThreadName(thread->name()); |
| 591 ASSERT(thread->data()->thread_ != kNoThread); |
| 592 thread->NotifyStartedAndRun(); |
| 593 return NULL; |
| 594 } |
| 595 |
| 596 |
| 597 void Thread::set_name(const char* name) { |
| 598 strncpy(name_, name, sizeof(name_)); |
| 599 name_[sizeof(name_) - 1] = '\0'; |
| 600 } |
| 601 |
| 602 |
| 603 void Thread::Start() { |
| 604 int result; |
| 605 pthread_attr_t attr; |
| 606 memset(&attr, 0, sizeof(attr)); |
| 607 result = pthread_attr_init(&attr); |
| 608 ASSERT_EQ(0, result); |
| 609 // Native client uses default stack size. |
| 610 #if !defined(__native_client__) |
| 611 if (stack_size_ > 0) { |
| 612 result = pthread_attr_setstacksize(&attr, static_cast<size_t>(stack_size_)); |
| 613 ASSERT_EQ(0, result); |
| 614 } |
| 615 #endif |
| 616 result = pthread_create(&data_->thread_, &attr, ThreadEntry, this); |
| 617 ASSERT_EQ(0, result); |
| 618 result = pthread_attr_destroy(&attr); |
| 619 ASSERT_EQ(0, result); |
| 620 ASSERT(data_->thread_ != kNoThread); |
| 621 USE(result); |
| 622 } |
| 623 |
| 624 |
| 625 void Thread::Join() { |
| 626 pthread_join(data_->thread_, NULL); |
| 627 } |
| 628 |
| 629 |
413 void Thread::YieldCPU() { | 630 void Thread::YieldCPU() { |
414 sched_yield(); | 631 int result = sched_yield(); |
415 } | 632 ASSERT_EQ(0, result); |
416 | 633 USE(result); |
417 | 634 } |
| 635 |
| 636 |
| 637 static Thread::LocalStorageKey PthreadKeyToLocalKey(pthread_key_t pthread_key) { |
| 638 #if defined(__CYGWIN__) |
| 639 // We need to cast pthread_key_t to Thread::LocalStorageKey in two steps |
| 640 // because pthread_key_t is a pointer type on Cygwin. This will probably not |
| 641 // work on 64-bit platforms, but Cygwin doesn't support 64-bit anyway. |
| 642 STATIC_ASSERT(sizeof(Thread::LocalStorageKey) == sizeof(pthread_key_t)); |
| 643 intptr_t ptr_key = reinterpret_cast<intptr_t>(pthread_key); |
| 644 return static_cast<Thread::LocalStorageKey>(ptr_key); |
| 645 #else |
| 646 return static_cast<Thread::LocalStorageKey>(pthread_key); |
| 647 #endif |
| 648 } |
| 649 |
| 650 |
| 651 static pthread_key_t LocalKeyToPthreadKey(Thread::LocalStorageKey local_key) { |
| 652 #if defined(__CYGWIN__) |
| 653 STATIC_ASSERT(sizeof(Thread::LocalStorageKey) == sizeof(pthread_key_t)); |
| 654 intptr_t ptr_key = static_cast<intptr_t>(local_key); |
| 655 return reinterpret_cast<pthread_key_t>(ptr_key); |
| 656 #else |
| 657 return static_cast<pthread_key_t>(local_key); |
| 658 #endif |
| 659 } |
| 660 |
| 661 |
| 662 #ifdef V8_FAST_TLS_SUPPORTED |
| 663 |
| 664 static Atomic32 tls_base_offset_initialized = 0; |
| 665 intptr_t kMacTlsBaseOffset = 0; |
| 666 |
| 667 // It's safe to do the initialization more that once, but it has to be |
| 668 // done at least once. |
| 669 static void InitializeTlsBaseOffset() { |
| 670 const size_t kBufferSize = 128; |
| 671 char buffer[kBufferSize]; |
| 672 size_t buffer_size = kBufferSize; |
| 673 int ctl_name[] = { CTL_KERN , KERN_OSRELEASE }; |
| 674 if (sysctl(ctl_name, 2, buffer, &buffer_size, NULL, 0) != 0) { |
| 675 V8_Fatal(__FILE__, __LINE__, "V8 failed to get kernel version"); |
| 676 } |
| 677 // The buffer now contains a string of the form XX.YY.ZZ, where |
| 678 // XX is the major kernel version component. |
| 679 // Make sure the buffer is 0-terminated. |
| 680 buffer[kBufferSize - 1] = '\0'; |
| 681 char* period_pos = strchr(buffer, '.'); |
| 682 *period_pos = '\0'; |
| 683 int kernel_version_major = |
| 684 static_cast<int>(strtol(buffer, NULL, 10)); // NOLINT |
| 685 // The constants below are taken from pthreads.s from the XNU kernel |
| 686 // sources archive at www.opensource.apple.com. |
| 687 if (kernel_version_major < 11) { |
| 688 // 8.x.x (Tiger), 9.x.x (Leopard), 10.x.x (Snow Leopard) have the |
| 689 // same offsets. |
| 690 #if V8_HOST_ARCH_IA32 |
| 691 kMacTlsBaseOffset = 0x48; |
| 692 #else |
| 693 kMacTlsBaseOffset = 0x60; |
| 694 #endif |
| 695 } else { |
| 696 // 11.x.x (Lion) changed the offset. |
| 697 kMacTlsBaseOffset = 0; |
| 698 } |
| 699 |
| 700 Release_Store(&tls_base_offset_initialized, 1); |
| 701 } |
| 702 |
| 703 |
| 704 static void CheckFastTls(Thread::LocalStorageKey key) { |
| 705 void* expected = reinterpret_cast<void*>(0x1234CAFE); |
| 706 Thread::SetThreadLocal(key, expected); |
| 707 void* actual = Thread::GetExistingThreadLocal(key); |
| 708 if (expected != actual) { |
| 709 V8_Fatal(__FILE__, __LINE__, |
| 710 "V8 failed to initialize fast TLS on current kernel"); |
| 711 } |
| 712 Thread::SetThreadLocal(key, NULL); |
| 713 } |
| 714 |
| 715 #endif // V8_FAST_TLS_SUPPORTED |
| 716 |
| 717 |
| 718 Thread::LocalStorageKey Thread::CreateThreadLocalKey() { |
| 719 #ifdef V8_FAST_TLS_SUPPORTED |
| 720 bool check_fast_tls = false; |
| 721 if (tls_base_offset_initialized == 0) { |
| 722 check_fast_tls = true; |
| 723 InitializeTlsBaseOffset(); |
| 724 } |
| 725 #endif |
| 726 pthread_key_t key; |
| 727 int result = pthread_key_create(&key, NULL); |
| 728 ASSERT_EQ(0, result); |
| 729 USE(result); |
| 730 LocalStorageKey local_key = PthreadKeyToLocalKey(key); |
| 731 #ifdef V8_FAST_TLS_SUPPORTED |
| 732 // If we just initialized fast TLS support, make sure it works. |
| 733 if (check_fast_tls) CheckFastTls(local_key); |
| 734 #endif |
| 735 return local_key; |
| 736 } |
| 737 |
| 738 |
| 739 void Thread::DeleteThreadLocalKey(LocalStorageKey key) { |
| 740 pthread_key_t pthread_key = LocalKeyToPthreadKey(key); |
| 741 int result = pthread_key_delete(pthread_key); |
| 742 ASSERT_EQ(0, result); |
| 743 USE(result); |
| 744 } |
| 745 |
| 746 |
| 747 void* Thread::GetThreadLocal(LocalStorageKey key) { |
| 748 pthread_key_t pthread_key = LocalKeyToPthreadKey(key); |
| 749 return pthread_getspecific(pthread_key); |
| 750 } |
| 751 |
| 752 |
| 753 void Thread::SetThreadLocal(LocalStorageKey key, void* value) { |
| 754 pthread_key_t pthread_key = LocalKeyToPthreadKey(key); |
| 755 int result = pthread_setspecific(pthread_key, value); |
| 756 ASSERT_EQ(0, result); |
| 757 USE(result); |
| 758 } |
| 759 |
| 760 |
418 class POSIXMutex : public Mutex { | 761 class POSIXMutex : public Mutex { |
419 public: | 762 public: |
420 POSIXMutex() { | 763 POSIXMutex() { |
421 pthread_mutexattr_t attr; | 764 pthread_mutexattr_t attr; |
422 memset(&attr, 0, sizeof(attr)); | 765 memset(&attr, 0, sizeof(attr)); |
423 int result = pthread_mutexattr_init(&attr); | 766 int result = pthread_mutexattr_init(&attr); |
424 ASSERT(result == 0); | 767 ASSERT(result == 0); |
425 result = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); | 768 result = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); |
426 ASSERT(result == 0); | 769 ASSERT(result == 0); |
427 result = pthread_mutex_init(&mutex_, &attr); | 770 result = pthread_mutex_init(&mutex_, &attr); |
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
649 return ntohl(value); | 992 return ntohl(value); |
650 } | 993 } |
651 | 994 |
652 | 995 |
653 Socket* OS::CreateSocket() { | 996 Socket* OS::CreateSocket() { |
654 return new POSIXSocket(); | 997 return new POSIXSocket(); |
655 } | 998 } |
656 | 999 |
657 | 1000 |
658 } } // namespace v8::internal | 1001 } } // namespace v8::internal |
OLD | NEW |