Skip to content

Conversation

hebasto
Copy link
Member

@hebasto hebasto commented Sep 27, 2022

Microsoft's C++ Standard Library uses the [[nodiscard]] attribute for try_lock().
See: https://github.com/microsoft/STL/blob/main/stl/inc/mutex

This change allows to drop the current suppression for the warning C4838 and helps to prevent the upcoming warning C4858.
See: microsoft/STL@539c26c

Fixes #26017.

Split from #25819.

Microsoft's C++ Standard Library uses the `[[nodiscard]]` attribute for
`try_lock()`.
See: https://github.com/microsoft/STL/blob/main/stl/inc/mutex

This change allows to drop the current suppression for the warning C4838
and helps to prevent the upcoming warning C4858.
See: microsoft/STL@539c26c
@hebasto
Copy link
Member Author

hebasto commented Sep 27, 2022

Friendly ping @ajtowns @ryanofsky @vasild

@ajtowns
Copy link
Contributor

ajtowns commented Sep 27, 2022

Concept ACK.

This seems to have been introduced in 712fd18 (April 2012) by @sipa -- the old code for TryEnter did result = mutex.try_lock(); if (!result) pop_lock(); return result; the new code discarded the result for try_lock() and instead then checked !lock.owns().

@DrahtBot
Copy link
Contributor

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

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #25081 (tracing: lock contention analysis by martinus)

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.

Copy link
Contributor

@vasild vasild left a comment

Choose a reason for hiding this comment

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

ACK bff4e06

The old and new code should have identical behavior. The new code is shorter and silences a warning.

https://en.cppreference.com/w/cpp/thread/unique_lock/try_lock
https://en.cppreference.com/w/cpp/thread/unique_lock/owns_lock

The other call to Base::owns_lock() can also be avoided:

        EnterCritical(pszName, pszFile, nLine, Base::mutex(), true);
        if (!Base::try_lock()) {
            LeaveCritical();
            return false;
        }     
        return true;

@fanquake fanquake requested a review from sipa October 2, 2022 16:15
Copy link
Member

@sipa sipa left a comment

Choose a reason for hiding this comment

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

ACK bff4e06

I'd suggest incorporating @vasild's suggestion above.

Co-authored-by: Vasil Dimov <vd@FreeBSD.org>
@hebasto
Copy link
Member Author

hebasto commented Oct 3, 2022

Updated bff4e06 -> 30cc1c6 (pr26189.01 -> pr26189.02, diff):

Copy link
Contributor

@vasild vasild left a comment

Choose a reason for hiding this comment

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

ACK 30cc1c6

@fanquake fanquake merged commit 1730f6c into bitcoin:master Oct 3, 2022
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Oct 4, 2022
30cc1c6 refactor: Drop `owns_lock()` call (Hennadii Stepanov)
bff4e06 refactor: Do not discard `try_lock()` return value (Hennadii Stepanov)

Pull request description:

  Microsoft's C++ Standard Library uses the `[[nodiscard]]` attribute for `try_lock()`.
  See: https://github.com/microsoft/STL/blob/main/stl/inc/mutex

  This change allows to drop the current suppression for the warning C4838 and helps to prevent the upcoming warning C4858.
  See: microsoft/STL@539c26c

  Fixes bitcoin#26017.

  Split from bitcoin#25819.

ACKs for top commit:
  vasild:
    ACK 30cc1c6

Tree-SHA512: ce17404e1c78af4f763129753caf8e5a0e1c91ba398778fe912f9fcc56a847e8112460d1a1a35bf905a593b7d8e0b16c6b099ad74976b67dca5f4f3eda6ff621
fanquake added a commit to bitcoin-core/gui that referenced this pull request Oct 4, 2022
f3e40c4 build, msvc: Enable C4834 warning (Hennadii Stepanov)

Pull request description:

  Since bitcoin/bitcoin#26189 our codebase is  C4834 warning free.

  See https://docs.microsoft.com/en-us/cpp/error-messages/compiler-warnings/c4834.

ACKs for top commit:
  sipsorcery:
    tACK f3e40c4.

Tree-SHA512: 2dd8fdb5d6c0d4eaf7ffec82039411963010ecea559adfe7e2b5587f467135bd665e0cef5c748e4d6d9ba3c2038d6a5c79980972884adbaaac3bda3b2fbbc408
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Oct 5, 2022
f3e40c4 build, msvc: Enable C4834 warning (Hennadii Stepanov)

Pull request description:

  Since bitcoin#26189 our codebase is  C4834 warning free.

  See https://docs.microsoft.com/en-us/cpp/error-messages/compiler-warnings/c4834.

ACKs for top commit:
  sipsorcery:
    tACK f3e40c4.

Tree-SHA512: 2dd8fdb5d6c0d4eaf7ffec82039411963010ecea559adfe7e2b5587f467135bd665e0cef5c748e4d6d9ba3c2038d6a5c79980972884adbaaac3bda3b2fbbc408
Fabcien pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Jun 7, 2023
Summary:
```
Microsoft's C++ Standard Library uses the [[nodiscard]] attribute for try_lock().
See: https://github.com/microsoft/STL/blob/main/stl/inc/mutex
```

It appears the same issue occurs with GCC 13.1.

Backport of [[bitcoin/bitcoin#26189 | core#26189]].

Test Plan:
With debug and GCC 13.1:
  ninja all check-all
It should return no warning.

Reviewers: #bitcoin_abc, PiRK

Reviewed By: #bitcoin_abc, PiRK

Differential Revision: https://reviews.bitcoinabc.org/D14005
@bitcoin bitcoin locked and limited conversation to collaborators Oct 3, 2023
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.

[MSVC] Bitcoin failed to build with waring c4858 on MSVC
6 participants