This is an archive of the discontinued LLVM Phabricator instance.

[NFC][libcxxabi] In cxa_guard, split the InitByte classes into InitByte... and ...Guard
AbandonedPublic

Authored by DanielMcIntosh-IBM on Sep 20 2021, 11:49 AM.

Details

Reviewers
ldionne
EricWF
nicholas
jroelofs
Group Reviewers
Restricted Project
Summary

We split each of the InitByteNoThreads, InitByteGlobalMutex, and
InitByteFutex classes into 2 different classes.

The three new classes NoThreadsGuard, GlobalMutexGuard, and FutexGuard
replace the old classes, and the old classes now focus only on
providing functions for managing reads/writes to the init byte. That is,
the definitions of acquire/release/abort_init_byte, remain in the old
classes, but nothing else.

The new classes are then responsible for everything else: dividing up
the raw guard object, providing cxa_guard_acquire/release/abort (by
inheriting from GuardObject), etc.

This is the 3rd of 6 changes to overhaul cxa_guard.
See D108343 for what the final result will be and more details on why
cxa_guard is getting overhauled.

Depends on D110088

Diff Detail

Event Timeline

DanielMcIntosh-IBM requested review of this revision.Sep 20 2021, 11:49 AM
DanielMcIntosh-IBM created this revision.
Herald added a project: Restricted Project. · View Herald TranscriptSep 20 2021, 11:49 AM
Herald added a reviewer: Restricted Project. · View Herald Transcript
DanielMcIntosh-IBM edited the summary of this revision. (Show Details)Sep 20 2021, 11:54 AM
DanielMcIntosh-IBM edited the summary of this revision. (Show Details)Sep 23 2021, 9:26 AM

Ping.

I have a bunch more PRs I'm going to be creating soon (they're getting reviewed downstream rn), and I'd like to get some of these older ones closed off so I don't have too many to keep track of.
@ldionne I know you're probably quite busy. Is there someone else who might also be suitable to review this? I can't see anyone other than you and @EricWF who have made significant modifications to libcxxabi/src/cxa_guard_impl.h (or cxa_guard.cpp).
I've added Nick Lewycky and Jon Roelofs since they seem to have made some smaller modifications, but the file has changed dramatically since they made their changes, and they're not members of the lib++abi project.

ldionne added inline comments.Dec 7 2021, 4:21 PM
libcxxabi/src/cxa_guard_impl.h
174

I was looking at this patch and I found that using CRTP for this was adding a lot of complexity. It it possible that using a free function (template) for implementing cxa_guard_acquire, cxa_guard_release and cxa_guard_abort would greatly simplify things? Imagine:

template <class Guard>
AcquireResult cxa_guard_acquire(Guard& guard) {
  AtomicInt<uint8_t> guard_byte(guard.get_init_byte()); // all types of guards need to answer to that
  if (guard_byte.load(std::_AO_Acquire) != UNSET)
    return INIT_IS_DONE;
  return derived()->acquire_init_byte();
}

// etc..

Then, for example NoThreadsGuard would become:

using NoThreadsGuard = InitByteNoThreads;

At that point we might want to simplify the whole thing and simply call InitByteNoThreads NoThreadsGuard directly.

libcxxabi/src/cxa_guard_impl.h
174

Yes, using CRTP is one of the reasons I found the current implementation a little difficult to follow, which prompted this whole refactor. I get rid of it in D110101. I'm not entirely sure if I fully understand what it is you're suggesting, but I think that change accomplishes the same goal, except they're part of GuardBase instead of free functions. This works a bit better with the SelectImplementation stuff at the end of the file. Plus, I think having separate classes dedicated to managing the guard byte, to managing the init byte, and to co-coordinating between them is probably a little better for separation of concerns.

DanielMcIntosh-IBM added inline comments.
libcxxabi/src/cxa_guard_impl.h
174

Actually, looking at this with fresh eyes, I think there is a better way of grouping these changes. See D115367, D115368 and D115369 for newer versions.