blob: ace4056d3969a3e8d3b60347adb2178bd6273883 [file] [log] [blame]
ulan@chromium.org2efb9002012-01-19 15:36:35 +00001// Copyright 2012 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// 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.orgbb29dc92009-03-24 13:25:23 +000028// Platform specific code for Linux goes here. For the POSIX comaptible parts
29// the implementation is in platform-posix.cc.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000030
31#include <pthread.h>
32#include <semaphore.h>
33#include <signal.h>
lrn@chromium.org5d00b602011-01-05 09:51:43 +000034#include <sys/prctl.h>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000035#include <sys/time.h>
36#include <sys/resource.h>
lrn@chromium.org303ada72010-10-27 09:33:13 +000037#include <sys/syscall.h>
ager@chromium.org381abbb2009-02-25 13:23:22 +000038#include <sys/types.h>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000039#include <stdlib.h>
40
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +000041#if defined(__GLIBC__) && !defined(__UCLIBC__)
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +000042#include <execinfo.h>
43#include <cxxabi.h>
44#endif
45
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000046// Ubuntu Dapper requires memory pages to be marked as
47// executable. Otherwise, OS raises an exception when executing code
48// in that page.
49#include <sys/types.h> // mmap & munmap
ager@chromium.org236ad962008-09-25 09:45:57 +000050#include <sys/mman.h> // mmap & munmap
51#include <sys/stat.h> // open
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000052#include <fcntl.h> // open
53#include <unistd.h> // sysconf
ager@chromium.org236ad962008-09-25 09:45:57 +000054#include <strings.h> // index
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000055#include <errno.h>
56#include <stdarg.h>
57
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +000058// GLibc on ARM defines mcontext_t has a typedef for 'struct sigcontext'.
59// Old versions of the C library <signal.h> didn't define the type.
60#if defined(__ANDROID__) && !defined(__BIONIC_HAVE_UCONTEXT_T) && \
61 defined(__arm__) && !defined(__BIONIC_HAVE_STRUCT_SIGCONTEXT)
62#include <asm/sigcontext.h>
63#endif
64
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000065#undef MAP_TYPE
66
67#include "v8.h"
68
danno@chromium.org8c0a43f2012-04-03 08:37:53 +000069#include "platform-posix.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000070#include "platform.h"
ager@chromium.orga1645e22009-09-09 19:27:10 +000071#include "v8threads.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000072#include "vm-state-inl.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000073
74
kasperl@chromium.org71affb52009-05-26 05:44:31 +000075namespace v8 {
76namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000077
78// 0 is never a valid thread id on Linux since tids and pids share a
79// name space and pid 0 is reserved (see man 2 kill).
80static const pthread_t kNoThread = (pthread_t) 0;
81
82
83double ceiling(double x) {
84 return ceil(x);
85}
86
87
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000088static Mutex* limit_mutex = NULL;
89
90
danno@chromium.org8c0a43f2012-04-03 08:37:53 +000091void OS::PostSetUp() {
fschneider@chromium.org7d10be52012-04-10 12:30:14 +000092 POSIXPostSetUp();
danno@chromium.org8c0a43f2012-04-03 08:37:53 +000093}
94
95
ager@chromium.orgc4c92722009-11-18 14:12:51 +000096uint64_t OS::CpuFeaturesImpliedByPlatform() {
ager@chromium.orgc4c92722009-11-18 14:12:51 +000097 return 0; // Linux runs on anything.
ager@chromium.orgc4c92722009-11-18 14:12:51 +000098}
99
100
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000101#ifdef __arm__
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000102static bool CPUInfoContainsString(const char * search_string) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000103 const char* file_name = "/proc/cpuinfo";
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000104 // This is written as a straight shot one pass parser
105 // and not using STL string and ifstream because,
106 // on Linux, it's reading from a (non-mmap-able)
107 // character special device.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000108 FILE* f = NULL;
109 const char* what = search_string;
110
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000111 if (NULL == (f = fopen(file_name, "r"))) {
112 OS::PrintError("Failed to open /proc/cpuinfo\n");
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000113 return false;
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000114 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000115
116 int k;
117 while (EOF != (k = fgetc(f))) {
118 if (k == *what) {
119 ++what;
120 while ((*what != '\0') && (*what == fgetc(f))) {
121 ++what;
122 }
123 if (*what == '\0') {
124 fclose(f);
125 return true;
126 } else {
127 what = search_string;
128 }
129 }
130 }
131 fclose(f);
132
133 // Did not find string in the proc file.
134 return false;
135}
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000136
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000137
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000138bool OS::ArmCpuHasFeature(CpuFeature feature) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000139 const char* search_string = NULL;
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000140 // Simple detection of VFP at runtime for Linux.
141 // It is based on /proc/cpuinfo, which reveals hardware configuration
142 // to user-space applications. According to ARM (mid 2009), no similar
143 // facility is universally available on the ARM architectures,
144 // so it's up to individual OSes to provide such.
145 switch (feature) {
146 case VFP3:
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000147 search_string = "vfpv3";
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000148 break;
danno@chromium.org169691d2013-07-15 08:01:13 +0000149 case NEON:
150 search_string = "neon";
151 break;
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000152 case ARMv7:
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000153 search_string = "ARMv7";
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000154 break;
verwaest@chromium.org33e09c82012-10-10 17:07:22 +0000155 case SUDIV:
156 search_string = "idiva";
157 break;
yangguo@chromium.org003650e2013-01-24 16:31:08 +0000158 case VFP32DREGS:
159 // This case is handled specially below.
160 break;
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000161 default:
162 UNREACHABLE();
163 }
164
yangguo@chromium.org003650e2013-01-24 16:31:08 +0000165 if (feature == VFP32DREGS) {
166 return ArmCpuHasFeature(VFP3) && !CPUInfoContainsString("d16");
167 }
168
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000169 if (CPUInfoContainsString(search_string)) {
170 return true;
171 }
172
173 if (feature == VFP3) {
174 // Some old kernels will report vfp not vfpv3. Here we make a last attempt
175 // to detect vfpv3 by checking for vfp *and* neon, since neon is only
176 // available on architectures with vfpv3.
177 // Checking neon on its own is not enough as it is possible to have neon
178 // without vfp.
179 if (CPUInfoContainsString("vfp") && CPUInfoContainsString("neon")) {
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000180 return true;
181 }
182 }
183
184 return false;
185}
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000186
187
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000188CpuImplementer OS::GetCpuImplementer() {
189 static bool use_cached_value = false;
190 static CpuImplementer cached_value = UNKNOWN_IMPLEMENTER;
191 if (use_cached_value) {
192 return cached_value;
193 }
194 if (CPUInfoContainsString("CPU implementer\t: 0x41")) {
195 cached_value = ARM_IMPLEMENTER;
196 } else if (CPUInfoContainsString("CPU implementer\t: 0x51")) {
197 cached_value = QUALCOMM_IMPLEMENTER;
198 } else {
199 cached_value = UNKNOWN_IMPLEMENTER;
200 }
201 use_cached_value = true;
202 return cached_value;
203}
204
205
danno@chromium.org169691d2013-07-15 08:01:13 +0000206CpuPart OS::GetCpuPart(CpuImplementer implementer) {
207 static bool use_cached_value = false;
208 static CpuPart cached_value = CPU_UNKNOWN;
209 if (use_cached_value) {
210 return cached_value;
211 }
212 if (implementer == ARM_IMPLEMENTER) {
213 if (CPUInfoContainsString("CPU part\t: 0xc0f")) {
214 cached_value = CORTEX_A15;
215 } else if (CPUInfoContainsString("CPU part\t: 0xc0c")) {
216 cached_value = CORTEX_A12;
217 } else if (CPUInfoContainsString("CPU part\t: 0xc09")) {
218 cached_value = CORTEX_A9;
219 } else if (CPUInfoContainsString("CPU part\t: 0xc08")) {
220 cached_value = CORTEX_A8;
221 } else if (CPUInfoContainsString("CPU part\t: 0xc07")) {
222 cached_value = CORTEX_A7;
223 } else if (CPUInfoContainsString("CPU part\t: 0xc05")) {
224 cached_value = CORTEX_A5;
225 } else {
226 cached_value = CPU_UNKNOWN;
227 }
228 } else {
229 cached_value = CPU_UNKNOWN;
230 }
231 use_cached_value = true;
232 return cached_value;
233}
234
235
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000236bool OS::ArmUsingHardFloat() {
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +0000237 // GCC versions 4.6 and above define __ARM_PCS or __ARM_PCS_VFP to specify
238 // the Floating Point ABI used (PCS stands for Procedure Call Standard).
239 // We use these as well as a couple of other defines to statically determine
240 // what FP ABI used.
241 // GCC versions 4.4 and below don't support hard-fp.
242 // GCC versions 4.5 may support hard-fp without defining __ARM_PCS or
243 // __ARM_PCS_VFP.
244
245#define GCC_VERSION (__GNUC__ * 10000 \
246 + __GNUC_MINOR__ * 100 \
247 + __GNUC_PATCHLEVEL__)
248#if GCC_VERSION >= 40600
249#if defined(__ARM_PCS_VFP)
250 return true;
251#else
252 return false;
253#endif
254
255#elif GCC_VERSION < 40500
256 return false;
257
258#else
259#if defined(__ARM_PCS_VFP)
260 return true;
261#elif defined(__ARM_PCS) || defined(__SOFTFP) || !defined(__VFP_FP__)
262 return false;
263#else
264#error "Your version of GCC does not report the FP ABI compiled for." \
265 "Please report it on this issue" \
266 "http://code.google.com/p/v8/issues/detail?id=2140"
267
268#endif
269#endif
270#undef GCC_VERSION
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000271}
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +0000272
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000273#endif // def __arm__
274
275
lrn@chromium.org7516f052011-03-30 08:52:27 +0000276#ifdef __mips__
277bool OS::MipsCpuHasFeature(CpuFeature feature) {
278 const char* search_string = NULL;
279 const char* file_name = "/proc/cpuinfo";
280 // Simple detection of FPU at runtime for Linux.
281 // It is based on /proc/cpuinfo, which reveals hardware configuration
282 // to user-space applications. According to MIPS (early 2010), no similar
283 // facility is universally available on the MIPS architectures,
284 // so it's up to individual OSes to provide such.
285 //
286 // This is written as a straight shot one pass parser
287 // and not using STL string and ifstream because,
288 // on Linux, it's reading from a (non-mmap-able)
289 // character special device.
290
291 switch (feature) {
292 case FPU:
293 search_string = "FPU";
294 break;
295 default:
296 UNREACHABLE();
297 }
298
299 FILE* f = NULL;
300 const char* what = search_string;
301
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000302 if (NULL == (f = fopen(file_name, "r"))) {
303 OS::PrintError("Failed to open /proc/cpuinfo\n");
lrn@chromium.org7516f052011-03-30 08:52:27 +0000304 return false;
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000305 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000306
307 int k;
308 while (EOF != (k = fgetc(f))) {
309 if (k == *what) {
310 ++what;
311 while ((*what != '\0') && (*what == fgetc(f))) {
312 ++what;
313 }
314 if (*what == '\0') {
315 fclose(f);
316 return true;
317 } else {
318 what = search_string;
319 }
320 }
321 }
322 fclose(f);
323
324 // Did not find string in the proc file.
325 return false;
326}
327#endif // def __mips__
328
329
ager@chromium.org236ad962008-09-25 09:45:57 +0000330int OS::ActivationFrameAlignment() {
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +0000331#if V8_TARGET_ARCH_ARM
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000332 // On EABI ARM targets this is required for fp correctness in the
333 // runtime system.
ager@chromium.org3a6061e2009-03-12 14:24:36 +0000334 return 8;
ager@chromium.org5c838252010-02-19 08:53:10 +0000335#elif V8_TARGET_ARCH_MIPS
336 return 8;
337#endif
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000338 // With gcc 4.4 the tree vectorization optimizer can generate code
ager@chromium.orge2902be2009-06-08 12:21:35 +0000339 // that requires 16 byte alignment such as movdqa on x86.
340 return 16;
ager@chromium.org236ad962008-09-25 09:45:57 +0000341}
342
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000343
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000344const char* OS::LocalTimezone(double time) {
ulan@chromium.org77ca49a2013-04-22 09:43:56 +0000345 if (std::isnan(time)) return "";
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000346 time_t tv = static_cast<time_t>(floor(time/msPerSecond));
347 struct tm* t = localtime(&tv);
348 if (NULL == t) return "";
349 return t->tm_zone;
350}
351
352
353double OS::LocalTimeOffset() {
354 time_t tv = time(NULL);
355 struct tm* t = localtime(&tv);
356 // tm_gmtoff includes any daylight savings offset, so subtract it.
357 return static_cast<double>(t->tm_gmtoff * msPerSecond -
358 (t->tm_isdst > 0 ? 3600 * msPerSecond : 0));
359}
360
361
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000362// We keep the lowest and highest addresses mapped as a quick way of
363// determining that pointers are outside the heap (used mostly in assertions
ulan@chromium.org2efb9002012-01-19 15:36:35 +0000364// and verification). The estimate is conservative, i.e., not all addresses in
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000365// 'allocated' space are actually allocated to our heap. The range is
366// [lowest, highest), inclusive on the low and and exclusive on the high end.
367static void* lowest_ever_allocated = reinterpret_cast<void*>(-1);
368static void* highest_ever_allocated = reinterpret_cast<void*>(0);
369
370
371static void UpdateAllocatedSpaceLimits(void* address, int size) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000372 ASSERT(limit_mutex != NULL);
373 ScopedLock lock(limit_mutex);
374
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000375 lowest_ever_allocated = Min(lowest_ever_allocated, address);
376 highest_ever_allocated =
377 Max(highest_ever_allocated,
378 reinterpret_cast<void*>(reinterpret_cast<char*>(address) + size));
379}
380
381
382bool OS::IsOutsideAllocatedSpace(void* address) {
383 return address < lowest_ever_allocated || address >= highest_ever_allocated;
384}
385
386
387size_t OS::AllocateAlignment() {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000388 return sysconf(_SC_PAGESIZE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000389}
390
391
kasper.lund7276f142008-07-30 08:49:36 +0000392void* OS::Allocate(const size_t requested,
393 size_t* allocated,
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +0000394 bool is_executable) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000395 const size_t msize = RoundUp(requested, AllocateAlignment());
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +0000396 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000397 void* addr = OS::GetRandomMmapAddr();
ricow@chromium.org9fa09672011-07-25 11:05:35 +0000398 void* mbase = mmap(addr, msize, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000399 if (mbase == MAP_FAILED) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000400 LOG(i::Isolate::Current(),
401 StringEvent("OS::Allocate", "mmap failed"));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000402 return NULL;
403 }
404 *allocated = msize;
405 UpdateAllocatedSpaceLimits(mbase, msize);
406 return mbase;
407}
408
409
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +0000410void OS::Free(void* address, const size_t size) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000411 // TODO(1240712): munmap has a return value which is ignored here.
ager@chromium.orga1645e22009-09-09 19:27:10 +0000412 int result = munmap(address, size);
413 USE(result);
414 ASSERT(result == 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000415}
416
417
418void OS::Sleep(int milliseconds) {
419 unsigned int ms = static_cast<unsigned int>(milliseconds);
420 usleep(1000 * ms);
421}
422
423
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000424int OS::NumberOfCores() {
425 return sysconf(_SC_NPROCESSORS_ONLN);
426}
427
428
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000429void OS::Abort() {
430 // Redirect to std abort to signal abnormal program termination.
rossberg@chromium.org2c067b12012-03-19 11:01:52 +0000431 if (FLAG_break_on_abort) {
432 DebugBreak();
433 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000434 abort();
435}
436
437
kasper.lund7276f142008-07-30 08:49:36 +0000438void OS::DebugBreak() {
ager@chromium.org5ec48922009-05-05 07:25:34 +0000439// TODO(lrn): Introduce processor define for runtime system (!= V8_ARCH_x,
440// which is the architecture of generated code).
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000441#if (defined(__arm__) || defined(__thumb__))
kasper.lund7276f142008-07-30 08:49:36 +0000442 asm("bkpt 0");
ager@chromium.org5c838252010-02-19 08:53:10 +0000443#elif defined(__mips__)
444 asm("break");
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000445#elif defined(__native_client__)
446 asm("hlt");
kasper.lund7276f142008-07-30 08:49:36 +0000447#else
448 asm("int $3");
449#endif
450}
451
452
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000453void OS::DumpBacktrace() {
danno@chromium.org169691d2013-07-15 08:01:13 +0000454 // backtrace is a glibc extension.
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000455#if defined(__GLIBC__) && !defined(__UCLIBC__)
danno@chromium.org169691d2013-07-15 08:01:13 +0000456 POSIXBacktraceHelper<backtrace, backtrace_symbols>::DumpBacktrace();
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000457#endif
458}
459
460
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000461class PosixMemoryMappedFile : public OS::MemoryMappedFile {
462 public:
463 PosixMemoryMappedFile(FILE* file, void* memory, int size)
464 : file_(file), memory_(memory), size_(size) { }
465 virtual ~PosixMemoryMappedFile();
466 virtual void* memory() { return memory_; }
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000467 virtual int size() { return size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000468 private:
469 FILE* file_;
470 void* memory_;
471 int size_;
472};
473
474
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000475OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) {
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000476 FILE* file = fopen(name, "r+");
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000477 if (file == NULL) return NULL;
478
479 fseek(file, 0, SEEK_END);
480 int size = ftell(file);
481
482 void* memory =
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000483 mmap(OS::GetRandomMmapAddr(),
484 size,
485 PROT_READ | PROT_WRITE,
486 MAP_SHARED,
487 fileno(file),
488 0);
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000489 return new PosixMemoryMappedFile(file, memory, size);
490}
491
492
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000493OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size,
494 void* initial) {
495 FILE* file = fopen(name, "w+");
496 if (file == NULL) return NULL;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000497 int result = fwrite(initial, size, 1, file);
498 if (result < 1) {
499 fclose(file);
500 return NULL;
501 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000502 void* memory =
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000503 mmap(OS::GetRandomMmapAddr(),
504 size,
505 PROT_READ | PROT_WRITE,
506 MAP_SHARED,
507 fileno(file),
508 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000509 return new PosixMemoryMappedFile(file, memory, size);
510}
511
512
513PosixMemoryMappedFile::~PosixMemoryMappedFile() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000514 if (memory_) OS::Free(memory_, size_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000515 fclose(file_);
516}
517
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000518
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000519void OS::LogSharedLibraryAddresses() {
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000520 // This function assumes that the layout of the file is as follows:
521 // hex_start_addr-hex_end_addr rwxp <unused data> [binary_file_name]
522 // If we encounter an unexpected situation we abort scanning further entries.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000523 FILE* fp = fopen("/proc/self/maps", "r");
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000524 if (fp == NULL) return;
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000525
526 // Allocate enough room to be able to store a full file name.
527 const int kLibNameLen = FILENAME_MAX + 1;
528 char* lib_name = reinterpret_cast<char*>(malloc(kLibNameLen));
529
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000530 i::Isolate* isolate = ISOLATE;
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000531 // This loop will terminate once the scanning hits an EOF.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000532 while (true) {
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000533 uintptr_t start, end;
534 char attr_r, attr_w, attr_x, attr_p;
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000535 // Parse the addresses and permission bits at the beginning of the line.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000536 if (fscanf(fp, "%" V8PRIxPTR "-%" V8PRIxPTR, &start, &end) != 2) break;
537 if (fscanf(fp, " %c%c%c%c", &attr_r, &attr_w, &attr_x, &attr_p) != 4) break;
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000538
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000539 int c;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000540 if (attr_r == 'r' && attr_w != 'w' && attr_x == 'x') {
541 // Found a read-only executable entry. Skip characters until we reach
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000542 // the beginning of the filename or the end of the line.
543 do {
544 c = getc(fp);
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000545 } while ((c != EOF) && (c != '\n') && (c != '/') && (c != '['));
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000546 if (c == EOF) break; // EOF: Was unexpected, just exit.
547
548 // Process the filename if found.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000549 if ((c == '/') || (c == '[')) {
550 // Push the '/' or '[' back into the stream to be read below.
551 ungetc(c, fp);
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000552
553 // Read to the end of the line. Exit if the read fails.
554 if (fgets(lib_name, kLibNameLen, fp) == NULL) break;
555
556 // Drop the newline character read by fgets. We do not need to check
557 // for a zero-length string because we know that we at least read the
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000558 // '/' or '[' character.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000559 lib_name[strlen(lib_name) - 1] = '\0';
560 } else {
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000561 // No library name found, just record the raw address range.
562 snprintf(lib_name, kLibNameLen,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000563 "%08" V8PRIxPTR "-%08" V8PRIxPTR, start, end);
564 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000565 LOG(isolate, SharedLibraryEvent(lib_name, start, end));
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000566 } else {
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000567 // Entry not describing executable data. Skip to end of line to set up
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000568 // reading the next entry.
569 do {
570 c = getc(fp);
571 } while ((c != EOF) && (c != '\n'));
572 if (c == EOF) break;
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000573 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000574 }
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000575 free(lib_name);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000576 fclose(fp);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000577}
578
579
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000580void OS::SignalCodeMovingGC() {
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000581 // Support for ll_prof.py.
582 //
583 // The Linux profiler built into the kernel logs all mmap's with
584 // PROT_EXEC so that analysis tools can properly attribute ticks. We
585 // do a mmap with a name known by ll_prof.py and immediately munmap
586 // it. This injects a GC marker into the stream of events generated
587 // by the kernel and allows us to synchronize V8 code log and the
588 // kernel log.
589 int size = sysconf(_SC_PAGESIZE);
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +0000590 FILE* f = fopen(FLAG_gc_fake_mmap, "w+");
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000591 if (f == NULL) {
592 OS::PrintError("Failed to open %s\n", FLAG_gc_fake_mmap);
593 OS::Abort();
594 }
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000595 void* addr = mmap(OS::GetRandomMmapAddr(),
596 size,
597 PROT_READ | PROT_EXEC,
598 MAP_PRIVATE,
599 fileno(f),
600 0);
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000601 ASSERT(addr != MAP_FAILED);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000602 OS::Free(addr, size);
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000603 fclose(f);
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000604}
605
606
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000607int OS::StackWalk(Vector<OS::StackFrame> frames) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000608 // backtrace is a glibc extension.
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000609#if defined(__GLIBC__) && !defined(__UCLIBC__)
danno@chromium.org169691d2013-07-15 08:01:13 +0000610 return POSIXBacktraceHelper<backtrace, backtrace_symbols>::StackWalk(frames);
611#else
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000612 return 0;
danno@chromium.org169691d2013-07-15 08:01:13 +0000613#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000614}
615
616
617// Constants used for mmap.
618static const int kMmapFd = -1;
619static const int kMmapFdOffset = 0;
620
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000621
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000622VirtualMemory::VirtualMemory() : address_(NULL), size_(0) { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000623
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000624
625VirtualMemory::VirtualMemory(size_t size)
626 : address_(ReserveRegion(size)), size_(size) { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000627
628
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000629VirtualMemory::VirtualMemory(size_t size, size_t alignment)
630 : address_(NULL), size_(0) {
631 ASSERT(IsAligned(alignment, static_cast<intptr_t>(OS::AllocateAlignment())));
632 size_t request_size = RoundUp(size + alignment,
633 static_cast<intptr_t>(OS::AllocateAlignment()));
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000634 void* reservation = mmap(OS::GetRandomMmapAddr(),
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000635 request_size,
636 PROT_NONE,
637 MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE,
638 kMmapFd,
639 kMmapFdOffset);
640 if (reservation == MAP_FAILED) return;
641
642 Address base = static_cast<Address>(reservation);
643 Address aligned_base = RoundUp(base, alignment);
644 ASSERT_LE(base, aligned_base);
645
646 // Unmap extra memory reserved before and after the desired block.
647 if (aligned_base != base) {
648 size_t prefix_size = static_cast<size_t>(aligned_base - base);
649 OS::Free(base, prefix_size);
650 request_size -= prefix_size;
651 }
652
653 size_t aligned_size = RoundUp(size, OS::AllocateAlignment());
654 ASSERT_LE(aligned_size, request_size);
655
656 if (aligned_size != request_size) {
657 size_t suffix_size = request_size - aligned_size;
658 OS::Free(aligned_base + aligned_size, suffix_size);
659 request_size -= suffix_size;
660 }
661
662 ASSERT(aligned_size == request_size);
663
664 address_ = static_cast<void*>(aligned_base);
665 size_ = aligned_size;
666}
667
668
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000669VirtualMemory::~VirtualMemory() {
670 if (IsReserved()) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000671 bool result = ReleaseRegion(address(), size());
672 ASSERT(result);
673 USE(result);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000674 }
675}
676
677
678bool VirtualMemory::IsReserved() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000679 return address_ != NULL;
680}
681
682
683void VirtualMemory::Reset() {
684 address_ = NULL;
685 size_ = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000686}
687
688
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +0000689bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000690 return CommitRegion(address, size, is_executable);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000691}
692
693
694bool VirtualMemory::Uncommit(void* address, size_t size) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000695 return UncommitRegion(address, size);
696}
697
698
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000699bool VirtualMemory::Guard(void* address) {
700 OS::Guard(address, OS::CommitPageSize());
701 return true;
702}
703
704
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000705void* VirtualMemory::ReserveRegion(size_t size) {
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000706 void* result = mmap(OS::GetRandomMmapAddr(),
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000707 size,
708 PROT_NONE,
709 MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE,
710 kMmapFd,
711 kMmapFdOffset);
712
713 if (result == MAP_FAILED) return NULL;
714
715 return result;
716}
717
718
719bool VirtualMemory::CommitRegion(void* base, size_t size, bool is_executable) {
720 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
721 if (MAP_FAILED == mmap(base,
722 size,
723 prot,
724 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED,
725 kMmapFd,
726 kMmapFdOffset)) {
727 return false;
728 }
729
730 UpdateAllocatedSpaceLimits(base, size);
731 return true;
732}
733
734
735bool VirtualMemory::UncommitRegion(void* base, size_t size) {
736 return mmap(base,
737 size,
738 PROT_NONE,
ager@chromium.orga1645e22009-09-09 19:27:10 +0000739 MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE | MAP_FIXED,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000740 kMmapFd,
741 kMmapFdOffset) != MAP_FAILED;
742}
743
744
745bool VirtualMemory::ReleaseRegion(void* base, size_t size) {
746 return munmap(base, size) == 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000747}
748
749
danno@chromium.org72204d52012-10-31 10:02:10 +0000750bool VirtualMemory::HasLazyCommits() {
751 return true;
752}
753
754
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000755class Thread::PlatformData : public Malloced {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000756 public:
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000757 PlatformData() : thread_(kNoThread) {}
ager@chromium.org41826e72009-03-30 13:30:57 +0000758
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000759 pthread_t thread_; // Thread handle for pthread.
760};
761
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000762Thread::Thread(const Options& options)
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000763 : data_(new PlatformData()),
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000764 stack_size_(options.stack_size()),
765 start_semaphore_(NULL) {
yangguo@chromium.org659ceec2012-01-26 07:37:54 +0000766 set_name(options.name());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000767}
768
769
770Thread::~Thread() {
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000771 delete data_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000772}
773
774
775static void* ThreadEntry(void* arg) {
776 Thread* thread = reinterpret_cast<Thread*>(arg);
777 // This is also initialized by the first argument to pthread_create() but we
778 // don't know which thread will run first (the original thread or the new
779 // one) so we initialize it here too.
danno@chromium.orgb6451162011-08-17 14:33:23 +0000780#ifdef PR_SET_NAME
karlklose@chromium.org8f806e82011-03-07 14:06:08 +0000781 prctl(PR_SET_NAME,
782 reinterpret_cast<unsigned long>(thread->name()), // NOLINT
783 0, 0, 0);
danno@chromium.orgb6451162011-08-17 14:33:23 +0000784#endif
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000785 thread->data()->thread_ = pthread_self();
786 ASSERT(thread->data()->thread_ != kNoThread);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000787 thread->NotifyStartedAndRun();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000788 return NULL;
789}
790
791
lrn@chromium.org5d00b602011-01-05 09:51:43 +0000792void Thread::set_name(const char* name) {
793 strncpy(name_, name, sizeof(name_));
794 name_[sizeof(name_) - 1] = '\0';
795}
796
797
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000798void Thread::Start() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000799 pthread_attr_t* attr_ptr = NULL;
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000800#if defined(__native_client__)
801 // use default stack size.
802#else
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000803 pthread_attr_t attr;
804 if (stack_size_ > 0) {
805 pthread_attr_init(&attr);
806 pthread_attr_setstacksize(&attr, static_cast<size_t>(stack_size_));
807 attr_ptr = &attr;
808 }
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000809#endif
danno@chromium.orgc612e022011-11-10 11:38:15 +0000810 int result = pthread_create(&data_->thread_, attr_ptr, ThreadEntry, this);
811 CHECK_EQ(0, result);
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000812 ASSERT(data_->thread_ != kNoThread);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000813}
814
815
816void Thread::Join() {
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000817 pthread_join(data_->thread_, NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000818}
819
820
821Thread::LocalStorageKey Thread::CreateThreadLocalKey() {
822 pthread_key_t key;
823 int result = pthread_key_create(&key, NULL);
824 USE(result);
825 ASSERT(result == 0);
826 return static_cast<LocalStorageKey>(key);
827}
828
829
830void Thread::DeleteThreadLocalKey(LocalStorageKey key) {
831 pthread_key_t pthread_key = static_cast<pthread_key_t>(key);
832 int result = pthread_key_delete(pthread_key);
833 USE(result);
834 ASSERT(result == 0);
835}
836
837
838void* Thread::GetThreadLocal(LocalStorageKey key) {
839 pthread_key_t pthread_key = static_cast<pthread_key_t>(key);
840 return pthread_getspecific(pthread_key);
841}
842
843
844void Thread::SetThreadLocal(LocalStorageKey key, void* value) {
845 pthread_key_t pthread_key = static_cast<pthread_key_t>(key);
846 pthread_setspecific(pthread_key, value);
847}
848
849
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000850class LinuxSemaphore : public Semaphore {
851 public:
852 explicit LinuxSemaphore(int count) { sem_init(&sem_, 0, count); }
853 virtual ~LinuxSemaphore() { sem_destroy(&sem_); }
854
kasper.lund7276f142008-07-30 08:49:36 +0000855 virtual void Wait();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000856 virtual bool Wait(int timeout);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000857 virtual void Signal() { sem_post(&sem_); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000858 private:
859 sem_t sem_;
860};
861
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000862
kasper.lund7276f142008-07-30 08:49:36 +0000863void LinuxSemaphore::Wait() {
864 while (true) {
865 int result = sem_wait(&sem_);
866 if (result == 0) return; // Successfully got semaphore.
867 CHECK(result == -1 && errno == EINTR); // Signal caused spurious wakeup.
868 }
869}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000870
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000871
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000872#ifndef TIMEVAL_TO_TIMESPEC
873#define TIMEVAL_TO_TIMESPEC(tv, ts) do { \
874 (ts)->tv_sec = (tv)->tv_sec; \
875 (ts)->tv_nsec = (tv)->tv_usec * 1000; \
876} while (false)
877#endif
878
879
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000880bool LinuxSemaphore::Wait(int timeout) {
881 const long kOneSecondMicros = 1000000; // NOLINT
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000882
883 // Split timeout into second and nanosecond parts.
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000884 struct timeval delta;
885 delta.tv_usec = timeout % kOneSecondMicros;
886 delta.tv_sec = timeout / kOneSecondMicros;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000887
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000888 struct timeval current_time;
889 // Get the current time.
890 if (gettimeofday(&current_time, NULL) == -1) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000891 return false;
892 }
893
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000894 // Calculate time for end of timeout.
895 struct timeval end_time;
896 timeradd(&current_time, &delta, &end_time);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000897
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000898 struct timespec ts;
899 TIMEVAL_TO_TIMESPEC(&end_time, &ts);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000900 // Wait for semaphore signalled or timeout.
901 while (true) {
902 int result = sem_timedwait(&sem_, &ts);
903 if (result == 0) return true; // Successfully got semaphore.
904 if (result > 0) {
905 // For glibc prior to 2.3.4 sem_timedwait returns the error instead of -1.
906 errno = result;
907 result = -1;
908 }
909 if (result == -1 && errno == ETIMEDOUT) return false; // Timeout.
910 CHECK(result == -1 && errno == EINTR); // Signal caused spurious wakeup.
911 }
912}
913
914
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000915Semaphore* OS::CreateSemaphore(int count) {
916 return new LinuxSemaphore(count);
917}
918
ager@chromium.org381abbb2009-02-25 13:23:22 +0000919
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000920void OS::SetUp() {
921 // Seed the random number generator. We preserve microsecond resolution.
922 uint64_t seed = Ticks() ^ (getpid() << 16);
923 srandom(static_cast<unsigned int>(seed));
924 limit_mutex = CreateMutex();
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000925}
926
927
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000928void OS::TearDown() {
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000929 delete limit_mutex;
930}
931
932
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000933} } // namespace v8::internal