1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-22 18:03:03 +01:00

Fix a couple data races

This commit is contained in:
jfreegman 2021-11-26 08:51:38 -05:00
parent 1cba726bb8
commit 090fcfffe3
No known key found for this signature in database
GPG Key ID: 3627F3144076AE63
3 changed files with 36 additions and 12 deletions

View File

@ -1285,13 +1285,19 @@ static void chat_onDraw(ToxWindow *self, Tox *m)
/* Draw status bar */
StatusBar *statusbar = self->stb;
pthread_mutex_lock(&Winthread.lock);
Tox_Connection connection = statusbar->connection;
Tox_User_Status status = statusbar->status;
pthread_mutex_unlock(&Winthread.lock);
wmove(statusbar->topline, 0, 0);
wattron(statusbar->topline, COLOR_PAIR(BAR_ACCENT));
wprintw(statusbar->topline, " [");
wattroff(statusbar->topline, COLOR_PAIR(BAR_ACCENT));
switch (statusbar->connection) {
switch (connection) {
case TOX_CONNECTION_TCP:
wattron(statusbar->topline, A_BOLD | COLOR_PAIR(STATUS_ONLINE));
wprintw(statusbar->topline, "TCP");
@ -1315,8 +1321,6 @@ static void chat_onDraw(ToxWindow *self, Tox *m)
wprintw(statusbar->topline, "]");
wattroff(statusbar->topline, COLOR_PAIR(BAR_ACCENT));
Tox_User_Status status = statusbar->status;
if (status != TOX_USER_STATUS_NONE) {
const char *status_text = "ERROR";
int colour = MAGENTA;
@ -1376,13 +1380,15 @@ static void chat_onDraw(ToxWindow *self, Tox *m)
char statusmsg[TOX_MAX_STATUS_MESSAGE_LENGTH] = {'\0'};
pthread_mutex_lock(&Winthread.lock);
tox_friend_get_status_message(m, self->num, (uint8_t *) statusmsg, NULL);
size_t s_len = tox_friend_get_status_message_size(m, self->num, NULL);
pthread_mutex_unlock(&Winthread.lock);
filter_str(statusmsg, s_len);
snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", statusmsg);
statusbar->statusmsg_len = strlen(statusbar->statusmsg);
pthread_mutex_unlock(&Winthread.lock);
}
self->x = x2;
@ -1390,19 +1396,27 @@ static void chat_onDraw(ToxWindow *self, Tox *m)
/* Truncate note if it doesn't fit in statusbar */
size_t maxlen = x2 - getcurx(statusbar->topline) - (KEY_IDENT_DIGITS * 2) - 6;
if (statusbar->statusmsg_len > maxlen) {
statusbar->statusmsg[maxlen - 3] = '\0';
pthread_mutex_lock(&Winthread.lock);
size_t statusmsg_len = statusbar->statusmsg_len;
pthread_mutex_unlock(&Winthread.lock);
if (statusmsg_len > maxlen) {
pthread_mutex_lock(&Winthread.lock);
statusbar->statusmsg[maxlen - 3] = 0;
strcat(statusbar->statusmsg, "...");
statusbar->statusmsg_len = maxlen;
pthread_mutex_unlock(&Winthread.lock);
}
if (statusbar->statusmsg[0]) {
if (statusmsg_len > 0) {
wattron(statusbar->topline, COLOR_PAIR(BAR_ACCENT));
wprintw(statusbar->topline, " | ");
wattroff(statusbar->topline, COLOR_PAIR(BAR_ACCENT));
wattron(statusbar->topline, COLOR_PAIR(BAR_TEXT));
pthread_mutex_lock(&Winthread.lock);
wprintw(statusbar->topline, "%s ", statusbar->statusmsg);
pthread_mutex_unlock(&Winthread.lock);
} else {
wattron(statusbar->topline, COLOR_PAIR(BAR_TEXT));
}
@ -1505,6 +1519,7 @@ static void chat_onInit(ToxWindow *self, Tox *m)
/* Init statusbar info */
StatusBar *statusbar = self->stb;
statusbar->status = get_friend_status(self->num);
statusbar->connection = get_friend_connection_status(self->num);

View File

@ -424,11 +424,14 @@ static void prompt_onDraw(ToxWindow *self, Tox *m)
char statusmsg[TOX_MAX_STATUS_MESSAGE_LENGTH];
pthread_mutex_lock(&Winthread.lock);
size_t slen = tox_self_get_status_message_size(m);
tox_self_get_status_message(m, (uint8_t *) statusmsg);
statusmsg[slen] = '\0';
snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", statusmsg);
statusbar->statusmsg_len = strlen(statusbar->statusmsg);
pthread_mutex_unlock(&Winthread.lock);
}
@ -438,25 +441,29 @@ static void prompt_onDraw(ToxWindow *self, Tox *m)
uint16_t maxlen = x2 - getcurx(statusbar->topline) - 3;
pthread_mutex_lock(&Winthread.lock);
size_t statusmsg_len = statusbar->statusmsg_len;
pthread_mutex_unlock(&Winthread.lock);
if (statusbar->statusmsg_len > maxlen) {
statusbar->statusmsg[maxlen - 3] = '\0';
if (statusmsg_len > maxlen) {
pthread_mutex_lock(&Winthread.lock);
statusbar->statusmsg[maxlen - 3] = 0;
strcat(statusbar->statusmsg, "...");
statusbar->statusmsg_len = maxlen;
pthread_mutex_unlock(&Winthread.lock);
}
if (statusbar->statusmsg[0]) {
if (statusmsg_len) {
wattron(statusbar->topline, COLOR_PAIR(BAR_ACCENT));
wprintw(statusbar->topline, " | ");
wattroff(statusbar->topline, COLOR_PAIR(BAR_ACCENT));
wattron(statusbar->topline, COLOR_PAIR(BAR_TEXT));
pthread_mutex_lock(&Winthread.lock);
wprintw(statusbar->topline, "%s", statusbar->statusmsg);
pthread_mutex_unlock(&Winthread.lock);
wattroff(statusbar->topline, COLOR_PAIR(BAR_TEXT));
}
pthread_mutex_unlock(&Winthread.lock);
int y;
int x;
getyx(self->window, y, x);

View File

@ -806,12 +806,14 @@ void draw_active_window(Tox *m)
a->onDraw(a, m);
wrefresh(a->window);
}
#ifdef AUDIO
else if (a->is_call && timed_out(a->chatwin->infobox.lastupdate, 1)) {
touchwin(a->window);
a->onDraw(a, m);
wrefresh(a->window);
}
#endif // AUDIO
#ifdef GAMES