From 0eb89e40a95931d7f36aef98561781f91914f8f2 Mon Sep 17 00:00:00 2001 From: fujita Date: Wed, 4 Feb 2009 02:55:52 +0000 Subject: [PATCH] =?utf8?q?mutex=E3=82=92shared=5Fptr=E3=81=A7=E7=AE=A1?= =?utf8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit git-svn-id: http://10.144.169.20/repos/um/branches/l7vsd-3.x-shamshel@6580 1ed66053-1c2d-0410-8867-f7571e6e31d3 --- include/realserver.h | 31 +++++++++++++++++++++----- unit_tests/realserver_test/Makefile | 2 +- unit_tests/realserver_test/realserver_test.cpp | 14 ++++++++++-- 3 files changed, 38 insertions(+), 9 deletions(-) diff --git a/include/realserver.h b/include/realserver.h index e59a4785..0d74f102 100644 --- a/include/realserver.h +++ b/include/realserver.h @@ -12,24 +12,33 @@ #define REALSERVER_H #include +#include #include "realserver_element.h" namespace l7vs{ class realserver : public realserver_element{ +public: + typedef boost::shared_ptr 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; } diff --git a/unit_tests/realserver_test/Makefile b/unit_tests/realserver_test/Makefile index a9bbd886..ed5b1d67 100644 --- a/unit_tests/realserver_test/Makefile +++ b/unit_tests/realserver_test/Makefile @@ -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) diff --git a/unit_tests/realserver_test/realserver_test.cpp b/unit_tests/realserver_test/realserver_test.cpp index f47ebb27..f6006d7b 100644 --- a/unit_tests/realserver_test/realserver_test.cpp +++ b/unit_tests/realserver_test/realserver_test.cpp @@ -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(); } -- 2.11.0