Skip to content

Commit

Permalink
Terminate background thread gracefully (#424)
Browse files Browse the repository at this point in the history
* Terminate background thread gracefully

* fix background thread pause/resume action naming
  • Loading branch information
zcsahok authored Feb 7, 2024
1 parent d632426 commit ffb8e0f
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 34 deletions.
59 changes: 33 additions & 26 deletions src/background_process.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,38 +41,44 @@
#include "tlf.h"
#include "write_keyer.h"

static bool go = true;
// don't start until we know what we are doing
static bool stop_backgrnd_process = true;

static pthread_mutex_t stop_backgrnd_process_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t start_backgrnd_process_cond = PTHREAD_COND_INITIALIZER;
static pthread_cond_t backgrnd_process_stopped_cond = PTHREAD_COND_INITIALIZER;

void stop_background_process(void) {
pthread_mutex_lock(&stop_backgrnd_process_mutex);
assert(!stop_backgrnd_process);
stop_backgrnd_process = true;
pthread_cond_wait(&backgrnd_process_stopped_cond, &stop_backgrnd_process_mutex);
pthread_mutex_unlock(&stop_backgrnd_process_mutex);
static bool pause_backgrnd_process = true;

static pthread_mutex_t pause_backgrnd_process_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t resume_backgrnd_process_cond = PTHREAD_COND_INITIALIZER;
static pthread_cond_t backgrnd_process_paused_cond = PTHREAD_COND_INITIALIZER;

void terminate_background_process() {
go = false;
}

void start_background_process(void) {
pthread_mutex_lock(&stop_backgrnd_process_mutex);
assert(stop_backgrnd_process);
stop_backgrnd_process = false;
pthread_cond_broadcast(&start_backgrnd_process_cond);
pthread_mutex_unlock(&stop_backgrnd_process_mutex);
void pause_background_process(void) {
pthread_mutex_lock(&pause_backgrnd_process_mutex);
assert(!pause_backgrnd_process);
pause_backgrnd_process = true;
pthread_cond_wait(&backgrnd_process_paused_cond, &pause_backgrnd_process_mutex);
pthread_mutex_unlock(&pause_backgrnd_process_mutex);
}

void resume_background_process(void) {
pthread_mutex_lock(&pause_backgrnd_process_mutex);
assert(pause_backgrnd_process);
pause_backgrnd_process = false;
pthread_cond_broadcast(&resume_backgrnd_process_cond);
pthread_mutex_unlock(&pause_backgrnd_process_mutex);
}

static void background_process_wait(void) {
pthread_mutex_lock(&stop_backgrnd_process_mutex);
if (stop_backgrnd_process) {
pthread_cond_broadcast(&backgrnd_process_stopped_cond);
pthread_cond_wait(&start_backgrnd_process_cond, &stop_backgrnd_process_mutex);
pthread_mutex_lock(&pause_backgrnd_process_mutex);
if (pause_backgrnd_process) {
pthread_cond_broadcast(&backgrnd_process_paused_cond);
pthread_cond_wait(&resume_backgrnd_process_cond, &pause_backgrnd_process_mutex);
}
pthread_mutex_unlock(&stop_backgrnd_process_mutex);
pthread_mutex_unlock(&pause_backgrnd_process_mutex);
}


void *background_process(void *ptr) {

char *prmessage;
Expand All @@ -84,7 +90,7 @@ void *background_process(void *ptr) {
char debugbuffer[160];
FILE *fp;

while (1) {
while (go) {

background_process_wait();

Expand Down Expand Up @@ -120,7 +126,7 @@ void *background_process(void *ptr) {
fldigi_rpc_cnt = 1 - fldigi_rpc_cnt;
}

if (!stop_backgrnd_process) {
if (!pause_backgrnd_process) {
write_keyer();
cqww_simulator();
}
Expand Down Expand Up @@ -150,7 +156,7 @@ void *background_process(void *ptr) {

if ((*lan_message != '\0')
&& (lan_message[0] != thisnode)
&& !stop_backgrnd_process) {
&& !pause_backgrnd_process) {

switch (lan_message[1]) {

Expand Down Expand Up @@ -250,5 +256,6 @@ void *background_process(void *ptr) {

}

pthread_exit(NULL);
}

5 changes: 3 additions & 2 deletions src/background_process.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
#define BACKGROUND_PROCESS_H

void *background_process(void *);
void stop_background_process(void);
void start_background_process(void);
void terminate_background_process();
void pause_background_process(void);
void resume_background_process(void);

#endif /* end of include guard: BACKGROUND_PROCESS_H */
4 changes: 2 additions & 2 deletions src/edit_last.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ void edit_last(void) {
if (NR_QSOS == 0)
return; /* nothing to edit */

stop_background_process(); // note: this freezes nr_qsos, as network is paused
pause_background_process(); // note: this freezes nr_qsos, as network is paused

const int topline = (NR_LINES > NR_QSOS ? NR_LINES - NR_QSOS : 0);

Expand Down Expand Up @@ -333,5 +333,5 @@ void edit_last(void) {

scroll_log();

start_background_process();
resume_background_process();
}
4 changes: 2 additions & 2 deletions src/editlog.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ void edit(char *filename) {

void logedit(void) {

stop_background_process();
pause_background_process();
edit(logfile);
checklogfile();

log_read_n_score();

start_background_process();
resume_background_process();

attron(COLOR_PAIR(C_LOG) | A_STANDOUT);
erase();
Expand Down
2 changes: 1 addition & 1 deletion src/logit.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void logit(void) {
clear_display();
defer_store = 0;

start_background_process(); /* start it up */
resume_background_process(); /* start it up */

while (1) {
printcall();
Expand Down
2 changes: 1 addition & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -962,7 +962,7 @@ static void mark_GPL_seen() {
*/
static void tlf_cleanup() {
if (pthread_self() != background_thread) {
pthread_cancel(background_thread);
terminate_background_process();
pthread_join(background_thread, NULL);
}

Expand Down

0 comments on commit ffb8e0f

Please sign in to comment.