Changeset 174660 in webkit


Ignore:
Timestamp:
Oct 13, 2014 3:30:20 PM (10 years ago)
Author:
Antti Koivisto
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):

Location:
trunk/Source/WTF
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WTF/ChangeLog

    r174633 r174660  
     12014-10-13  Antti Koivisto  <antti@apple.com>
     2
     3        Add StringCapture helper for thread-safe lambda capture
     4        https://bugs.webkit.org/show_bug.cgi?id=137664
     5
     6        Reviewed by Anders Carlsson.
     7
     8        There is currently no clean way to capture a String in a thread-safe manner. This will now work:
     9
     10        StringCapture stringCapture(string);
     11        auto lambdaThatRunsInAnotherThread = [stringCapture] { String string = stringCapture.string(); ... }
     12
     13        This type won't be necessary with C++14 initialized lambda capture: [string = string.isolatedCopy()].
     14
     15        * wtf/text/WTFString.h:
     16        (WTF::StringCapture::StringCapture): Create isolated copy in copy-constructor.
     17        (WTF::StringCapture::string):
     18
    1192014-10-11  KwangHyuk Kim  <hyuki.kim@samsung.com>
    220
  • trunk/Source/WTF/wtf/text/WTFString.h

    r173761 r174660  
    633633private:
    634634    const char* m_characters;
     635};
     636
     637// For thread-safe lambda capture:
     638// StringCapture stringCapture(string);
     639// auto lambdaThatRunsInOtherThread = [stringCapture] { String string = stringCapture.string(); ... }
     640// FIXME: Remove when we can use C++14 initialized lambda capture: [string = string.isolatedCopy()].
     641class StringCapture {
     642public:
     643    explicit StringCapture(const String& string) : m_string(string) { }
     644    explicit StringCapture(String&& string) : m_string(string) { }
     645    StringCapture(const StringCapture& other) : m_string(other.m_string.isolatedCopy()) { }
     646    const String& string() const { return m_string; }
     647    String releaseString() { return WTF::move(m_string); }
     648
     649private:
     650    void operator=(const StringCapture&) = delete;
     651    String m_string;
    635652};
    636653
     
    665682using WTF::reverseFind;
    666683using WTF::ASCIILiteral;
     684using WTF::StringCapture;
    667685
    668686#include <wtf/text/AtomicString.h>
Note: See TracChangeset for help on using the changeset viewer.