tsan: add ReleaseStore() function that merely copies vector clock rather than combines two clocks
fix clock setup for finalizer goroutine (Go runtime)

llvm-svn: 160918
diff --git a/compiler-rt/lib/tsan/go/buildgo.sh b/compiler-rt/lib/tsan/go/buildgo.sh
index 0015b5d..a0d2f67 100755
--- a/compiler-rt/lib/tsan/go/buildgo.sh
+++ b/compiler-rt/lib/tsan/go/buildgo.sh
@@ -74,5 +74,5 @@
 as gotsan.s -o race_$SUFFIX.syso
 
 gcc test.c race_$SUFFIX.syso -lpthread -o test
-./test
+TSAN_OPTIONS="exitcode=0" ./test
 
diff --git a/compiler-rt/lib/tsan/go/test.c b/compiler-rt/lib/tsan/go/test.c
index 5ef81be..a9a5b3d 100644
--- a/compiler-rt/lib/tsan/go/test.c
+++ b/compiler-rt/lib/tsan/go/test.c
@@ -46,7 +46,6 @@
   __tsan_read(0, buf, 0);
   __tsan_free(buf);
   __tsan_func_exit(0);
-  printf("OK\n");
   __tsan_fini();
   return 0;
 }
diff --git a/compiler-rt/lib/tsan/go/tsan_go.cc b/compiler-rt/lib/tsan/go/tsan_go.cc
index f0e2e45..4b3076c 100644
--- a/compiler-rt/lib/tsan/go/tsan_go.cc
+++ b/compiler-rt/lib/tsan/go/tsan_go.cc
@@ -165,7 +165,7 @@
 void __tsan_release(int goid, void *addr) {
   ThreadState *thr = goroutines[goid];
   thr->in_rtl++;
-  Release(thr, 0, (uptr)addr);
+  ReleaseStore(thr, 0, (uptr)addr);
   thr->in_rtl--;
 }
 
@@ -173,7 +173,6 @@
   ThreadState *thr = goroutines[goid];
   thr->in_rtl++;
   Release(thr, 0, (uptr)addr);
-  //ReleaseMerge(thr, 0, (uptr)addr);
   thr->in_rtl--;
 }
 
diff --git a/compiler-rt/lib/tsan/rtl/tsan_clock.cc b/compiler-rt/lib/tsan/rtl/tsan_clock.cc
index 1918f8d..0872192 100644
--- a/compiler-rt/lib/tsan/rtl/tsan_clock.cc
+++ b/compiler-rt/lib/tsan/rtl/tsan_clock.cc
@@ -88,14 +88,28 @@
   }
 }
 
+void ThreadClock::ReleaseStore(SyncClock *dst) const {
+  DCHECK(nclk_ <= kMaxTid);
+  DCHECK(dst->clk_.Size() <= kMaxTid);
+
+  if (dst->clk_.Size() < nclk_)
+    dst->clk_.Resize(nclk_);
+  for (uptr i = 0; i < nclk_; i++)
+    dst->clk_[i] = clk_[i];
+  for (uptr i = nclk_; i < dst->clk_.Size(); i++)  
+    dst->clk_[i] = 0;
+}
+
 void ThreadClock::acq_rel(SyncClock *dst) {
   acquire(dst);
   release(dst);
 }
 
-void ThreadClock::Disable() {
+void ThreadClock::Disable(unsigned tid) {
+  u64 c0 = clk_[tid];
   for (uptr i = 0; i < kMaxTidInClock; i++)
     clk_[i] = (u64)-1;
+  clk_[tid] = c0;
 }
 
 SyncClock::SyncClock()
diff --git a/compiler-rt/lib/tsan/rtl/tsan_clock.h b/compiler-rt/lib/tsan/rtl/tsan_clock.h
index c6a8062..02ddb9a 100644
--- a/compiler-rt/lib/tsan/rtl/tsan_clock.h
+++ b/compiler-rt/lib/tsan/rtl/tsan_clock.h
@@ -61,7 +61,7 @@
       nclk_ = tid + 1;
   }
 
-  void Disable();
+  void Disable(unsigned tid);
 
   uptr size() const {
     return nclk_;
@@ -70,6 +70,7 @@
   void acquire(const SyncClock *src);
   void release(SyncClock *dst) const;
   void acq_rel(SyncClock *dst);
+  void ReleaseStore(SyncClock *dst) const;
 
  private:
   uptr nclk_;
diff --git a/compiler-rt/lib/tsan/rtl/tsan_rtl.h b/compiler-rt/lib/tsan/rtl/tsan_rtl.h
index 4064351..c559cb2 100644
--- a/compiler-rt/lib/tsan/rtl/tsan_rtl.h
+++ b/compiler-rt/lib/tsan/rtl/tsan_rtl.h
@@ -449,6 +449,7 @@
 
 void Acquire(ThreadState *thr, uptr pc, uptr addr);
 void Release(ThreadState *thr, uptr pc, uptr addr);
+void ReleaseStore(ThreadState *thr, uptr pc, uptr addr);
 
 // The hacky call uses custom calling convention and an assembly thunk.
 // It is considerably faster that a normal call for the caller
diff --git a/compiler-rt/lib/tsan/rtl/tsan_rtl_mutex.cc b/compiler-rt/lib/tsan/rtl/tsan_rtl_mutex.cc
index 959001c..882def8 100644
--- a/compiler-rt/lib/tsan/rtl/tsan_rtl_mutex.cc
+++ b/compiler-rt/lib/tsan/rtl/tsan_rtl_mutex.cc
@@ -207,4 +207,14 @@
   s->mtx.Unlock();
 }
 
+void ReleaseStore(ThreadState *thr, uptr pc, uptr addr) {
+  CHECK_GT(thr->in_rtl, 0);
+  DPrintf("#%d: ReleaseStore %zx\n", thr->tid, addr);
+  SyncVar *s = CTX()->synctab.GetAndLock(thr, pc, addr, true);
+  thr->clock.set(thr->tid, thr->fast_state.epoch());
+  thr->clock.ReleaseStore(&s->clock);
+  StatInc(thr, StatSyncRelease);
+  s->mtx.Unlock();
+}
+
 }  // namespace __tsan
diff --git a/compiler-rt/lib/tsan/rtl/tsan_rtl_thread.cc b/compiler-rt/lib/tsan/rtl/tsan_rtl_thread.cc
index 65449ff..f7d5f13 100644
--- a/compiler-rt/lib/tsan/rtl/tsan_rtl_thread.cc
+++ b/compiler-rt/lib/tsan/rtl/tsan_rtl_thread.cc
@@ -299,7 +299,7 @@
 }
 
 void ThreadFinalizerGoroutine(ThreadState *thr) {
-  thr->clock.Disable();
+  thr->clock.Disable(thr->tid);
 }
 
 void MemoryAccessRange(ThreadState *thr, uptr pc, uptr addr,