From 66d69c2025b17ee3bb7d1a47e931116c831e5a14 Mon Sep 17 00:00:00 2001 From: Bart Van Assche Date: Wed, 17 Dec 2008 19:20:13 +0000 Subject: [PATCH] Fixed semaphore vector clock updating / simplified semaphore tracing. git-svn-id: svn://svn.valgrind.org/valgrind/trunk@8836 --- drd/drd_clientobj.h | 3 +- drd/drd_semaphore.c | 92 +++++++++++++------ .../tc20_verifywrap2.stderr.exp-glibc2.3 | 5 +- .../tc20_verifywrap2.stderr.exp-glibc2.3-b | 5 +- .../tc20_verifywrap2.stderr.exp-glibc2.5 | 5 +- .../tc20_verifywrap2.stderr.exp-glibc2.5-ppc | 5 +- .../tc20_verifywrap2.stderr.exp-glibc2.8 | 5 +- 7 files changed, 77 insertions(+), 43 deletions(-) diff --git a/drd/drd_clientobj.h b/drd/drd_clientobj.h index 39e97f94f3..98f4fd93c3 100644 --- a/drd/drd_clientobj.h +++ b/drd/drd_clientobj.h @@ -32,6 +32,7 @@ #include "pub_tool_basics.h" #include "pub_tool_execontext.h" /* ExeContext */ #include "pub_tool_oset.h" +#include "pub_tool_xarray.h" // Forward declarations. @@ -91,7 +92,7 @@ struct semaphore_info UInt value; // Semaphore value. UWord waiters; // Number of threads inside sem_wait(). DrdThreadId last_sem_post_tid; // Thread ID associated with last sem_post(). - Segment* last_sem_post_segment; + XArray* last_sem_post_seg; // array of Segment*, used as a stack. }; struct barrier_info diff --git a/drd/drd_semaphore.c b/drd/drd_semaphore.c index 0f163053d0..2cc59dc3ad 100644 --- a/drd/drd_semaphore.c +++ b/drd/drd_semaphore.c @@ -31,6 +31,7 @@ #include "pub_tool_libcassert.h" // tl_assert() #include "pub_tool_libcprint.h" // VG_(printf)() #include "pub_tool_machine.h" // VG_(get_IP)() +#include "pub_tool_mallocfree.h" // VG_(malloc), VG_(free) #include "pub_tool_threadstate.h" // VG_(get_running_tid)() @@ -47,6 +48,39 @@ static ULong s_semaphore_segment_creation_count; // Function definitions. +static void segment_push(struct semaphore_info* p, Segment* sg) +{ + Word n; + + tl_assert(sg); + n = VG_(addToXA)(p->last_sem_post_seg, &sg); +#if 0 + VG_(message)(Vg_UserMsg, "0x%lx push: added at position %ld/%ld", + p->a1, n, VG_(sizeXA)(p->last_sem_post_seg)); +#endif + tl_assert(*(Segment**)VG_(indexXA)(p->last_sem_post_seg, n) == sg); +} + +static Segment* segment_pop(struct semaphore_info* p) +{ + Word sz; + Segment* sg; + + sz = VG_(sizeXA)(p->last_sem_post_seg); +#if 0 + VG_(message)(Vg_UserMsg, "0x%lx pop: removed from position %ld/%ld", + p->a1, sz - 1, sz); +#endif + sg = 0; + if (sz > 0) + { + sg = *(Segment**)VG_(indexXA)(p->last_sem_post_seg, sz - 1); + tl_assert(sg); + VG_(dropTailXA)(p->last_sem_post_seg, 1); + } + return sg; +} + void semaphore_set_trace(const Bool trace_semaphore) { s_trace_semaphore = trace_semaphore; @@ -64,7 +98,8 @@ void semaphore_initialize(struct semaphore_info* const p, p->value = value; p->waiters = 0; p->last_sem_post_tid = DRD_INVALID_THREADID; - p->last_sem_post_segment = 0; + p->last_sem_post_seg = VG_(newXA)(VG_(malloc), "drd.sg-stack", + VG_(free), sizeof(Segment*)); } /** Free the memory that was allocated by semaphore_initialize(). Called by @@ -72,6 +107,8 @@ void semaphore_initialize(struct semaphore_info* const p, */ static void semaphore_cleanup(struct semaphore_info* p) { + Segment* sg; + if (p->waiters > 0) { SemaphoreErrInfo sei = { p->a1 }; @@ -82,7 +119,9 @@ static void semaphore_cleanup(struct semaphore_info* p) " upon", &sei); } - sg_put(p->last_sem_post_segment); + while ((sg = segment_pop(p))) + sg_put(sg); + VG_(deleteXA)(p->last_sem_post_seg); } static @@ -180,15 +219,6 @@ void semaphore_pre_wait(const Addr semaphore) struct semaphore_info* p; p = semaphore_get_or_allocate(semaphore); - if (s_trace_semaphore) - { - VG_(message)(Vg_UserMsg, - "[%d/%d] semaphore_pre_wait 0x%lx value %u", - VG_(get_running_tid)(), - thread_get_running_tid(), - semaphore, - p->value); - } tl_assert(p); tl_assert((int)p->waiters >= 0); p->waiters++; @@ -203,17 +233,20 @@ void semaphore_post_wait(const DrdThreadId tid, const Addr semaphore, const Bool waited) { struct semaphore_info* p; + Segment* sg; p = semaphore_get(semaphore); if (s_trace_semaphore) { VG_(message)(Vg_UserMsg, - "[%d/%d] semaphore_post_wait 0x%lx value %u", + "[%d/%d] semaphore_wait 0x%lx value %u -> %u", VG_(get_running_tid)(), thread_get_running_tid(), semaphore, + p ? p->value : 0, p ? p->value - 1 : 0); } + tl_assert(p); tl_assert(p->waiters > 0); p->waiters--; tl_assert((int)p->waiters >= 0); @@ -230,20 +263,25 @@ void semaphore_post_wait(const DrdThreadId tid, const Addr semaphore, } p->value--; tl_assert((int)p->value >= 0); - if (p->last_sem_post_tid != tid - && p->last_sem_post_tid != DRD_INVALID_THREADID) + sg = segment_pop(p); + if (sg) { - tl_assert(p->last_sem_post_segment); - thread_combine_vc2(tid, &p->last_sem_post_segment->vc); + if (p->last_sem_post_tid != tid + && p->last_sem_post_tid != DRD_INVALID_THREADID) + { + thread_combine_vc2(tid, &sg->vc); + } + sg_put(sg); + thread_new_segment(tid); + s_semaphore_segment_creation_count++; } - thread_new_segment(tid); - s_semaphore_segment_creation_count++; } /** Called before sem_post(). */ void semaphore_pre_post(const DrdThreadId tid, const Addr semaphore) { struct semaphore_info* p; + Segment* sg; p = semaphore_get_or_allocate(semaphore); p->value++; @@ -251,20 +289,20 @@ void semaphore_pre_post(const DrdThreadId tid, const Addr semaphore) if (s_trace_semaphore) { VG_(message)(Vg_UserMsg, - "[%d/%d] semaphore_post 0x%lx value %u", + "[%d/%d] semaphore_post 0x%lx value %u -> %u", VG_(get_running_tid)(), thread_get_running_tid(), semaphore, - p->value); + p->value - 1, p->value); } - if (p->value == 1) - { - p->last_sem_post_tid = tid; - thread_new_segment(tid); - thread_get_latest_segment(&p->last_sem_post_segment, tid); - s_semaphore_segment_creation_count++; - } + p->last_sem_post_tid = tid; + thread_new_segment(tid); + sg = 0; + thread_get_latest_segment(&sg, tid); + tl_assert(sg); + segment_push(p, sg); + s_semaphore_segment_creation_count++; } /** Called after sem_post() finished successfully. */ diff --git a/drd/tests/tc20_verifywrap2.stderr.exp-glibc2.3 b/drd/tests/tc20_verifywrap2.stderr.exp-glibc2.3 index 987a55b4cd..9e29a244e5 100644 --- a/drd/tests/tc20_verifywrap2.stderr.exp-glibc2.3 +++ b/drd/tests/tc20_verifywrap2.stderr.exp-glibc2.3 @@ -140,8 +140,7 @@ semaphore 0x........ was first observed at: FIXME: can't figure out how to verify wrap of sem_destroy -[1/1] semaphore_pre_wait 0x........ value 0 -[1/1] semaphore_post_wait 0x........ value 4294967295 +[1/1] semaphore_wait 0x........ value 0 -> 4294967295 Invalid semaphore: semaphore 0x........ at 0x........: sem_wait* (drd_pthread_intercepts.c:?) @@ -149,7 +148,7 @@ Invalid semaphore: semaphore 0x........ semaphore 0x........ was first observed at: at 0x........: sem_init* (drd_pthread_intercepts.c:?) by 0x........: main (tc20_verifywrap.c:228) -[1/1] semaphore_post 0x........ value 1 +[1/1] semaphore_post 0x........ value 0 -> 1 FIXME: can't figure out how to verify wrap of sem_post diff --git a/drd/tests/tc20_verifywrap2.stderr.exp-glibc2.3-b b/drd/tests/tc20_verifywrap2.stderr.exp-glibc2.3-b index 9cf6853256..4be55c3794 100644 --- a/drd/tests/tc20_verifywrap2.stderr.exp-glibc2.3-b +++ b/drd/tests/tc20_verifywrap2.stderr.exp-glibc2.3-b @@ -140,8 +140,7 @@ semaphore 0x........ was first observed at: FIXME: can't figure out how to verify wrap of sem_destroy -[1/1] semaphore_pre_wait 0x........ value 0 -[1/1] semaphore_post_wait 0x........ value 4294967295 +[1/1] semaphore_wait 0x........ value 0 -> 4294967295 Invalid semaphore: semaphore 0x........ at 0x........: sem_wait* (drd_pthread_intercepts.c:?) @@ -149,7 +148,7 @@ Invalid semaphore: semaphore 0x........ semaphore 0x........ was first observed at: at 0x........: sem_init* (drd_pthread_intercepts.c:?) by 0x........: main (tc20_verifywrap.c:228) -[1/1] semaphore_post 0x........ value 1 +[1/1] semaphore_post 0x........ value 0 -> 1 FIXME: can't figure out how to verify wrap of sem_post diff --git a/drd/tests/tc20_verifywrap2.stderr.exp-glibc2.5 b/drd/tests/tc20_verifywrap2.stderr.exp-glibc2.5 index 5eff6ac6bc..9b729075ea 100644 --- a/drd/tests/tc20_verifywrap2.stderr.exp-glibc2.5 +++ b/drd/tests/tc20_verifywrap2.stderr.exp-glibc2.5 @@ -146,8 +146,7 @@ semaphore 0x........ was first observed at: FIXME: can't figure out how to verify wrap of sem_destroy -[1/1] semaphore_pre_wait 0x........ value 0 -[1/1] semaphore_post_wait 0x........ value 4294967295 +[1/1] semaphore_wait 0x........ value 0 -> 4294967295 Invalid semaphore: semaphore 0x........ at 0x........: sem_wait* (drd_pthread_intercepts.c:?) @@ -155,7 +154,7 @@ Invalid semaphore: semaphore 0x........ semaphore 0x........ was first observed at: at 0x........: sem_init* (drd_pthread_intercepts.c:?) by 0x........: main (tc20_verifywrap.c:228) -[1/1] semaphore_post 0x........ value 1 +[1/1] semaphore_post 0x........ value 0 -> 1 FIXME: can't figure out how to verify wrap of sem_post diff --git a/drd/tests/tc20_verifywrap2.stderr.exp-glibc2.5-ppc b/drd/tests/tc20_verifywrap2.stderr.exp-glibc2.5-ppc index 427e3f2e07..0b7f0c14ae 100644 --- a/drd/tests/tc20_verifywrap2.stderr.exp-glibc2.5-ppc +++ b/drd/tests/tc20_verifywrap2.stderr.exp-glibc2.5-ppc @@ -146,8 +146,7 @@ semaphore 0x........ was first observed at: FIXME: can't figure out how to verify wrap of sem_destroy -[1/1] semaphore_pre_wait 0x........ value 0 -[1/1] semaphore_post_wait 0x........ value 4294967295 +[1/1] semaphore_wait 0x........ value 0 -> 4294967295 Invalid semaphore: semaphore 0x........ at 0x........: sem_wait* (drd_pthread_intercepts.c:?) @@ -155,7 +154,7 @@ Invalid semaphore: semaphore 0x........ semaphore 0x........ was first observed at: at 0x........: sem_init* (drd_pthread_intercepts.c:?) by 0x........: main (tc20_verifywrap.c:228) -[1/1] semaphore_post 0x........ value 1 +[1/1] semaphore_post 0x........ value 0 -> 1 FIXME: can't figure out how to verify wrap of sem_post diff --git a/drd/tests/tc20_verifywrap2.stderr.exp-glibc2.8 b/drd/tests/tc20_verifywrap2.stderr.exp-glibc2.8 index accfaef18e..26410b6d4f 100644 --- a/drd/tests/tc20_verifywrap2.stderr.exp-glibc2.8 +++ b/drd/tests/tc20_verifywrap2.stderr.exp-glibc2.8 @@ -139,8 +139,7 @@ rwlock 0x........ was first observed at: FIXME: can't figure out how to verify wrap of sem_destroy -[1/1] semaphore_pre_wait 0x........ value 0 -[1/1] semaphore_post_wait 0x........ value 4294967295 +[1/1] semaphore_wait 0x........ value 0 -> 4294967295 Invalid semaphore: semaphore 0x........ at 0x........: sem_wait* (drd_pthread_intercepts.c:?) @@ -148,7 +147,7 @@ Invalid semaphore: semaphore 0x........ semaphore 0x........ was first observed at: at 0x........: sem_init* (drd_pthread_intercepts.c:?) by 0x........: main (tc20_verifywrap.c:228) -[1/1] semaphore_post 0x........ value 1 +[1/1] semaphore_post 0x........ value 0 -> 1 FIXME: can't figure out how to verify wrap of sem_post -- 2.47.2