summaryrefslogtreecommitdiffstats
path: root/third_party/WebKit/Source/platform/WebThreadSupportingGC.h
blob: 792faef93af740b9f7821f77cfae84f7fef43ba6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef WebThreadSupportingGC_h
#define WebThreadSupportingGC_h

#include "platform/heap/GCTaskRunner.h"
#include "public/platform/Platform.h"
#include "public/platform/WebTaskRunner.h"
#include "public/platform/WebThread.h"
#include "wtf/Allocator.h"
#include "wtf/Noncopyable.h"
#include "wtf/OwnPtr.h"
#include "wtf/PassOwnPtr.h"

namespace blink {

// WebThreadSupportingGC wraps a WebThread and adds support for attaching
// to and detaching from the Blink GC infrastructure. The initialize method
// must be called during initialization on the WebThread and before the
// thread allocates any objects managed by the Blink GC. The shutdown
// method must be called on the WebThread during shutdown when the thread
// no longer needs to access objects managed by the Blink GC.
//
// WebThreadSupportingGC usually internally creates and owns WebThread unless
// an existing WebThread is given via createForThread.
class PLATFORM_EXPORT WebThreadSupportingGC final {
    USING_FAST_MALLOC(WebThreadSupportingGC);
    WTF_MAKE_NONCOPYABLE(WebThreadSupportingGC);
public:
    static PassOwnPtr<WebThreadSupportingGC> create(const char* name);
    static PassOwnPtr<WebThreadSupportingGC> createForThread(WebThread*);
    ~WebThreadSupportingGC();

    void postTask(const WebTraceLocation& location, WebTaskRunner::Task* task)
    {
        m_thread->taskRunner()->postTask(location, task);
    }

    void postDelayedTask(const WebTraceLocation& location, WebTaskRunner::Task* task, long long delayMs)
    {
        m_thread->taskRunner()->postDelayedTask(location, task, delayMs);
    }

    bool isCurrentThread() const
    {
        return m_thread->isCurrentThread();
    }

    void addTaskObserver(WebThread::TaskObserver* observer)
    {
        m_thread->addTaskObserver(observer);
    }

    void removeTaskObserver(WebThread::TaskObserver* observer)
    {
        m_thread->removeTaskObserver(observer);
    }

    void initialize();
    void shutdown();

    WebThread& platformThread() const
    {
        ASSERT(m_thread);
        return *m_thread;
    }

private:
    WebThreadSupportingGC(const char* name, WebThread*);

    OwnPtr<GCTaskRunner> m_gcTaskRunner;

    // m_thread is guaranteed to be non-null after this instance is constructed.
    // m_owningThread is non-null unless this instance is constructed for an
    // existing thread via createForThread().
    WebThread* m_thread = nullptr;
    OwnPtr<WebThread> m_owningThread;
};

} // namespace blink

#endif