Skip to content

Commit

Permalink
Add an item-free method to the thread-pool
Browse files Browse the repository at this point in the history
  • Loading branch information
DaveDavenport committed Mar 1, 2024
1 parent 42d6bb9 commit 8061e4e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
1 change: 1 addition & 0 deletions include/rofi-types.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
5 changes: 4 additions & 1 deletion source/rofi-icon-fetcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down
15 changes: 13 additions & 2 deletions source/view.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand Down

0 comments on commit 8061e4e

Please sign in to comment.