Use mutex for thread safety
This commit is contained in:
parent
d159bd84b7
commit
17d05b4a19
2 changed files with 19 additions and 2 deletions
|
@ -27,6 +27,7 @@
|
||||||
#include <random>
|
#include <random>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
#include <mutex>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include "TSQueue.hpp"
|
#include "TSQueue.hpp"
|
||||||
|
@ -256,6 +257,7 @@ public:
|
||||||
|
|
||||||
std::thread thread;
|
std::thread thread;
|
||||||
std::atomic_bool threadRunning;
|
std::atomic_bool threadRunning;
|
||||||
|
std::mutex mutex;
|
||||||
|
|
||||||
}; // struct Context
|
}; // struct Context
|
||||||
|
|
||||||
|
|
|
@ -97,7 +97,8 @@ loggingType(INFO),
|
||||||
loggingType(WARNING),
|
loggingType(WARNING),
|
||||||
#endif
|
#endif
|
||||||
atostrBufIndex(0),
|
atostrBufIndex(0),
|
||||||
rng_engine()
|
rng_engine(),
|
||||||
|
mutex()
|
||||||
{
|
{
|
||||||
if(isThreaded) {
|
if(isThreaded) {
|
||||||
flags.set(0);
|
flags.set(0);
|
||||||
|
@ -796,7 +797,9 @@ void UDPC::threadedUpdate(Context *ctx) {
|
||||||
decltype(now) nextNow;
|
decltype(now) nextNow;
|
||||||
while(ctx->threadRunning.load()) {
|
while(ctx->threadRunning.load()) {
|
||||||
now = std::chrono::steady_clock::now();
|
now = std::chrono::steady_clock::now();
|
||||||
|
ctx->mutex.lock();
|
||||||
ctx->update_impl();
|
ctx->update_impl();
|
||||||
|
ctx->mutex.unlock();
|
||||||
nextNow = std::chrono::steady_clock::now();
|
nextNow = std::chrono::steady_clock::now();
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(33) - (nextNow - now));
|
std::this_thread::sleep_for(std::chrono::milliseconds(33) - (nextNow - now));
|
||||||
}
|
}
|
||||||
|
@ -907,9 +910,10 @@ void UDPC_client_initiate_connection(UDPC_HContext ctx, UDPC_ConnectionId connec
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::lock_guard<std::mutex> lock(c->mutex);
|
||||||
|
|
||||||
UDPC::ConnectionData newCon(false, c);
|
UDPC::ConnectionData newCon(false, c);
|
||||||
|
|
||||||
// TODO make thread safe by using mutex
|
|
||||||
c->conMap.insert(std::make_pair(connectionId, std::move(newCon)));
|
c->conMap.insert(std::make_pair(connectionId, std::move(newCon)));
|
||||||
auto addrConIter = c->addrConMap.find(connectionId.addr);
|
auto addrConIter = c->addrConMap.find(connectionId.addr);
|
||||||
if(addrConIter == c->addrConMap.end()) {
|
if(addrConIter == c->addrConMap.end()) {
|
||||||
|
@ -929,6 +933,8 @@ int UDPC_get_queue_send_available(UDPC_HContext ctx, UDPC_ConnectionId connectio
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::lock_guard<std::mutex> lock(c->mutex);
|
||||||
|
|
||||||
auto iter = c->conMap.find(connectionId);
|
auto iter = c->conMap.find(connectionId);
|
||||||
if(iter != c->conMap.end()) {
|
if(iter != c->conMap.end()) {
|
||||||
return iter->second.sendPkts.capacity() - iter->second.sendPkts.size();
|
return iter->second.sendPkts.capacity() - iter->second.sendPkts.size();
|
||||||
|
@ -948,6 +954,8 @@ void UDPC_queue_send(UDPC_HContext ctx, UDPC_ConnectionId destinationId,
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::lock_guard<std::mutex> lock(c->mutex);
|
||||||
|
|
||||||
auto iter = c->conMap.find(destinationId);
|
auto iter = c->conMap.find(destinationId);
|
||||||
if(iter == c->conMap.end()) {
|
if(iter == c->conMap.end()) {
|
||||||
c->log(
|
c->log(
|
||||||
|
@ -974,6 +982,7 @@ int UDPC_set_accept_new_connections(UDPC_HContext ctx, int isAccepting) {
|
||||||
if(!c) {
|
if(!c) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
std::lock_guard<std::mutex> lock(c->mutex);
|
||||||
return c->isAcceptNewConnections.exchange(isAccepting == 0 ? false : true);
|
return c->isAcceptNewConnections.exchange(isAccepting == 0 ? false : true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -983,6 +992,8 @@ int UDPC_drop_connection(UDPC_HContext ctx, UDPC_ConnectionId connectionId, bool
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::lock_guard<std::mutex> lock(c->mutex);
|
||||||
|
|
||||||
if(dropAllWithAddr) {
|
if(dropAllWithAddr) {
|
||||||
auto addrConIter = c->addrConMap.find(connectionId.addr);
|
auto addrConIter = c->addrConMap.find(connectionId.addr);
|
||||||
if(addrConIter != c->addrConMap.end()) {
|
if(addrConIter != c->addrConMap.end()) {
|
||||||
|
@ -1025,6 +1036,7 @@ uint32_t UDPC_set_protocol_id(UDPC_HContext ctx, uint32_t id) {
|
||||||
if(!c) {
|
if(!c) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
std::lock_guard<std::mutex> lock(c->mutex);
|
||||||
return c->protocolID.exchange(id);
|
return c->protocolID.exchange(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1041,6 +1053,9 @@ UDPC_PacketInfo UDPC_get_received(UDPC_HContext ctx) {
|
||||||
if(!c) {
|
if(!c) {
|
||||||
return UDPC::get_empty_pinfo();
|
return UDPC::get_empty_pinfo();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::lock_guard<std::mutex> lock(c->mutex);
|
||||||
|
|
||||||
// TODO impl
|
// TODO impl
|
||||||
return UDPC::get_empty_pinfo();
|
return UDPC::get_empty_pinfo();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue