jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 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. |
| 27 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 28 | // Platform specific code for MacOS goes here. For the POSIX comaptible parts |
| 29 | // the implementation is in platform-posix.cc. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 30 | |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 31 | #include <dlfcn.h> |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 32 | #include <unistd.h> |
| 33 | #include <sys/mman.h> |
| 34 | #include <mach/mach_init.h> |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 35 | #include <mach-o/dyld.h> |
| 36 | #include <mach-o/getsect.h> |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 37 | |
| 38 | #include <AvailabilityMacros.h> |
| 39 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 40 | #include <pthread.h> |
| 41 | #include <semaphore.h> |
| 42 | #include <signal.h> |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 43 | #include <libkern/OSAtomic.h> |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 44 | #include <mach/mach.h> |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 45 | #include <mach/semaphore.h> |
| 46 | #include <mach/task.h> |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 47 | #include <mach/vm_statistics.h> |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 48 | #include <sys/time.h> |
| 49 | #include <sys/resource.h> |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 50 | #include <sys/types.h> |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 51 | #include <sys/sysctl.h> |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 52 | #include <stdarg.h> |
| 53 | #include <stdlib.h> |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 54 | #include <string.h> |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 55 | #include <errno.h> |
| 56 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 57 | #undef MAP_TYPE |
| 58 | |
| 59 | #include "v8.h" |
| 60 | |
danno@chromium.org | 8c0a43f | 2012-04-03 08:37:53 +0000 | [diff] [blame] | 61 | #include "platform-posix.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 62 | #include "platform.h" |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 63 | #include "simulator.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 64 | #include "vm-state-inl.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 65 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 66 | // Manually define these here as weak imports, rather than including execinfo.h. |
| 67 | // This lets us launch on 10.4 which does not have these calls. |
| 68 | extern "C" { |
| 69 | extern int backtrace(void**, int) __attribute__((weak_import)); |
| 70 | extern char** backtrace_symbols(void* const*, int) |
| 71 | __attribute__((weak_import)); |
| 72 | extern void backtrace_symbols_fd(void* const*, int, int) |
| 73 | __attribute__((weak_import)); |
| 74 | } |
| 75 | |
| 76 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 77 | namespace v8 { |
| 78 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 79 | |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 80 | // 0 is never a valid thread id on MacOSX since a pthread_t is |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 81 | // a pointer. |
| 82 | static const pthread_t kNoThread = (pthread_t) 0; |
| 83 | |
| 84 | |
| 85 | double ceiling(double x) { |
| 86 | // Correct Mac OS X Leopard 'ceil' behavior. |
| 87 | if (-1.0 < x && x < 0.0) { |
| 88 | return -0.0; |
| 89 | } else { |
| 90 | return ceil(x); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 95 | static Mutex* limit_mutex = NULL; |
| 96 | |
| 97 | |
danno@chromium.org | 8c0a43f | 2012-04-03 08:37:53 +0000 | [diff] [blame] | 98 | void OS::PostSetUp() { |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 99 | POSIXPostSetUp(); |
danno@chromium.org | 8c0a43f | 2012-04-03 08:37:53 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 103 | // We keep the lowest and highest addresses mapped as a quick way of |
| 104 | // determining that pointers are outside the heap (used mostly in assertions |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 105 | // and verification). The estimate is conservative, i.e., not all addresses in |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 106 | // 'allocated' space are actually allocated to our heap. The range is |
| 107 | // [lowest, highest), inclusive on the low and and exclusive on the high end. |
| 108 | static void* lowest_ever_allocated = reinterpret_cast<void*>(-1); |
| 109 | static void* highest_ever_allocated = reinterpret_cast<void*>(0); |
| 110 | |
| 111 | |
| 112 | static void UpdateAllocatedSpaceLimits(void* address, int size) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 113 | ASSERT(limit_mutex != NULL); |
| 114 | ScopedLock lock(limit_mutex); |
| 115 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 116 | lowest_ever_allocated = Min(lowest_ever_allocated, address); |
| 117 | highest_ever_allocated = |
| 118 | Max(highest_ever_allocated, |
| 119 | reinterpret_cast<void*>(reinterpret_cast<char*>(address) + size)); |
| 120 | } |
| 121 | |
| 122 | |
| 123 | bool OS::IsOutsideAllocatedSpace(void* address) { |
| 124 | return address < lowest_ever_allocated || address >= highest_ever_allocated; |
| 125 | } |
| 126 | |
| 127 | |
| 128 | size_t OS::AllocateAlignment() { |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 129 | return getpagesize(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 133 | // Constants used for mmap. |
| 134 | // kMmapFd is used to pass vm_alloc flags to tag the region with the user |
| 135 | // defined tag 255 This helps identify V8-allocated regions in memory analysis |
| 136 | // tools like vmmap(1). |
| 137 | static const int kMmapFd = VM_MAKE_TAG(255); |
| 138 | static const off_t kMmapFdOffset = 0; |
| 139 | |
| 140 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 141 | void* OS::Allocate(const size_t requested, |
| 142 | size_t* allocated, |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 143 | bool is_executable) { |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 144 | const size_t msize = RoundUp(requested, getpagesize()); |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 145 | int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0); |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 146 | void* mbase = mmap(OS::GetRandomMmapAddr(), |
| 147 | msize, |
| 148 | prot, |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 149 | MAP_PRIVATE | MAP_ANON, |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 150 | kMmapFd, |
| 151 | kMmapFdOffset); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 152 | if (mbase == MAP_FAILED) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 153 | LOG(Isolate::Current(), StringEvent("OS::Allocate", "mmap failed")); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 154 | return NULL; |
| 155 | } |
| 156 | *allocated = msize; |
| 157 | UpdateAllocatedSpaceLimits(mbase, msize); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 158 | return mbase; |
| 159 | } |
| 160 | |
| 161 | |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 162 | void OS::Free(void* address, const size_t size) { |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 163 | // TODO(1240712): munmap has a return value which is ignored here. |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 164 | int result = munmap(address, size); |
| 165 | USE(result); |
| 166 | ASSERT(result == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 170 | void OS::Sleep(int milliseconds) { |
| 171 | usleep(1000 * milliseconds); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 175 | int OS::NumberOfCores() { |
| 176 | return sysconf(_SC_NPROCESSORS_ONLN); |
| 177 | } |
| 178 | |
| 179 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 180 | void OS::Abort() { |
| 181 | // Redirect to std abort to signal abnormal program termination |
| 182 | abort(); |
| 183 | } |
| 184 | |
| 185 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 186 | void OS::DebugBreak() { |
| 187 | asm("int $3"); |
| 188 | } |
| 189 | |
| 190 | |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 191 | void OS::DumpBacktrace() { |
| 192 | // Currently unsupported. |
| 193 | } |
| 194 | |
| 195 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 196 | class PosixMemoryMappedFile : public OS::MemoryMappedFile { |
| 197 | public: |
| 198 | PosixMemoryMappedFile(FILE* file, void* memory, int size) |
| 199 | : file_(file), memory_(memory), size_(size) { } |
| 200 | virtual ~PosixMemoryMappedFile(); |
| 201 | virtual void* memory() { return memory_; } |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 202 | virtual int size() { return size_; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 203 | private: |
| 204 | FILE* file_; |
| 205 | void* memory_; |
| 206 | int size_; |
| 207 | }; |
| 208 | |
| 209 | |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 210 | OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) { |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 211 | FILE* file = fopen(name, "r+"); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 212 | if (file == NULL) return NULL; |
| 213 | |
| 214 | fseek(file, 0, SEEK_END); |
| 215 | int size = ftell(file); |
| 216 | |
| 217 | void* memory = |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 218 | mmap(OS::GetRandomMmapAddr(), |
| 219 | size, |
| 220 | PROT_READ | PROT_WRITE, |
| 221 | MAP_SHARED, |
| 222 | fileno(file), |
| 223 | 0); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 224 | return new PosixMemoryMappedFile(file, memory, size); |
| 225 | } |
| 226 | |
| 227 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 228 | OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size, |
| 229 | void* initial) { |
| 230 | FILE* file = fopen(name, "w+"); |
| 231 | if (file == NULL) return NULL; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 232 | int result = fwrite(initial, size, 1, file); |
| 233 | if (result < 1) { |
| 234 | fclose(file); |
| 235 | return NULL; |
| 236 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 237 | void* memory = |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 238 | mmap(OS::GetRandomMmapAddr(), |
| 239 | size, |
| 240 | PROT_READ | PROT_WRITE, |
| 241 | MAP_SHARED, |
| 242 | fileno(file), |
| 243 | 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 244 | return new PosixMemoryMappedFile(file, memory, size); |
| 245 | } |
| 246 | |
| 247 | |
| 248 | PosixMemoryMappedFile::~PosixMemoryMappedFile() { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 249 | if (memory_) OS::Free(memory_, size_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 250 | fclose(file_); |
| 251 | } |
| 252 | |
| 253 | |
| 254 | void OS::LogSharedLibraryAddresses() { |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 255 | unsigned int images_count = _dyld_image_count(); |
| 256 | for (unsigned int i = 0; i < images_count; ++i) { |
| 257 | const mach_header* header = _dyld_get_image_header(i); |
| 258 | if (header == NULL) continue; |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 259 | #if V8_HOST_ARCH_X64 |
| 260 | uint64_t size; |
| 261 | char* code_ptr = getsectdatafromheader_64( |
| 262 | reinterpret_cast<const mach_header_64*>(header), |
| 263 | SEG_TEXT, |
| 264 | SECT_TEXT, |
| 265 | &size); |
| 266 | #else |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 267 | unsigned int size; |
| 268 | char* code_ptr = getsectdatafromheader(header, SEG_TEXT, SECT_TEXT, &size); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 269 | #endif |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 270 | if (code_ptr == NULL) continue; |
| 271 | const uintptr_t slide = _dyld_get_image_vmaddr_slide(i); |
| 272 | const uintptr_t start = reinterpret_cast<uintptr_t>(code_ptr) + slide; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 273 | LOG(Isolate::Current(), |
| 274 | SharedLibraryEvent(_dyld_get_image_name(i), start, start + size)); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 275 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 279 | void OS::SignalCodeMovingGC() { |
| 280 | } |
| 281 | |
| 282 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 283 | uint64_t OS::CpuFeaturesImpliedByPlatform() { |
| 284 | // MacOSX requires all these to install so we can assume they are present. |
| 285 | // These constants are defined by the CPUid instructions. |
| 286 | const uint64_t one = 1; |
| 287 | return (one << SSE2) | (one << CMOV) | (one << RDTSC) | (one << CPUID); |
| 288 | } |
| 289 | |
| 290 | |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 291 | int OS::ActivationFrameAlignment() { |
| 292 | // OS X activation frames must be 16 byte-aligned; see "Mac OS X ABI |
| 293 | // Function Call Guide". |
| 294 | return 16; |
| 295 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 296 | |
| 297 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 298 | const char* OS::LocalTimezone(double time) { |
ulan@chromium.org | 77ca49a | 2013-04-22 09:43:56 +0000 | [diff] [blame] | 299 | if (std::isnan(time)) return ""; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 300 | time_t tv = static_cast<time_t>(floor(time/msPerSecond)); |
| 301 | struct tm* t = localtime(&tv); |
| 302 | if (NULL == t) return ""; |
| 303 | return t->tm_zone; |
| 304 | } |
| 305 | |
| 306 | |
| 307 | double OS::LocalTimeOffset() { |
| 308 | time_t tv = time(NULL); |
| 309 | struct tm* t = localtime(&tv); |
| 310 | // tm_gmtoff includes any daylight savings offset, so subtract it. |
| 311 | return static_cast<double>(t->tm_gmtoff * msPerSecond - |
| 312 | (t->tm_isdst > 0 ? 3600 * msPerSecond : 0)); |
| 313 | } |
| 314 | |
| 315 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 316 | int OS::StackWalk(Vector<StackFrame> frames) { |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 317 | // If weak link to execinfo lib has failed, ie because we are on 10.4, abort. |
| 318 | if (backtrace == NULL) |
| 319 | return 0; |
| 320 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 321 | int frames_size = frames.length(); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 322 | ScopedVector<void*> addresses(frames_size); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 323 | |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 324 | int frames_count = backtrace(addresses.start(), frames_size); |
| 325 | |
| 326 | char** symbols = backtrace_symbols(addresses.start(), frames_count); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 327 | if (symbols == NULL) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 328 | return kStackWalkError; |
| 329 | } |
| 330 | |
| 331 | for (int i = 0; i < frames_count; i++) { |
| 332 | frames[i].address = addresses[i]; |
| 333 | // Format a text representation of the frame based on the information |
| 334 | // available. |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 335 | SNPrintF(MutableCStrVector(frames[i].text, |
| 336 | kStackWalkMaxTextLen), |
| 337 | "%s", |
| 338 | symbols[i]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 339 | // Make sure line termination is in place. |
| 340 | frames[i].text[kStackWalkMaxTextLen - 1] = '\0'; |
| 341 | } |
| 342 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 343 | free(symbols); |
| 344 | |
| 345 | return frames_count; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 349 | VirtualMemory::VirtualMemory() : address_(NULL), size_(0) { } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 350 | |
| 351 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 352 | VirtualMemory::VirtualMemory(size_t size) |
| 353 | : address_(ReserveRegion(size)), size_(size) { } |
| 354 | |
| 355 | |
| 356 | VirtualMemory::VirtualMemory(size_t size, size_t alignment) |
| 357 | : address_(NULL), size_(0) { |
| 358 | ASSERT(IsAligned(alignment, static_cast<intptr_t>(OS::AllocateAlignment()))); |
| 359 | size_t request_size = RoundUp(size + alignment, |
| 360 | static_cast<intptr_t>(OS::AllocateAlignment())); |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 361 | void* reservation = mmap(OS::GetRandomMmapAddr(), |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 362 | request_size, |
| 363 | PROT_NONE, |
| 364 | MAP_PRIVATE | MAP_ANON | MAP_NORESERVE, |
| 365 | kMmapFd, |
| 366 | kMmapFdOffset); |
| 367 | if (reservation == MAP_FAILED) return; |
| 368 | |
| 369 | Address base = static_cast<Address>(reservation); |
| 370 | Address aligned_base = RoundUp(base, alignment); |
| 371 | ASSERT_LE(base, aligned_base); |
| 372 | |
| 373 | // Unmap extra memory reserved before and after the desired block. |
| 374 | if (aligned_base != base) { |
| 375 | size_t prefix_size = static_cast<size_t>(aligned_base - base); |
| 376 | OS::Free(base, prefix_size); |
| 377 | request_size -= prefix_size; |
| 378 | } |
| 379 | |
| 380 | size_t aligned_size = RoundUp(size, OS::AllocateAlignment()); |
| 381 | ASSERT_LE(aligned_size, request_size); |
| 382 | |
| 383 | if (aligned_size != request_size) { |
| 384 | size_t suffix_size = request_size - aligned_size; |
| 385 | OS::Free(aligned_base + aligned_size, suffix_size); |
| 386 | request_size -= suffix_size; |
| 387 | } |
| 388 | |
| 389 | ASSERT(aligned_size == request_size); |
| 390 | |
| 391 | address_ = static_cast<void*>(aligned_base); |
| 392 | size_ = aligned_size; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | |
| 396 | VirtualMemory::~VirtualMemory() { |
| 397 | if (IsReserved()) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 398 | bool result = ReleaseRegion(address(), size()); |
| 399 | ASSERT(result); |
| 400 | USE(result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | |
| 404 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 405 | bool VirtualMemory::IsReserved() { |
| 406 | return address_ != NULL; |
| 407 | } |
| 408 | |
| 409 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 410 | void VirtualMemory::Reset() { |
| 411 | address_ = NULL; |
| 412 | size_ = 0; |
| 413 | } |
| 414 | |
| 415 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 416 | bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) { |
| 417 | return CommitRegion(address, size, is_executable); |
| 418 | } |
| 419 | |
| 420 | |
| 421 | bool VirtualMemory::Uncommit(void* address, size_t size) { |
| 422 | return UncommitRegion(address, size); |
| 423 | } |
| 424 | |
| 425 | |
| 426 | bool VirtualMemory::Guard(void* address) { |
| 427 | OS::Guard(address, OS::CommitPageSize()); |
| 428 | return true; |
| 429 | } |
| 430 | |
| 431 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 432 | void* VirtualMemory::ReserveRegion(size_t size) { |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 433 | void* result = mmap(OS::GetRandomMmapAddr(), |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 434 | size, |
| 435 | PROT_NONE, |
| 436 | MAP_PRIVATE | MAP_ANON | MAP_NORESERVE, |
| 437 | kMmapFd, |
| 438 | kMmapFdOffset); |
| 439 | |
| 440 | if (result == MAP_FAILED) return NULL; |
| 441 | |
| 442 | return result; |
| 443 | } |
| 444 | |
| 445 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 446 | bool VirtualMemory::CommitRegion(void* address, |
| 447 | size_t size, |
| 448 | bool is_executable) { |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 449 | int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 450 | if (MAP_FAILED == mmap(address, |
| 451 | size, |
| 452 | prot, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 453 | MAP_PRIVATE | MAP_ANON | MAP_FIXED, |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 454 | kMmapFd, |
| 455 | kMmapFdOffset)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 456 | return false; |
| 457 | } |
| 458 | |
| 459 | UpdateAllocatedSpaceLimits(address, size); |
| 460 | return true; |
| 461 | } |
| 462 | |
| 463 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 464 | bool VirtualMemory::UncommitRegion(void* address, size_t size) { |
| 465 | return mmap(address, |
| 466 | size, |
| 467 | PROT_NONE, |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 468 | MAP_PRIVATE | MAP_ANON | MAP_NORESERVE | MAP_FIXED, |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 469 | kMmapFd, |
| 470 | kMmapFdOffset) != MAP_FAILED; |
| 471 | } |
| 472 | |
| 473 | |
| 474 | bool VirtualMemory::ReleaseRegion(void* address, size_t size) { |
| 475 | return munmap(address, size) == 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 476 | } |
| 477 | |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 478 | |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 479 | bool VirtualMemory::HasLazyCommits() { |
| 480 | return false; |
| 481 | } |
| 482 | |
| 483 | |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 484 | class Thread::PlatformData : public Malloced { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 485 | public: |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 486 | PlatformData() : thread_(kNoThread) {} |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 487 | pthread_t thread_; // Thread handle for pthread. |
| 488 | }; |
| 489 | |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 490 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 491 | Thread::Thread(const Options& options) |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 492 | : data_(new PlatformData), |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 493 | stack_size_(options.stack_size()), |
| 494 | start_semaphore_(NULL) { |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 495 | set_name(options.name()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | |
| 499 | Thread::~Thread() { |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 500 | delete data_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 504 | static void SetThreadName(const char* name) { |
| 505 | // pthread_setname_np is only available in 10.6 or later, so test |
| 506 | // for it at runtime. |
| 507 | int (*dynamic_pthread_setname_np)(const char*); |
| 508 | *reinterpret_cast<void**>(&dynamic_pthread_setname_np) = |
| 509 | dlsym(RTLD_DEFAULT, "pthread_setname_np"); |
| 510 | if (!dynamic_pthread_setname_np) |
| 511 | return; |
| 512 | |
| 513 | // Mac OS X does not expose the length limit of the name, so hardcode it. |
| 514 | static const int kMaxNameLength = 63; |
| 515 | USE(kMaxNameLength); |
| 516 | ASSERT(Thread::kMaxThreadNameLength <= kMaxNameLength); |
| 517 | dynamic_pthread_setname_np(name); |
| 518 | } |
| 519 | |
| 520 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 521 | static void* ThreadEntry(void* arg) { |
| 522 | Thread* thread = reinterpret_cast<Thread*>(arg); |
| 523 | // This is also initialized by the first argument to pthread_create() but we |
| 524 | // don't know which thread will run first (the original thread or the new |
| 525 | // one) so we initialize it here too. |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 526 | thread->data()->thread_ = pthread_self(); |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 527 | SetThreadName(thread->name()); |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 528 | ASSERT(thread->data()->thread_ != kNoThread); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 529 | thread->NotifyStartedAndRun(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 530 | return NULL; |
| 531 | } |
| 532 | |
| 533 | |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 534 | void Thread::set_name(const char* name) { |
| 535 | strncpy(name_, name, sizeof(name_)); |
| 536 | name_[sizeof(name_) - 1] = '\0'; |
| 537 | } |
| 538 | |
| 539 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 540 | void Thread::Start() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 541 | pthread_attr_t* attr_ptr = NULL; |
| 542 | pthread_attr_t attr; |
| 543 | if (stack_size_ > 0) { |
| 544 | pthread_attr_init(&attr); |
| 545 | pthread_attr_setstacksize(&attr, static_cast<size_t>(stack_size_)); |
| 546 | attr_ptr = &attr; |
| 547 | } |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 548 | pthread_create(&data_->thread_, attr_ptr, ThreadEntry, this); |
| 549 | ASSERT(data_->thread_ != kNoThread); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | |
| 553 | void Thread::Join() { |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 554 | pthread_join(data_->thread_, NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 558 | #ifdef V8_FAST_TLS_SUPPORTED |
| 559 | |
| 560 | static Atomic32 tls_base_offset_initialized = 0; |
| 561 | intptr_t kMacTlsBaseOffset = 0; |
| 562 | |
| 563 | // It's safe to do the initialization more that once, but it has to be |
| 564 | // done at least once. |
| 565 | static void InitializeTlsBaseOffset() { |
| 566 | const size_t kBufferSize = 128; |
| 567 | char buffer[kBufferSize]; |
| 568 | size_t buffer_size = kBufferSize; |
| 569 | int ctl_name[] = { CTL_KERN , KERN_OSRELEASE }; |
| 570 | if (sysctl(ctl_name, 2, buffer, &buffer_size, NULL, 0) != 0) { |
| 571 | V8_Fatal(__FILE__, __LINE__, "V8 failed to get kernel version"); |
| 572 | } |
| 573 | // The buffer now contains a string of the form XX.YY.ZZ, where |
| 574 | // XX is the major kernel version component. |
| 575 | // Make sure the buffer is 0-terminated. |
| 576 | buffer[kBufferSize - 1] = '\0'; |
| 577 | char* period_pos = strchr(buffer, '.'); |
| 578 | *period_pos = '\0'; |
| 579 | int kernel_version_major = |
| 580 | static_cast<int>(strtol(buffer, NULL, 10)); // NOLINT |
| 581 | // The constants below are taken from pthreads.s from the XNU kernel |
| 582 | // sources archive at www.opensource.apple.com. |
| 583 | if (kernel_version_major < 11) { |
| 584 | // 8.x.x (Tiger), 9.x.x (Leopard), 10.x.x (Snow Leopard) have the |
| 585 | // same offsets. |
| 586 | #if defined(V8_HOST_ARCH_IA32) |
| 587 | kMacTlsBaseOffset = 0x48; |
| 588 | #else |
| 589 | kMacTlsBaseOffset = 0x60; |
| 590 | #endif |
| 591 | } else { |
| 592 | // 11.x.x (Lion) changed the offset. |
| 593 | kMacTlsBaseOffset = 0; |
| 594 | } |
| 595 | |
| 596 | Release_Store(&tls_base_offset_initialized, 1); |
| 597 | } |
| 598 | |
| 599 | static void CheckFastTls(Thread::LocalStorageKey key) { |
| 600 | void* expected = reinterpret_cast<void*>(0x1234CAFE); |
| 601 | Thread::SetThreadLocal(key, expected); |
| 602 | void* actual = Thread::GetExistingThreadLocal(key); |
| 603 | if (expected != actual) { |
| 604 | V8_Fatal(__FILE__, __LINE__, |
| 605 | "V8 failed to initialize fast TLS on current kernel"); |
| 606 | } |
| 607 | Thread::SetThreadLocal(key, NULL); |
| 608 | } |
| 609 | |
| 610 | #endif // V8_FAST_TLS_SUPPORTED |
| 611 | |
| 612 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 613 | Thread::LocalStorageKey Thread::CreateThreadLocalKey() { |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 614 | #ifdef V8_FAST_TLS_SUPPORTED |
| 615 | bool check_fast_tls = false; |
| 616 | if (tls_base_offset_initialized == 0) { |
| 617 | check_fast_tls = true; |
| 618 | InitializeTlsBaseOffset(); |
| 619 | } |
| 620 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 621 | pthread_key_t key; |
| 622 | int result = pthread_key_create(&key, NULL); |
| 623 | USE(result); |
| 624 | ASSERT(result == 0); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 625 | LocalStorageKey typed_key = static_cast<LocalStorageKey>(key); |
| 626 | #ifdef V8_FAST_TLS_SUPPORTED |
| 627 | // If we just initialized fast TLS support, make sure it works. |
| 628 | if (check_fast_tls) CheckFastTls(typed_key); |
| 629 | #endif |
| 630 | return typed_key; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | |
| 634 | void Thread::DeleteThreadLocalKey(LocalStorageKey key) { |
| 635 | pthread_key_t pthread_key = static_cast<pthread_key_t>(key); |
| 636 | int result = pthread_key_delete(pthread_key); |
| 637 | USE(result); |
| 638 | ASSERT(result == 0); |
| 639 | } |
| 640 | |
| 641 | |
| 642 | void* Thread::GetThreadLocal(LocalStorageKey key) { |
| 643 | pthread_key_t pthread_key = static_cast<pthread_key_t>(key); |
| 644 | return pthread_getspecific(pthread_key); |
| 645 | } |
| 646 | |
| 647 | |
| 648 | void Thread::SetThreadLocal(LocalStorageKey key, void* value) { |
| 649 | pthread_key_t pthread_key = static_cast<pthread_key_t>(key); |
| 650 | pthread_setspecific(pthread_key, value); |
| 651 | } |
| 652 | |
| 653 | |
| 654 | void Thread::YieldCPU() { |
| 655 | sched_yield(); |
| 656 | } |
| 657 | |
| 658 | |
| 659 | class MacOSMutex : public Mutex { |
| 660 | public: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 661 | MacOSMutex() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 662 | pthread_mutexattr_t attr; |
| 663 | pthread_mutexattr_init(&attr); |
| 664 | pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 665 | pthread_mutex_init(&mutex_, &attr); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 666 | } |
| 667 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 668 | virtual ~MacOSMutex() { pthread_mutex_destroy(&mutex_); } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 669 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 670 | virtual int Lock() { return pthread_mutex_lock(&mutex_); } |
| 671 | virtual int Unlock() { return pthread_mutex_unlock(&mutex_); } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 672 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 673 | virtual bool TryLock() { |
| 674 | int result = pthread_mutex_trylock(&mutex_); |
| 675 | // Return false if the lock is busy and locking failed. |
| 676 | if (result == EBUSY) { |
| 677 | return false; |
| 678 | } |
| 679 | ASSERT(result == 0); // Verify no other errors. |
| 680 | return true; |
| 681 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 682 | |
| 683 | private: |
| 684 | pthread_mutex_t mutex_; |
| 685 | }; |
| 686 | |
| 687 | |
| 688 | Mutex* OS::CreateMutex() { |
| 689 | return new MacOSMutex(); |
| 690 | } |
| 691 | |
| 692 | |
| 693 | class MacOSSemaphore : public Semaphore { |
| 694 | public: |
| 695 | explicit MacOSSemaphore(int count) { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 696 | int r; |
| 697 | r = semaphore_create(mach_task_self(), |
| 698 | &semaphore_, |
| 699 | SYNC_POLICY_FIFO, |
| 700 | count); |
| 701 | ASSERT(r == KERN_SUCCESS); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | ~MacOSSemaphore() { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 705 | int r; |
| 706 | r = semaphore_destroy(mach_task_self(), semaphore_); |
| 707 | ASSERT(r == KERN_SUCCESS); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 708 | } |
| 709 | |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 710 | void Wait() { |
| 711 | int r; |
| 712 | do { |
| 713 | r = semaphore_wait(semaphore_); |
| 714 | ASSERT(r == KERN_SUCCESS || r == KERN_ABORTED); |
| 715 | } while (r == KERN_ABORTED); |
| 716 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 717 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 718 | bool Wait(int timeout); |
| 719 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 720 | void Signal() { semaphore_signal(semaphore_); } |
| 721 | |
| 722 | private: |
| 723 | semaphore_t semaphore_; |
| 724 | }; |
| 725 | |
| 726 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 727 | bool MacOSSemaphore::Wait(int timeout) { |
| 728 | mach_timespec_t ts; |
| 729 | ts.tv_sec = timeout / 1000000; |
| 730 | ts.tv_nsec = (timeout % 1000000) * 1000; |
| 731 | return semaphore_timedwait(semaphore_, ts) != KERN_OPERATION_TIMED_OUT; |
| 732 | } |
| 733 | |
| 734 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 735 | Semaphore* OS::CreateSemaphore(int count) { |
| 736 | return new MacOSSemaphore(count); |
| 737 | } |
| 738 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 739 | |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 740 | void OS::SetUp() { |
| 741 | // Seed the random number generator. We preserve microsecond resolution. |
| 742 | uint64_t seed = Ticks() ^ (getpid() << 16); |
| 743 | srandom(static_cast<unsigned int>(seed)); |
| 744 | limit_mutex = CreateMutex(); |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 748 | void OS::TearDown() { |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 749 | delete limit_mutex; |
| 750 | } |
| 751 | |
| 752 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 753 | } } // namespace v8::internal |