WindowServer: Port threading to LibThread

This commit is contained in:
Sergey Bugaev 2019-08-25 19:28:09 +03:00 committed by Andreas Kling
parent 1ac7fedefe
commit 8d59b10022
Notes: sideshowbarker 2024-07-19 12:31:08 +09:00
5 changed files with 43 additions and 53 deletions

View file

@ -26,7 +26,7 @@ DEFINES += -DUSERLAND
all: $(APP)
$(APP): $(OBJS)
$(LD) -o $(APP) $(LDFLAGS) $(OBJS) -lc -lcore -ldraw
$(LD) -o $(APP) $(LDFLAGS) $(OBJS) -lc -lcore -ldraw -lthread
.cpp.o:
@echo "CXX $<"; $(CXX) $(CXXFLAGS) -o $@ -c $<

View file

@ -9,26 +9,31 @@
#include <unistd.h>
WSCPUMonitor::WSCPUMonitor()
: m_thread([this] {
monitor();
return 0;
})
{
create_thread([](void* context) -> int {
auto& monitor = *(WSCPUMonitor*)context;
for (;;) {
static unsigned last_busy;
static unsigned last_idle;
unsigned busy;
unsigned idle;
monitor.get_cpu_usage(busy, idle);
unsigned busy_diff = busy - last_busy;
unsigned idle_diff = idle - last_idle;
last_busy = busy;
last_idle = idle;
float cpu = (float)busy_diff / (float)(busy_diff + idle_diff);
monitor.m_cpu_history.enqueue(cpu);
monitor.m_dirty = true;
sleep(1);
}
},
this);
m_thread.start();
}
void WSCPUMonitor::monitor()
{
for (;;) {
static unsigned last_busy;
static unsigned last_idle;
unsigned busy;
unsigned idle;
get_cpu_usage(busy, idle);
unsigned busy_diff = busy - last_busy;
unsigned idle_diff = idle - last_idle;
last_busy = busy;
last_idle = idle;
float cpu = (float)busy_diff / (float)(busy_diff + idle_diff);
m_cpu_history.enqueue(cpu);
m_dirty = true;
sleep(1);
}
}
void WSCPUMonitor::get_cpu_usage(unsigned& busy, unsigned& idle)

View file

@ -1,6 +1,7 @@
#pragma once
#include <AK/CircularQueue.h>
#include <LibThread/Thread.h>
#include <stdio.h>
class Painter;
@ -16,8 +17,11 @@ public:
void paint(Painter&, const Rect&);
private:
void monitor();
void get_cpu_usage(unsigned& busy, unsigned& idle);
CircularQueue<float, 30> m_cpu_history;
bool m_dirty { false };
LibThread::Thread m_thread;
};

View file

@ -7,6 +7,7 @@
#include <LibDraw/Font.h>
#include <LibDraw/PNGLoader.h>
#include <LibDraw/Painter.h>
#include <LibThread/BackgroundAction.h>
// #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)
{
struct Context {
String path;
RefPtr<GraphicsBitmap> bitmap;
Function<void(bool)> callback;
};
auto context = make<Context>();
context->path = path;
context->callback = move(callback);
LibThread::BackgroundAction<RefPtr<GraphicsBitmap>>::create(
[path] {
return load_png(path);
},
int rc = create_thread([](void* ctx) -> int {
OwnPtr<Context> context((Context*)ctx);
context->bitmap = load_png(context->path);
if (!context->bitmap) {
context->callback(false);
exit_thread(0);
return 0;
}
the().deferred_invoke([context = move(context)](auto&) {
the().finish_setting_wallpaper(context->path, *context->bitmap);
context->callback(true);
[this, path, callback = move(callback)](RefPtr<GraphicsBitmap> bitmap) {
if (!bitmap) {
callback(false);
return;
}
m_wallpaper_path = path;
m_wallpaper = move(bitmap);
invalidate();
callback(true);
});
exit_thread(0);
return 0;
},
context.leak_ptr());
ASSERT(rc > 0);
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()
{
ASSERT(m_screen_can_set_buffer);

View file

@ -43,7 +43,6 @@ private:
void draw_cursor();
void draw_geometry_label();
void draw_menubar();
void finish_setting_wallpaper(const String& path, NonnullRefPtr<GraphicsBitmap>&&);
unsigned m_compose_count { 0 };
unsigned m_flush_count { 0 };