[WTF] Drop Thread::create(obsolete things) API since we can use lambda
[WebKit-https.git] / Source / WebCore / page / ResourceUsageThread.cpp
1 /*
2  * Copyright (C) 2016 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 "ResourceUsageThread.h"
28
29 #if ENABLE(RESOURCE_USAGE)
30
31 #include "CommonVM.h"
32 #include "JSDOMWindow.h"
33 #include <thread>
34 #include <wtf/MainThread.h>
35 #include <wtf/Vector.h>
36
37 namespace WebCore {
38
39 ResourceUsageThread::ResourceUsageThread()
40 {
41 }
42
43 ResourceUsageThread& ResourceUsageThread::singleton()
44 {
45     static NeverDestroyed<ResourceUsageThread> resourceUsageThread;
46     return resourceUsageThread;
47 }
48
49 void ResourceUsageThread::addObserver(void* key, std::function<void (const ResourceUsageData&)> function)
50 {
51     auto& resourceUsageThread = ResourceUsageThread::singleton();
52     resourceUsageThread.createThreadIfNeeded();
53
54     {
55         LockHolder locker(resourceUsageThread.m_lock);
56         bool wasEmpty = resourceUsageThread.m_observers.isEmpty();
57         resourceUsageThread.m_observers.set(key, function);
58
59         if (wasEmpty)
60             resourceUsageThread.m_condition.notifyAll();
61     }
62 }
63
64 void ResourceUsageThread::removeObserver(void* key)
65 {
66     auto& resourceUsageThread = ResourceUsageThread::singleton();
67
68     {
69         LockHolder locker(resourceUsageThread.m_lock);
70         resourceUsageThread.m_observers.remove(key);
71     }
72 }
73
74 void ResourceUsageThread::waitUntilObservers()
75 {
76     LockHolder locker(m_lock);
77     while (m_observers.isEmpty())
78         m_condition.wait(m_lock);
79 }
80
81 void ResourceUsageThread::notifyObservers(ResourceUsageData&& data)
82 {
83     callOnMainThread([data = WTFMove(data)]() mutable {
84         Vector<std::function<void (const ResourceUsageData&)>> functions;
85         
86         {
87             auto& resourceUsageThread = ResourceUsageThread::singleton();
88             LockHolder locker(resourceUsageThread.m_lock);
89             copyValuesToVector(resourceUsageThread.m_observers, functions);
90         }
91
92         for (auto& function : functions)
93             function(data);
94     });
95 }
96
97 void ResourceUsageThread::createThreadIfNeeded()
98 {
99     if (m_thread)
100         return;
101
102     m_vm = &commonVM();
103     m_thread = Thread::create("WebCore: ResourceUsage", [this] {
104         threadBody();
105     });
106 }
107
108 NO_RETURN void ResourceUsageThread::threadBody()
109 {
110     using namespace std::literals::chrono_literals;
111
112     while (true) {
113         // Only do work if we have observers.
114         waitUntilObservers();
115
116         auto start = std::chrono::system_clock::now();
117
118         ResourceUsageData data;
119         platformThreadBody(m_vm, data);
120         notifyObservers(WTFMove(data));
121
122         auto duration = std::chrono::system_clock::now() - start;
123         auto difference = 500ms - duration;
124         std::this_thread::sleep_for(difference);
125     }
126 }
127
128 } // namespace WebCore
129
130 #endif // ENABLE(RESOURCE_USAGE)