Add an item-free method to the thread-pool

This commit is contained in:
Dave Davenport 2024-03-01 15:28:48 +01:00
parent 42d6bb9af4
commit 8061e4e7c2
3 changed files with 18 additions and 3 deletions

View File

@ -370,6 +370,7 @@ typedef struct rofi_int_matcher_t {
*/
typedef struct _thread_state {
void (*callback)(struct _thread_state *t, gpointer data);
void (*free)(void *);
int priority;
} thread_state;

View File

@ -86,6 +86,8 @@ typedef struct {
IconFetcherNameEntry *entry;
} IconFetcherEntry;
// Free method.
static void rofi_icon_fetch_entry_free(gpointer data);
/**
* The icon fetcher internal state.
*/
@ -361,7 +363,7 @@ static void rofi_icon_fetcher_worker(thread_state *sdata,
icon_path, sentry->wsize, sentry->hsize, TRUE, &error);
if (error != NULL) {
g_warning("Failed to load image: |%s| %d %d %s (%p)", icon_path,
sentry->wsize, sentry->hsize, error->message, (void*)pb);
sentry->wsize, sentry->hsize, error->message, (void *)pb);
g_error_free(error);
if (pb) {
g_object_unref(pb);
@ -411,6 +413,7 @@ uint32_t rofi_icon_fetcher_query_advanced(const char *name, const int wsize,
// Push into fetching queue.
sentry->state.callback = rofi_icon_fetcher_worker;
sentry->state.free = rofi_icon_fetch_entry_free;
sentry->state.priority = G_PRIORITY_LOW;
g_thread_pool_push(tpool, sentry, NULL);

View File

@ -1460,6 +1460,7 @@ static gboolean rofi_view_refilter_real(RofiViewState *state) {
states[i].plen = plen;
states[i].pattern = pattern;
states[i].st.callback = filter_elements;
states[i].st.free = NULL;
states[i].st.priority = G_PRIORITY_HIGH;
if (i > 0) {
g_thread_pool_push(tpool, &states[i], NULL);
@ -2650,6 +2651,15 @@ static int rofi_thread_workers_sort(gconstpointer a, gconstpointer b,
return tsa->priority - tsb->priority;
}
static void rofi_thread_pool_state_free(gpointer data) {
if (data) {
thread_state *ts = (thread_state *)data;
if (ts->free) {
ts->free(data);
}
}
}
void rofi_view_workers_initialize(void) {
TICK_N("Setup Threadpool, start");
if (config.threads == 0) {
@ -2661,8 +2671,9 @@ void rofi_view_workers_initialize(void) {
}
// Create thread pool
GError *error = NULL;
tpool = g_thread_pool_new(rofi_view_call_thread, NULL, config.threads, FALSE,
&error);
tpool = g_thread_pool_new_full(rofi_view_call_thread, NULL,
rofi_thread_pool_state_free, config.threads,
FALSE, &error);
if (error == NULL) {
// Idle threads should stick around for a max of 60 seconds.
g_thread_pool_set_max_idle_time(60000);