ulan@chromium.org | 0e3f88b | 2012-05-22 09:16:05 +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 | |
| 28 | // This module contains the platform-specific code. This make the rest of the |
| 29 | // code less dependent on operating system, compilers and runtime libraries. |
| 30 | // This module does specifically not deal with differences between different |
| 31 | // processor architecture. |
| 32 | // The platform classes have the same definition for all platforms. The |
| 33 | // implementation for a particular platform is put in platform_<os>.cc. |
| 34 | // The build system then uses the implementation for the target platform. |
| 35 | // |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 36 | // This design has been chosen because it is simple and fast. Alternatively, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 37 | // the platform dependent classes could have been implemented using abstract |
| 38 | // superclasses with virtual methods and having specializations for each |
| 39 | // platform. This design was rejected because it was more complicated and |
| 40 | // slower. It would require factory methods for selecting the right |
| 41 | // implementation and the overhead of virtual methods for performance |
| 42 | // sensitive like mutex locking/unlocking. |
| 43 | |
| 44 | #ifndef V8_PLATFORM_H_ |
| 45 | #define V8_PLATFORM_H_ |
| 46 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 47 | #ifdef __sun |
| 48 | # ifndef signbit |
| 49 | int signbit(double x); |
| 50 | # endif |
| 51 | #endif |
| 52 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 53 | // GCC specific stuff |
| 54 | #ifdef __GNUC__ |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 55 | |
| 56 | // Needed for va_list on at least MinGW and Android. |
| 57 | #include <stdarg.h> |
| 58 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 59 | #define __GNUC_VERSION__ (__GNUC__ * 10000 + __GNUC_MINOR__ * 100) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 60 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 61 | #endif // __GNUC__ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 62 | |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 63 | |
| 64 | // Windows specific stuff. |
| 65 | #ifdef WIN32 |
| 66 | |
| 67 | // Microsoft Visual C++ specific stuff. |
| 68 | #ifdef _MSC_VER |
| 69 | |
| 70 | #include "win32-math.h" |
| 71 | |
| 72 | int strncasecmp(const char* s1, const char* s2, int n); |
| 73 | |
| 74 | #endif // _MSC_VER |
| 75 | |
| 76 | // Random is missing on both Visual Studio and MinGW. |
| 77 | int random(); |
| 78 | |
| 79 | #endif // WIN32 |
| 80 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 81 | #include "atomicops.h" |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 82 | #include "lazy-instance.h" |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 83 | #include "platform-tls.h" |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 84 | #include "utils.h" |
| 85 | #include "v8globals.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 86 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 87 | namespace v8 { |
| 88 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 89 | |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 90 | // Use AtomicWord for a machine-sized pointer. It is assumed that |
| 91 | // reads and writes of naturally aligned values of this type are atomic. |
| 92 | typedef intptr_t AtomicWord; |
| 93 | |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 94 | class Semaphore; |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 95 | class Mutex; |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 96 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 97 | double ceiling(double x); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 98 | double modulo(double x, double y); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 99 | |
ulan@chromium.org | 9a21ec4 | 2012-03-06 08:42:24 +0000 | [diff] [blame] | 100 | // Custom implementation of sin, cos, tan and log. |
| 101 | double fast_sin(double input); |
| 102 | double fast_cos(double input); |
| 103 | double fast_tan(double input); |
| 104 | double fast_log(double input); |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 105 | double fast_sqrt(double input); |
ulan@chromium.org | 9a21ec4 | 2012-03-06 08:42:24 +0000 | [diff] [blame] | 106 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 107 | // Forward declarations. |
| 108 | class Socket; |
| 109 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 110 | // ---------------------------------------------------------------------------- |
| 111 | // OS |
| 112 | // |
| 113 | // This class has static methods for the different platform specific |
| 114 | // functions. Add methods here to cope with differences between the |
| 115 | // supported platforms. |
| 116 | |
| 117 | class OS { |
| 118 | public: |
| 119 | // Initializes the platform OS support. Called once at VM startup. |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 120 | static void SetUp(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 121 | |
danno@chromium.org | 8c0a43f | 2012-04-03 08:37:53 +0000 | [diff] [blame] | 122 | // Initializes the platform OS support that depend on CPU features. This is |
| 123 | // called after CPU initialization. |
| 124 | static void PostSetUp(); |
| 125 | |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 126 | // Clean up platform-OS-related things. Called once at VM shutdown. |
| 127 | static void TearDown(); |
| 128 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 129 | // Returns the accumulated user time for thread. This routine |
| 130 | // can be used for profiling. The implementation should |
| 131 | // strive for high-precision timer resolution, preferable |
| 132 | // micro-second resolution. |
| 133 | static int GetUserTime(uint32_t* secs, uint32_t* usecs); |
| 134 | |
| 135 | // Get a tick counter normalized to one tick per microsecond. |
| 136 | // Used for calculating time intervals. |
| 137 | static int64_t Ticks(); |
| 138 | |
| 139 | // Returns current time as the number of milliseconds since |
| 140 | // 00:00:00 UTC, January 1, 1970. |
| 141 | static double TimeCurrentMillis(); |
| 142 | |
| 143 | // Returns a string identifying the current time zone. The |
| 144 | // timestamp is used for determining if DST is in effect. |
sgjesse@chromium.org | b9d7da1 | 2009-08-05 08:38:10 +0000 | [diff] [blame] | 145 | static const char* LocalTimezone(double time); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 146 | |
| 147 | // Returns the local time offset in milliseconds east of UTC without |
| 148 | // taking daylight savings time into account. |
| 149 | static double LocalTimeOffset(); |
| 150 | |
| 151 | // Returns the daylight savings offset for the given time. |
| 152 | static double DaylightSavingsOffset(double time); |
| 153 | |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 154 | // Returns last OS error. |
| 155 | static int GetLastError(); |
| 156 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 157 | static FILE* FOpen(const char* path, const char* mode); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 158 | static bool Remove(const char* path); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 159 | |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 160 | // Opens a temporary file, the file is auto removed on close. |
| 161 | static FILE* OpenTemporaryFile(); |
| 162 | |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 163 | // Log file open mode is platform-dependent due to line ends issues. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 164 | static const char* const LogFileOpenMode; |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 165 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 166 | // Print output to console. This is mostly used for debugging output. |
| 167 | // On platforms that has standard terminal output, the output |
| 168 | // should go to stdout. |
| 169 | static void Print(const char* format, ...); |
| 170 | static void VPrint(const char* format, va_list args); |
| 171 | |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 172 | // Print output to a file. This is mostly used for debugging output. |
| 173 | static void FPrint(FILE* out, const char* format, ...); |
| 174 | static void VFPrint(FILE* out, const char* format, va_list args); |
| 175 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 176 | // Print error output to console. This is mostly used for error message |
| 177 | // output. On platforms that has standard terminal output, the output |
| 178 | // should go to stderr. |
| 179 | static void PrintError(const char* format, ...); |
| 180 | static void VPrintError(const char* format, va_list args); |
| 181 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 182 | // Allocate/Free memory used by JS heap. Pages are readable/writable, but |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 183 | // they are not guaranteed to be executable unless 'executable' is true. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 184 | // Returns the address of allocated memory, or NULL if failed. |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 185 | static void* Allocate(const size_t requested, |
| 186 | size_t* allocated, |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 187 | bool is_executable); |
| 188 | static void Free(void* address, const size_t size); |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 189 | |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 190 | // This is the granularity at which the ProtectCode(...) call can set page |
| 191 | // permissions. |
| 192 | static intptr_t CommitPageSize(); |
| 193 | |
lrn@chromium.org | d4e9e22 | 2011-08-03 12:01:58 +0000 | [diff] [blame] | 194 | // Mark code segments non-writable. |
| 195 | static void ProtectCode(void* address, const size_t size); |
| 196 | |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 197 | // Assign memory as a guard page so that access will cause an exception. |
| 198 | static void Guard(void* address, const size_t size); |
| 199 | |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 200 | // Generate a random address to be used for hinting mmap(). |
| 201 | static void* GetRandomMmapAddr(); |
| 202 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 203 | // Get the Alignment guaranteed by Allocate(). |
| 204 | static size_t AllocateAlignment(); |
| 205 | |
| 206 | // Returns an indication of whether a pointer is in a space that |
| 207 | // has been allocated by Allocate(). This method may conservatively |
| 208 | // always return false, but giving more accurate information may |
| 209 | // improve the robustness of the stack dump code in the presence of |
| 210 | // heap corruption. |
| 211 | static bool IsOutsideAllocatedSpace(void* pointer); |
| 212 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 213 | // Sleep for a number of milliseconds. |
| 214 | static void Sleep(const int milliseconds); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 215 | |
| 216 | // Abort the current process. |
| 217 | static void Abort(); |
| 218 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 219 | // Debug break. |
| 220 | static void DebugBreak(); |
| 221 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 222 | // Walk the stack. |
| 223 | static const int kStackWalkError = -1; |
| 224 | static const int kStackWalkMaxNameLen = 256; |
| 225 | static const int kStackWalkMaxTextLen = 256; |
| 226 | struct StackFrame { |
| 227 | void* address; |
| 228 | char text[kStackWalkMaxTextLen]; |
| 229 | }; |
| 230 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 231 | static int StackWalk(Vector<StackFrame> frames); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 232 | |
| 233 | // Factory method for creating platform dependent Mutex. |
| 234 | // Please use delete to reclaim the storage for the returned Mutex. |
| 235 | static Mutex* CreateMutex(); |
| 236 | |
| 237 | // Factory method for creating platform dependent Semaphore. |
| 238 | // Please use delete to reclaim the storage for the returned Semaphore. |
| 239 | static Semaphore* CreateSemaphore(int count); |
| 240 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 241 | // Factory method for creating platform dependent Socket. |
| 242 | // Please use delete to reclaim the storage for the returned Socket. |
| 243 | static Socket* CreateSocket(); |
| 244 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 245 | class MemoryMappedFile { |
| 246 | public: |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 247 | static MemoryMappedFile* open(const char* name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 248 | static MemoryMappedFile* create(const char* name, int size, void* initial); |
| 249 | virtual ~MemoryMappedFile() { } |
| 250 | virtual void* memory() = 0; |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 251 | virtual int size() = 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 252 | }; |
| 253 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 254 | // Safe formatting print. Ensures that str is always null-terminated. |
| 255 | // Returns the number of chars written, or -1 if output was truncated. |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 256 | static int SNPrintF(Vector<char> str, const char* format, ...); |
| 257 | static int VSNPrintF(Vector<char> str, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 258 | const char* format, |
| 259 | va_list args); |
| 260 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 261 | static char* StrChr(char* str, int c); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 262 | static void StrNCpy(Vector<char> dest, const char* src, size_t n); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 263 | |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 264 | // Support for the profiler. Can do nothing, in which case ticks |
| 265 | // occuring in shared libraries will not be properly accounted for. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 266 | static void LogSharedLibraryAddresses(); |
| 267 | |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 268 | // Support for the profiler. Notifies the external profiling |
| 269 | // process that a code moving garbage collection starts. Can do |
| 270 | // nothing, in which case the code objects must not move (e.g., by |
| 271 | // using --never-compact) if accurate profiling is desired. |
| 272 | static void SignalCodeMovingGC(); |
| 273 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 274 | // The return value indicates the CPU features we are sure of because of the |
| 275 | // OS. For example MacOSX doesn't run on any x86 CPUs that don't have SSE2 |
| 276 | // instructions. |
| 277 | // This is a little messy because the interpretation is subject to the cross |
| 278 | // of the CPU and the OS. The bits in the answer correspond to the bit |
| 279 | // positions indicated by the members of the CpuFeature enum from globals.h |
| 280 | static uint64_t CpuFeaturesImpliedByPlatform(); |
| 281 | |
lrn@chromium.org | ac2828d | 2011-06-23 06:29:21 +0000 | [diff] [blame] | 282 | // Maximum size of the virtual memory. 0 means there is no artificial |
| 283 | // limit. |
| 284 | static intptr_t MaxVirtualMemory(); |
| 285 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 286 | // Returns the double constant NAN |
| 287 | static double nan_value(); |
| 288 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 289 | // Support runtime detection of VFP3 on ARM CPUs. |
| 290 | static bool ArmCpuHasFeature(CpuFeature feature); |
| 291 | |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 292 | // Support runtime detection of whether the hard float option of the |
| 293 | // EABI is used. |
| 294 | static bool ArmUsingHardFloat(); |
| 295 | |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 296 | // Support runtime detection of FPU on MIPS CPUs. |
| 297 | static bool MipsCpuHasFeature(CpuFeature feature); |
| 298 | |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 299 | // Returns the activation frame alignment constraint or zero if |
| 300 | // the platform doesn't care. Guaranteed to be a power of two. |
| 301 | static int ActivationFrameAlignment(); |
| 302 | |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 303 | static void ReleaseStore(volatile AtomicWord* ptr, AtomicWord value); |
| 304 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 305 | #if defined(V8_TARGET_ARCH_IA32) |
| 306 | // Copy memory area to disjoint memory area. |
| 307 | static void MemCopy(void* dest, const void* src, size_t size); |
| 308 | // Limit below which the extra overhead of the MemCopy function is likely |
| 309 | // to outweigh the benefits of faster copying. |
| 310 | static const int kMinComplexMemCopy = 64; |
| 311 | typedef void (*MemCopyFunction)(void* dest, const void* src, size_t size); |
| 312 | |
| 313 | #else // V8_TARGET_ARCH_IA32 |
| 314 | static void MemCopy(void* dest, const void* src, size_t size) { |
| 315 | memcpy(dest, src, size); |
| 316 | } |
| 317 | static const int kMinComplexMemCopy = 256; |
| 318 | #endif // V8_TARGET_ARCH_IA32 |
| 319 | |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 320 | static int GetCurrentProcessId(); |
| 321 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 322 | private: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 323 | static const int msPerSecond = 1000; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 324 | |
| 325 | DISALLOW_IMPLICIT_CONSTRUCTORS(OS); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 326 | }; |
| 327 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 328 | // Represents and controls an area of reserved memory. |
| 329 | // Control of the reserved memory can be assigned to another VirtualMemory |
| 330 | // object by assignment or copy-contructing. This removes the reserved memory |
| 331 | // from the original object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 332 | class VirtualMemory { |
| 333 | public: |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 334 | // Empty VirtualMemory object, controlling no reserved memory. |
| 335 | VirtualMemory(); |
| 336 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 337 | // Reserves virtual memory with size. |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 338 | explicit VirtualMemory(size_t size); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 339 | |
| 340 | // Reserves virtual memory containing an area of the given size that |
| 341 | // is aligned per alignment. This may not be at the position returned |
| 342 | // by address(). |
| 343 | VirtualMemory(size_t size, size_t alignment); |
| 344 | |
| 345 | // Releases the reserved memory, if any, controlled by this VirtualMemory |
| 346 | // object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 347 | ~VirtualMemory(); |
| 348 | |
| 349 | // Returns whether the memory has been reserved. |
| 350 | bool IsReserved(); |
| 351 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 352 | // Initialize or resets an embedded VirtualMemory object. |
| 353 | void Reset(); |
| 354 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 355 | // Returns the start address of the reserved memory. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 356 | // If the memory was reserved with an alignment, this address is not |
| 357 | // necessarily aligned. The user might need to round it up to a multiple of |
| 358 | // the alignment to get the start of the aligned block. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 359 | void* address() { |
| 360 | ASSERT(IsReserved()); |
| 361 | return address_; |
kmillikin@chromium.org | 3cdd9e1 | 2010-09-06 11:39:48 +0000 | [diff] [blame] | 362 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 363 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 364 | // Returns the size of the reserved memory. The returned value is only |
| 365 | // meaningful when IsReserved() returns true. |
| 366 | // If the memory was reserved with an alignment, this size may be larger |
| 367 | // than the requested size. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 368 | size_t size() { return size_; } |
| 369 | |
| 370 | // Commits real memory. Returns whether the operation succeeded. |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 371 | bool Commit(void* address, size_t size, bool is_executable); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 372 | |
| 373 | // Uncommit real memory. Returns whether the operation succeeded. |
| 374 | bool Uncommit(void* address, size_t size); |
| 375 | |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 376 | // Creates a single guard page at the given address. |
| 377 | bool Guard(void* address); |
| 378 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 379 | void Release() { |
| 380 | ASSERT(IsReserved()); |
| 381 | // Notice: Order is important here. The VirtualMemory object might live |
| 382 | // inside the allocated region. |
| 383 | void* address = address_; |
| 384 | size_t size = size_; |
| 385 | Reset(); |
| 386 | bool result = ReleaseRegion(address, size); |
| 387 | USE(result); |
| 388 | ASSERT(result); |
| 389 | } |
| 390 | |
| 391 | // Assign control of the reserved region to a different VirtualMemory object. |
| 392 | // The old object is no longer functional (IsReserved() returns false). |
| 393 | void TakeControl(VirtualMemory* from) { |
| 394 | ASSERT(!IsReserved()); |
| 395 | address_ = from->address_; |
| 396 | size_ = from->size_; |
| 397 | from->Reset(); |
| 398 | } |
| 399 | |
| 400 | static void* ReserveRegion(size_t size); |
| 401 | |
| 402 | static bool CommitRegion(void* base, size_t size, bool is_executable); |
| 403 | |
| 404 | static bool UncommitRegion(void* base, size_t size); |
| 405 | |
| 406 | // Must be called with a base pointer that has been returned by ReserveRegion |
| 407 | // and the same size it was reserved with. |
| 408 | static bool ReleaseRegion(void* base, size_t size); |
| 409 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 410 | private: |
| 411 | void* address_; // Start address of the virtual memory. |
| 412 | size_t size_; // Size of the virtual memory. |
| 413 | }; |
| 414 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 415 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 416 | // ---------------------------------------------------------------------------- |
| 417 | // Thread |
| 418 | // |
| 419 | // Thread objects are used for creating and running threads. When the start() |
| 420 | // method is called the new thread starts running the run() method in the new |
| 421 | // thread. The Thread object should not be deallocated before the thread has |
| 422 | // terminated. |
| 423 | |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 424 | class Thread { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 425 | public: |
| 426 | // Opaque data type for thread-local storage keys. |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 427 | // LOCAL_STORAGE_KEY_MIN_VALUE and LOCAL_STORAGE_KEY_MAX_VALUE are specified |
| 428 | // to ensure that enumeration type has correct value range (see Issue 830 for |
| 429 | // more details). |
| 430 | enum LocalStorageKey { |
| 431 | LOCAL_STORAGE_KEY_MIN_VALUE = kMinInt, |
| 432 | LOCAL_STORAGE_KEY_MAX_VALUE = kMaxInt |
| 433 | }; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 434 | |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 435 | class Options { |
| 436 | public: |
| 437 | Options() : name_("v8:<unknown>"), stack_size_(0) {} |
| 438 | Options(const char* name, int stack_size = 0) |
| 439 | : name_(name), stack_size_(stack_size) {} |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 440 | |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 441 | const char* name() const { return name_; } |
| 442 | int stack_size() const { return stack_size_; } |
| 443 | |
| 444 | private: |
| 445 | const char* name_; |
| 446 | int stack_size_; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 447 | }; |
| 448 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 449 | // Create new thread. |
| 450 | explicit Thread(const Options& options); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 451 | virtual ~Thread(); |
| 452 | |
| 453 | // Start new thread by calling the Run() method in the new thread. |
| 454 | void Start(); |
| 455 | |
| 456 | // Wait until thread terminates. |
| 457 | void Join(); |
| 458 | |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 459 | inline const char* name() const { |
| 460 | return name_; |
| 461 | } |
| 462 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 463 | // Abstract method for run handler. |
| 464 | virtual void Run() = 0; |
| 465 | |
| 466 | // Thread-local storage. |
| 467 | static LocalStorageKey CreateThreadLocalKey(); |
| 468 | static void DeleteThreadLocalKey(LocalStorageKey key); |
| 469 | static void* GetThreadLocal(LocalStorageKey key); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 470 | static int GetThreadLocalInt(LocalStorageKey key) { |
| 471 | return static_cast<int>(reinterpret_cast<intptr_t>(GetThreadLocal(key))); |
| 472 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 473 | static void SetThreadLocal(LocalStorageKey key, void* value); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 474 | static void SetThreadLocalInt(LocalStorageKey key, int value) { |
| 475 | SetThreadLocal(key, reinterpret_cast<void*>(static_cast<intptr_t>(value))); |
| 476 | } |
| 477 | static bool HasThreadLocal(LocalStorageKey key) { |
| 478 | return GetThreadLocal(key) != NULL; |
| 479 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 480 | |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 481 | #ifdef V8_FAST_TLS_SUPPORTED |
| 482 | static inline void* GetExistingThreadLocal(LocalStorageKey key) { |
| 483 | void* result = reinterpret_cast<void*>( |
| 484 | InternalGetExistingThreadLocal(static_cast<intptr_t>(key))); |
| 485 | ASSERT(result == GetThreadLocal(key)); |
| 486 | return result; |
| 487 | } |
| 488 | #else |
| 489 | static inline void* GetExistingThreadLocal(LocalStorageKey key) { |
| 490 | return GetThreadLocal(key); |
| 491 | } |
| 492 | #endif |
| 493 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 494 | // A hint to the scheduler to let another thread run. |
| 495 | static void YieldCPU(); |
| 496 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 497 | |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 498 | // The thread name length is limited to 16 based on Linux's implementation of |
| 499 | // prctl(). |
| 500 | static const int kMaxThreadNameLength = 16; |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 501 | |
| 502 | class PlatformData; |
| 503 | PlatformData* data() { return data_; } |
| 504 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 505 | private: |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 506 | void set_name(const char* name); |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 507 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 508 | PlatformData* data_; |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 509 | |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 510 | char name_[kMaxThreadNameLength]; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 511 | int stack_size_; |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 512 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 513 | DISALLOW_COPY_AND_ASSIGN(Thread); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 514 | }; |
| 515 | |
| 516 | |
| 517 | // ---------------------------------------------------------------------------- |
| 518 | // Mutex |
| 519 | // |
| 520 | // Mutexes are used for serializing access to non-reentrant sections of code. |
| 521 | // The implementations of mutex should allow for nested/recursive locking. |
| 522 | |
| 523 | class Mutex { |
| 524 | public: |
| 525 | virtual ~Mutex() {} |
| 526 | |
| 527 | // Locks the given mutex. If the mutex is currently unlocked, it becomes |
| 528 | // locked and owned by the calling thread, and immediately. If the mutex |
| 529 | // is already locked by another thread, suspends the calling thread until |
| 530 | // the mutex is unlocked. |
| 531 | virtual int Lock() = 0; |
| 532 | |
| 533 | // Unlocks the given mutex. The mutex is assumed to be locked and owned by |
| 534 | // the calling thread on entrance. |
| 535 | virtual int Unlock() = 0; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 536 | |
| 537 | // Tries to lock the given mutex. Returns whether the mutex was |
| 538 | // successfully locked. |
| 539 | virtual bool TryLock() = 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 540 | }; |
| 541 | |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 542 | struct CreateMutexTrait { |
| 543 | static Mutex* Create() { |
| 544 | return OS::CreateMutex(); |
| 545 | } |
| 546 | }; |
| 547 | |
| 548 | // POD Mutex initialized lazily (i.e. the first time Pointer() is called). |
| 549 | // Usage: |
| 550 | // static LazyMutex my_mutex = LAZY_MUTEX_INITIALIZER; |
| 551 | // |
| 552 | // void my_function() { |
| 553 | // ScopedLock my_lock(my_mutex.Pointer()); |
| 554 | // // Do something. |
| 555 | // } |
| 556 | // |
danno@chromium.org | 8c0a43f | 2012-04-03 08:37:53 +0000 | [diff] [blame] | 557 | typedef LazyDynamicInstance< |
| 558 | Mutex, CreateMutexTrait, ThreadSafeInitOnceTrait>::type LazyMutex; |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 559 | |
| 560 | #define LAZY_MUTEX_INITIALIZER LAZY_DYNAMIC_INSTANCE_INITIALIZER |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 561 | |
| 562 | // ---------------------------------------------------------------------------- |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 563 | // ScopedLock |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 564 | // |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 565 | // Stack-allocated ScopedLocks provide block-scoped locking and |
| 566 | // unlocking of a mutex. |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 567 | class ScopedLock { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 568 | public: |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 569 | explicit ScopedLock(Mutex* mutex): mutex_(mutex) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 570 | ASSERT(mutex_ != NULL); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 571 | mutex_->Lock(); |
| 572 | } |
| 573 | ~ScopedLock() { |
| 574 | mutex_->Unlock(); |
| 575 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 576 | |
| 577 | private: |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 578 | Mutex* mutex_; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 579 | DISALLOW_COPY_AND_ASSIGN(ScopedLock); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 580 | }; |
| 581 | |
| 582 | |
| 583 | // ---------------------------------------------------------------------------- |
| 584 | // Semaphore |
| 585 | // |
| 586 | // A semaphore object is a synchronization object that maintains a count. The |
| 587 | // count is decremented each time a thread completes a wait for the semaphore |
| 588 | // object and incremented each time a thread signals the semaphore. When the |
| 589 | // count reaches zero, threads waiting for the semaphore blocks until the |
| 590 | // count becomes non-zero. |
| 591 | |
| 592 | class Semaphore { |
| 593 | public: |
| 594 | virtual ~Semaphore() {} |
| 595 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 596 | // Suspends the calling thread until the semaphore counter is non zero |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 597 | // and then decrements the semaphore counter. |
| 598 | virtual void Wait() = 0; |
| 599 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 600 | // Suspends the calling thread until the counter is non zero or the timeout |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 601 | // time has passed. If timeout happens the return value is false and the |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 602 | // counter is unchanged. Otherwise the semaphore counter is decremented and |
| 603 | // true is returned. The timeout value is specified in microseconds. |
| 604 | virtual bool Wait(int timeout) = 0; |
| 605 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 606 | // Increments the semaphore counter. |
| 607 | virtual void Signal() = 0; |
| 608 | }; |
| 609 | |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 610 | template <int InitialValue> |
| 611 | struct CreateSemaphoreTrait { |
| 612 | static Semaphore* Create() { |
| 613 | return OS::CreateSemaphore(InitialValue); |
| 614 | } |
| 615 | }; |
| 616 | |
| 617 | // POD Semaphore initialized lazily (i.e. the first time Pointer() is called). |
| 618 | // Usage: |
| 619 | // // The following semaphore starts at 0. |
| 620 | // static LazySemaphore<0>::type my_semaphore = LAZY_SEMAPHORE_INITIALIZER; |
| 621 | // |
| 622 | // void my_function() { |
| 623 | // // Do something with my_semaphore.Pointer(). |
| 624 | // } |
| 625 | // |
| 626 | template <int InitialValue> |
| 627 | struct LazySemaphore { |
| 628 | typedef typename LazyDynamicInstance< |
danno@chromium.org | 8c0a43f | 2012-04-03 08:37:53 +0000 | [diff] [blame] | 629 | Semaphore, CreateSemaphoreTrait<InitialValue>, |
| 630 | ThreadSafeInitOnceTrait>::type type; |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 631 | }; |
| 632 | |
| 633 | #define LAZY_SEMAPHORE_INITIALIZER LAZY_DYNAMIC_INSTANCE_INITIALIZER |
| 634 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 635 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 636 | // ---------------------------------------------------------------------------- |
| 637 | // Socket |
| 638 | // |
| 639 | |
| 640 | class Socket { |
| 641 | public: |
| 642 | virtual ~Socket() {} |
| 643 | |
| 644 | // Server initialization. |
| 645 | virtual bool Bind(const int port) = 0; |
| 646 | virtual bool Listen(int backlog) const = 0; |
| 647 | virtual Socket* Accept() const = 0; |
| 648 | |
| 649 | // Client initialization. |
| 650 | virtual bool Connect(const char* host, const char* port) = 0; |
| 651 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 652 | // Shutdown socket for both read and write. This causes blocking Send and |
| 653 | // Receive calls to exit. After Shutdown the Socket object cannot be used for |
| 654 | // any communication. |
| 655 | virtual bool Shutdown() = 0; |
| 656 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 657 | // Data Transimission |
ulan@chromium.org | 0e3f88b | 2012-05-22 09:16:05 +0000 | [diff] [blame] | 658 | // Return 0 on failure. |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 659 | virtual int Send(const char* data, int len) const = 0; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 660 | virtual int Receive(char* data, int len) const = 0; |
| 661 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 662 | // Set the value of the SO_REUSEADDR socket option. |
| 663 | virtual bool SetReuseAddress(bool reuse_address) = 0; |
| 664 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 665 | virtual bool IsValid() const = 0; |
| 666 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 667 | static bool SetUp(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 668 | static int LastError(); |
| 669 | static uint16_t HToN(uint16_t value); |
| 670 | static uint16_t NToH(uint16_t value); |
| 671 | static uint32_t HToN(uint32_t value); |
| 672 | static uint32_t NToH(uint32_t value); |
| 673 | }; |
| 674 | |
| 675 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 676 | // ---------------------------------------------------------------------------- |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 677 | // Sampler |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 678 | // |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 679 | // A sampler periodically samples the state of the VM and optionally |
| 680 | // (if used for profiling) the program counter and stack pointer for |
| 681 | // the thread that created it. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 682 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 683 | // TickSample captures the information collected for each sample. |
| 684 | class TickSample { |
| 685 | public: |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 686 | TickSample() |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 687 | : state(OTHER), |
| 688 | pc(NULL), |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 689 | sp(NULL), |
| 690 | fp(NULL), |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 691 | tos(NULL), |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 692 | frames_count(0), |
| 693 | has_external_callback(false) {} |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 694 | StateTag state; // The state of the VM. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 695 | Address pc; // Instruction pointer. |
| 696 | Address sp; // Stack pointer. |
| 697 | Address fp; // Frame pointer. |
| 698 | union { |
| 699 | Address tos; // Top stack value (*sp). |
| 700 | Address external_callback; |
| 701 | }; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 702 | static const int kMaxFramesCount = 64; |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 703 | Address stack[kMaxFramesCount]; // Call stack. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 704 | int frames_count : 8; // Number of captured frames. |
| 705 | bool has_external_callback : 1; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 706 | }; |
| 707 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 708 | class Sampler { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 709 | public: |
| 710 | // Initialize sampler. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 711 | Sampler(Isolate* isolate, int interval); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 712 | virtual ~Sampler(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 713 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 714 | int interval() const { return interval_; } |
| 715 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 716 | // Performs stack sampling. |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 717 | void SampleStack(TickSample* sample) { |
| 718 | DoSampleStack(sample); |
| 719 | IncSamplesTaken(); |
| 720 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 721 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 722 | // This method is called for each sampling period with the current |
| 723 | // program counter. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 724 | virtual void Tick(TickSample* sample) = 0; |
| 725 | |
| 726 | // Start and stop sampler. |
| 727 | void Start(); |
| 728 | void Stop(); |
| 729 | |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 730 | // Is the sampler used for profiling? |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 731 | bool IsProfiling() const { return NoBarrier_Load(&profiling_) > 0; } |
| 732 | void IncreaseProfilingDepth() { NoBarrier_AtomicIncrement(&profiling_, 1); } |
| 733 | void DecreaseProfilingDepth() { NoBarrier_AtomicIncrement(&profiling_, -1); } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 734 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 735 | // Whether the sampler is running (that is, consumes resources). |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 736 | bool IsActive() const { return NoBarrier_Load(&active_); } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 737 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 738 | Isolate* isolate() { return isolate_; } |
| 739 | |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 740 | // Used in tests to make sure that stack sampling is performed. |
| 741 | int samples_taken() const { return samples_taken_; } |
| 742 | void ResetSamplesTaken() { samples_taken_ = 0; } |
| 743 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 744 | class PlatformData; |
ricow@chromium.org | badaffc | 2011-03-17 12:15:27 +0000 | [diff] [blame] | 745 | PlatformData* data() { return data_; } |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 746 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 747 | PlatformData* platform_data() { return data_; } |
| 748 | |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 749 | protected: |
| 750 | virtual void DoSampleStack(TickSample* sample) = 0; |
| 751 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 752 | private: |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 753 | void SetActive(bool value) { NoBarrier_Store(&active_, value); } |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 754 | void IncSamplesTaken() { if (++samples_taken_ < 0) samples_taken_ = 0; } |
| 755 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 756 | Isolate* isolate_; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 757 | const int interval_; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 758 | Atomic32 profiling_; |
| 759 | Atomic32 active_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 760 | PlatformData* data_; // Platform specific data. |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 761 | int samples_taken_; // Counts stack samples taken. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 762 | DISALLOW_IMPLICIT_CONSTRUCTORS(Sampler); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 763 | }; |
| 764 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 765 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 766 | } } // namespace v8::internal |
| 767 | |
| 768 | #endif // V8_PLATFORM_H_ |