From 1bc9e6bdba70fd2e23d087a2ad9823b1386cd342 Mon Sep 17 00:00:00 2001 From: Bert Hubert Date: Sun, 29 Aug 2010 14:05:18 +0000 Subject: [PATCH] implement atomic counters for tcp connection tracking git-svn-id: svn://svn.powerdns.com/pdns/trunk/pdns@1698 d19b8d6e-7fed-0310-83ef-9ca221ded41b --- pdns/misc.hh | 29 +++++++++++++++++++++++++++++ pdns/pdns_recursor.cc | 6 +++--- pdns/syncres.hh | 2 +- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/pdns/misc.hh b/pdns/misc.hh index 50d9e3515..71c8f6390 100644 --- a/pdns/misc.hh +++ b/pdns/misc.hh @@ -343,6 +343,35 @@ inline bool pdns_iequals(const std::string& a, const std::string& b) return aLen == bLen; // strings are equal (in length) } +// lifted from boost, with thanks +class AtomicCounter +{ +public: + + explicit AtomicCounter( unsigned int v = 0) : value_( v ) {} + + void operator++() + { + __sync_add_and_fetch( &value_, 1 ); + } + + unsigned int operator--() + { + return __sync_add_and_fetch( &value_, -1 ); + } + + operator unsigned int() const + { + return __sync_fetch_and_add( &value_, 0 ); + } + +private: + AtomicCounter(AtomicCounter const &); + AtomicCounter &operator=(AtomicCounter const &); + + mutable unsigned int value_; +}; + struct CIStringCompare: public binary_function { diff --git a/pdns/pdns_recursor.cc b/pdns/pdns_recursor.cc index 8f1336439..dc3af7eab 100644 --- a/pdns/pdns_recursor.cc +++ b/pdns/pdns_recursor.cc @@ -452,7 +452,7 @@ tcpClientCounts_t __thread* t_tcpClientCounts; TCPConnection::TCPConnection(int fd, const ComboAddress& addr) : d_remote(addr), d_fd(fd) { - s_currentConnections++; + ++s_currentConnections; (*t_tcpClientCounts)[d_remote]++; } @@ -462,10 +462,10 @@ TCPConnection::~TCPConnection() unixDie("closing socket for TCPConnection"); if(t_tcpClientCounts->count(d_remote) && !(*t_tcpClientCounts)[d_remote]--) t_tcpClientCounts->erase(d_remote); - s_currentConnections--; + --s_currentConnections; } -volatile unsigned int TCPConnection::s_currentConnections; +AtomicCounter TCPConnection::s_currentConnections; void handleRunningTCPQuestion(int fd, FDMultiplexer::funcparam_t& var); void updateRcodeStats(int res) diff --git a/pdns/syncres.hh b/pdns/syncres.hh index 6ab686a21..9d11b9dbc 100644 --- a/pdns/syncres.hh +++ b/pdns/syncres.hh @@ -499,7 +499,7 @@ public: static unsigned int getCurrentConnections() { return s_currentConnections; } private: const int d_fd; - static volatile unsigned int s_currentConnections; //!< total number of current TCP connections + static AtomicCounter s_currentConnections; //!< total number of current TCP connections }; -- 2.49.0