Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP DONOTMERGE] Replace boost with C++17 (std::shared_mutex) #19183

Closed
wants to merge 1 commit into from

Conversation

maflcko
Copy link
Member

@maflcko maflcko commented Jun 5, 2020

No description provided.

@DrahtBot
Copy link
Contributor

DrahtBot commented Jun 6, 2020

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Conflicts

Reviewers, this pull request conflicts with the following ones:

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@kiminuo
Copy link
Contributor

kiminuo commented Jun 7, 2020

@MarcoFalke Just an idea - it looks like boost::fs can be replaced with std::filesystem:

The Filesystem library provides facilities for performing operations on file systems and their components, such as paths, regular files, and directories.

The filesystem library was originally developed as boost.filesystem, was published as the technical specification ISO/IEC TS 18822:2015, and finally merged to ISO C++ as of C++17. The boost implementation is currently available on more compilers and platforms than the C++17 library.

https://en.cppreference.com/w/cpp/filesystem

Maybe it's already on your roadmap. I don't know. But I can give a hand if you like. (edit: see #19245)

@@ -26,7 +26,7 @@ class CSignatureCache
CSHA256 m_salted_hasher;
typedef CuckooCache::cache<uint256, SignatureCacheHasher> map_type;
map_type setValid;
boost::shared_mutex cs_sigcache;
std::shared_mutex cs_sigcache;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: I think this has little risk of difference with boost vs std since we never use a condition variable with this mutex.

offtopic note for anyone curious is that cs_sigcache is overkill since we generally know that there are no concurrent readers and writers ever, the read lock could be held at a higher layer for the entire span of block validation, it's just harder to expose the right API for that (@hebasto seems to be looking at this stuff so figured I would mention it somewhere).

laanwj added a commit to bitcoin-core/gui that referenced this pull request Nov 23, 2020
a52ecc9 build: set minimum supported macOS to 10.14 (fanquake)

Pull request description:

  This is a requirement for C++17 support. See my comments [here](bitcoin/bitcoin#16684 (comment)):

  > You cannot use std::get with std::variant on macOS < 10.14, because Apples libc++ doesn't support the std::bad_variant_access exception. [Relevant comment](bitcoin/bitcoin#19183 (comment)) in #19183.

  > While we could work around this in our own code, using std::get_if, this would still be a problem for 3rd-party dependencies.

  > I've been testing Qt 5.15LTS (we'll have to enable C++17 in qt, and may upgrade to a newer version at the same time), and you can't enable -std c++17, while targeting a macOS deployment version < 10.14, configuring will fail. They are making use of std::get with std::variant throughout their cocoa code.

  We would have to had to have bumped to at least 10.13 in any case, as Qt 5.15 (#19716) [requires 10.13+](https://doc.qt.io/qt-5/supported-platforms.html).

ACKs for top commit:
  hebasto:
    ACK a52ecc9, I have reviewed the code and it looks OK, I agree it can be merged.

Tree-SHA512: f669b2fc777aeea1e9afdbbc7bd9afe3997418211db6ba53c934cae0e62a9b999603da539518c229f34961d275c9e2f315c7b022cf5fb97bd201a69c85d470cc
@practicalswift
Copy link
Contributor

Concept ACK

@MarcoFalke What is left to do here until the "[WIP DONTMERGE]" can be dropped? :)

@maflcko
Copy link
Member Author

maflcko commented Nov 23, 2020

commit a52ecc9 was required, which was only merged today

@hebasto
Copy link
Member

hebasto commented Nov 23, 2020

Could 2d48314 also be reverted?

src/optional.h Outdated
template <typename T>
using Optional = boost::optional<T>;
using Optional = std::optional<T>;

//! Substitute for C++17 std::make_optional
template <typename T>
Optional<T> MakeOptional(bool condition, T&& value)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MakeOptional can be dropped if these three workarounds are removed?

src/wallet/rpcwallet.cpp:    Optional<int> height = MakeOptional(false, int()); // Height of the specified block or the common ancestor, if the block provided was in a deactivated chain.
src/wallet/rpcwallet.cpp:    Optional<int> stop_height = MakeOptional(false, int());
src/wallet/wallet.cpp:        Optional<int64_t> time_first_key = MakeOptional(false, int64_t());;

@mjdietzx
Copy link
Contributor

Concept ACK

Just an idea - it looks like boost::fs can be replaced with std::filesystem:

and also like this idea

@practicalswift
Copy link
Contributor

practicalswift commented Nov 25, 2020

@MarcoFalke CI is happily green now. Do you want to make any further changes before removing the draft status, or do you plan to submit these changes as individual PRs?

I'm particularly looking forward to the std::optional change. I'm a bit afraid that mixing boost::optional and std::optional in our code base might lead to some unnecessary confusion.

@maflcko
Copy link
Member Author

maflcko commented Nov 25, 2020

A green CI doesn't mean there are no bugs. No one knows if the shared_mutex can be replaced at all: #19183 (comment) #16684 (comment)

Also, commit 8b173d4 introduces a bug in the gui, which isn't covered by tests.

Finally, optional can't be replaced with mechanical changes because the standard library doesn't have a non-throwing accessor by pointer. So the imo confusing use of the non-throwing accessor should be removed first. See #19426

@elichai
Copy link
Contributor

elichai commented Nov 25, 2020

Finally, optional can't be replaced with mechanical changes because the standard library doesn't have a non-throwing accessor by pointer. So the imo confusing use of the non-throwing accessor should be removed first. See #19426

std::optional acts like a "non-throwing accessor by pointer" by itself, e.g.:

void print_if(const std::optional<int>& val) {
    if(val) {
        std::cout << *val << "\n";
    } else {
        std::cout << "empty\n";
    }
}

https://godbolt.org/z/jPndjP

@maflcko
Copy link
Member Author

maflcko commented Nov 25, 2020

What I meant to say was that it doesn't have a get_ptr member function. Our current usage would need to be replaced by val.has_value() ? &val.value() : nullptr. So my goal is to first get rid of the get_ptr call.

@maflcko
Copy link
Member Author

maflcko commented Jan 12, 2021

Closing for now due to #19183 (comment) and #16684 (comment)

@maflcko maflcko closed this Jan 12, 2021
@maflcko maflcko deleted the 2005-StdVariantScriptedDiff branch January 12, 2021 05:59
@maflcko maflcko changed the title [WIP DONOTMERGE] Replace boost with C++17 [WIP DONOTMERGE] Replace boost with C++17 (std::shared_mutex) Jan 12, 2021
laanwj added a commit that referenced this pull request Feb 12, 2021
060a2a6 ci: remove boost thread installation (fanquake)
06e1d7d build: don't build or use Boost Thread (fanquake)
7097add refactor: replace Boost shared_mutex with std shared_mutex in sigcache (fanquake)
8e55981 refactor: replace Boost shared_mutex with std shared_mutex in cuckoocache tests (fanquake)

Pull request description:

  This replaces `boost::shared_mutex` and `boost::unique_lock` with [`std::shared_mutex`](https://en.cppreference.com/w/cpp/thread/shared_mutex) & [`std::unique_lock`](https://en.cppreference.com/w/cpp/thread/unique_lock).

  Even though [some concerns were raised](#16684 (comment)) in #16684 with regard to `std::shared_mutex` being unsafe to use across some glibc versions, I still think this change is an improvement. As I mentioned in #21022, I also think trying to restrict standard library feature usage based on bugs in glibc is not only hard to do, but it's not currently clear exactly how we do that in practice (does it also extend to patching out use in our dependencies, should we be implementing more runtime checks for features we are using, when do we consider an affected glibc "old enough" not to worry about? etc). If you take a look through the [glibc bug tracker](https://sourceware.org/bugzilla/describecomponents.cgi?product=glibc) you'll no doubt find plenty of (active) bug reports for standard library code we already using. Obviously not to say we shouldn't try and avoid buggy code where possible.

  Two other points:

  [Cory mentioned in #21022](#21022 (comment)):
  > It also seems reasonable to me to worry that boost hits the same underlying glibc bug, and we've just not happened to trigger the right conditions yet.

  Moving away from Boost to the standard library also removes the potential for differences related to Boosts configuration. Boost has multiple versions of `shared_mutex`, and what you end up using, and what it's backed by depends on:
  * The version of Boost.
  * The platform you're building for.
  * Which version of `BOOST_THREAD_VERSION` is defined: (2,3,4 or 5) default=2. (see [here](https://www.boost.org/doc/libs/1_70_0/doc/html/thread/build.html#thread.build.configuration) for some of the differences).
  * Is `BOOST_THREAD_V2_SHARED_MUTEX` defined? (not by default). If so, you might get the ["less performant, but more robust"](boostorg/thread#230 (comment)) version of `shared_mutex`.

  A lot of these factors are eliminated by our use of depends, but users will have varying configurations. It's also not inconceivable to think that a distro, or some package manager might start defining something like `BOOST_THREAD_VERSION=3`. Boost tried to change the default from 2 to 3 at one point.

  With this change, we no longer use Boost Thread, so this PR also removes it from depends, the build system, CI etc.

  Previous similar PRs were #19183 & #20922. The authors are included in the commits here.
  Also related to #21022 - pthread sanity checking.

ACKs for top commit:
  laanwj:
    Code review ACK 060a2a6
  vasild:
    ACK 060a2a6

Tree-SHA512: 572d14d8c9de20bc434511f20d3f431836393ff915b2fe9de5a47a02dca76805ad5c3fc4cceecb4cd43f3ba939a0508178c4e60e62abdbaaa6b3e8db20b75b03
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Aug 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet