Skip to content
Snippets Groups Projects
Commit 97f6ec1c 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 5e271014
No related branches found
No related tags found
No related merge requests found
......@@ -25,8 +25,7 @@
static SOCKET telnets_sock;
static CRYPT_SESSION telnets_session;
static atomic_bool telnets_active = true;
static pthread_once_t telnets_active_once = PTHREAD_ONCE_INIT;
static atomic_bool telnets_active = false;
static pthread_mutex_t telnets_mutex;
void
......@@ -38,7 +37,7 @@ telnets_input_thread(void *args)
size_t buffer;
SetThreadName("TelnetS Input");
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))
continue;
pthread_mutex_lock(&telnets_mutex);
......@@ -50,11 +49,11 @@ telnets_input_thread(void *args)
continue;
if (cryptStatusError(status)) {
if ((status == CRYPT_ERROR_COMPLETE) || (status == CRYPT_ERROR_READ)) { /* connection closed */
atomic_store(&telnets_active, false);
telnets_active = false;
break;
}
cryptlib_error_message(status, "recieving data");
atomic_store(&telnets_active, false);
telnets_active = false;
break;
}
else {
......@@ -78,24 +77,24 @@ telnets_output_thread(void *args)
int status;
SetThreadName("TelnetS Output");
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));
wr = conn_buf_wait_bytes(&conn_outbuf, 1, 100);
if (wr) {
wr = conn_buf_get(&conn_outbuf, conn_api.wr_buf, conn_api.wr_buf_size);
pthread_mutex_unlock(&(conn_outbuf.mutex));
sent = 0;
while (atomic_load(&telnets_active) && sent < wr) {
while (telnets_active && sent < wr) {
pthread_mutex_lock(&telnets_mutex);
status = cryptPushData(telnets_session, conn_api.wr_buf + sent, wr - sent, &ret);
pthread_mutex_unlock(&telnets_mutex);
if (cryptStatusError(status)) {
if (status == CRYPT_ERROR_COMPLETE) { /* connection closed */
atomic_store(&telnets_active, false);
telnets_active = false;
break;
}
cryptlib_error_message(status, "sending data");
atomic_store(&telnets_active, false);
telnets_active = false;
break;
}
sent += ret;
......@@ -113,14 +112,6 @@ telnets_output_thread(void *args)
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
telnets_connect(struct bbslist *bbs)
{
......@@ -130,8 +121,7 @@ telnets_connect(struct bbslist *bbs)
if (!bbs->hidepopups)
init_uifc(true, true);
pthread_mutex_init(&telnets_mutex, NULL);
pthread_once(&telnets_active_once, init_once);
atomic_store(&telnets_active, false);
telnets_active = false;
telnets_sock = conn_socket_connect(bbs);
if (telnets_sock == INVALID_SOCKET)
......@@ -189,7 +179,7 @@ telnets_connect(struct bbslist *bbs)
return -1;
}
atomic_store(&telnets_active, true);
telnets_active = true;
if (!bbs->hidepopups) {
/* Clear ownership */
uifc.pop(NULL); // TODO: Why is this called twice?
......@@ -203,7 +193,7 @@ telnets_connect(struct bbslist *bbs)
conn_api.terminate = 1;
if (!bbs->hidepopups)
uifc.pop(NULL);
atomic_store(&telnets_active, false);
telnets_active = false;
return -1;
}
if (!bbs->hidepopups)
......@@ -233,7 +223,7 @@ telnets_close(void)
char garbage[1024];
conn_api.terminate = 1;
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) {
conn_recv_upto(garbage, sizeof(garbage), 0);
SLEEP(1);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment