瀏覽代碼

增加CountBarrier类,test暂未通过

Tang Hai 14 年之前
父節點
當前提交
223ce3322e

+ 2 - 0
Src/Egametang/Base/Marcos.h

@@ -5,4 +5,6 @@
 
 
 #define private public
 #define private public
 
 
+#define protected public
+
 #endif // BASE_MARCOS_H
 #endif // BASE_MARCOS_H

+ 2 - 2
Src/Egametang/Python/CMakeLists.txt

@@ -4,7 +4,7 @@ set(PythonSrc
 )
 )
 add_library(PythonEntry ${PythonSrc})
 add_library(PythonEntry ${PythonSrc})
 
 
-include_directories(/usr/include/python2.5)
+include_directories(/usr/include/python2.6)
 
 
 add_executable(PythonInitTest PythonInitTest.cc)
 add_executable(PythonInitTest PythonInitTest.cc)
 add_executable(PythonEntryTest PythonEntryTest.cc)
 add_executable(PythonEntryTest PythonEntryTest.cc)
@@ -18,7 +18,7 @@ foreach(Excute ${Excutes})
 	target_link_libraries(${Excute}
 	target_link_libraries(${Excute}
 		PythonEntry
 		PythonEntry
 		boost_python
 		boost_python
-		python2.5
+		python2.6
 		glog
 		glog
 		gtest
 		gtest
 		gmock
 		gmock

+ 17 - 8
Src/Egametang/Thread/CMakeLists.txt

@@ -1,15 +1,24 @@
-set(ThreadSrc ThreadPool.cc)
+set(ThreadSrc 
+	ThreadPool.cc
+	CountBarrier.cc)
 
 
 add_library(Thread ${ThreadSrc})
 add_library(Thread ${ThreadSrc})
 
 
 add_executable(ThreadPoolTest ThreadPoolTest.cc)
 add_executable(ThreadPoolTest ThreadPoolTest.cc)
+add_executable(CountBarrierTest CountBarrierTest.cc)
 
 
-target_link_libraries(ThreadPoolTest
-	Thread
-	boost_thread
-	glog
-	gtest
-	gmock
+set(Excutes 
+	ThreadPoolTest 
+	CountBarrierTest
 )
 )
 
 
-add_test(ThreadPoolTest ThreadPoolTest)
+foreach(Excute ${Excutes})
+	target_link_libraries(${Excute}
+		Thread
+		boost_thread
+		glog
+		gtest
+		gmock
+	)
+	add_test(${Excute} ${Excute})
+endforeach()

+ 35 - 0
Src/Egametang/Thread/CountBarrier.cc

@@ -0,0 +1,35 @@
+#include "Thread/CountBarrier.h"
+
+namespace Egametang {
+
+CountBarrier::CountBarrier(int count):
+	count_(count), mutex_(), condition_()
+{
+}
+
+void CountBarrier::Wait()
+{
+	boost::mutex::scoped_lock lock(mutex_);
+	while (count_ > 0)
+	{
+		condition_.wait(lock);
+	}
+}
+
+void CountBarrier::Signal()
+{
+	boost::mutex::scoped_lock lock(mutex_);
+	--count_;
+	if (count_ == 0)
+	{
+		condition_.notify_all();
+	}
+}
+
+int CountBarrier::Count() const
+{
+	boost::mutex::scoped_lock lock(mutex_);
+	return count_;
+}
+
+} // namespace Egametang

+ 24 - 0
Src/Egametang/Thread/CountBarrier.h

@@ -0,0 +1,24 @@
+#ifndef THREAD_COUNT_LATCH_H
+#define THREAD_COUNT_LATCH_H
+
+#include <boost/thread.hpp>
+
+namespace Egametang {
+
+class CountBarrier
+{
+private:
+	int count_;
+	mutable boost::mutex mutex_;
+	boost::condition_variable condition_;
+
+public:
+	explicit CountBarrier(int count);
+	void Wait();
+	void Signal();
+	int Count() const;
+};
+
+} // namespace Egametang
+
+#endif // THREAD_COUNT_LATCH_H

+ 70 - 0
Src/Egametang/Thread/CountBarrierTest.cc

@@ -0,0 +1,70 @@
+#include <boost/bind.hpp>
+#include <boost/ref.hpp>
+#include <boost/detail/atomic_count.hpp>
+#include <gtest/gtest.h>
+#include <glog/logging.h>
+#include <gflags/gflags.h>
+#include "Thread/ThreadPool.h"
+#include "Thread/CountBarrier.h"
+
+namespace Egametang {
+
+class CountBarrierTest: public testing::Test
+{
+protected:
+	boost::detail::atomic_count count_;
+public:
+	CountBarrierTest():
+		count_(0)
+	{
+	}
+	void Wait(CountBarrier& barrier)
+	{
+		barrier.Wait();
+		++count_;
+		VLOG(3) << "count barrier test wait end";
+	}
+	void Signal(CountBarrier& barrier)
+	{
+		barrier.Signal();
+	}
+};
+
+TEST_F(CountBarrierTest, Count)
+{
+	CountBarrier barrier(10);
+	ASSERT_EQ(10, barrier.Count());
+}
+
+TEST_F(CountBarrierTest, WaitAndSignal)
+{
+	CountBarrier barrier(10);
+	ThreadPool pool(11);
+	for (int i = 0; i < 10; ++i)
+	{
+		pool.PushTask(
+				boost::bind(&CountBarrierTest::Wait,
+						this, boost::ref(barrier)));
+	}
+	ASSERT_EQ(0, this->count_);
+	for (int i = 0; i < 10; ++i)
+	{
+		pool.PushTask(
+				boost::bind(&CountBarrierTest::Signal,
+						this, boost::ref(barrier)));
+	}
+	pool.Stop();
+	ASSERT_EQ(10, this->count_);
+}
+
+} // namespace Egametang
+
+int main(int argc, char* argv[])
+{
+	FLAGS_logtostderr = true;
+	testing::InitGoogleTest(&argc, argv);
+	google::ParseCommandLineFlags(&argc, &argv, true);
+	google::InitGoogleLogging(argv[0]);
+	return RUN_ALL_TESTS();
+}
+

+ 3 - 1
Src/Egametang/Thread/ThreadPoolTest.cc

@@ -30,7 +30,8 @@ TEST_F(ThreadPoolTest, Test1)
 	for (int i = 0; i < 100; ++i)
 	for (int i = 0; i < 100; ++i)
 	{
 	{
 		pool_.PushTask(
 		pool_.PushTask(
-				boost::bind(&ThreadPoolTest::Max, this, x[i], y[i], &z[i]));
+				boost::bind(&ThreadPoolTest::Max,
+						this, x[i], y[i], &z[i]));
 	}
 	}
 	pool_.Stop();
 	pool_.Stop();
 	for (int i = 0; i < 100; ++i)
 	for (int i = 0; i < 100; ++i)
@@ -38,6 +39,7 @@ TEST_F(ThreadPoolTest, Test1)
 		ASSERT_EQ(9, z[i]) << "i = " << i;
 		ASSERT_EQ(9, z[i]) << "i = " << i;
 	}
 	}
 }
 }
+
 } // namespace Egametang
 } // namespace Egametang
 
 
 int main(int argc, char* argv[])
 int main(int argc, char* argv[])

+ 10 - 0
Tools/dbg.rb

@@ -0,0 +1,10 @@
+#!/usr/bin/env ruby
+
+def main()
+	puts "tanghai"
+end
+
+if __FILE__ == $0
+	# TODO Generated stub
+	main()
+end