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 85 86 87 88 89 90 91 92
|
// 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/WebTaskRunner.h"
#include "platform/heap/GCTaskRunner.h"
#include "public/platform/Platform.h"
#include "public/platform/WebThread.h"
#include "wtf/Allocator.h"
#include "wtf/Noncopyable.h"
#include <memory>
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 std::unique_ptr<WebThreadSupportingGC> create(const char* name);
static std::unique_ptr<WebThreadSupportingGC> createForThread(WebThread*);
~WebThreadSupportingGC();
void postTask(const WebTraceLocation& location,
std::unique_ptr<WTF::Closure> task) {
m_thread->getWebTaskRunner()->postTask(location, std::move(task));
}
void postDelayedTask(const WebTraceLocation& location,
std::unique_ptr<WTF::Closure> task,
long long delayMs) {
m_thread->getWebTaskRunner()->postDelayedTask(location, std::move(task),
delayMs);
}
void postTask(const WebTraceLocation& location,
std::unique_ptr<CrossThreadClosure> task) {
m_thread->getWebTaskRunner()->postTask(location, std::move(task));
}
void postDelayedTask(const WebTraceLocation& location,
std::unique_ptr<CrossThreadClosure> task,
long long delayMs) {
m_thread->getWebTaskRunner()->postDelayedTask(location, std::move(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*);
std::unique_ptr<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;
std::unique_ptr<WebThread> m_owningThread;
};
} // namespace blink
#endif
|