Assert that Supplementable objects is only used in their creator thread.
authormark.lam@apple.com <mark.lam@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 23 Jan 2013 21:26:11 +0000 (21:26 +0000)
committermark.lam@apple.com <mark.lam@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 23 Jan 2013 21:26:11 +0000 (21:26 +0000)
https://bugs.webkit.org/show_bug.cgi?id=107717.

Reviewed by Adam Barth.

No new tests.

* platform/Supplementable.h:
(WebCore):
(WebCore::Supplementable::provideSupplement):
(WebCore::Supplementable::removeSupplement):
(WebCore::Supplementable::requireSupplement):
(Supplementable):
(WebCore::Supplementable::Supplementable):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@140580 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/platform/Supplementable.h

index ff14da4..de52df0 100644 (file)
@@ -1,3 +1,20 @@
+2013-01-23  Mark Lam  <mark.lam@apple.com>
+
+        Assert that Supplementable objects is only used in their creator thread.
+        https://bugs.webkit.org/show_bug.cgi?id=107717.
+
+        Reviewed by Adam Barth.
+
+        No new tests.
+
+        * platform/Supplementable.h:
+        (WebCore):
+        (WebCore::Supplementable::provideSupplement):
+        (WebCore::Supplementable::removeSupplement):
+        (WebCore::Supplementable::requireSupplement):
+        (Supplementable):
+        (WebCore::Supplementable::Supplementable):
+
 2013-01-23  Tony Chang  <tony@chromium.org>
 
         Incorrect scrollable height during simplified layout
index 1b81cf7..10e373f 100644 (file)
 #ifndef Supplementable_h
 #define Supplementable_h
 
+#include <wtf/Assertions.h>
 #include <wtf/HashMap.h>
 #include <wtf/OwnPtr.h>
 #include <wtf/PassOwnPtr.h>
 
+#if !ASSERT_DISABLED
+#include <wtf/Threading.h>
+#endif
+
 namespace WebCore {
 
+// What you should know about Supplementable and Supplement
+// ========================================================
+// Supplementable and Supplement instances are meant to be thread local. They
+// should only be accessed from within the thread that created them. The
+// 2 classes are not designed for safe access from another thread. Violating
+// this design assumption can result in memory corruption and unpredictable
+// behavior.
+//
 // What you should know about the Supplement keys
 // ==============================================
 // The Supplement is expected to use the same const char* string instance
@@ -86,25 +99,37 @@ class Supplementable {
 public:
     void provideSupplement(const char* key, PassOwnPtr<Supplement<T> > supplement)
     {
+        ASSERT(m_threadId == currentThread());
         ASSERT(!m_supplements.get(key));
         m_supplements.set(key, supplement);
     }
 
     void removeSupplement(const char* key)
     {
+        ASSERT(m_threadId == currentThread());
         m_supplements.remove(key);
     }
 
     Supplement<T>* requireSupplement(const char* key)
     {
+        ASSERT(m_threadId == currentThread());
         return m_supplements.get(key);
     }
 
+#if !ASSERT_DISABLED
+protected:
+    Supplementable() : m_threadId(currentThread()) { }
+#endif
+
 private:
     typedef HashMap<const char*, OwnPtr<Supplement<T> >, PtrHash<const char*> > SupplementMap;
     SupplementMap m_supplements;
+#if !ASSERT_DISABLED
+    ThreadIdentifier m_threadId;
+#endif
 };
 
 } // namespace WebCore
 
 #endif // Supplementable_h
+