fixed segfault from not joining thread

This commit is contained in:
2025-08-25 12:31:00 -06:00
parent e721165090
commit e15eb3e129
+21 -23
View File
@@ -38,7 +38,7 @@ char s[INET6_ADDRSTRLEN];
/* notcurses stuff */ /* notcurses stuff */
struct notcurses *nc; struct notcurses *nc;
struct ncplane *chatlog, *stdn, *typingareaplane; struct ncplane *chatlog, *stdn;
int rows, cols; int rows, cols;
/* Accept input from the user and add it to the send list */ /* Accept input from the user and add it to the send list */
@@ -47,7 +47,7 @@ void *input(void *arg) {
struct ncinput ni; struct ncinput ni;
uint32_t character; uint32_t character;
int i; int i;
int placement = 16;
while (1) { while (1) {
/* Get a message from the user */ /* Get a message from the user */
msg = malloc(512); msg = malloc(512);
@@ -80,19 +80,16 @@ void *input(void *arg) {
i--; i--;
msg[i] = '\0'; msg[i] = '\0';
} }
else if (character < 0x110000 && i < 511) { else if (i < 511) {
msg[i++] = (char)character; msg[i++] = (char)character;
msg[i] = '\0'; msg[i] = '\0';
} }
ncplane_printf_yx(stdn, rows-1, 0, "Type a message: %-*s", 511, msg); ncplane_printf_yx(stdn, rows - 1, 0, "Type a message: %s", msg);
pthread_mutex_lock(&rendermut); pthread_mutex_lock(&rendermut);
notcurses_render(nc); notcurses_render(nc);
pthread_mutex_unlock(&rendermut); pthread_mutex_unlock(&rendermut);
} }
pthread_mutex_lock(&rendermut);
notcurses_render(nc);
pthread_mutex_unlock(&rendermut);
/* Wake up thread if it was waiting for a msg to send */ /* Wake up thread if it was waiting for a msg to send */
if (ListCount(sendlist) == 1) { if (ListCount(sendlist) == 1) {
@@ -128,7 +125,8 @@ void *sender(void *arg) {
bytes = send(confd, msg, 512, 0); bytes = send(confd, msg, 512, 0);
notcurses_cursor_enable(nc, rows, 16); ncplane_move_yx(chatlog, ncplane_y(chatlog) - 1, 0);
ncplane_printf(chatlog, "You: %s\n", msg);
pthread_mutex_lock(&rendermut); pthread_mutex_lock(&rendermut);
notcurses_render(nc); notcurses_render(nc);
@@ -145,7 +143,7 @@ void *sender(void *arg) {
return 0; return 0;
} }
/* Receive messages and print them */ /* Receive messages, add to print queue */
void *receiver(void *arg) { void *receiver(void *arg) {
char *buf; char *buf;
int bytes; int bytes;
@@ -183,11 +181,19 @@ void *output(void *arg) {
pthread_cond_wait(&receivecv, &receivecvmut); pthread_cond_wait(&receivecv, &receivecvmut);
pthread_mutex_unlock(&receivecvmut); pthread_mutex_unlock(&receivecvmut);
} }
pthread_mutex_lock(&receivemut); pthread_mutex_lock(&receivemut);
ListFirst(receivelist); ListFirst(receivelist);
strcpy(msg, (char*)ListCurr(receivelist)); strcpy(msg, (char*)ListCurr(receivelist));
ListRemove(receivelist); ListRemove(receivelist);
pthread_mutex_unlock(&receivemut); pthread_mutex_unlock(&receivemut);
ncplane_move_yx(chatlog, ncplane_y(chatlog) - 1, 0);
ncplane_printf(chatlog, "%s: %s\n", s, msg);
pthread_mutex_lock(&rendermut);
notcurses_render(nc);
pthread_mutex_unlock(&rendermut);
} }
return 0; return 0;
} }
@@ -208,21 +214,14 @@ int main(int argc, char* argv[]) {
struct sockaddr_storage their_addr; struct sockaddr_storage their_addr;
int status; int status;
int yes = 1; int yes = 1;
struct ncplane_options nopts = { struct ncplane_options nopts = {
.y = 0, .y = 0,
.x = 0, .x = 0,
.rows = 0, .rows = 0,
.cols = 0 .cols = 0
}; };
struct ncplane_options typeplaneopts = {
.y = 0,
.x = 16,
.rows = 1,
.cols = 0
};
/*typeopts->flags = NCREADER_OPTION_NOCMDKEYS | NCREADER_OPTION_CURSOR;
*/
if (argc != 4) { if (argc != 4) {
printf("Wrong number of arguments. Usage: ./chatty <local port> \ printf("Wrong number of arguments. Usage: ./chatty <local port> \
<remote IP> <remote port>\n"); <remote IP> <remote port>\n");
@@ -377,16 +376,14 @@ int main(int argc, char* argv[]) {
cols = nopts.cols; cols = nopts.cols;
nopts.rows -= 1; nopts.rows -= 1;
nopts.cols -= 1; nopts.cols -= 1;
notcurses_cursor_disable(nc);
typeplaneopts.cols = cols - 16;
typeplaneopts.y = rows - 1;
if ((chatlog = ncplane_create(stdn, &nopts)) == NULL) { if ((chatlog = ncplane_create(stdn, &nopts)) == NULL) {
return -1; return -1;
} }
if ((typingareaplane = ncplane_create(stdn, &typeplaneopts)) == NULL) {
return -1; ncplane_set_scrolling(chatlog, true);
} ncplane_move_yx(chatlog, rows - 2, 0);
pthread_create(&input_thread, NULL, input, NULL); pthread_create(&input_thread, NULL, input, NULL);
pthread_create(&send_thread, NULL, sender, NULL); pthread_create(&send_thread, NULL, sender, NULL);
pthread_create(&output_thread, NULL, output, NULL); pthread_create(&output_thread, NULL, output, NULL);
@@ -395,6 +392,7 @@ int main(int argc, char* argv[]) {
pthread_join(send_thread, NULL); pthread_join(send_thread, NULL);
pthread_join(receive_thread, NULL); pthread_join(receive_thread, NULL);
pthread_join(input_thread, NULL); pthread_join(input_thread, NULL);
pthread_join(output_thread, NULL);
pthread_cond_destroy(&sendcv); pthread_cond_destroy(&sendcv);
pthread_cond_destroy(&receivecv); pthread_cond_destroy(&receivecv);