]> granicus.if.org Git - pdns/commitdiff
implement atomic counters for tcp connection tracking
authorBert Hubert <bert.hubert@netherlabs.nl>
Sun, 29 Aug 2010 14:05:18 +0000 (14:05 +0000)
committerBert Hubert <bert.hubert@netherlabs.nl>
Sun, 29 Aug 2010 14:05:18 +0000 (14:05 +0000)
git-svn-id: svn://svn.powerdns.com/pdns/trunk/pdns@1698 d19b8d6e-7fed-0310-83ef-9ca221ded41b

pdns/misc.hh
pdns/pdns_recursor.cc
pdns/syncres.hh

index 50d9e3515bee408f287f0c780d890cbb66adf70a..71c8f63904bf857e06be770764b19b7e3be13da8 100644 (file)
@@ -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<string, string, bool>  
 {
index 8f1336439a735fae90e4938b6e1e6277d8ce3d60..dc3af7eab64e5b0a148a5ed3e5a334c1e2738518 100644 (file)
@@ -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)
index 6ab686a21047e94ba78e4c45ae57b0d65b8a3b19..9d11b9dbc6f0ca99ee615e3e270d6a81c9493b5f 100644 (file)
@@ -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
 };