2
* Copyright (C) 2012 Apple Inc. All rights reserved.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions
7
* 1. Redistributions of source code must retain the above copyright
8
* notice, this list of conditions and the following disclaimer.
9
* 2. Redistributions in binary form must reproduce the above copyright
10
* notice, this list of conditions and the following disclaimer in the
11
* documentation and/or other materials provided with the distribution.
13
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23
* THE POSSIBILITY OF SUCH DAMAGE.
27
#include "BlockAllocator.h"
29
#include "CopiedBlock.h"
30
#include "CopyWorkList.h"
31
#include "MarkedBlock.h"
32
#include "WeakBlock.h"
33
#include <wtf/CurrentTime.h>
37
BlockAllocator::BlockAllocator()
38
: m_copiedRegionSet(CopiedBlock::blockSize)
39
, m_markedRegionSet(MarkedBlock::blockSize)
40
, m_weakAndMarkStackRegionSet(WeakBlock::blockSize)
41
, m_workListRegionSet(CopyWorkListSegment::blockSize)
42
, m_numberOfEmptyRegions(0)
43
, m_isCurrentlyAllocating(false)
44
, m_blockFreeingThreadShouldQuit(false)
45
, m_blockFreeingThread(createThread(blockFreeingThreadStartFunc, this, "JavaScriptCore::BlockFree"))
47
ASSERT(m_blockFreeingThread);
51
BlockAllocator::~BlockAllocator()
55
MutexLocker locker(m_emptyRegionConditionLock);
56
m_blockFreeingThreadShouldQuit = true;
57
m_emptyRegionCondition.broadcast();
59
waitForThreadCompletion(m_blockFreeingThread);
62
void BlockAllocator::releaseFreeRegions()
67
SpinLockHolder locker(&m_regionLock);
68
if (!m_numberOfEmptyRegions)
71
region = m_emptyRegions.removeHead();
73
m_numberOfEmptyRegions--;
84
void BlockAllocator::waitForRelativeTimeWhileHoldingLock(double relative)
86
if (m_blockFreeingThreadShouldQuit)
89
m_emptyRegionCondition.timedWait(m_emptyRegionConditionLock, currentTime() + relative);
92
void BlockAllocator::waitForRelativeTime(double relative)
94
// If this returns early, that's fine, so long as it doesn't do it too
95
// frequently. It would only be a bug if this function failed to return
96
// when it was asked to do so.
98
MutexLocker locker(m_emptyRegionConditionLock);
99
waitForRelativeTimeWhileHoldingLock(relative);
102
void BlockAllocator::blockFreeingThreadStartFunc(void* blockAllocator)
104
static_cast<BlockAllocator*>(blockAllocator)->blockFreeingThreadMain();
107
void BlockAllocator::blockFreeingThreadMain()
109
size_t currentNumberOfEmptyRegions;
110
while (!m_blockFreeingThreadShouldQuit) {
111
// Generally wait for one second before scavenging free blocks. This
112
// may return early, particularly when we're being asked to quit.
113
waitForRelativeTime(1.0);
114
if (m_blockFreeingThreadShouldQuit)
117
if (m_isCurrentlyAllocating) {
118
m_isCurrentlyAllocating = false;
122
// Sleep until there is actually work to do rather than waking up every second to check.
124
MutexLocker locker(m_emptyRegionConditionLock);
125
SpinLockHolder regionLocker(&m_regionLock);
126
while (!m_numberOfEmptyRegions && !m_blockFreeingThreadShouldQuit) {
127
m_regionLock.Unlock();
128
m_emptyRegionCondition.wait(m_emptyRegionConditionLock);
131
currentNumberOfEmptyRegions = m_numberOfEmptyRegions;
134
size_t desiredNumberOfEmptyRegions = currentNumberOfEmptyRegions / 2;
136
while (!m_blockFreeingThreadShouldQuit) {
139
SpinLockHolder locker(&m_regionLock);
140
if (m_numberOfEmptyRegions <= desiredNumberOfEmptyRegions)
143
region = m_emptyRegions.removeHead();
145
m_numberOfEmptyRegions--;