From ed8ebf26a00858aa49f4d9bf1713c2939914828c Mon Sep 17 00:00:00 2001 From: Tom Early Date: Fri, 10 Feb 2017 12:48:19 -0500 Subject: [PATCH] std::atomic for all keep_running and G2_COUNTER in g2_ircddb --- dvap_rptr.cpp | 3 ++- g2_ircddb.cpp | 25 ++++++++----------------- g2_link.cpp | 4 ++-- 3 files changed, 12 insertions(+), 20 deletions(-) diff --git a/dvap_rptr.cpp b/dvap_rptr.cpp index 729b0e3..110b112 100644 --- a/dvap_rptr.cpp +++ b/dvap_rptr.cpp @@ -41,6 +41,7 @@ #include #include "versions.h" +#include #include #include using namespace libconfig; @@ -161,7 +162,7 @@ static int insock = -1; static struct sockaddr_in outaddr; static int serfd = -1; static bool busy20000 = false; -static bool keep_running = true; +static std::atomic keep_running(true); static unsigned char DVP_RQST_NAME[] = {0x04, 0x20, 0x01, 0x00}; static unsigned char DVP_REPL_NAME[] = {0x10, 0x00, 0x01, 0x00, 'D', 'V', 'A', 'P', ' ', 'D', 'o', 'n', 'g', 'l', 'e', 0x00}; static unsigned char DVP_RQST_SER[] = {0x04, 0x20, 0x02, 0x00}; diff --git a/g2_ircddb.cpp b/g2_ircddb.cpp index 629eda8..6c30fb6 100644 --- a/g2_ircddb.cpp +++ b/g2_ircddb.cpp @@ -48,6 +48,7 @@ #include #include +#include #include #include #include @@ -117,7 +118,7 @@ static struct { uint32_t adr; struct sockaddr_in band_addr; time_t last_time; - unsigned short G2_COUNTER; + std::atomic G2_COUNTER; unsigned char sequence; } toRptr[3]; /* 0=A, 1=B, 2=C */ @@ -128,7 +129,7 @@ static struct sockaddr_in fromRptr; static unsigned char end_of_audio[29]; -static volatile bool keep_running = true; +static std::atomic keep_running(true); /* send packets to g2_link */ static struct sockaddr_in plug; @@ -906,7 +907,7 @@ static void runit() for (j = 0; j < 2; j++) sendto(srv_sock, (char *)end_of_audio, 29, 0, (struct sockaddr *)&toRptr[i].band_addr, sizeof(struct sockaddr_in)); - toRptr[i].G2_COUNTER ++; + toRptr[i].G2_COUNTER++; toRptr[i].streamid[0] = toRptr[i].streamid[1] = '\0'; toRptr[i].adr = 0; @@ -1029,7 +1030,7 @@ static void runit() time(&toRptr[i].last_time); /* bump the G2 counter */ - toRptr[i].G2_COUNTER ++; + toRptr[i].G2_COUNTER++; toRptr[i].sequence = readBuffer[16]; } @@ -1108,7 +1109,7 @@ static void runit() sizeof(struct sockaddr_in)); /* bump G2 counter */ - toRptr[i].G2_COUNTER ++; + toRptr[i].G2_COUNTER++; /* send this audio packet to repeater */ memcpy(readBuffer,"DSTR", 4); @@ -1134,7 +1135,7 @@ static void runit() time(&toRptr[i].last_time); /* bump the G2 counter */ - toRptr[i].G2_COUNTER ++; + toRptr[i].G2_COUNTER++; toRptr[i].sequence = readBuffer[16]; @@ -1483,7 +1484,7 @@ static void runit() time(&toRptr[i].last_time); /* bump the G2 counter */ - toRptr[i].G2_COUNTER ++; + toRptr[i].G2_COUNTER++; toRptr[i].sequence = readBuffer[16]; } @@ -2588,16 +2589,6 @@ static void *echotest(void *arg) else if (dstar_buf[25] == 'C') i = 2; - - /*** - WARNING: G2_COUNTER is accessed by both threads. - It should be protected with a MUTEX, - but since this version is NOT for RP2C, but for home-brew - it does not really matter. - Anyway, accessing G2_COUNTER and adding 1 to it, - should be an atomic operation. - ***/ - memcpy(rptr_buf, "DSTR", 4); rptr_buf[5] = (unsigned char)(toRptr[i].G2_COUNTER & 0xff); rptr_buf[4] = (unsigned char)((toRptr[i].G2_COUNTER >> 8) & 0xff); diff --git a/g2_link.cpp b/g2_link.cpp index 6535bbc..ef22021 100644 --- a/g2_link.cpp +++ b/g2_link.cpp @@ -45,7 +45,7 @@ #include #include - +#include /* Required for Binary search trees using C++ STL */ #include #include @@ -198,7 +198,7 @@ static struct sockaddr_in fromRptr; static fd_set fdset; static struct timeval tv; -static bool keep_running = true; +static std::atomic keep_running(true); /* Used to validate incoming donglers */ static regex_t preg;