commit f63946201db4f5eec903956074aa213bf7d65f5192a5c2a88cdc17532f17357b from: Mark Jamsek date: Wed Jan 22 05:53:11 2025 UTC tog: don't make users wait for the worktree diff to quit Reported by naddy: if the user opens tog and quickly quits before the log thread has finished fetching work tree state for displaying dirty work tree entries, the main thread waits for the log thread to finish before acting on the user's quit input. This produces a noticeable lag between entering 'q' and tog actually terminating. Pass a pointer to the quit flag to the worktree diff cancel callback so we can act on it immediately without making the user wait. tested by naddy who confirmed it greatly reduces the lag commit - 52f1ade2981d662c4555a2eed4cabc997f5264695cdee210188a021c7f78f0ee commit + f63946201db4f5eec903956074aa213bf7d65f5192a5c2a88cdc17532f17357b blob - 68d8b28ee6a1fa9059476803accebcf78a6a7097467664096f18c0766d3932a4 blob + 52daa873c21da576d336ced7d1ef7a2e7a4daf332d7765c49df856873fadf0eb --- tog/tog.c +++ tog/tog.c @@ -3140,7 +3140,15 @@ push_worktree_entry(struct tog_log_thread_args *ta, in static const struct got_error * check_cancelled(void *arg) { - if (tog_sigint_received || tog_sigpipe_received) + int rc, quit = 0; + + if ((rc = pthread_mutex_lock(&tog_mutex)) != 0) + return got_error_set_errno(rc, "pthread_mutex_lock"); + if (tog_sigint_received || tog_sigpipe_received || *((int *)arg)) + quit = 1; + if ((rc = pthread_mutex_unlock(&tog_mutex)) != 0) + return got_error_set_errno(rc, "pthread_mutex_unlock"); + if (quit) return got_error(GOT_ERR_CANCELLED); return NULL; } @@ -3212,12 +3220,9 @@ tog_worktree_status(struct tog_log_thread_args *ta) goto done; err = got_worktree_status(wt, &paths, ta->repo, 0, - check_local_changes, &wt_state, check_cancelled, NULL); - if (err != NULL) { - if (err->code != GOT_ERR_CANCELLED) - goto done; - err = NULL; - } + check_local_changes, &wt_state, check_cancelled, ta->quit); + if (err != NULL) + goto done; if (wt_state != 0) { err = get_author(&wctx->wt_author, ta->repo, wt); @@ -4049,8 +4054,11 @@ log_thread(void *arg) goto done; } err = tog_worktree_status(a); - if (err != NULL) + if (err != NULL) { + if (err->code == GOT_ERR_CANCELLED) + err = NULL; goto done; + } errcode = pthread_mutex_lock(&tog_mutex); if (errcode) { err = got_error_set_errno(errcode,