From 9c21760c9dcb6f9b561436ac6a94dd1e87411a00 Mon Sep 17 00:00:00 2001 From: Gerrit Renker Date: Sat, 17 Oct 2020 09:45:31 -0400 Subject: [PATCH] Revert "Scan screen: further simplify mutex use (#78)" This reverts commit c74e52d162c4dd74bf7069e42c91df4051b15ce0. The fix was not right, using deferred pthread_cancel (the default). Also the use of locks needs to be revised. --- iw_if.h | 4 ++-- iw_scan.c | 23 ++++++++++++++++++++--- scan_scr.c | 6 ++---- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/iw_if.h b/iw_if.h index e2199d3..3ea6f91 100644 --- a/iw_if.h +++ b/iw_if.h @@ -251,8 +251,8 @@ struct scan_result { pthread_mutex_t mutex; }; -extern void init_scan_list(struct scan_result *sr); -extern void free_scan_list(struct scan_entry *head); +extern void scan_result_init(struct scan_result *sr); +extern void scan_result_fini(struct scan_result *sr); extern void *do_scan(void *sr_ptr); /* diff --git a/iw_scan.c b/iw_scan.c index 62da3cd..197c88c 100644 --- a/iw_scan.c +++ b/iw_scan.c @@ -295,7 +295,7 @@ void sort_scan_list(struct scan_entry **headp) } /** De-allocate list. Use after all threads are terminated. */ -void free_scan_list(struct scan_entry *head) +static void free_scan_list(struct scan_entry *head) { if (head) { free_scan_list(head->next); @@ -304,7 +304,7 @@ void free_scan_list(struct scan_entry *head) } /** Initialize scan results. Requires lock to be taken. */ -void init_scan_list(struct scan_result *sr) +static void init_scan_list(struct scan_result *sr) { free_scan_list(sr->head); free(sr->channel_stats); @@ -313,7 +313,6 @@ void init_scan_list(struct scan_result *sr) sr->msg[0] = '\0'; sr->max_essid_len = MAX_ESSID_LEN; memset(&(sr->num), 0, sizeof(sr->num)); - sr->mutex = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER; } /* @@ -368,6 +367,24 @@ static void compute_channel_stats(struct scan_result *sr) sr->num.ch_stats = n < MAX_CH_STATS ? n : MAX_CH_STATS; } +/* + * Scan results. + */ +void scan_result_init(struct scan_result *sr) +{ + init_scan_list(sr); + pthread_mutex_init(&sr->mutex, NULL); +} + +void scan_result_fini(struct scan_result *sr) +{ + pthread_mutex_lock(&sr->mutex); + free_scan_list(sr->head); + free(sr->channel_stats); + pthread_mutex_unlock(&sr->mutex); + pthread_mutex_destroy(&sr->mutex); +} + /** The actual scan thread. */ void *do_scan(void *sr_ptr) { diff --git a/scan_scr.c b/scan_scr.c index 51d0285..6373b6a 100644 --- a/scan_scr.c +++ b/scan_scr.c @@ -203,7 +203,7 @@ void scr_aplst_init(void) mvwaddstr(w_aplst, START_LINE, 1, "Waiting for scan data ..."); wrefresh(w_aplst); - init_scan_list(&sr); + scan_result_init(&sr); pthread_create(&scan_thread, NULL, do_scan, &sr); } @@ -251,8 +251,6 @@ int scr_aplst_loop(WINDOW *w_menu) void scr_aplst_fini(void) { pthread_cancel(scan_thread); - free_scan_list(sr.head); - free(sr.channel_stats); - + scan_result_fini(&sr); delwin(w_aplst); }