Skip to content

Commit

Permalink
[ThreadPool] Sort items in the queue based on priority
Browse files Browse the repository at this point in the history
  • Loading branch information
DaveDavenport committed Feb 11, 2024
1 parent 3dc3e2d commit 13c2a61
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 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);
int priority;
} thread_state;

extern GThreadPool *tpool;
Expand Down
2 changes: 2 additions & 0 deletions source/rofi-icon-fetcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,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.priority = G_PRIORITY_LOW;
g_thread_pool_push(tpool, sentry, NULL);

return sentry->uid;
Expand Down Expand Up @@ -447,6 +448,7 @@ uint32_t rofi_icon_fetcher_query(const char *name, const int size) {

// Push into fetching queue.
sentry->state.callback = rofi_icon_fetcher_worker;
sentry->state.priority = G_PRIORITY_LOW;
g_thread_pool_push(tpool, sentry, NULL);

return sentry->uid;
Expand Down
11 changes: 11 additions & 0 deletions source/view.c
Original file line number Diff line number Diff line change
Expand Up @@ -1444,6 +1444,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.priority = G_PRIORITY_HIGH;
if (i > 0) {
g_thread_pool_push(tpool, &states[i], NULL);
}
Expand Down Expand Up @@ -2624,6 +2625,15 @@ void rofi_view_cleanup() {

input_history_save();
}

static int rofi_thread_workers_sort(gconstpointer a,gconstpointer b, gpointer data G_GNUC_UNUSED)
{
thread_state *tsa = (thread_state *)a;
thread_state *tsb = (thread_state *)b;
// lower number is lower priority.. a is sorted above is a > b.
return tsa->priority-tsb->priority;
}

void rofi_view_workers_initialize(void) {
TICK_N("Setup Threadpool, start");
if (config.threads == 0) {
Expand All @@ -2649,6 +2659,7 @@ void rofi_view_workers_initialize(void) {
g_error_free(error);
exit(EXIT_FAILURE);
}
g_thread_pool_set_sort_function(tpool, rofi_thread_workers_sort, NULL);
TICK_N("Setup Threadpool, done");
}
void rofi_view_workers_finalize(void) {
Expand Down

0 comments on commit 13c2a61

Please sign in to comment.