refactor: add kernel/cs_main.h #26251

pull fanquake wants to merge 1 commits into bitcoin:master from fanquake:move_cs_main_own_file changing 17 files +44 −27
  1. fanquake commented at 2:59 pm on October 4, 2022: member

    One place to find / include cs_main. No more:

    // Actually declared in validation.cpp; can’t include because of circular dependency. extern RecursiveMutex cs_main;

    Ultimately, no more need to include validation.h (which also includes (heavy/boost filled) txmempool.h) everywhere for cs_main. See #26087 for another example of why that is useful.

  2. in src/cs_main.h:20 in 7e2a68204e outdated
    15+ * AcceptToMemoryPool. See CTxMemPool::cs comment for details.
    16+ *
    17+ * The transaction pool has a separate lock to allow reading from it and the
    18+ * chainstate at the same time.
    19+ */
    20+static RecursiveMutex cs_main;
    


    ryanofsky commented at 3:09 pm on October 4, 2022:

    In commit “refactor: add cs_main.h” (7e2a68204ecbc15a6f661075a34300fe5c101232)

    I think this still needs to be extern not static. Otherwise there would be a different instance of cs_main variable in each translation unit and locking one of the cs_main variables would not lock all the other ones.


    fanquake commented at 3:22 pm on October 4, 2022:
    Making this extern causes link errors on multiple platforms, will take another look.

    ryanofsky commented at 3:36 pm on October 4, 2022:

    Making this extern causes link errors on multiple platforms, will take another look.

    Right you would also need to add RecursiveMutex cs_main; without extern to a .cpp file like cs_main.cpp, or undelete it from validation.cpp to actually instantiate the variable and create the linker symbol. Declaring a variable without static or extern will creates a linker symbol. Declaring a variable extern just tells the compiler the type of the variable and makes it look for an exisiting symbol to import. Declaring a variable static makes the compiler create a private variable in the current translation unit without importing or exporting any symbol.


    fanquake commented at 3:50 pm on October 4, 2022:
    Yep, you are completely correct. My oversight here.
  3. maflcko commented at 3:14 pm on October 4, 2022: member
    Looks like this is just adding a bunch of bloat that should later-on be removed anyway. How is a one-line include different/better than a simple one-line extern RecursiveMutex cs_main;?
  4. fanquake commented at 3:45 pm on October 4, 2022: member

    How is a one-line include different/better than a simple one-line extern RecursiveMutex cs_main;?

    It’s probably neither much different or better, just one way of acheiving the same thing. The point is to remove unecessary/heavy includes, which aside from improving compilation time / # of units to rebuilt due to source changes, also enables cleanup like #26087. If we’d rather use extern RecursiveMutex cs_main; everywhere, and then drop unused validation includes when possible, that could also be fine, I’m not tied to a specific approach.

    that should later-on be removed anyway.

    If it’s going to be removed later on, having a single file/include to delete is at least somewhat convenient. Although similarly so to removing a number of extern RecursiveMutex....

  5. fanquake force-pushed on Oct 4, 2022
  6. maflcko commented at 3:52 pm on October 4, 2022: member

    The point is to remove unecessary/heavy includes

    I don’t see how this pull achieves any of that or would help there. You are adding the cs_main.h include to 40 files, all of which either have validation already included or had their extern cs_main replaced.

  7. DrahtBot added the label Refactoring on Oct 4, 2022
  8. fanquake force-pushed on Oct 4, 2022
  9. ryanofsky commented at 5:22 pm on October 4, 2022: contributor

    How is a one-line include different/better than a simple one-line extern RecursiveMutex cs_main;?

    It’s probably neither much different or better, just one way of acheiving the same thing. The point is to remove unecessary/heavy includes, which aside from improving compilation time / # of units to rebuilt due to source changes

    Agree with the goal of cleaning up includes, and also agree neither extern RecursiveMutex cs_main; nor #include <cs_main.h> seem much better or worse. Would probably favor the extern over the include for a smaller diff and a little less complexity, unless there are other reasons for wanting the include.

  10. theuni commented at 7:37 pm on October 4, 2022: member

    Concept ACK. For files that need it, I think it makes sense to be able to include just cs_main without having to know its signature or pull in a header that thinks it knows it.

    And giving it a home in its own compilation unit means that we no longer have to pull in the arbitrary object where it resides just to get it linked. So I think this is a nice cleanup.

  11. DrahtBot commented at 8:21 pm on October 4, 2022: contributor

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

    Reviews

    See the guideline for information on the review process.

    Type Reviewers
    ACK ajtowns
    Concept ACK theuni, glozow

    If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #26762 (refactor: Make CCheckQueue RAII-styled by hebasto)
    • #26705 (clang-tidy: Check headers and fixes them by hebasto)
    • #25862 (refactor, kernel: Remove gArgs accesses from dbwrapper and txdb by ryanofsky)
    • #25781 (Remove almost all blockstorage globals by MarcoFalke)

    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.

  12. ajtowns commented at 4:00 am on October 5, 2022: contributor
    An #include seems preferable to redeclaring it directly; not really seeing the need to add the #include to cpp files that already #include <validation.h> though. Adding #include <cs_main.h> // IWYU pragma: export in validation.h should satisfy IWYU, I think?
  13. maflcko commented at 7:42 am on October 5, 2022: member
    IWYU can’t even produce the correct includes for a single line of code like int main() {std::regex{std::string{}};}, let alone on our code base as a whole. So I don’t think IWYU should be used as a reason for this change.
  14. glozow commented at 8:39 am on October 5, 2022: member
    Concept ACK
  15. ajtowns commented at 2:25 am on October 7, 2022: contributor

    So I don’t think IWYU should be used as a reason for this change.

    I was assuming silencing IWYU complaints was the only reason for adding for adding cs_main.h where validation.h was already included.

  16. DrahtBot added the label Needs rebase on Oct 10, 2022
  17. fanquake force-pushed on Oct 11, 2022
  18. DrahtBot removed the label Needs rebase on Oct 11, 2022
  19. DrahtBot added the label Needs rebase on Oct 26, 2022
  20. in src/bitcoin-chainstate.cpp:21 in 1d097f3a07 outdated
    17@@ -18,6 +18,7 @@
    18 #include <chainparams.h>
    19 #include <consensus/validation.h>
    20 #include <core_io.h>
    21+#include <cs_main.h>
    


    ajtowns commented at 0:56 am on January 4, 2023:

    I think it makes more sense to have #include <validation.h> imply that cs_main is available – in the short term it reduces the diff here, but even in the long term, I think we can expect cs_main to become a validation specific lock.

    Here’s what that would look like: https://github.com/ajtowns/bitcoin/commit/f4d29cf1fe2edeaf2892fe69502fd7dcab9f5a5d – it just takes the current patch, drops the redundant cs_main includes and adds an IWYU pragma to prevent iwyu from suggesting they get added back.


    fanquake commented at 12:31 pm on January 4, 2023:
    This does seem like a better approach, and reducing the diff. Have rebased and taken this with minor changes.
  21. ajtowns commented at 0:58 am on January 4, 2023: contributor
    Concept ACK – I think it’s better to #include things than re-declare them, and since we use cs_main in places where we don’t need all of validation, it makes sense to pull that out into a separate module.
  22. promag commented at 1:13 am on January 4, 2023: member

    @fanquake did you check if any #include <validation.h> could be removed?

    EDIT: irrelevant after #26251 (review)

  23. fanquake force-pushed on Jan 4, 2023
  24. fanquake force-pushed on Jan 4, 2023
  25. in src/Makefile.am:642 in e0bdefa001 outdated
    638@@ -638,6 +639,7 @@ libbitcoin_common_a_SOURCES = \
    639   compressor.cpp \
    640   core_read.cpp \
    641   core_write.cpp \
    642+  cs_main.cpp \
    


    maflcko commented at 12:48 pm on January 4, 2023:
    Why is this in common when it should be in kernel?

    fanquake commented at 3:51 pm on January 4, 2023:
    Right. Moved to util.

    maflcko commented at 3:56 pm on January 4, 2023:
    Why is this in util when it should be in kernel?

    fanquake commented at 3:57 pm on January 4, 2023:
    It also needs to be in common or util, otherwise you wont be able to link without the kernel?

    maflcko commented at 4:00 pm on January 4, 2023:

    Yes, bitcoin_kernel is linked into bitcoin_node according to https://github.com/bitcoin/bitcoin/blob/master/doc/design/libraries.md#dependencies

    cs_main shouldn’t be put into any other binary or library


    fanquake commented at 4:05 pm on January 4, 2023:

    Yes, bitcoin_kernel is linked into bitcoin_node

    Yes, but we don’t build the kernel by default. So if you drop cs_main out of the util lib, and try and ./autogen && ./configure && make this PR, it wont link.


    maflcko commented at 4:17 pm on January 4, 2023:
    validation.cpp is part of the kernel, and we do build that. If you are asking what to do here, you’ll have to put kernel/cs_main.cpp in the same section that holds validation.cpp. This should compile and link, no?

    fanquake commented at 4:46 pm on January 4, 2023:
    Right. I didn’t understand you actually meant move this to kernel/cs_main.*, and have that exist in the node lib. I have moved things around now.

    ajtowns commented at 3:00 am on January 5, 2023:
    lint is whining
  26. DrahtBot removed the label Needs rebase on Jan 4, 2023
  27. fanquake force-pushed on Jan 4, 2023
  28. fanquake force-pushed on Jan 4, 2023
  29. fanquake renamed this:
    refactor: add cs_main.h
    refactor: add kernel/cs_main.h
    on Jan 4, 2023
  30. refactor: add kernel/cs_main.*
    Co-authored-by: Anthony Towns <aj@erisian.com.au>
    282019cd3d
  31. fanquake force-pushed on Jan 5, 2023
  32. ajtowns commented at 11:19 pm on January 5, 2023: contributor
    ACK 282019cd3ddb060db350654e6f855f7ea37e0d34
  33. maflcko merged this on Jan 16, 2023
  34. maflcko closed this on Jan 16, 2023

  35. fanquake deleted the branch on Jan 16, 2023
  36. sidhujag referenced this in commit 3c4d79288a on Jan 16, 2023
  37. bitcoin locked this on Jan 16, 2024

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2024-11-17 09:12 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me