Block freeing thread should sleep indefinitely when there's no work to do
[WebKit-https.git] / Source / JavaScriptCore / heap / BlockAllocator.h
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 #ifndef BlockAllocator_h
27 #define BlockAllocator_h
28
29 #include "HeapBlock.h"
30 #include <wtf/DoublyLinkedList.h>
31 #include <wtf/Forward.h>
32 #include <wtf/PageAllocationAligned.h>
33 #include <wtf/TCSpinLock.h>
34 #include <wtf/Threading.h>
35
36 namespace JSC {
37
38 // Simple allocator to reduce VM cost by holding onto blocks of memory for
39 // short periods of time and then freeing them on a secondary thread.
40
41 class DeadBlock : public HeapBlock<DeadBlock> {
42 public:
43     static DeadBlock* create(const PageAllocationAligned&);
44
45 private:
46     DeadBlock(const PageAllocationAligned&);
47 };
48
49 inline DeadBlock::DeadBlock(const PageAllocationAligned& allocation)
50     : HeapBlock<DeadBlock>(allocation)
51 {
52 }
53
54 inline DeadBlock* DeadBlock::create(const PageAllocationAligned& allocation)
55 {
56     return new(NotNull, allocation.base()) DeadBlock(allocation);
57 }
58
59 class BlockAllocator {
60 public:
61     BlockAllocator();
62     ~BlockAllocator();
63
64     PageAllocationAligned allocate();
65     void deallocate(PageAllocationAligned);
66
67 private:
68     void waitForRelativeTimeWhileHoldingLock(double relative);
69     void waitForRelativeTime(double relative);
70
71     void blockFreeingThreadMain();
72     static void blockFreeingThreadStartFunc(void* heap);
73
74     void releaseFreeBlocks();
75
76     DoublyLinkedList<DeadBlock> m_freeBlocks;
77     size_t m_numberOfFreeBlocks;
78     bool m_isCurrentlyAllocating;
79     bool m_blockFreeingThreadShouldQuit;
80     SpinLock m_freeBlockLock;
81     Mutex m_freeBlockConditionLock;
82     ThreadCondition m_freeBlockCondition;
83     ThreadIdentifier m_blockFreeingThread;
84 };
85
86 inline PageAllocationAligned BlockAllocator::allocate()
87 {
88     {
89         SpinLockHolder locker(&m_freeBlockLock);
90         m_isCurrentlyAllocating = true;
91         if (m_numberOfFreeBlocks) {
92             ASSERT(!m_freeBlocks.isEmpty());
93             m_numberOfFreeBlocks--;
94             return DeadBlock::destroy(m_freeBlocks.removeHead());
95         }
96     }
97
98     ASSERT(m_freeBlocks.isEmpty());
99     PageAllocationAligned allocation = PageAllocationAligned::allocate(DeadBlock::s_blockSize, DeadBlock::s_blockSize, OSAllocator::JSGCHeapPages);
100     if (!static_cast<bool>(allocation))
101         CRASH();
102     return allocation;
103 }
104
105 inline void BlockAllocator::deallocate(PageAllocationAligned allocation)
106 {
107     size_t numberOfFreeBlocks;
108     {
109         SpinLockHolder locker(&m_freeBlockLock);
110         m_freeBlocks.push(DeadBlock::create(allocation));
111         numberOfFreeBlocks = m_numberOfFreeBlocks++;
112     }
113
114     if (!numberOfFreeBlocks) {
115         MutexLocker mutexLocker(m_freeBlockConditionLock);
116         m_freeBlockCondition.signal();
117     }
118 }
119
120 } // namespace JSC
121
122 #endif // BlockAllocator_h