Skip to content
Snippets Groups Projects
Commit 4966f430 authored by Deucе's avatar Deucе :ok_hand_tone4:
Browse files

C17 deprecated ATOMIC_INIT_VAR() so we can use regular initialization

Since even Microsoft (experimentally) claims to support C17 atomics
correctly, don't bother with the extra verbosity of using explicit
atomic "stuff" and just use assigment/access as normal.
parent f5571902
No related branches found
No related tags found
1 merge request!488Overhaul LZH code
Pipeline #7503 passed
...@@ -25,8 +25,7 @@ ...@@ -25,8 +25,7 @@
static SOCKET telnets_sock; static SOCKET telnets_sock;
static CRYPT_SESSION telnets_session; static CRYPT_SESSION telnets_session;
static atomic_bool telnets_active = true; static atomic_bool telnets_active = false;
static pthread_once_t telnets_active_once = PTHREAD_ONCE_INIT;
static pthread_mutex_t telnets_mutex; static pthread_mutex_t telnets_mutex;
void void
...@@ -38,7 +37,7 @@ telnets_input_thread(void *args) ...@@ -38,7 +37,7 @@ telnets_input_thread(void *args)
size_t buffer; size_t buffer;
SetThreadName("TelnetS Input"); SetThreadName("TelnetS Input");
conn_api.input_thread_running = 1; conn_api.input_thread_running = 1;
while (atomic_load(&telnets_active) && !conn_api.terminate) { while (telnets_active && !conn_api.terminate) {
if (!socket_readable(telnets_sock, 100)) if (!socket_readable(telnets_sock, 100))
continue; continue;
pthread_mutex_lock(&telnets_mutex); pthread_mutex_lock(&telnets_mutex);
...@@ -50,11 +49,11 @@ telnets_input_thread(void *args) ...@@ -50,11 +49,11 @@ telnets_input_thread(void *args)
continue; continue;
if (cryptStatusError(status)) { if (cryptStatusError(status)) {
if ((status == CRYPT_ERROR_COMPLETE) || (status == CRYPT_ERROR_READ)) { /* connection closed */ if ((status == CRYPT_ERROR_COMPLETE) || (status == CRYPT_ERROR_READ)) { /* connection closed */
atomic_store(&telnets_active, false); telnets_active = false;
break; break;
} }
cryptlib_error_message(status, "recieving data"); cryptlib_error_message(status, "recieving data");
atomic_store(&telnets_active, false); telnets_active = false;
break; break;
} }
else { else {
...@@ -78,24 +77,24 @@ telnets_output_thread(void *args) ...@@ -78,24 +77,24 @@ telnets_output_thread(void *args)
int status; int status;
SetThreadName("TelnetS Output"); SetThreadName("TelnetS Output");
conn_api.output_thread_running = 1; conn_api.output_thread_running = 1;
while (atomic_load(&telnets_active) && !conn_api.terminate) { while (telnets_active && !conn_api.terminate) {
pthread_mutex_lock(&(conn_outbuf.mutex)); pthread_mutex_lock(&(conn_outbuf.mutex));
wr = conn_buf_wait_bytes(&conn_outbuf, 1, 100); wr = conn_buf_wait_bytes(&conn_outbuf, 1, 100);
if (wr) { if (wr) {
wr = conn_buf_get(&conn_outbuf, conn_api.wr_buf, conn_api.wr_buf_size); wr = conn_buf_get(&conn_outbuf, conn_api.wr_buf, conn_api.wr_buf_size);
pthread_mutex_unlock(&(conn_outbuf.mutex)); pthread_mutex_unlock(&(conn_outbuf.mutex));
sent = 0; sent = 0;
while (atomic_load(&telnets_active) && sent < wr) { while (telnets_active && sent < wr) {
pthread_mutex_lock(&telnets_mutex); pthread_mutex_lock(&telnets_mutex);
status = cryptPushData(telnets_session, conn_api.wr_buf + sent, wr - sent, &ret); status = cryptPushData(telnets_session, conn_api.wr_buf + sent, wr - sent, &ret);
pthread_mutex_unlock(&telnets_mutex); pthread_mutex_unlock(&telnets_mutex);
if (cryptStatusError(status)) { if (cryptStatusError(status)) {
if (status == CRYPT_ERROR_COMPLETE) { /* connection closed */ if (status == CRYPT_ERROR_COMPLETE) { /* connection closed */
atomic_store(&telnets_active, false); telnets_active = false;
break; break;
} }
cryptlib_error_message(status, "sending data"); cryptlib_error_message(status, "sending data");
atomic_store(&telnets_active, false); telnets_active = false;
break; break;
} }
sent += ret; sent += ret;
...@@ -113,14 +112,6 @@ telnets_output_thread(void *args) ...@@ -113,14 +112,6 @@ telnets_output_thread(void *args)
conn_api.output_thread_running = 2; conn_api.output_thread_running = 2;
} }
static void
init_once(void)
{
// We do this with a once because there's no destroy function, and it may not be lock-free
atomic_init(&telnets_active, false);
}
int int
telnets_connect(struct bbslist *bbs) telnets_connect(struct bbslist *bbs)
{ {
...@@ -130,8 +121,7 @@ telnets_connect(struct bbslist *bbs) ...@@ -130,8 +121,7 @@ telnets_connect(struct bbslist *bbs)
if (!bbs->hidepopups) if (!bbs->hidepopups)
init_uifc(true, true); init_uifc(true, true);
pthread_mutex_init(&telnets_mutex, NULL); pthread_mutex_init(&telnets_mutex, NULL);
pthread_once(&telnets_active_once, init_once); telnets_active = false;
atomic_store(&telnets_active, false);
telnets_sock = conn_socket_connect(bbs); telnets_sock = conn_socket_connect(bbs);
if (telnets_sock == INVALID_SOCKET) if (telnets_sock == INVALID_SOCKET)
...@@ -189,7 +179,7 @@ telnets_connect(struct bbslist *bbs) ...@@ -189,7 +179,7 @@ telnets_connect(struct bbslist *bbs)
return -1; return -1;
} }
atomic_store(&telnets_active, true); telnets_active = true;
if (!bbs->hidepopups) { if (!bbs->hidepopups) {
/* Clear ownership */ /* Clear ownership */
uifc.pop(NULL); // TODO: Why is this called twice? uifc.pop(NULL); // TODO: Why is this called twice?
...@@ -203,7 +193,7 @@ telnets_connect(struct bbslist *bbs) ...@@ -203,7 +193,7 @@ telnets_connect(struct bbslist *bbs)
conn_api.terminate = 1; conn_api.terminate = 1;
if (!bbs->hidepopups) if (!bbs->hidepopups)
uifc.pop(NULL); uifc.pop(NULL);
atomic_store(&telnets_active, false); telnets_active = false;
return -1; return -1;
} }
if (!bbs->hidepopups) if (!bbs->hidepopups)
...@@ -233,7 +223,7 @@ telnets_close(void) ...@@ -233,7 +223,7 @@ telnets_close(void)
char garbage[1024]; char garbage[1024];
conn_api.terminate = 1; conn_api.terminate = 1;
cryptSetAttribute(telnets_session, CRYPT_SESSINFO_ACTIVE, 0); cryptSetAttribute(telnets_session, CRYPT_SESSINFO_ACTIVE, 0);
atomic_store(&telnets_active, false); telnets_active = false;
while (conn_api.input_thread_running == 1 || conn_api.output_thread_running == 1) { while (conn_api.input_thread_running == 1 || conn_api.output_thread_running == 1) {
conn_recv_upto(garbage, sizeof(garbage), 0); conn_recv_upto(garbage, sizeof(garbage), 0);
SLEEP(1); SLEEP(1);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment