OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 1379 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1390 #endif | 1390 #endif |
1391 } | 1391 } |
1392 | 1392 |
1393 | 1393 |
1394 void OS::ReleaseStore(volatile AtomicWord* ptr, AtomicWord value) { | 1394 void OS::ReleaseStore(volatile AtomicWord* ptr, AtomicWord value) { |
1395 MemoryBarrier(); | 1395 MemoryBarrier(); |
1396 *ptr = value; | 1396 *ptr = value; |
1397 } | 1397 } |
1398 | 1398 |
1399 | 1399 |
| 1400 VirtualMemory::VirtualMemory() : address_(NULL), size_(0) { } |
| 1401 |
| 1402 |
| 1403 VirtualMemory::VirtualMemory(size_t size) |
| 1404 : address_(ReserveRegion(size)), size_(size) { } |
| 1405 |
| 1406 |
| 1407 VirtualMemory::VirtualMemory(size_t size, size_t alignment) |
| 1408 : address_(NULL), size_(0) { |
| 1409 ASSERT(IsAligned(alignment, static_cast<intptr_t>(OS::AllocateAlignment()))); |
| 1410 size_t request_size = RoundUp(size + alignment, |
| 1411 static_cast<intptr_t>(OS::AllocateAlignment())); |
| 1412 void* address = ReserveRegion(request_size); |
| 1413 if (address == NULL) return; |
| 1414 Address base = RoundUp(static_cast<Address>(address), alignment); |
| 1415 // Try reducing the size by freeing and then reallocating a specific area. |
| 1416 bool result = ReleaseRegion(address, request_size); |
| 1417 USE(result); |
| 1418 ASSERT(result); |
| 1419 address = VirtualAlloc(base, size, MEM_RESERVE, PAGE_NOACCESS); |
| 1420 if (address != NULL) { |
| 1421 request_size = size; |
| 1422 ASSERT(base == static_cast<Address>(address)); |
| 1423 } else { |
| 1424 // Resizing failed, just go with a bigger area. |
| 1425 address = ReserveRegion(request_size); |
| 1426 if (address == NULL) return; |
| 1427 } |
| 1428 address_ = address; |
| 1429 size_ = request_size; |
| 1430 } |
| 1431 |
| 1432 |
| 1433 VirtualMemory::~VirtualMemory() { |
| 1434 if (IsReserved()) { |
| 1435 bool result = ReleaseRegion(address_, size_); |
| 1436 ASSERT(result); |
| 1437 USE(result); |
| 1438 } |
| 1439 } |
| 1440 |
| 1441 |
1400 bool VirtualMemory::IsReserved() { | 1442 bool VirtualMemory::IsReserved() { |
1401 return address_ != NULL; | 1443 return address_ != NULL; |
1402 } | 1444 } |
1403 | 1445 |
1404 | 1446 |
1405 VirtualMemory::VirtualMemory(size_t size) { | 1447 void VirtualMemory::Reset() { |
1406 address_ = VirtualAlloc(NULL, size, MEM_RESERVE, PAGE_NOACCESS); | 1448 address_ = NULL; |
1407 size_ = size; | 1449 size_ = 0; |
1408 } | |
1409 | |
1410 | |
1411 VirtualMemory::~VirtualMemory() { | |
1412 if (IsReserved()) { | |
1413 if (0 == VirtualFree(address(), 0, MEM_RELEASE)) address_ = NULL; | |
1414 } | |
1415 } | 1450 } |
1416 | 1451 |
1417 | 1452 |
1418 bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) { | 1453 bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) { |
1419 int prot = is_executable ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE; | 1454 if (CommitRegion(address, size, is_executable)) { |
1420 if (NULL == VirtualAlloc(address, size, MEM_COMMIT, prot)) { | 1455 UpdateAllocatedSpaceLimits(address, static_cast<int>(size)); |
1421 return false; | 1456 return true; |
1422 } | 1457 } |
1423 | 1458 return false; |
1424 UpdateAllocatedSpaceLimits(address, static_cast<int>(size)); | |
1425 return true; | |
1426 } | 1459 } |
1427 | 1460 |
1428 | 1461 |
1429 bool VirtualMemory::Uncommit(void* address, size_t size) { | 1462 bool VirtualMemory::Uncommit(void* address, size_t size) { |
1430 ASSERT(IsReserved()); | 1463 ASSERT(IsReserved()); |
1431 return VirtualFree(address, size, MEM_DECOMMIT) != false; | 1464 return UncommitRegion(address, size); |
1432 } | 1465 } |
1433 | 1466 |
1434 | 1467 |
| 1468 void* VirtualMemory::ReserveRegion(size_t size) { |
| 1469 return VirtualAlloc(NULL, size, MEM_RESERVE, PAGE_NOACCESS); |
| 1470 } |
| 1471 |
| 1472 |
| 1473 bool VirtualMemory::CommitRegion(void* base, size_t size, bool is_executable) { |
| 1474 int prot = is_executable ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE; |
| 1475 if (NULL == VirtualAlloc(base, size, MEM_COMMIT, prot)) { |
| 1476 return false; |
| 1477 } |
| 1478 |
| 1479 UpdateAllocatedSpaceLimits(base, static_cast<int>(size)); |
| 1480 return true; |
| 1481 } |
| 1482 |
| 1483 |
| 1484 bool VirtualMemory::UncommitRegion(void* base, size_t size) { |
| 1485 return VirtualFree(base, size, MEM_DECOMMIT) != 0; |
| 1486 } |
| 1487 |
| 1488 |
| 1489 bool VirtualMemory::ReleaseRegion(void* base, size_t size) { |
| 1490 return VirtualFree(base, 0, MEM_RELEASE) != 0; |
| 1491 } |
| 1492 |
| 1493 |
| 1494 |
1435 // ---------------------------------------------------------------------------- | 1495 // ---------------------------------------------------------------------------- |
1436 // Win32 thread support. | 1496 // Win32 thread support. |
1437 | 1497 |
1438 // Definition of invalid thread handle and id. | 1498 // Definition of invalid thread handle and id. |
1439 static const HANDLE kNoThread = INVALID_HANDLE_VALUE; | 1499 static const HANDLE kNoThread = INVALID_HANDLE_VALUE; |
1440 | 1500 |
1441 // Entry point for threads. The supplied argument is a pointer to the thread | 1501 // Entry point for threads. The supplied argument is a pointer to the thread |
1442 // object. The entry function dispatches to the run method in the thread | 1502 // object. The entry function dispatches to the run method in the thread |
1443 // object. It is important that this function has __stdcall calling | 1503 // object. It is important that this function has __stdcall calling |
1444 // convention. | 1504 // convention. |
1445 static unsigned int __stdcall ThreadEntry(void* arg) { | 1505 static unsigned int __stdcall ThreadEntry(void* arg) { |
1446 Thread* thread = reinterpret_cast<Thread*>(arg); | 1506 Thread* thread = reinterpret_cast<Thread*>(arg); |
1447 thread->Run(); | 1507 thread->Run(); |
1448 return 0; | 1508 return 0; |
1449 } | 1509 } |
1450 | 1510 |
1451 | 1511 |
1452 class Thread::PlatformData : public Malloced { | 1512 class Thread::PlatformData : public Malloced { |
1453 public: | 1513 public: |
1454 explicit PlatformData(HANDLE thread) : thread_(thread) {} | 1514 explicit PlatformData(HANDLE thread) : thread_(thread) {} |
1455 HANDLE thread_; | 1515 HANDLE thread_; |
| 1516 unsigned thread_id_; |
1456 }; | 1517 }; |
1457 | 1518 |
1458 | 1519 |
1459 // Initialize a Win32 thread object. The thread has an invalid thread | 1520 // Initialize a Win32 thread object. The thread has an invalid thread |
1460 // handle until it is started. | 1521 // handle until it is started. |
1461 | 1522 |
1462 Thread::Thread(const Options& options) | 1523 Thread::Thread(const Options& options) |
1463 : stack_size_(options.stack_size) { | 1524 : stack_size_(options.stack_size) { |
1464 data_ = new PlatformData(kNoThread); | 1525 data_ = new PlatformData(kNoThread); |
1465 set_name(options.name); | 1526 set_name(options.name); |
(...skipping 23 matching lines...) Expand all Loading... |
1489 // Create a new thread. It is important to use _beginthreadex() instead of | 1550 // Create a new thread. It is important to use _beginthreadex() instead of |
1490 // the Win32 function CreateThread(), because the CreateThread() does not | 1551 // the Win32 function CreateThread(), because the CreateThread() does not |
1491 // initialize thread specific structures in the C runtime library. | 1552 // initialize thread specific structures in the C runtime library. |
1492 void Thread::Start() { | 1553 void Thread::Start() { |
1493 data_->thread_ = reinterpret_cast<HANDLE>( | 1554 data_->thread_ = reinterpret_cast<HANDLE>( |
1494 _beginthreadex(NULL, | 1555 _beginthreadex(NULL, |
1495 static_cast<unsigned>(stack_size_), | 1556 static_cast<unsigned>(stack_size_), |
1496 ThreadEntry, | 1557 ThreadEntry, |
1497 this, | 1558 this, |
1498 0, | 1559 0, |
1499 NULL)); | 1560 &data_->thread_id_)); |
1500 } | 1561 } |
1501 | 1562 |
1502 | 1563 |
1503 // Wait for thread to terminate. | 1564 // Wait for thread to terminate. |
1504 void Thread::Join() { | 1565 void Thread::Join() { |
1505 WaitForSingleObject(data_->thread_, INFINITE); | 1566 if (data_->thread_id_ != GetCurrentThreadId()) { |
| 1567 WaitForSingleObject(data_->thread_, INFINITE); |
| 1568 } |
1506 } | 1569 } |
1507 | 1570 |
1508 | 1571 |
1509 Thread::LocalStorageKey Thread::CreateThreadLocalKey() { | 1572 Thread::LocalStorageKey Thread::CreateThreadLocalKey() { |
1510 DWORD result = TlsAlloc(); | 1573 DWORD result = TlsAlloc(); |
1511 ASSERT(result != TLS_OUT_OF_INDEXES); | 1574 ASSERT(result != TLS_OUT_OF_INDEXES); |
1512 return static_cast<LocalStorageKey>(result); | 1575 return static_cast<LocalStorageKey>(result); |
1513 } | 1576 } |
1514 | 1577 |
1515 | 1578 |
(...skipping 454 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1970 | 2033 |
1971 | 2034 |
1972 void Sampler::Stop() { | 2035 void Sampler::Stop() { |
1973 ASSERT(IsActive()); | 2036 ASSERT(IsActive()); |
1974 SamplerThread::RemoveActiveSampler(this); | 2037 SamplerThread::RemoveActiveSampler(this); |
1975 SetActive(false); | 2038 SetActive(false); |
1976 } | 2039 } |
1977 | 2040 |
1978 | 2041 |
1979 } } // namespace v8::internal | 2042 } } // namespace v8::internal |
OLD | NEW |