From a05b788a786518f0b18941f4fd9e5111862710a0 Mon Sep 17 00:00:00 2001 From: Leonid Pliushch Date: Thu, 1 Oct 2020 20:29:21 +0300 Subject: [PATCH] calcurse: update patches for pthread_cancel Add pthread_cancel replacement through atomic_flag instead of using pthread_kill(thread, 0). Fixes https://github.com/termux/termux-packages/issues/4378. --- packages/calcurse/io.c.patch | 21 ---- packages/calcurse/src-notify.c.patch | 13 -- packages/calcurse/src-ui-calendar.c.patch | 13 -- packages/calcurse/threading.patch | 139 ++++++++++++++++++++++ 4 files changed, 139 insertions(+), 47 deletions(-) delete mode 100644 packages/calcurse/io.c.patch delete mode 100644 packages/calcurse/src-notify.c.patch delete mode 100644 packages/calcurse/src-ui-calendar.c.patch create mode 100644 packages/calcurse/threading.patch diff --git a/packages/calcurse/io.c.patch b/packages/calcurse/io.c.patch deleted file mode 100644 index 830e3974f..000000000 --- a/packages/calcurse/io.c.patch +++ /dev/null @@ -1,21 +0,0 @@ -diff -u -r ../calcurse-4.4.0/src/io.c ./src/io.c ---- ../calcurse-4.4.0/src/io.c 2019-02-04 20:44:04.000000000 +0000 -+++ ./src/io.c 2019-03-01 20:49:16.393057747 +0000 -@@ -1399,7 +1399,6 @@ - EXIT_IF(delay < 0, _("Invalid delay")); - char *mesg = _("Periodic save: data files have changed. Save cancelled."); - -- pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); - for (;;) { - sleep(delay * MININSEC); - pthread_mutex_lock(&io_periodic_save_mutex); -@@ -1424,8 +1423,7 @@ - - /* Lock the mutex to avoid cancelling the thread during saving. */ - pthread_mutex_lock(&io_periodic_save_mutex); -- pthread_cancel(io_t_psave); -- pthread_join(io_t_psave, NULL); -+ pthread_kill(io_t_psave, 0); - pthread_mutex_unlock(&io_periodic_save_mutex); - io_t_psave = pthread_self(); - } diff --git a/packages/calcurse/src-notify.c.patch b/packages/calcurse/src-notify.c.patch deleted file mode 100644 index e6da91aba..000000000 --- a/packages/calcurse/src-notify.c.patch +++ /dev/null @@ -1,13 +0,0 @@ -diff -u -r ../calcurse-4.4.0/src/notify.c ./src/notify.c ---- ../calcurse-4.4.0/src/notify.c 2019-01-18 22:38:58.000000000 +0000 -+++ ./src/notify.c 2019-03-01 20:45:55.019585998 +0000 -@@ -196,8 +196,7 @@ - if (pthread_equal(notify_t_main, pthread_self())) - return; - -- pthread_cancel(notify_t_main); -- pthread_join(notify_t_main, NULL); -+ pthread_kill(notify_t_main, 0); - notify_t_main = pthread_self(); - } - diff --git a/packages/calcurse/src-ui-calendar.c.patch b/packages/calcurse/src-ui-calendar.c.patch deleted file mode 100644 index 5489c43fb..000000000 --- a/packages/calcurse/src-ui-calendar.c.patch +++ /dev/null @@ -1,13 +0,0 @@ -diff -u -r ../calcurse-4.4.0/src/ui-calendar.c ./src/ui-calendar.c ---- ../calcurse-4.4.0/src/ui-calendar.c 2019-01-18 22:38:58.000000000 +0000 -+++ ./src/ui-calendar.c 2019-03-01 20:47:58.622033795 +0000 -@@ -123,8 +123,7 @@ - if (pthread_equal(ui_calendar_t_date, pthread_self())) - return; - -- pthread_cancel(ui_calendar_t_date); -- pthread_join(ui_calendar_t_date, NULL); -+ pthread_kill(ui_calendar_t_date, 0); - ui_calendar_t_date = pthread_self(); - } - diff --git a/packages/calcurse/threading.patch b/packages/calcurse/threading.patch new file mode 100644 index 000000000..70ba36232 --- /dev/null +++ b/packages/calcurse/threading.patch @@ -0,0 +1,139 @@ +diff -uNr calcurse-4.6.0/src/io.c calcurse-4.6.0.mod/src/io.c +--- calcurse-4.6.0/src/io.c 2020-03-27 15:14:07.000000000 +0200 ++++ calcurse-4.6.0.mod/src/io.c 2020-10-01 20:39:41.632326755 +0300 +@@ -44,6 +44,7 @@ + #include + #include + #include ++#include + + #include "calcurse.h" + #include "sha1.h" +@@ -69,6 +70,8 @@ + static char apts_sha1[SHA1_DIGESTLEN * 2 + 1]; + static char todo_sha1[SHA1_DIGESTLEN * 2 + 1]; + ++static atomic_flag psave_thread_cancel; ++ + /* Ask user for a file name to export data to. */ + static FILE *get_export_stream(enum export_type type) + { +@@ -1455,12 +1458,15 @@ + /* Thread used to periodically save data. */ + static void *io_psave_thread(void *arg) + { ++ atomic_flag_clear(&psave_thread_cancel); ++ + int delay = conf.periodic_save; + EXIT_IF(delay < 0, _("Invalid delay")); + char *mesg = _("Periodic save: data files have changed. Save cancelled."); + +- pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); + for (;;) { ++ if (atomic_flag_test_and_set(&psave_thread_cancel)) pthread_exit(NULL); ++ atomic_flag_clear(&psave_thread_cancel); + sleep(delay * MININSEC); + pthread_mutex_lock(&io_periodic_save_mutex); + if (io_save_cal(periodic) == IO_SAVE_CANCEL) +@@ -1484,8 +1490,7 @@ + + /* Lock the mutex to avoid cancelling the thread during saving. */ + pthread_mutex_lock(&io_periodic_save_mutex); +- pthread_cancel(io_t_psave); +- pthread_join(io_t_psave, NULL); ++ atomic_flag_test_and_set(&psave_thread_cancel); + pthread_mutex_unlock(&io_periodic_save_mutex); + io_t_psave = pthread_self(); + } +diff -uNr calcurse-4.6.0/src/notify.c calcurse-4.6.0.mod/src/notify.c +--- calcurse-4.6.0/src/notify.c 2020-02-04 23:46:34.000000000 +0200 ++++ calcurse-4.6.0.mod/src/notify.c 2020-10-01 20:39:57.884475331 +0300 +@@ -38,6 +38,7 @@ + #include + #include + #include ++#include + + #include "calcurse.h" + +@@ -55,6 +56,8 @@ + static struct notify_app notify_app; + static pthread_attr_t detached_thread_attr; + ++static atomic_flag main_thread_cancel; ++ + /* + * Return the number of seconds before next appointment + * (0 if no upcoming appointment). +@@ -196,8 +199,7 @@ + if (pthread_equal(notify_t_main, pthread_self())) + return; + +- pthread_cancel(notify_t_main); +- pthread_join(notify_t_main, NULL); ++ atomic_flag_test_and_set(&main_thread_cancel); + notify_t_main = pthread_self(); + } + +@@ -334,6 +336,8 @@ + /* ARGSUSED0 */ + static void *notify_main_thread(void *arg) + { ++ atomic_flag_clear(&main_thread_cancel); ++ + const unsigned thread_sleep = 1; + const unsigned check_app = MININSEC; + int elapse = 0; +@@ -346,6 +350,8 @@ + pthread_cleanup_push(notify_main_thread_cleanup, NULL); + + for (;;) { ++ if (atomic_flag_test_and_set(&main_thread_cancel)) pthread_exit(NULL); ++ atomic_flag_clear(&main_thread_cancel); + ntimer = time(NULL); + localtime_r(&ntimer, &ntime); + pthread_mutex_lock(¬ify.mutex); +diff -uNr calcurse-4.6.0/src/ui-calendar.c calcurse-4.6.0.mod/src/ui-calendar.c +--- calcurse-4.6.0/src/ui-calendar.c 2020-02-04 23:46:34.000000000 +0200 ++++ calcurse-4.6.0.mod/src/ui-calendar.c 2020-10-01 20:40:09.024576943 +0300 +@@ -41,6 +41,7 @@ + #include + #include + #include ++#include + + #include "calcurse.h" + +@@ -59,6 +60,8 @@ + static int monthly_view_cache_valid = 0; + static int monthly_view_cache_month = 0; + ++static atomic_flag date_thread_cancel; ++ + /* Switch between calendar views (monthly view is selected by default). */ + void ui_calendar_view_next(void) + { +@@ -89,9 +92,13 @@ + /* ARGSUSED0 */ + static void *ui_calendar_date_thread(void *arg) + { ++ atomic_flag_clear(&date_thread_cancel); ++ + time_t actual, tomorrow; + + for (;;) { ++ if (atomic_flag_test_and_set(&date_thread_cancel)) pthread_exit(NULL); ++ atomic_flag_clear(&date_thread_cancel); + tomorrow = date2sec(today, 24, 0); + + while ((actual = time(NULL)) < tomorrow) +@@ -118,8 +125,7 @@ + if (pthread_equal(ui_calendar_t_date, pthread_self())) + return; + +- pthread_cancel(ui_calendar_t_date); +- pthread_join(ui_calendar_t_date, NULL); ++ atomic_flag_test_and_set(&date_thread_cancel); + ui_calendar_t_date = pthread_self(); + } +