|
From: <sv...@va...> - 2010-03-02 08:58:07
|
Author: bart
Date: 2010-03-02 08:57:50 +0000 (Tue, 02 Mar 2010)
New Revision: 11059
Log:
Added the source code of one more unit test.
Added:
trunk/drd/tests/annotate_smart_pointer.cpp
Modified:
trunk/drd/tests/
trunk/drd/tests/Makefile.am
Property changes on: trunk/drd/tests
___________________________________________________________________
Name: svn:ignore
- *.dSYM
*.stderr.diff*
*.stderr.out
*.stdout.diff*
*.stdout.out
.deps
annotate_ignore_rw
annotate_ignore_write
annotate_publish_hg
annotate_rwlock
atomic_var
bar_bad
bar_trivial
boost_thread
circular_buffer
custom_alloc
drd_bitmap_test
fp_race
hg01_all_ok
hg02_deadlock
hg03_inherit
hg04_race
hg05_race2
hg06_readshared
hold_lock
linuxthreads_det
Makefile
Makefile.in
matinv
memory_allocation
monitor_example
new_delete
omp_matinv
omp_prime
omp_printf
pth_barrier
pth_barrier_race
pth_barrier_reinit
pth_broadcast
pth_cancel_locked
pth_cleanup_handler
pth_cond_race
pth_create_chain
pth_create_glibc_2_0
pth_detached
pth_detached_sem
pth_inconsistent_cond_wait
pth_mutex_reinit
pth_process_shared_mutex
pth_spinlock
qt4_atomic
qt4_mutex
qt4_rwlock
qt4_semaphore
recursive_mutex
rwlock_race
rwlock_test
rwlock_type_checking
sem_as_mutex
sem_open
sigalrm
tc01_simple_race
tc02_simple_tls
tc03_re_excl
tc04_free_lock
tc05_simple_race
tc06_two_races
tc07_hbl1
tc08_hbl2
tc09_bad_unlock
tc10_rec_lock
tc11_XCHG
tc12_rwl_trivial
tc13_laog1
tc15_laog_lockdel
tc16_byterace
tc17_sembar
tc18_semabuse
tc19_shadowmem
tc20_verifywrap
tc21_pthonce
tc22_exit_w_lock
tc23_bogus_condwait
tc24_nonzero_sem
thread_name
trylock
tsan_unittest
unit_bitmap
unit_vc
vg_regtest.tmp*
+ *.dSYM
*.stderr.diff*
*.stderr.out
*.stdout.diff*
*.stdout.out
.deps
Makefile
Makefile.in
annotate_ignore_rw
annotate_ignore_write
annotate_publish_hg
annotate_rwlock
annotate_smart_pointer
atomic_var
bar_bad
bar_trivial
boost_thread
circular_buffer
custom_alloc
drd_bitmap_test
fp_race
hg01_all_ok
hg02_deadlock
hg03_inherit
hg04_race
hg05_race2
hg06_readshared
hold_lock
linuxthreads_det
matinv
memory_allocation
monitor_example
new_delete
omp_matinv
omp_prime
omp_printf
pth_barrier
pth_barrier_race
pth_barrier_reinit
pth_broadcast
pth_cancel_locked
pth_cleanup_handler
pth_cond_race
pth_create_chain
pth_create_glibc_2_0
pth_detached
pth_detached_sem
pth_inconsistent_cond_wait
pth_mutex_reinit
pth_process_shared_mutex
pth_spinlock
qt4_atomic
qt4_mutex
qt4_rwlock
qt4_semaphore
recursive_mutex
rwlock_race
rwlock_test
rwlock_type_checking
sem_as_mutex
sem_open
sigalrm
tc01_simple_race
tc02_simple_tls
tc03_re_excl
tc04_free_lock
tc05_simple_race
tc06_two_races
tc07_hbl1
tc08_hbl2
tc09_bad_unlock
tc10_rec_lock
tc11_XCHG
tc12_rwl_trivial
tc13_laog1
tc15_laog_lockdel
tc16_byterace
tc17_sembar
tc18_semabuse
tc19_shadowmem
tc20_verifywrap
tc21_pthonce
tc22_exit_w_lock
tc23_bogus_condwait
tc24_nonzero_sem
thread_name
trylock
tsan_unittest
unit_bitmap
unit_vc
vg_regtest.tmp*
Modified: trunk/drd/tests/Makefile.am
===================================================================
--- trunk/drd/tests/Makefile.am 2010-03-01 16:42:56 UTC (rev 11058)
+++ trunk/drd/tests/Makefile.am 2010-03-02 08:57:50 UTC (rev 11059)
@@ -281,8 +281,12 @@
endif
if HAVE_BUILTIN_ATOMIC
-check_PROGRAMS += annotate_rwlock atomic_var circular_buffer
-check_PROGRAMS += tsan_unittest
+check_PROGRAMS += \
+ annotate_rwlock \
+ annotate_smart_pointer \
+ atomic_var \
+ circular_buffer \
+ tsan_unittest
endif
if HAVE_OPENMP
@@ -339,6 +343,10 @@
boost_thread_LDADD = $(BOOST_LIBS)
endif
+if HAVE_BUILTIN_ATOMIC
+annotate_smart_pointer_SOURCES = annotate_smart_pointer.cpp
+endif
+
if HAVE_OPENMP
omp_matinv_CFLAGS = $(AM_CFLAGS) -fopenmp
omp_matinv_LDFLAGS = -fopenmp
Added: trunk/drd/tests/annotate_smart_pointer.cpp
===================================================================
--- trunk/drd/tests/annotate_smart_pointer.cpp (rev 0)
+++ trunk/drd/tests/annotate_smart_pointer.cpp 2010-03-02 08:57:50 UTC (rev 11059)
@@ -0,0 +1,162 @@
+#include <cassert>
+#include <iostream>
+#include <pthread.h>
+#include <semaphore.h>
+
+template<class T>
+class smart_ptr
+{
+public:
+ typedef unsigned counter_t;
+
+ template <typename Q> friend class smart_ptr;
+
+ explicit smart_ptr()
+ : m_ptr(NULL), m_count_ptr(NULL)
+ { }
+
+ explicit smart_ptr(T* const pT)
+ : m_ptr(NULL), m_count_ptr(NULL)
+ {
+ set(pT, pT ? new counter_t(0) : NULL);
+ }
+
+ template <typename Q>
+ explicit smart_ptr(Q* const q)
+ : m_ptr(NULL), m_count_ptr(NULL)
+ {
+ set(q, q ? new counter_t(0) : NULL);
+ }
+
+ ~smart_ptr()
+ {
+ set(NULL, NULL);
+ }
+
+ smart_ptr(const smart_ptr<T>& sp)
+ : m_ptr(NULL), m_count_ptr(NULL)
+ {
+ set(sp.m_ptr, sp.m_count_ptr);
+ }
+
+ template <typename Q>
+ smart_ptr(const smart_ptr<Q>& sp)
+ : m_ptr(NULL), m_count_ptr(NULL)
+ {
+ set(sp.m_ptr, sp.m_count_ptr);
+ }
+
+ smart_ptr& operator=(const smart_ptr<T>& sp)
+ {
+ set(sp.m_ptr, sp.m_count_ptr);
+ return *this;
+ }
+
+ smart_ptr& operator=(T* const p)
+ {
+ set(p, p ? new counter_t(0) : NULL);
+ return *this;
+ }
+
+ template <typename Q>
+ smart_ptr& operator=(Q* const q)
+ {
+ set(q, q ? new counter_t(0) : NULL);
+ return *this;
+ }
+
+ T* operator->() const
+ {
+ assert(m_ptr);
+ return m_ptr;
+ }
+
+ T& operator*() const
+ {
+ assert(m_ptr);
+ return *m_ptr;
+ }
+
+private:
+ void set(T* const pT, volatile counter_t* const count_ptr)
+ {
+ if (m_ptr != pT)
+ {
+ if (m_count_ptr && __sync_sub_and_fetch(m_count_ptr, 1) == 0)
+ {
+ delete m_ptr;
+ delete m_count_ptr;
+ }
+ m_ptr = pT;
+ m_count_ptr = count_ptr;
+ if (count_ptr)
+ __sync_add_and_fetch(count_ptr, 1);
+ }
+ }
+
+ T* m_ptr;
+ volatile counter_t* m_count_ptr;
+};
+
+class counter
+{
+public:
+ counter()
+ : m_mutex(), m_count()
+ {
+ pthread_mutex_init(&m_mutex, NULL);
+ }
+ ~counter()
+ {
+ m_count = -1;
+ pthread_mutex_destroy(&m_mutex);
+ }
+ int get() const
+ {
+ int result;
+ pthread_mutex_lock(&m_mutex);
+ result = m_count;
+ pthread_mutex_unlock(&m_mutex);
+ return result;
+ }
+ int post_increment()
+ {
+ int result;
+ pthread_mutex_lock(&m_mutex);
+ result = m_count++;
+ pthread_mutex_unlock(&m_mutex);
+ return result;
+ }
+
+private:
+ mutable pthread_mutex_t m_mutex;
+ int m_count;
+};
+
+static sem_t s_sem;
+
+static void* thread_func(void* arg)
+{
+ smart_ptr<counter> p(*reinterpret_cast<smart_ptr<counter>*>(arg));
+ sem_post(&s_sem);
+ p->post_increment();
+ p = NULL;
+ return NULL;
+}
+
+int main(int argc, char** argv)
+{
+ smart_ptr<counter> p(new counter);
+ pthread_t tid;
+
+ sem_init(&s_sem, 0, 0);
+ p->post_increment();
+ pthread_create(&tid, NULL, thread_func, &p);
+ // Wait until the created thread has copied the shared pointer.
+ sem_wait(&s_sem);
+ p = NULL;
+ pthread_join(tid, NULL);
+ sem_destroy(&s_sem);
+ std::cout << "Done.\n";
+ return 0;
+}
Property changes on: trunk/drd/tests/annotate_smart_pointer.cpp
___________________________________________________________________
Name: svn:executable
+ *
|