RESOLVED FIXED 199831
Add DataMutex and MainThreadData wrappers
https://bugs.webkit.org/show_bug.cgi?id=199831
Summary Add DataMutex and MainThreadData wrappers
Alicia Boya García
Reported 2019-07-16 09:27:32 PDT
DataMutex<T> stores an instance of T in a private member along with a mutex. In order to use its fields, users need to instantiate a DataMutex<T>::LockedWrapper instance in the stack. This class uses RAII to lock and unlock the mutex in construction and destruction respectively, and by using the arrow operator lets the user access T's members. This way, DataMutex<T> prevents most instances of accidental access to data fields that should only be read and modified in an atomic matter. Still, both the Lock and the LockHolder are exposed once the user has taken the lock so that special cases such as waiting for a condition variable or performing an early unlock are doable. MainThreadData<T> is another wrapper class, in this case for data fields that should only be accessed from the main thread. In this case, it works similar to a smart pointer, except that (1) there is no actual memory indirection, T is stored directly inside MainThreadData<T> and (2) attempts to use the -> or * operator have an isMainThread() assertion. Together, these two wrapper classes make it easier to write multi-threaded code in a safer, more self-documented way by letting the author group data into structures that have certain access safety requirements. These structures were originally part of the new GStreamer WebKitMediaSrc rework patch: https://bugs.webkit.org/show_bug.cgi?id=199719
Attachments
Patch (10.51 KB, patch)
2019-07-16 10:08 PDT, Alicia Boya García
no flags
Alicia Boya García
Comment 1 2019-07-16 10:08:02 PDT
Alex Christensen
Comment 2 2019-07-17 10:46:04 PDT
Comment on attachment 374214 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=374214&action=review > Source/WTF/wtf/MainThreadData.h:29 > +class MainThreadData { This feels like it will want an operator T&()
Alicia Boya García
Comment 3 2019-07-17 11:22:39 PDT
Comment on attachment 374214 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=374214&action=review >> Source/WTF/wtf/MainThreadData.h:29 >> +class MainThreadData { > > This feels like it will want an operator T&() What would it do?
Alex Christensen
Comment 4 2019-07-17 12:24:21 PDT
Comment on attachment 374214 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=374214&action=review >>> Source/WTF/wtf/MainThreadData.h:29 >>> +class MainThreadData { >> >> This feels like it will want an operator T&() > > What would it do? return a reference to m_data
Alicia Boya García
Comment 5 2019-07-17 13:02:00 PDT
Comment on attachment 374214 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=374214&action=review >>>> Source/WTF/wtf/MainThreadData.h:29 >>>> +class MainThreadData { >>> >>> This feels like it will want an operator T&() >> >> What would it do? > > return a reference to m_data That would make it easier to store it and pass it away, allowing for unprotected access. I want to make that uncomfortable on purpose :) If someone really really wants, they can use &*, in a similar way to how you can get an unprotected pointer from a shared_ptr<>.
Alex Christensen
Comment 6 2019-07-22 11:14:51 PDT
ok
WebKit Commit Bot
Comment 7 2019-07-23 07:51:35 PDT
Comment on attachment 374214 [details] Patch Clearing flags on attachment: 374214 Committed r247723: <https://trac.webkit.org/changeset/247723>
WebKit Commit Bot
Comment 8 2019-07-23 07:51:37 PDT
All reviewed patches have been landed. Closing bug.
Radar WebKit Bug Importer
Comment 9 2019-07-23 07:52:17 PDT
Note You need to log in before you can comment on or make changes to this bug.