From 13f383ff5fc8ff095501794d4ce758f0067ff9b5 Mon Sep 17 00:00:00 2001 From: "darin@chromium.org" Date: Mon, 28 Sep 2009 18:12:55 +0000 Subject: Assert that thread-safe reference counting is used with cross-thread NewRunnableMethod. This assertion caught such an error in VisitedLinkMaster! My approach, modify RunnableMethodTraits to assert that when ReleaseCallee happens on a different thread from RetainCallee that the type supports thread-safe reference counting. I do this by adding a static method to both RefCounted and RefCountedThreadSafe. This results in a little ugliness in cases where people implement AddRef and Release by hand (to make the no-ops). There may be a nicer way to deal with those few cases. R=brettw BUG=none TEST=none Review URL: http://codereview.chromium.org/251012 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@27379 0039d316-1c4b-4281-b951-d872f2087c98 --- base/task.h | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) (limited to 'base/task.h') diff --git a/base/task.h b/base/task.h index c827681..e9da8b3 100644 --- a/base/task.h +++ b/base/task.h @@ -205,12 +205,33 @@ class ReleaseTask : public CancelableTask { template struct RunnableMethodTraits { - static void RetainCallee(T* obj) { + RunnableMethodTraits() { +#ifndef NDEBUG + origin_thread_id_ = PlatformThread::CurrentId(); +#endif + } + + ~RunnableMethodTraits() { +#ifndef NDEBUG + // If destroyed on a separate thread, then we had better have been using + // thread-safe reference counting! + if (origin_thread_id_ != PlatformThread::CurrentId()) + DCHECK(T::ImplementsThreadSafeReferenceCounting()); +#endif + } + + void RetainCallee(T* obj) { obj->AddRef(); } - static void ReleaseCallee(T* obj) { + + void ReleaseCallee(T* obj) { obj->Release(); } + + private: +#ifndef NDEBUG + PlatformThreadId origin_thread_id_; +#endif }; // RunnableMethod and RunnableFunction ----------------------------------------- @@ -240,13 +261,13 @@ struct RunnableMethodTraits { // RunnableMethod and NewRunnableMethod implementation ------------------------- template -class RunnableMethod : public CancelableTask, - public RunnableMethodTraits { +class RunnableMethod : public CancelableTask { public: RunnableMethod(T* obj, Method meth, const Params& params) : obj_(obj), meth_(meth), params_(params) { - RetainCallee(obj_); + traits_.RetainCallee(obj_); } + ~RunnableMethod() { ReleaseCallee(); } @@ -263,7 +284,7 @@ class RunnableMethod : public CancelableTask, private: void ReleaseCallee() { if (obj_) { - RunnableMethodTraits::ReleaseCallee(obj_); + traits_.ReleaseCallee(obj_); obj_ = NULL; } } @@ -271,6 +292,7 @@ class RunnableMethod : public CancelableTask, T* obj_; Method meth_; Params params_; + RunnableMethodTraits traits_; }; template -- cgit v1.1