OSDN Git Service

mutexをshared_ptrで管理
authorfujita <fujita@1ed66053-1c2d-0410-8867-f7571e6e31d3>
Wed, 4 Feb 2009 02:55:52 +0000 (02:55 +0000)
committerfujita <fujita@1ed66053-1c2d-0410-8867-f7571e6e31d3>
Wed, 4 Feb 2009 02:55:52 +0000 (02:55 +0000)
git-svn-id: http://10.144.169.20/repos/um/branches/l7vsd-3.x-shamshel@6580 1ed66053-1c2d-0410-8867-f7571e6e31d3

include/realserver.h
unit_tests/realserver_test/Makefile
unit_tests/realserver_test/realserver_test.cpp

index e59a478..0d74f10 100644 (file)
 #define        REALSERVER_H
 
 #include       <boost/thread.hpp>
+#include       <boost/shared_ptr.hpp>
 #include       "realserver_element.h"
 
 namespace l7vs{
 
 class  realserver : public realserver_element{
+public:
+       typedef boost::shared_ptr<boost::mutex>         mutex_ptr;
 protected:
-       int                             nactive;
-       int                             ninact;
-       boost::mutex    active_mutex;
-       boost::mutex    inact_mutex;
+       int                                     nactive;
+       int                                     ninact;
+       mutex_ptr                       active_mutex_ptr;
+       mutex_ptr                       inact_mutex_ptr;
 public:
        unsigned long long      send_byte;
 
-       realserver() : nactive(0), ninact(0), send_byte(0LL){}
+       realserver() : nactive(0), ninact(0), send_byte(0LL){
+               active_mutex_ptr = mutex_ptr( new boost::mutex );
+               inact_mutex_ptr = mutex_ptr( new boost::mutex );
+       }
        realserver( const realserver& in ) : realserver_element( in ),
                                                                                 nactive( in.nactive ),
                                                                                 ninact( in.ninact ),
-                                                                                send_byte( in.send_byte ){}
+                                                                                send_byte( in.send_byte ){
+               active_mutex_ptr = mutex_ptr( new boost::mutex );
+               inact_mutex_ptr = mutex_ptr( new boost::mutex );
+       }
 
        realserver& operator=( const realserver& rs ){
                realserver_element::operator= (rs);
@@ -66,25 +75,35 @@ public:
        }
 
        void    increment_active(){
+               boost::mutex::scoped_lock( active_mutex_ptr );
+
                nactive++;
                if ( nactive == INT_MAX ){
                        nactive = 0;
                }
        }
+
        void    decrement_active(){
+               boost::mutex::scoped_lock( active_mutex_ptr );
+
                if ( nactive > 0 ){
                        nactive--;
                }
        }
+
        void    increment_inact(){
+               boost::mutex::scoped_lock( inact_mutex_ptr );
+
                ninact++;
                if ( ninact == INT_MAX ){
                        ninact = 0;
                }
        }
+
        int             get_active(){
                return nactive;
        }
+
        int             get_inact(){
                return ninact;
        }
index a9bbd88..ed5b1d6 100644 (file)
@@ -44,7 +44,7 @@ LOGGER_OBJS   = $(LOGGER_SRCS:.cpp=.o)
 
 all:   $(TARGET)
 
-$(TARGET):     $(OBJS)
+$(TARGET):     $(OBJS) ../../include/realserver.h
        $(CPP) $(INCLUDES) -o $@ $(OBJS) $(LIBS)
 
 #$(TARGET):    $(OBJS) $(PARAMETER) $(LOGGER)
index f47ebb2..f6006d7 100644 (file)
@@ -24,8 +24,16 @@ public:
        //! destractor
        ~realserver_fake(){}
 
-    void       set_active( const int in_active ){ nactive = in_active ; }
-    void       set_inact( const int in_inact ){ ninact = in_inact ; }
+    void       set_active( const int in_active ){ 
+               boost::mutex::scoped_lock( active_mutex_ptr );
+
+               nactive = in_active ;
+       }
+    void       set_inact( const int in_inact ){
+               boost::mutex::scoped_lock( inact_mutex_ptr );
+
+               ninact = in_inact ;
+       }
 
        void    increment_active2( const std::string& msg1, const std::string& msg2 ){
                boost::mutex::scoped_lock       lock( starting_mutex );
@@ -352,6 +360,7 @@ void        realserver_test(){
        BOOST_CHECK_EQUAL( server1.get_active(), 0 );
 
        // unit_test[26]  接続数インクリメントメソッドのテスト2(上限INT_MAXに達すると0にクリア)
+       BOOST_MESSAGE( "wait a minute to INT_MAX" );
        for ( loop = 0; loop < INT_MAX; loop++ ){
                server1.increment_active();
        }
@@ -375,6 +384,7 @@ void        realserver_test(){
        BOOST_CHECK_EQUAL( server1.get_inact(), 1 );
 
        // unit_test[29]  切断数インクリメントメソッドのテスト2(上限INT_MAXに達すると0にクリア)
+       BOOST_MESSAGE( "wait a minute to INT_MAX" );
        for ( loop = server1.get_inact(); loop < INT_MAX; loop++ ){
                server1.increment_inact();
        }