mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-01-24 02:03:06 -05:00
WindowServer: Port threading to LibThread
This commit is contained in:
parent
1ac7fedefe
commit
8d59b10022
Notes:
sideshowbarker
2024-07-19 12:31:08 +09:00
Author: https://github.com/bugaevc Commit: https://github.com/SerenityOS/serenity/commit/8d59b100225 Pull-request: https://github.com/SerenityOS/serenity/pull/486 Reviewed-by: https://github.com/awesomekling
5 changed files with 43 additions and 53 deletions
|
@ -26,7 +26,7 @@ DEFINES += -DUSERLAND
|
||||||
all: $(APP)
|
all: $(APP)
|
||||||
|
|
||||||
$(APP): $(OBJS)
|
$(APP): $(OBJS)
|
||||||
$(LD) -o $(APP) $(LDFLAGS) $(OBJS) -lc -lcore -ldraw
|
$(LD) -o $(APP) $(LDFLAGS) $(OBJS) -lc -lcore -ldraw -lthread
|
||||||
|
|
||||||
.cpp.o:
|
.cpp.o:
|
||||||
@echo "CXX $<"; $(CXX) $(CXXFLAGS) -o $@ -c $<
|
@echo "CXX $<"; $(CXX) $(CXXFLAGS) -o $@ -c $<
|
||||||
|
|
|
@ -9,26 +9,31 @@
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
WSCPUMonitor::WSCPUMonitor()
|
WSCPUMonitor::WSCPUMonitor()
|
||||||
|
: m_thread([this] {
|
||||||
|
monitor();
|
||||||
|
return 0;
|
||||||
|
})
|
||||||
{
|
{
|
||||||
create_thread([](void* context) -> int {
|
m_thread.start();
|
||||||
auto& monitor = *(WSCPUMonitor*)context;
|
}
|
||||||
for (;;) {
|
|
||||||
static unsigned last_busy;
|
void WSCPUMonitor::monitor()
|
||||||
static unsigned last_idle;
|
{
|
||||||
unsigned busy;
|
for (;;) {
|
||||||
unsigned idle;
|
static unsigned last_busy;
|
||||||
monitor.get_cpu_usage(busy, idle);
|
static unsigned last_idle;
|
||||||
unsigned busy_diff = busy - last_busy;
|
unsigned busy;
|
||||||
unsigned idle_diff = idle - last_idle;
|
unsigned idle;
|
||||||
last_busy = busy;
|
get_cpu_usage(busy, idle);
|
||||||
last_idle = idle;
|
unsigned busy_diff = busy - last_busy;
|
||||||
float cpu = (float)busy_diff / (float)(busy_diff + idle_diff);
|
unsigned idle_diff = idle - last_idle;
|
||||||
monitor.m_cpu_history.enqueue(cpu);
|
last_busy = busy;
|
||||||
monitor.m_dirty = true;
|
last_idle = idle;
|
||||||
sleep(1);
|
float cpu = (float)busy_diff / (float)(busy_diff + idle_diff);
|
||||||
}
|
m_cpu_history.enqueue(cpu);
|
||||||
},
|
m_dirty = true;
|
||||||
this);
|
sleep(1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void WSCPUMonitor::get_cpu_usage(unsigned& busy, unsigned& idle)
|
void WSCPUMonitor::get_cpu_usage(unsigned& busy, unsigned& idle)
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <AK/CircularQueue.h>
|
#include <AK/CircularQueue.h>
|
||||||
|
#include <LibThread/Thread.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
class Painter;
|
class Painter;
|
||||||
|
@ -16,8 +17,11 @@ public:
|
||||||
void paint(Painter&, const Rect&);
|
void paint(Painter&, const Rect&);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void monitor();
|
||||||
|
|
||||||
void get_cpu_usage(unsigned& busy, unsigned& idle);
|
void get_cpu_usage(unsigned& busy, unsigned& idle);
|
||||||
|
|
||||||
CircularQueue<float, 30> m_cpu_history;
|
CircularQueue<float, 30> m_cpu_history;
|
||||||
bool m_dirty { false };
|
bool m_dirty { false };
|
||||||
|
LibThread::Thread m_thread;
|
||||||
};
|
};
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
#include <LibDraw/Font.h>
|
#include <LibDraw/Font.h>
|
||||||
#include <LibDraw/PNGLoader.h>
|
#include <LibDraw/PNGLoader.h>
|
||||||
#include <LibDraw/Painter.h>
|
#include <LibDraw/Painter.h>
|
||||||
|
#include <LibThread/BackgroundAction.h>
|
||||||
|
|
||||||
// #define COMPOSITOR_DEBUG
|
// #define COMPOSITOR_DEBUG
|
||||||
|
|
||||||
|
@ -265,43 +266,24 @@ void WSCompositor::invalidate(const Rect& a_rect)
|
||||||
|
|
||||||
bool WSCompositor::set_wallpaper(const String& path, Function<void(bool)>&& callback)
|
bool WSCompositor::set_wallpaper(const String& path, Function<void(bool)>&& callback)
|
||||||
{
|
{
|
||||||
struct Context {
|
LibThread::BackgroundAction<RefPtr<GraphicsBitmap>>::create(
|
||||||
String path;
|
[path] {
|
||||||
RefPtr<GraphicsBitmap> bitmap;
|
return load_png(path);
|
||||||
Function<void(bool)> callback;
|
},
|
||||||
};
|
|
||||||
auto context = make<Context>();
|
|
||||||
context->path = path;
|
|
||||||
context->callback = move(callback);
|
|
||||||
|
|
||||||
int rc = create_thread([](void* ctx) -> int {
|
[this, path, callback = move(callback)](RefPtr<GraphicsBitmap> bitmap) {
|
||||||
OwnPtr<Context> context((Context*)ctx);
|
if (!bitmap) {
|
||||||
context->bitmap = load_png(context->path);
|
callback(false);
|
||||||
if (!context->bitmap) {
|
return;
|
||||||
context->callback(false);
|
}
|
||||||
exit_thread(0);
|
m_wallpaper_path = path;
|
||||||
return 0;
|
m_wallpaper = move(bitmap);
|
||||||
}
|
invalidate();
|
||||||
the().deferred_invoke([context = move(context)](auto&) {
|
callback(true);
|
||||||
the().finish_setting_wallpaper(context->path, *context->bitmap);
|
|
||||||
context->callback(true);
|
|
||||||
});
|
});
|
||||||
exit_thread(0);
|
|
||||||
return 0;
|
|
||||||
},
|
|
||||||
context.leak_ptr());
|
|
||||||
ASSERT(rc > 0);
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void WSCompositor::finish_setting_wallpaper(const String& path, NonnullRefPtr<GraphicsBitmap>&& bitmap)
|
|
||||||
{
|
|
||||||
m_wallpaper_path = path;
|
|
||||||
m_wallpaper = move(bitmap);
|
|
||||||
invalidate();
|
|
||||||
}
|
|
||||||
|
|
||||||
void WSCompositor::flip_buffers()
|
void WSCompositor::flip_buffers()
|
||||||
{
|
{
|
||||||
ASSERT(m_screen_can_set_buffer);
|
ASSERT(m_screen_can_set_buffer);
|
||||||
|
|
|
@ -43,7 +43,6 @@ private:
|
||||||
void draw_cursor();
|
void draw_cursor();
|
||||||
void draw_geometry_label();
|
void draw_geometry_label();
|
||||||
void draw_menubar();
|
void draw_menubar();
|
||||||
void finish_setting_wallpaper(const String& path, NonnullRefPtr<GraphicsBitmap>&&);
|
|
||||||
|
|
||||||
unsigned m_compose_count { 0 };
|
unsigned m_compose_count { 0 };
|
||||||
unsigned m_flush_count { 0 };
|
unsigned m_flush_count { 0 };
|
||||||
|
|
Loading…
Add table
Reference in a new issue