~ubuntu-branches/ubuntu/raring/qtwebkit-source/raring-proposed

« back to all changes in this revision

Viewing changes to Source/JavaScriptCore/heap/BlockAllocator.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-02-18 14:24:18 UTC
  • Revision ID: package-import@ubuntu.com-20130218142418-eon0jmjg3nj438uy
Tags: upstream-2.3
ImportĀ upstreamĀ versionĀ 2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2012 Apple Inc. All rights reserved.
 
3
 *
 
4
 * Redistribution and use in source and binary forms, with or without
 
5
 * modification, are permitted provided that the following conditions
 
6
 * are met:
 
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.
 
12
 *
 
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.
 
24
 */
 
25
 
 
26
#include "config.h"
 
27
#include "BlockAllocator.h"
 
28
 
 
29
#include "CopiedBlock.h"
 
30
#include "CopyWorkList.h"
 
31
#include "MarkedBlock.h"
 
32
#include "WeakBlock.h"
 
33
#include <wtf/CurrentTime.h>
 
34
 
 
35
namespace JSC {
 
36
 
 
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"))
 
46
{
 
47
    ASSERT(m_blockFreeingThread);
 
48
    m_regionLock.Init();
 
49
}
 
50
 
 
51
BlockAllocator::~BlockAllocator()
 
52
{
 
53
    releaseFreeRegions();
 
54
    {
 
55
        MutexLocker locker(m_emptyRegionConditionLock);
 
56
        m_blockFreeingThreadShouldQuit = true;
 
57
        m_emptyRegionCondition.broadcast();
 
58
    }
 
59
    waitForThreadCompletion(m_blockFreeingThread);
 
60
}
 
61
 
 
62
void BlockAllocator::releaseFreeRegions()
 
63
{
 
64
    while (true) {
 
65
        Region* region;
 
66
        {
 
67
            SpinLockHolder locker(&m_regionLock);
 
68
            if (!m_numberOfEmptyRegions)
 
69
                region = 0;
 
70
            else {
 
71
                region = m_emptyRegions.removeHead();
 
72
                ASSERT(region);
 
73
                m_numberOfEmptyRegions--;
 
74
            }
 
75
        }
 
76
        
 
77
        if (!region)
 
78
            break;
 
79
 
 
80
        delete region;
 
81
    }
 
82
}
 
83
 
 
84
void BlockAllocator::waitForRelativeTimeWhileHoldingLock(double relative)
 
85
{
 
86
    if (m_blockFreeingThreadShouldQuit)
 
87
        return;
 
88
 
 
89
    m_emptyRegionCondition.timedWait(m_emptyRegionConditionLock, currentTime() + relative);
 
90
}
 
91
 
 
92
void BlockAllocator::waitForRelativeTime(double relative)
 
93
{
 
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.
 
97
    
 
98
    MutexLocker locker(m_emptyRegionConditionLock);
 
99
    waitForRelativeTimeWhileHoldingLock(relative);
 
100
}
 
101
 
 
102
void BlockAllocator::blockFreeingThreadStartFunc(void* blockAllocator)
 
103
{
 
104
    static_cast<BlockAllocator*>(blockAllocator)->blockFreeingThreadMain();
 
105
}
 
106
 
 
107
void BlockAllocator::blockFreeingThreadMain()
 
108
{
 
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)
 
115
            break;
 
116
        
 
117
        if (m_isCurrentlyAllocating) {
 
118
            m_isCurrentlyAllocating = false;
 
119
            continue;
 
120
        }
 
121
 
 
122
        // Sleep until there is actually work to do rather than waking up every second to check.
 
123
        {
 
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);
 
129
                m_regionLock.Lock();
 
130
            }
 
131
            currentNumberOfEmptyRegions = m_numberOfEmptyRegions;
 
132
        }
 
133
        
 
134
        size_t desiredNumberOfEmptyRegions = currentNumberOfEmptyRegions / 2;
 
135
        
 
136
        while (!m_blockFreeingThreadShouldQuit) {
 
137
            Region* region;
 
138
            {
 
139
                SpinLockHolder locker(&m_regionLock);
 
140
                if (m_numberOfEmptyRegions <= desiredNumberOfEmptyRegions)
 
141
                    region = 0;
 
142
                else {
 
143
                    region = m_emptyRegions.removeHead();
 
144
                    ASSERT(region);
 
145
                    m_numberOfEmptyRegions--;
 
146
                }
 
147
            }
 
148
            
 
149
            if (!region)
 
150
                break;
 
151
            
 
152
            delete region;
 
153
        }
 
154
    }
 
155
}
 
156
 
 
157
} // namespace JSC