|
Boost-Commit : |
From: john.groups_at_[hidden]
Date: 2007-11-03 09:56:35
Author: jtorjo
Date: 2007-11-03 09:56:34 EDT (Sat, 03 Nov 2007)
New Revision: 40721
URL: http://svn.boost.org/trac/boost/changeset/40721
Log:
[logging]
v0.10.5, 3 nov 2007
- added TSS tests
- test test_tss FAILS - to check!
- test test_ts_resource_with_cache - not tested
Added:
sandbox/logging/lib/logging/tests/test_ts_resouce_with_cache/
- copied from r40713, /sandbox/logging/lib/logging/tests/test_ts_resource/
sandbox/logging/lib/logging/tests/test_ts_resouce_with_cache/test_ts_resource.sln
- copied unchanged from r40695, /sandbox/logging/lib/logging/tests/test_ts_resource/test_ts_resource.sln
sandbox/logging/lib/logging/tests/test_ts_resouce_with_cache/test_ts_resource.vcproj
- copied, changed from r40695, /sandbox/logging/lib/logging/tests/test_ts_resource/test_ts_resource.vcproj
Removed:
sandbox/logging/lib/logging/tests/test_ts_resource/
Text files modified:
sandbox/logging/boost/logging/detail/raw_doc/changelog.hpp | 4
sandbox/logging/lib/logging/tests/test_ts_resouce_with_cache/test_ts_resource.cpp | 189 +++++++++++++++++++++++++++++++++++++++
sandbox/logging/lib/logging/tests/test_ts_resouce_with_cache/test_ts_resource.vcproj | 2
sandbox/logging/lib/logging/tests/test_tss/test_tss_objects.cpp | 3
4 files changed, 192 insertions(+), 6 deletions(-)
Modified: sandbox/logging/boost/logging/detail/raw_doc/changelog.hpp
==============================================================================
--- sandbox/logging/boost/logging/detail/raw_doc/changelog.hpp (original)
+++ sandbox/logging/boost/logging/detail/raw_doc/changelog.hpp 2007-11-03 09:56:34 EDT (Sat, 03 Nov 2007)
@@ -1,10 +1,10 @@
/**
@page page_changelog Changelog
-v0.10.4, 2 nov 2007
+v0.10.5, 3 nov 2007
- added TSS tests
- test test_tss FAILS - to check!
- - test test_ts_resource not yet implemented
+ - test test_ts_resource_with_cache - not tested
- added TSS - however, not tested (TSS is off, for now)
- compiles on VS2005, gcc 3.4.2 , gcc 4.1 and gcc 4.3
Modified: sandbox/logging/lib/logging/tests/test_ts_resouce_with_cache/test_ts_resource.cpp
==============================================================================
--- /sandbox/logging/lib/logging/tests/test_ts_resource/test_ts_resource.cpp (original)
+++ sandbox/logging/lib/logging/tests/test_ts_resouce_with_cache/test_ts_resource.cpp 2007-11-03 09:56:34 EDT (Sat, 03 Nov 2007)
@@ -2,20 +2,203 @@
//
/*
+ Tests tss_resource_with_cache
+
I have a vector, that is modified by different threads.
Every once in a while I take a snapshot of this vector. Different threads reading this, they will be equal to the last snapshot, or
the snapshot took before. I repeat this several times, to see that what I write to the vector, really propagates.
+ Changing the vector:
+ - first we start with one element : "0"
+ - at each iteration:
+ - see the last element in the vector = last_val
+ - refill the vector with all elements starting with "last_val + 1"
+ - the vector will have the same size as before
+ - at each 500 iterations
+ - I increase the size of the vector by 1
*/
-// make a scenario where every X seconds, we re-load a certain configuration;
-// like a std::vector or something
-/
+#define BOOST_LOG_TSS_USE_INTERNAL
+// this includes tss_value class
+#include <boost/logging/logging.hpp>
+#include <boost/thread/thread.hpp>
+#include <boost/thread/mutex.hpp>
+#include <boost/thread/xtime.hpp>
+#include <boost/bind.hpp>
+#include <boost/assert.hpp>
+#include <boost/shared_ptr.hpp>
+#include <string>
+#include <vector>
+#include <sstream>
+#include <iostream>
+
+using namespace boost;
+typedef std::vector<int> array;
+typedef logging::locker::tss_resource_with_cache<array> resource;
+
+template<class type> struct ts_value {
+ void set(const type & src) {
+ mutex::scoped_lock lk(cs);
+ m_val = src;
+ }
+
+ type get() const {
+ mutex::scoped_lock lk(cs);
+ return m_val;
+ }
+
+private:
+ mutable mutex cs;
+ type m_val;
+};
+
+// for our resource, at what period is it updated on all threads?
+// note: this value should be at least 5 in order for the test to work:
+// we sleep a bit, then we do lots of modifications, then we sleep a lot - so that the snapshots can be accurately taken
+int g_cache_period_secs = 10;
+
+// the vector we're constantly changing
+resource g_resource( array(), g_cache_period_secs);
+
+// the current value we've set in the resource
+ts_value<array> g_cur_val;
+// make sure only one thread updates the vector
+mutex g_cur_val_cs;
+// the index of the current change
+ts_value<int> g_change_idx;
+// at how many iterations do I increase vector size
+const int INCREASE_VECTOR_SIZE_PERIOD = 500;
+
+// the 2 snapshots of the vector
+ts_value<array> g_snapshot;
+ts_value<array> g_prev_snapshot;
+
+
+void update_cur_val() {
+ // only one thread at a time ;)
+ mutex::scoped_lock lk(g_cur_val_cs);
+
+ array cur = g_cur_val.get();
+ int change_idx = g_change_idx.get();
+ int last_val = 0;
+ if ( !cur.empty() )
+ last_val = cur.back();
+ if ( change_idx % INCREASE_VECTOR_SIZE_PERIOD ) {
+ cur.resize( cur.size() + 1);
+ std::cout << "****** new vector size " << cur.size() + 1;
+ }
+
+ for ( int i = 0 ; i < (int)cur.size(); ++i)
+ cur[i] = ++last_val;
+ g_cur_val.set(cur);
+ g_change_idx.set( g_change_idx.get() + 1) ;
+}
+
+
+void update_resource() {
+ update_cur_val();
+ array cur_val = g_cur_val.get();
+ resource::write res(g_resource);
+ res.use() = cur_val;
+}
+
+
+void get_snapshot() {
+ array snap = g_cur_val.get() ;
+
+ array prev_snapshot = g_snapshot.get();
+ g_snapshot.set(snap);
+ g_prev_snapshot.set( prev_snapshot);
+}
+
+void test_resource() {
+ array cur_val ;
+ {
+ resource::read res(g_resource);
+ cur_val = res.use() ;
+ }
+
+ array snap = g_snapshot.get();
+ array prev_snap = g_snapshot.get();
+
+ BOOST_ASSERT( cur_val == snap || cur_val == prev_snap);
+}
+
+void do_sleep(int ms) {
+ xtime next;
+ xtime_get( &next, TIME_UTC);
+ next.nsec += (ms % 1000) * 1000000;
+
+ int nano_per_sec = 1000000000;
+ next.sec += next.nsec / nano_per_sec;
+ next.sec += ms / 1000;
+ next.nsec %= nano_per_sec;
+ thread::sleep( next);
+}
+
+
+
+
+
+// how many times do we update the resource, in a given pass?
+int g_update_per_thread_count = 200;
+
+ts_value<int> g_thread_idx;
+// start time - we need all update threads to syncronize - to know until when to sleep
+xtime g_start;
+
+void update_thread() {
+ xtime next = g_start;
+ int thread_idx = g_thread_idx.get();
+ g_thread_idx.set(thread_idx + 1);
+
+ while ( true) {
+ next.sec += g_cache_period_secs;
+ do_sleep(1000);
+
+ std::cout << "thread " << thread_idx << " working" << std::endl;
+ for ( int i = 0; i < g_update_per_thread_count ; ++i) {
+ update_resource();
+ do_sleep(10);
+ }
+ std::cout << "thread " << thread_idx << " sleeping" << std::endl;
+
+ thread::sleep( next);
+ }
+}
+
+void test_thread() {
+ while ( true) {
+ do_sleep(100);
+ test_resource();
+ }
+}
+
+
+
+
+
+
+// how many threads that update the resource?
+int g_update_thread_count = 10;
+
+// how many threads that test the resource?
+int g_test_thread_count = 10;
int main()
{
+ xtime_get( &g_start, TIME_UTC);
+
+ for ( int i = 0; i < g_update_thread_count; ++i)
+ thread t(&update_thread);
+
+ for ( int i = 0; i < g_test_thread_count; ++i)
+ thread t(&test_thread);
+
return 0;
}
+
+
Copied: sandbox/logging/lib/logging/tests/test_ts_resouce_with_cache/test_ts_resource.vcproj (from r40695, /sandbox/logging/lib/logging/tests/test_ts_resource/test_ts_resource.vcproj)
==============================================================================
--- /sandbox/logging/lib/logging/tests/test_ts_resource/test_ts_resource.vcproj (original)
+++ sandbox/logging/lib/logging/tests/test_ts_resouce_with_cache/test_ts_resource.vcproj 2007-11-03 09:56:34 EDT (Sat, 03 Nov 2007)
@@ -40,6 +40,7 @@
<Tool
Name="VCCLCompilerTool"
Optimization="0"
+ AdditionalIncludeDirectories=".,../../../.."
PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE"
MinimalRebuild="true"
BasicRuntimeChecks="3"
@@ -61,6 +62,7 @@
<Tool
Name="VCLinkerTool"
LinkIncremental="2"
+ AdditionalLibraryDirectories="D:\boosts\boost_1_33_1\bin\boost\libs\thread\build\libboost_thread.lib\vc-8_0\debug\threading-multi"
GenerateDebugInformation="true"
SubSystem="1"
TargetMachine="1"
Modified: sandbox/logging/lib/logging/tests/test_tss/test_tss_objects.cpp
==============================================================================
--- sandbox/logging/lib/logging/tests/test_tss/test_tss_objects.cpp (original)
+++ sandbox/logging/lib/logging/tests/test_tss/test_tss_objects.cpp 2007-11-03 09:56:34 EDT (Sat, 03 Nov 2007)
@@ -87,10 +87,11 @@
void do_sleep(int ms) {
xtime next;
xtime_get( &next, TIME_UTC);
- next.nsec += ms * 1000;
+ next.nsec += (ms % 1000) * 1000000;
int nano_per_sec = 1000000000;
next.sec += next.nsec / nano_per_sec;
+ next.sec += ms / 1000;
next.nsec %= nano_per_sec;
thread::sleep( next);
}
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk