From: Bart Van Assche Date: Sun, 7 Mar 2010 10:42:15 +0000 (+0000) Subject: Made the behavior of the annotate_smart_pointer test program configurable X-Git-Tag: svn/VALGRIND_3_6_0~357 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=016af749e0ff162a58acd67da96c8e4ee61fd3fc;p=thirdparty%2Fvalgrind.git Made the behavior of the annotate_smart_pointer test program configurable through command-line arguments, and made sure that the annotations present in the source code are recognized by all Valgrind-based data race detection tools. git-svn-id: svn://svn.valgrind.org/valgrind/trunk@11067 --- diff --git a/drd/tests/annotate_smart_pointer.cpp b/drd/tests/annotate_smart_pointer.cpp index 3c6b7b46c2..c3b9d37ddb 100755 --- a/drd/tests/annotate_smart_pointer.cpp +++ b/drd/tests/annotate_smart_pointer.cpp @@ -27,7 +27,7 @@ #include // assert() #include // PTHREAD_STACK_MIN -#include // std::cout +#include // std::cerr #include // atoi() #ifdef _WIN32 #include // _beginthreadex() @@ -35,7 +35,10 @@ #else #include // pthread_mutex_t #endif -#include "../../drd/drd.h" +#include "unified_annotations.h" + + +static bool s_enable_annotations = true; #ifdef _WIN32 @@ -232,10 +235,15 @@ private: { if (m_count_ptr) { - ANNOTATE_HAPPENS_BEFORE(m_count_ptr); + if (s_enable_annotations) + ANNOTATE_HAPPENS_BEFORE(m_count_ptr); if (--(*m_count_ptr) == 0) { - ANNOTATE_HAPPENS_AFTER(m_count_ptr); + if (s_enable_annotations) + { + ANNOTATE_HAPPENS_AFTER(m_count_ptr); + ANNOTATE_HAPPENS_DONE(m_count_ptr); + } delete m_ptr; m_ptr = NULL; delete m_count_ptr; @@ -300,17 +308,23 @@ static void* thread_func(void* arg) int main(int argc, char** argv) { - smart_ptr p(new counter); const int nthreads = std::max(argc > 1 ? atoi(argv[1]) : 1, 1); - Thread T[nthreads]; - - p->post_increment(); - for (int i = 0; i < nthreads; ++i) - T[i].Create(thread_func, new smart_ptr(p)); - p = NULL; - for (int i = 0; i < nthreads; ++i) - T[i].Join(); - std::cout << "Done.\n"; + const int iterations = std::max(argc > 2 ? atoi(argv[2]) : 1, 1); + s_enable_annotations = argc > 3 ? !!atoi(argv[3]) : true; + + for (int j = 0; j < iterations; ++j) + { + Thread T[nthreads]; + + smart_ptr p(new counter); + p->post_increment(); + for (int i = 0; i < nthreads; ++i) + T[i].Create(thread_func, new smart_ptr(p)); + p = NULL; + for (int i = 0; i < nthreads; ++i) + T[i].Join(); + } + std::cerr << "Done.\n"; return 0; } diff --git a/drd/tests/unified_annotations.h b/drd/tests/unified_annotations.h new file mode 100644 index 0000000000..8bf66888b8 --- /dev/null +++ b/drd/tests/unified_annotations.h @@ -0,0 +1,68 @@ +#ifndef _UNIFIED_ANNOTATIONS_H_ +#define _UNIFIED_ANNOTATIONS_H_ + + +#include "../../drd/drd.h" + + +/* + * Redefine the happens before/after/done annotation macros such that these + * can be intercepted by DRD, Helgrind and ThreadSanitizer. See also + * http://code.google.com/p/data-race-test/source/browse/trunk/dynamic_annotations/dynamic_annotations.h + */ +#undef ANNOTATE_HAPPENS_BEFORE +#define ANNOTATE_HAPPENS_BEFORE(addr) \ + do { \ + DRDCL_(annotate_happens_before)(addr); \ + AnnotateCondVarSignal(__FILE__, __LINE__, addr); \ + } while(0) +#undef ANNOTATE_HAPPENS_AFTER +#define ANNOTATE_HAPPENS_AFTER(addr) \ + do { \ + DRDCL_(annotate_happens_after)(addr); \ + AnnotateCondVarWait(__FILE__, __LINE__, addr, NULL); \ + } while(0) +#undef ANNOTATE_HAPPENS_DONE +#define ANNOTATE_HAPPENS_DONE(addr) \ + do { \ + DRDCL_(annotate_happens_done)(addr); \ + } while(0) + + +#ifdef __cplusplus +extern "C" { +#endif +#if 0 +} +#endif + + +void __attribute__((weak,noinline)) +AnnotateCondVarSignal(const char *file, int line, const volatile void *cv) +{ + asm(""); +} + +void __attribute__((weak,noinline)) +AnnotateCondVarWait(const char *file, int line, const volatile void *cv, + const volatile void *lock) +{ + asm(""); +} + + +#if 0 +{ +#endif +#ifdef __cplusplus +} +#endif + + +#endif /* _UNIFIED_ANNOTATIONS_H_ */ + +/* + * Local variables: + * c-basic-offset: 2 + * End: + */