This is an archive of the discontinued LLVM Phabricator instance.

[clangd] Mark AsyncTaskRunner::runAsync as const
AbandonedPublic

Authored by kadircet on Nov 4 2020, 2:14 AM.

Details

Reviewers
sammccall
Summary

RunAsync performs a write that's internally synchronized, so in theory
it shouldn't introduce any data-races. This will enable spawning async tasks in
const contexts.

Diff Detail

Event Timeline

kadircet created this revision.Nov 4 2020, 2:14 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 4 2020, 2:14 AM
kadircet requested review of this revision.Nov 4 2020, 2:14 AM

AsyncTaskRunner is definitely threadsafe, but const doesn't exactly mean threadsafe...

This might be OK or maybe we should put mutable at the callsite - can you show the real example?

AsyncTaskRunner is definitely threadsafe, but const doesn't exactly mean threadsafe...

Right, I was also trying to punt on the idea of "writes are synchronized" rather than promising threadsafety here.

This might be OK or maybe we should put mutable at the callsite - can you show the real example?

Oops, sorry forgot to update the stack for this one. The usage is in https://reviews.llvm.org/D90750, line 86 of ProjectAware.cpp specifically.
We definitely can make the AsyncTaskRunner mutable there too, but I would feel bad about mutating a "mutable" member without holding a lock in general, hence this change.

This isn't a hill I'm going to die on, it doesn't matter a great deal - so if this solves
But I don't think this is an appropriate use of const - it changes the object in a significant way.

"significant" is vague, but i think "wait()ing on a threadpool will now block" qualifies whereas "destroying (long-lived) ProjectAwareIndex will now block" doesn't.
It's as much about the scope of the object as the nature of the change.

A couple of other examples of "thread pool" classes don't make the "schedule" option const:

(Boost's threadpool is threadsafe like this one)

This might be OK or maybe we should put mutable at the callsite - can you show the real example?

Oops, sorry forgot to update the stack for this one. The usage is in https://reviews.llvm.org/D90750, line 86 of ProjectAware.cpp specifically.

Yeah, this does seem to me like a place to mark the class as "threadsafe" and the member as mutable.

We definitely can make the AsyncTaskRunner mutable there too, but I would feel bad about mutating a "mutable" member without holding a lock in general, hence this change.

I don't think that's bad - mutating requires access to be synchronized, either externally (holding a lock) or internally (class is threadsafe).
This is why it's safe to mutate (lock) the mutable mutex without holding yet another mutex first: the mutex class is threadsafe.

kadircet abandoned this revision.Nov 9 2020, 1:42 PM

SG, gonna abandon this one and mark the threadpool as mutable in the ProjectAwareIndex instead.