Bug 225935 - Use WTF::Locker for locking BaseAudioContext's graph lock
Summary: Use WTF::Locker for locking BaseAudioContext's graph lock
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: Web Audio (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Chris Dumez
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2021-05-18 13:28 PDT by Chris Dumez
Modified: 2021-05-18 18:52 PDT (History)
14 users (show)

See Also:


Attachments
Patch (30.44 KB, patch)
2021-05-18 15:12 PDT, Chris Dumez
no flags Details | Formatted Diff | Diff
Patch (29.08 KB, patch)
2021-05-18 16:38 PDT, Chris Dumez
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Chris Dumez 2021-05-18 13:28:31 PDT
Use WTF::Locker for locking BaseAudioContext's graph lock instead of our own AutoLocker.
Comment 1 Chris Dumez 2021-05-18 15:12:32 PDT
Created attachment 428986 [details]
Patch
Comment 2 Sam Weinig 2021-05-18 15:30:33 PDT
Comment on attachment 428986 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=428986&action=review

> Source/WebCore/Modules/webaudio/AudioBufferSourceNode.cpp:419
> +    auto contextLocker = holdLock(context().graphLock());

I believe we are trying to move away from holdLock() and just use:

Locker locker { context().graphLock() };

because holdLock does not play nice with ThreadSafetyAnalysis.

We really should just do a global replace of holdLock() to get this over with.
Comment 3 Chris Dumez 2021-05-18 15:32:36 PDT
(In reply to Sam Weinig from comment #2)
> Comment on attachment 428986 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=428986&action=review
> 
> > Source/WebCore/Modules/webaudio/AudioBufferSourceNode.cpp:419
> > +    auto contextLocker = holdLock(context().graphLock());
> 
> I believe we are trying to move away from holdLock() and just use:
> 
> Locker locker { context().graphLock() };
> 
> because holdLock does not play nice with ThreadSafetyAnalysis.
> 
> We really should just do a global replace of holdLock() to get this over
> with.

holdLock() looks nicer and there is no harm in using it here since we're not using a Lock type that supports static analysis. My understanding was that we just can use holdLock() with StaticLock but it was OK to use holdLock() for other types of locks?
Comment 4 Chris Dumez 2021-05-18 15:34:25 PDT
(In reply to Chris Dumez from comment #3)
> (In reply to Sam Weinig from comment #2)
> > Comment on attachment 428986 [details]
> > Patch
> > 
> > View in context:
> > https://bugs.webkit.org/attachment.cgi?id=428986&action=review
> > 
> > > Source/WebCore/Modules/webaudio/AudioBufferSourceNode.cpp:419
> > > +    auto contextLocker = holdLock(context().graphLock());
> > 
> > I believe we are trying to move away from holdLock() and just use:
> > 
> > Locker locker { context().graphLock() };
> > 
> > because holdLock does not play nice with ThreadSafetyAnalysis.
> > 
> > We really should just do a global replace of holdLock() to get this over
> > with.
> 
> holdLock() looks nicer and there is no harm in using it here since we're not
> using a Lock type that supports static analysis. My understanding was that
> we just can use holdLock() with StaticLock but it was OK to use holdLock()
> for other types of locks?

Also note that it wouldn't be:
Locker locker { context().graphLock() };

It would be:
Locker<RecursiveLock> locker { context().graphLock() };

Which would look even worse. I much prefer using auto that having to name the lock type.
Comment 5 Sam Weinig 2021-05-18 15:53:07 PDT
(In reply to Chris Dumez from comment #3)
> (In reply to Sam Weinig from comment #2)
> > Comment on attachment 428986 [details]
> > Patch
> > 
> > View in context:
> > https://bugs.webkit.org/attachment.cgi?id=428986&action=review
> > 
> > > Source/WebCore/Modules/webaudio/AudioBufferSourceNode.cpp:419
> > > +    auto contextLocker = holdLock(context().graphLock());
> > 
> > I believe we are trying to move away from holdLock() and just use:
> > 
> > Locker locker { context().graphLock() };
> > 
> > because holdLock does not play nice with ThreadSafetyAnalysis.
> > 
> > We really should just do a global replace of holdLock() to get this over
> > with.
> 
> holdLock() looks nicer and there is no harm in using it here since we're not
> using a Lock type that supports static analysis. My understanding was that
> we just can use holdLock() with StaticLock but it was OK to use holdLock()
> for other types of locks?

I think the only reason CheckedLock exists is that we holdLock. There is comment that states:

// FIXME: Maybe should be folded back to Lock once holdLock is not used globally.
Comment 6 Chris Dumez 2021-05-18 15:54:27 PDT
(In reply to Sam Weinig from comment #5)
> (In reply to Chris Dumez from comment #3)
> > (In reply to Sam Weinig from comment #2)
> > > Comment on attachment 428986 [details]
> > > Patch
> > > 
> > > View in context:
> > > https://bugs.webkit.org/attachment.cgi?id=428986&action=review
> > > 
> > > > Source/WebCore/Modules/webaudio/AudioBufferSourceNode.cpp:419
> > > > +    auto contextLocker = holdLock(context().graphLock());
> > > 
> > > I believe we are trying to move away from holdLock() and just use:
> > > 
> > > Locker locker { context().graphLock() };
> > > 
> > > because holdLock does not play nice with ThreadSafetyAnalysis.
> > > 
> > > We really should just do a global replace of holdLock() to get this over
> > > with.
> > 
> > holdLock() looks nicer and there is no harm in using it here since we're not
> > using a Lock type that supports static analysis. My understanding was that
> > we just can use holdLock() with StaticLock but it was OK to use holdLock()
> > for other types of locks?
> 
> I think the only reason CheckedLock exists is that we holdLock. There is
> comment that states:
> 
> // FIXME: Maybe should be folded back to Lock once holdLock is not used
> globally.

And what are you proposing for tryLock()? Keep using tryHoldLock()?
Comment 7 Sam Weinig 2021-05-18 15:55:37 PDT
(In reply to Chris Dumez from comment #4)
> (In reply to Chris Dumez from comment #3)
> > (In reply to Sam Weinig from comment #2)
> > > Comment on attachment 428986 [details]
> > > Patch
> > > 
> > > View in context:
> > > https://bugs.webkit.org/attachment.cgi?id=428986&action=review
> > > 
> > > > Source/WebCore/Modules/webaudio/AudioBufferSourceNode.cpp:419
> > > > +    auto contextLocker = holdLock(context().graphLock());
> > > 
> > > I believe we are trying to move away from holdLock() and just use:
> > > 
> > > Locker locker { context().graphLock() };
> > > 
> > > because holdLock does not play nice with ThreadSafetyAnalysis.
> > > 
> > > We really should just do a global replace of holdLock() to get this over
> > > with.
> > 
> > holdLock() looks nicer and there is no harm in using it here since we're not
> > using a Lock type that supports static analysis. My understanding was that
> > we just can use holdLock() with StaticLock but it was OK to use holdLock()
> > for other types of locks?
> 
> Also note that it wouldn't be:
> Locker locker { context().graphLock() };
> 
> It would be:
> Locker<RecursiveLock> locker { context().graphLock() };
> 
> Which would look even worse. I much prefer using auto that having to name
> the lock type.

Why do you have to specify RecursiveLock here? Is the argument deduction not working?
Comment 8 Sam Weinig 2021-05-18 15:58:24 PDT
(In reply to Chris Dumez from comment #6)
> (In reply to Sam Weinig from comment #5)
> > (In reply to Chris Dumez from comment #3)
> > > (In reply to Sam Weinig from comment #2)
> > > > Comment on attachment 428986 [details]
> > > > Patch
> > > > 
> > > > View in context:
> > > > https://bugs.webkit.org/attachment.cgi?id=428986&action=review
> > > > 
> > > > > Source/WebCore/Modules/webaudio/AudioBufferSourceNode.cpp:419
> > > > > +    auto contextLocker = holdLock(context().graphLock());
> > > > 
> > > > I believe we are trying to move away from holdLock() and just use:
> > > > 
> > > > Locker locker { context().graphLock() };
> > > > 
> > > > because holdLock does not play nice with ThreadSafetyAnalysis.
> > > > 
> > > > We really should just do a global replace of holdLock() to get this over
> > > > with.
> > > 
> > > holdLock() looks nicer and there is no harm in using it here since we're not
> > > using a Lock type that supports static analysis. My understanding was that
> > > we just can use holdLock() with StaticLock but it was OK to use holdLock()
> > > for other types of locks?
> > 
> > I think the only reason CheckedLock exists is that we holdLock. There is
> > comment that states:
> > 
> > // FIXME: Maybe should be folded back to Lock once holdLock is not used
> > globally.
> 
> And what are you proposing for tryLock()? Keep using tryHoldLock()?

I guess I would recommend we add a TryLocker class or something, not sure. But since this doesn't seem as settled as I thought it was, perhaps we should continue with holdLock/tryHoldLock for now.
Comment 9 Chris Dumez 2021-05-18 16:31:07 PDT
(In reply to Sam Weinig from comment #7)
> (In reply to Chris Dumez from comment #4)
> > (In reply to Chris Dumez from comment #3)
> > > (In reply to Sam Weinig from comment #2)
> > > > Comment on attachment 428986 [details]
> > > > Patch
> > > > 
> > > > View in context:
> > > > https://bugs.webkit.org/attachment.cgi?id=428986&action=review
> > > > 
> > > > > Source/WebCore/Modules/webaudio/AudioBufferSourceNode.cpp:419
> > > > > +    auto contextLocker = holdLock(context().graphLock());
> > > > 
> > > > I believe we are trying to move away from holdLock() and just use:
> > > > 
> > > > Locker locker { context().graphLock() };
> > > > 
> > > > because holdLock does not play nice with ThreadSafetyAnalysis.
> > > > 
> > > > We really should just do a global replace of holdLock() to get this over
> > > > with.
> > > 
> > > holdLock() looks nicer and there is no harm in using it here since we're not
> > > using a Lock type that supports static analysis. My understanding was that
> > > we just can use holdLock() with StaticLock but it was OK to use holdLock()
> > > for other types of locks?
> > 
> > Also note that it wouldn't be:
> > Locker locker { context().graphLock() };
> > 
> > It would be:
> > Locker<RecursiveLock> locker { context().graphLock() };
> > 
> > Which would look even worse. I much prefer using auto that having to name
> > the lock type.
> 
> Why do you have to specify RecursiveLock here? Is the argument deduction not
> working?

My bad, I am surprised but `Locker locker { context().graphLock() };` appears to be compiling fine. I thought this only used with a CheckedLock for some reason.

For tryHoldLock(), it looks like one alternative would be:
auto locker = Locker<RecursiveLock>::tryLock(context().graphLock());

Should I adopt that?
Comment 10 Chris Dumez 2021-05-18 16:38:37 PDT
Created attachment 428997 [details]
Patch
Comment 11 Sam Weinig 2021-05-18 17:11:12 PDT
(In reply to Chris Dumez from comment #9)
> (In reply to Sam Weinig from comment #7)
> > (In reply to Chris Dumez from comment #4)
> > > (In reply to Chris Dumez from comment #3)
> > > > (In reply to Sam Weinig from comment #2)
> > > > > Comment on attachment 428986 [details]
> > > > > Patch
> > > > > 
> > > > > View in context:
> > > > > https://bugs.webkit.org/attachment.cgi?id=428986&action=review
> > > > > 
> > > > > > Source/WebCore/Modules/webaudio/AudioBufferSourceNode.cpp:419
> > > > > > +    auto contextLocker = holdLock(context().graphLock());
> > > > > 
> > > > > I believe we are trying to move away from holdLock() and just use:
> > > > > 
> > > > > Locker locker { context().graphLock() };
> > > > > 
> > > > > because holdLock does not play nice with ThreadSafetyAnalysis.
> > > > > 
> > > > > We really should just do a global replace of holdLock() to get this over
> > > > > with.
> > > > 
> > > > holdLock() looks nicer and there is no harm in using it here since we're not
> > > > using a Lock type that supports static analysis. My understanding was that
> > > > we just can use holdLock() with StaticLock but it was OK to use holdLock()
> > > > for other types of locks?
> > > 
> > > Also note that it wouldn't be:
> > > Locker locker { context().graphLock() };
> > > 
> > > It would be:
> > > Locker<RecursiveLock> locker { context().graphLock() };
> > > 
> > > Which would look even worse. I much prefer using auto that having to name
> > > the lock type.
> > 
> > Why do you have to specify RecursiveLock here? Is the argument deduction not
> > working?
> 
> My bad, I am surprised but `Locker locker { context().graphLock() };`
> appears to be compiling fine. I thought this only used with a CheckedLock
> for some reason.
> 
> For tryHoldLock(), it looks like one alternative would be:
> auto locker = Locker<RecursiveLock>::tryLock(context().graphLock());
> 
> Should I adopt that?

That looks good for now. I will add something to make this nicer soon and fix this up. Probably make it something like:

Locker locker { context().graphLock(), Locker::Try };
Comment 12 Sam Weinig 2021-05-18 17:12:11 PDT
> My bad, I am surprised but `Locker locker { context().graphLock() };`
> appears to be compiling fine. I thought this only used with a CheckedLock
> for some reason.

The wonders of CTAD! https://en.cppreference.com/w/cpp/language/class_template_argument_deduction
Comment 13 EWS 2021-05-18 18:51:55 PDT
Committed r277709 (237890@main): <https://commits.webkit.org/237890@main>

All reviewed patches have been landed. Closing bug and clearing flags on attachment 428997 [details].
Comment 14 Radar WebKit Bug Importer 2021-05-18 18:52:19 PDT
<rdar://problem/78184315>