diff --git a/Servers/WindowServer/Makefile b/Servers/WindowServer/Makefile index 01e2de476e..a1cbbd8d67 100644 --- a/Servers/WindowServer/Makefile +++ b/Servers/WindowServer/Makefile @@ -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 $< diff --git a/Servers/WindowServer/WSCPUMonitor.cpp b/Servers/WindowServer/WSCPUMonitor.cpp index c29d408c22..d1fb2d8653 100644 --- a/Servers/WindowServer/WSCPUMonitor.cpp +++ b/Servers/WindowServer/WSCPUMonitor.cpp @@ -9,26 +9,31 @@ #include 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) diff --git a/Servers/WindowServer/WSCPUMonitor.h b/Servers/WindowServer/WSCPUMonitor.h index 2a87bc1e90..d69e79cfe3 100644 --- a/Servers/WindowServer/WSCPUMonitor.h +++ b/Servers/WindowServer/WSCPUMonitor.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include class Painter; @@ -16,8 +17,11 @@ public: void paint(Painter&, const Rect&); private: + void monitor(); + void get_cpu_usage(unsigned& busy, unsigned& idle); CircularQueue m_cpu_history; bool m_dirty { false }; + LibThread::Thread m_thread; }; diff --git a/Servers/WindowServer/WSCompositor.cpp b/Servers/WindowServer/WSCompositor.cpp index 985e07d624..23096361d6 100644 --- a/Servers/WindowServer/WSCompositor.cpp +++ b/Servers/WindowServer/WSCompositor.cpp @@ -7,6 +7,7 @@ #include #include #include +#include // #define COMPOSITOR_DEBUG @@ -265,43 +266,24 @@ void WSCompositor::invalidate(const Rect& a_rect) bool WSCompositor::set_wallpaper(const String& path, Function&& callback) { - struct Context { - String path; - RefPtr bitmap; - Function callback; - }; - auto context = make(); - context->path = path; - context->callback = move(callback); + LibThread::BackgroundAction>::create( + [path] { + return load_png(path); + }, - int rc = create_thread([](void* ctx) -> int { - OwnPtr 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 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&& bitmap) -{ - m_wallpaper_path = path; - m_wallpaper = move(bitmap); - invalidate(); -} - void WSCompositor::flip_buffers() { ASSERT(m_screen_can_set_buffer); diff --git a/Servers/WindowServer/WSCompositor.h b/Servers/WindowServer/WSCompositor.h index 7f7e02f091..cfbcf09270 100644 --- a/Servers/WindowServer/WSCompositor.h +++ b/Servers/WindowServer/WSCompositor.h @@ -43,7 +43,6 @@ private: void draw_cursor(); void draw_geometry_label(); void draw_menubar(); - void finish_setting_wallpaper(const String& path, NonnullRefPtr&&); unsigned m_compose_count { 0 }; unsigned m_flush_count { 0 };