render,types/wlr_shm: Offload slow cleanup ops

Defers close()/munmap() calls into a cleanup thread in to avoid blocking
when they are slow. This is most noticable on low end hardware with
large screens where large surfaces can spend considerable time
invalidating mmus on the gpu or clearing shmem pages.
This commit is contained in:
Kurt Kartaltepe 2024-07-22 19:09:27 -07:00
parent 015bb8512e
commit 5cc737d599
6 changed files with 130 additions and 4 deletions

86
util/cleanup.c Normal file
View file

@ -0,0 +1,86 @@
#include "util/cleanup.h"
#include <pthread.h>
#include <stdbool.h>
#include <string.h>
#include <sys/eventfd.h>
#include <unistd.h>
#define MAX_TASKS 16
struct work_queue {
struct wlr_task list[MAX_TASKS];
int size;
bool running;
pthread_mutex_t lock;
pthread_t thread;
int efd;
};
static struct work_queue queue;
static void *cleanup_thread(void *unused)
{
struct wlr_task work[MAX_TASKS];
eventfd_t v;
do {
eventfd_read(queue.efd, &v);
pthread_mutex_lock(&queue.lock);
for (int i = 0; i < (int)v; i++) {
work[i] = queue.list[i];
}
for (int i = (int)v; i < queue.size; i++) {
queue.list[i - v] = queue.list[i];
}
queue.size -= (int)v;
pthread_mutex_unlock(&queue.lock);
for (int i = 0; i < (int)v; i++) {
work[i].task(work[i].data);
}
} while (queue.running);
return NULL;
}
extern void wlr_cleanup_defer(struct wlr_task t)
{
wlr_cleanup_queue_init();
pthread_mutex_lock(&queue.lock);
if (queue.size < MAX_TASKS) {
queue.list[queue.size] = t;
queue.size++;
} else {
// In case too much work is queue, apply backpressure.
pthread_mutex_unlock(&queue.lock);
return t.task(t.data);
}
pthread_mutex_unlock(&queue.lock);
eventfd_write(queue.efd, 1);
}
extern void wlr_cleanup_queue_init(void)
{
if (!queue.running) {
queue = (struct work_queue){0};
queue.running = true;
queue.efd = eventfd(0, 0);
pthread_mutex_init(&queue.lock, NULL);
pthread_create(&queue.thread, NULL, cleanup_thread, NULL);
}
}
static void done(void *unused) { queue.running = false; }
extern void wlr_cleanup_queue_finish(void)
{
if (queue.running) {
// Notify the cleanup thread that it's done.
wlr_cleanup_defer((struct wlr_task){&done, NULL});
pthread_join(queue.thread, NULL);
close(queue.efd);
queue = (struct work_queue){0};
}
}

View file

@ -13,4 +13,5 @@ wlr_files += files(
'token.c',
'transform.c',
'utf8.c',
'cleanup.c',
)