Title: [174660] trunk/Source/WTF
Revision
174660
Author
[email protected]
Date
2014-10-13 15:30:20 -0700 (Mon, 13 Oct 2014)

Log Message

Add StringCapture helper for thread-safe lambda capture
https://bugs.webkit.org/show_bug.cgi?id=137664

Reviewed by Anders Carlsson.

There is currently no clean way to capture a String in a thread-safe manner. This will now work:

StringCapture stringCapture(string);
auto lambdaThatRunsInAnotherThread = [stringCapture] { String string = stringCapture.string(); ... }

This type won't be necessary with C++14 initialized lambda capture: [string = string.isolatedCopy()].

* wtf/text/WTFString.h:
(WTF::StringCapture::StringCapture): Create isolated copy in copy-constructor.
(WTF::StringCapture::string):

Modified Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (174659 => 174660)


--- trunk/Source/WTF/ChangeLog	2014-10-13 21:58:30 UTC (rev 174659)
+++ trunk/Source/WTF/ChangeLog	2014-10-13 22:30:20 UTC (rev 174660)
@@ -1,3 +1,21 @@
+2014-10-13  Antti Koivisto  <[email protected]>
+
+        Add StringCapture helper for thread-safe lambda capture
+        https://bugs.webkit.org/show_bug.cgi?id=137664
+
+        Reviewed by Anders Carlsson.
+
+        There is currently no clean way to capture a String in a thread-safe manner. This will now work:
+
+        StringCapture stringCapture(string);
+        auto lambdaThatRunsInAnotherThread = [stringCapture] { String string = stringCapture.string(); ... }
+
+        This type won't be necessary with C++14 initialized lambda capture: [string = string.isolatedCopy()].
+
+        * wtf/text/WTFString.h:
+        (WTF::StringCapture::StringCapture): Create isolated copy in copy-constructor.
+        (WTF::StringCapture::string):
+
 2014-10-11  KwangHyuk Kim  <[email protected]>
 
         [EFL] Enable WebP support.

Modified: trunk/Source/WTF/wtf/text/WTFString.h (174659 => 174660)


--- trunk/Source/WTF/wtf/text/WTFString.h	2014-10-13 21:58:30 UTC (rev 174659)
+++ trunk/Source/WTF/wtf/text/WTFString.h	2014-10-13 22:30:20 UTC (rev 174660)
@@ -634,6 +634,23 @@
     const char* m_characters;
 };
 
+// For thread-safe lambda capture:
+// StringCapture stringCapture(string);
+// auto lambdaThatRunsInOtherThread = [stringCapture] { String string = stringCapture.string(); ... }
+// FIXME: Remove when we can use C++14 initialized lambda capture: [string = string.isolatedCopy()].
+class StringCapture {
+public:
+    explicit StringCapture(const String& string) : m_string(string) { }
+    explicit StringCapture(String&& string) : m_string(string) { }
+    StringCapture(const StringCapture& other) : m_string(other.m_string.isolatedCopy()) { }
+    const String& string() const { return m_string; }
+    String releaseString() { return WTF::move(m_string); }
+
+private:
+    void operator=(const StringCapture&) = delete;
+    String m_string;
+};
+
 // Shared global empty string.
 WTF_EXPORT_STRING_API const String& emptyString();
 
@@ -664,6 +681,7 @@
 using WTF::isSpaceOrNewline;
 using WTF::reverseFind;
 using WTF::ASCIILiteral;
+using WTF::StringCapture;
 
 #include <wtf/text/AtomicString.h>
 #endif
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to