refactor: Cleanup thread ctor calls #19064

pull hebasto wants to merge 3 commits into bitcoin:master from hebasto:200524-bind changing 11 files +71 −39
  1. hebasto commented at 3:45 PM on May 24, 2020: member

    This PR does not change behavior. Its goal is to improve readability and maintainability of the code.

  2. MarcoFalke commented at 4:29 PM on May 24, 2020: member

    Concept ACK to enforce the void return type on threads

  3. MarcoFalke added the label Refactoring on May 24, 2020
  4. Empact commented at 11:15 PM on May 26, 2020: member

    Concept ACK on the first two commits, ~0 on https://github.com/bitcoin/bitcoin/pull/19064/commits/2a816bd79093eac7d0bbae3106fdd7fddbc99cf2 as I don't see the cited improvements justifying the change.

  5. DrahtBot commented at 8:03 PM on May 27, 2020: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #21800 ([WIP] mempool/validation: mempool ancestor/descendant limits for packages by glozow)
    • #21796 (index: Avoid async shutdown on init error 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.

  6. DrahtBot cross-referenced this on May 27, 2020 from issue test: Set -logthreadnames in unit tests by MarcoFalke
  7. DrahtBot cross-referenced this on May 27, 2020 from issue Add extra thread for scheduler, move TorControl and OpenAddedConnections to scheduler by naumenkogs
  8. DrahtBot cross-referenced this on May 27, 2020 from issue Periodically update DNS caches for better privacy of non-reachable nodes by naumenkogs
  9. DrahtBot cross-referenced this on May 27, 2020 from issue net: Add NAT-PMP port forwarding support by hebasto
  10. in src/util/system.cpp:1135 in 2a816bd790 outdated
    1131 | @@ -1132,6 +1132,25 @@ int GetNumCores()
    1132 |      return std::thread::hardware_concurrency();
    1133 |  }
    1134 |  
    1135 | +void TraceThread(const char* thread_name, std::function<void()> thread_func)
    


    MarcoFalke commented at 12:13 AM on May 28, 2020:

    As you move this anyway, what about moving threadnames.h to thread.h and adding this to that file? Advantage would be that stuff like src/index/base.cpp that doesn't need the whole system.h can just include the slim thread.h


    hebasto commented at 9:40 AM on April 25, 2021:

    Thanks! Updated.

  11. DrahtBot cross-referenced this on May 28, 2020 from issue p2p: Speed up initial connection to p2p network by ajtowns
  12. hebasto force-pushed on May 28, 2020
  13. hebasto commented at 9:52 AM on May 28, 2020: member

    Updated 2a816bd79093eac7d0bbae3106fdd7fddbc99cf2 -> bcd04708a6fe32cf7fba2969ec378c34e6950278 (pr19064.01 -> pr19064.03, diff):

    As you move this anyway, what about moving threadnames.h to thread.h and adding this to that file? Advantage would be that stuff like src/index/base.cpp that doesn't need the whole system.h can just include the slim thread.h

  14. DrahtBot cross-referenced this on May 28, 2020 from issue addrman: improve performance by using more suitable containers by vasild
  15. DrahtBot cross-referenced this on May 28, 2020 from issue Add BIP324 encrypted p2p transport de-/serializer (only used in tests) by jonasschnelli
  16. DrahtBot cross-referenced this on May 28, 2020 from issue refactor: Nuke policy/fees->mempool circular dependencies by hebasto
  17. DrahtBot cross-referenced this on May 28, 2020 from issue util: add RunCommandParseJSON by Sjors
  18. in src/util/thread.h:12 in bcd04708a6 outdated
      21 | @@ -21,6 +22,11 @@ void ThreadSetInternalName(std::string&&);
      22 |  //! logging.
      23 |  const std::string& ThreadGetInternalName();
      24 |  
      25 | +/**
      26 | + * A wrapper for do-something-once thread functions.
    


    MarcoFalke commented at 4:45 PM on May 30, 2020:

    Based on the circular dependency and what this function does (Run a thread with log statements), maybe this is better suited in logging.h? No strong opinion, so feel free to not move this at all.


    hebasto commented at 4:49 PM on May 30, 2020:

    Maybe keep threadnames.{h,cpp} and move TraceThread() to new thread.{h,cpp}?


    MarcoFalke commented at 4:52 PM on May 30, 2020:

    Sure if that works. Cutting down the util/system bloat can't hurt.


    hebasto commented at 5:35 PM on May 30, 2020:
  19. hebasto force-pushed on May 30, 2020
  20. hebasto commented at 5:35 PM on May 30, 2020: member

    Updated bcd04708a6fe32cf7fba2969ec378c34e6950278 -> 90aa1f7f503818a67d18ec20401707d5e4ca658d (pr19064.03 -> pr19064.04, diff):

    • fixed circular dependencies
  21. DrahtBot cross-referenced this on Jun 6, 2020 from issue refactor: Replace RecursiveMutex with Mutex in Shutdown() by hebasto
  22. DrahtBot added the label Needs rebase on Jun 8, 2020
  23. hebasto force-pushed on Jun 8, 2020
  24. hebasto commented at 3:14 PM on June 8, 2020: member

    Rebased 90aa1f7f503818a67d18ec20401707d5e4ca658d -> dd00f7c2c3d3af64f8a09d6b36d80f80fa96ff48 (pr19064.04 -> pr19064.05) due to the conflict with #19180.

  25. DrahtBot removed the label Needs rebase on Jun 8, 2020
  26. hebasto force-pushed on Jun 9, 2020
  27. hebasto commented at 6:13 AM on June 9, 2020: member

    Updated dd00f7c2c3d3af64f8a09d6b36d80f80fa96ff48 -> 73370d039ec42a71171569e9b3f012e936a4ebe3 (pr19064.05 -> pr19064.06, diff):

    • fixed Windows builds
  28. DrahtBot cross-referenced this on Jun 9, 2020 from issue [WIP DONOTMERGE] Replace boost with C++17 (std::shared_mutex) by MarcoFalke
  29. DrahtBot cross-referenced this on Jun 11, 2020 from issue UI external signer support (e.g. hardware wallet) by Sjors
  30. DrahtBot cross-referenced this on Jun 11, 2020 from issue External signer support - Wallet Box edition by Sjors
  31. DrahtBot cross-referenced this on Jun 16, 2020 from issue Replace boost::filesystem with std::filesystem by kiminuo
  32. DrahtBot cross-referenced this on Jun 20, 2020 from issue build: Do not include server symbols in wallet by MarcoFalke
  33. DrahtBot cross-referenced this on Jun 25, 2020 from issue [net] Cleanup logic around connection types by amitiuttarwar
  34. DrahtBot added the label Needs rebase on Jul 1, 2020
  35. hebasto force-pushed on Jul 3, 2020
  36. hebasto commented at 8:06 AM on July 3, 2020: member

    Rebased 73370d039ec42a71171569e9b3f012e936a4ebe3 -> ea12b8f04dc869f740c41fdc28d9d9fb6486f20c (pr19064.06 -> pr19064.07) due to the conflicts with #19028 and #19197.

  37. DrahtBot removed the label Needs rebase on Jul 3, 2020
  38. DrahtBot cross-referenced this on Jul 20, 2020 from issue Remove mempool global by MarcoFalke
  39. DrahtBot cross-referenced this on Jul 30, 2020 from issue Disable fee estimation in blocksonly mode (by removing the fee estimates global) by darosior
  40. DrahtBot cross-referenced this on Aug 2, 2020 from issue Add -netinfo peer connections dashboard by jonatack
  41. DrahtBot added the label Needs rebase on Aug 5, 2020
  42. hebasto force-pushed on Aug 5, 2020
  43. hebasto commented at 5:52 PM on August 5, 2020: member

    Rebased ea12b8f04dc869f740c41fdc28d9d9fb6486f20c -> 427e396b16564d9c735238a2b2d24472bfcd55c3 (pr19064.07 -> pr19064.08) due to the conflict with #15382.

  44. DrahtBot removed the label Needs rebase on Aug 5, 2020
  45. DrahtBot added the label Needs rebase on Aug 12, 2020
  46. hebasto force-pushed on Aug 13, 2020
  47. hebasto commented at 2:11 PM on August 13, 2020: member

    Rebased 427e396b16564d9c735238a2b2d24472bfcd55c3 -> 338488f056169898905bf03fb3df34e6877833ac (pr19064.08 -> pr19064.09) due to the conflict with #19316.

  48. DrahtBot removed the label Needs rebase on Aug 13, 2020
  49. hebasto closed this on Aug 13, 2020

  50. hebasto reopened this on Aug 13, 2020

  51. in src/txmempool.cpp:22 in 338488f056 outdated
      18 | @@ -19,6 +19,8 @@
      19 |  #include <util/time.h>
      20 |  #include <validationinterface.h>
      21 |  
      22 | +#include <math.h>
    


    practicalswift commented at 7:00 AM on August 16, 2020:
    #include <cmath>
    

    hebasto commented at 7:22 AM on August 16, 2020:

    Thanks! Updated.

  52. practicalswift commented at 7:00 AM on August 16, 2020: contributor

    Concept ACK

  53. hebasto force-pushed on Aug 16, 2020
  54. hebasto commented at 7:21 AM on August 16, 2020: member

    Updated 338488f056169898905bf03fb3df34e6877833ac -> 4335a15ac6aca65430471ead52d389813f4260e7 (pr19064.09 -> pr19064.10, diff):

  55. DrahtBot cross-referenced this on Aug 22, 2020 from issue Remove gArgs global from init by MarcoFalke
  56. DrahtBot added the label Needs rebase on Aug 26, 2020
  57. hebasto force-pushed on Aug 26, 2020
  58. hebasto commented at 9:45 AM on August 26, 2020: member

    Rebased 4335a15ac6aca65430471ead52d389813f4260e7 -> 806aedebc2b0627e681480be39c2d93af87e0863 (pr19064.10 -> pr19064.11) due to the conflict with #19779.

  59. DrahtBot removed the label Needs rebase on Aug 26, 2020
  60. DrahtBot cross-referenced this on Sep 5, 2020 from issue eBPF Linux tracepoints by jb55
  61. DrahtBot cross-referenced this on Sep 22, 2020 from issue net: Use alternative port for incoming Tor connections by hebasto
  62. DrahtBot cross-referenced this on Sep 23, 2020 from issue net: Add CNode::ConnectedThroughNetwork member function by hebasto
  63. DrahtBot cross-referenced this on Sep 24, 2020 from issue net, rpc, cli: expose peer network in getpeerinfo; simplify/improve -netinfo by jonatack
  64. DrahtBot added the label Needs rebase on Oct 2, 2020
  65. hebasto force-pushed on Oct 2, 2020
  66. hebasto commented at 9:05 PM on October 2, 2020: member

    Rebased 806aedebc2b0627e681480be39c2d93af87e0863 -> 8cb5f901dfeb8de5427532e6646c3a5a131fed14 (pr19064.11 -> pr19064.12) due to the conflict with #19991.

  67. DrahtBot removed the label Needs rebase on Oct 2, 2020
  68. DrahtBot cross-referenced this on Oct 25, 2020 from issue cli: -netinfo quick updates/fixups for 0.21 by jonatack
  69. hebasto force-pushed on Oct 26, 2020
  70. hebasto commented at 8:55 PM on October 26, 2020: member

    Rebased 8cb5f901dfeb8de5427532e6646c3a5a131fed14 -> 7e7a999e9e4bdef9726037afa4ec0325a355528e (pr19064.12 -> pr19064.13).

  71. DrahtBot added the label Needs rebase on Oct 29, 2020
  72. hebasto force-pushed on Oct 29, 2020
  73. hebasto commented at 3:11 PM on October 29, 2020: member

    Rebased 7e7a999e9e4bdef9726037afa4ec0325a355528e -> 25c8e21b0f316ce7804e10de813c4bcd81d3c2f3 (pr19064.13 -> pr19064.14).

  74. DrahtBot removed the label Needs rebase on Oct 29, 2020
  75. DrahtBot cross-referenced this on Oct 31, 2020 from issue Add missing thread safety annotations by vasild
  76. DrahtBot cross-referenced this on Nov 25, 2020 from issue Add syscall sandboxing using seccomp-bpf (Linux secure computing mode) by practicalswift
  77. DrahtBot cross-referenced this on Dec 5, 2020 from issue tests: Create or use existing properly initialized NodeContexts by dongcarl
  78. DrahtBot added the label Needs rebase on Dec 9, 2020
  79. hebasto force-pushed on Dec 10, 2020
  80. hebasto commented at 7:33 PM on December 10, 2020: member

    Rebased 25c8e21b0f316ce7804e10de813c4bcd81d3c2f3 -> 358a0447f59608af7bd51fe200e27baefb78159e (pr19064.14 -> pr19064.15) due to the conflict with #20323.

  81. DrahtBot removed the label Needs rebase on Dec 10, 2020
  82. DrahtBot added the label Needs rebase on Jan 7, 2021
  83. hebasto force-pushed on Jan 7, 2021
  84. hebasto commented at 11:42 PM on January 7, 2021: member

    Updated 358a0447f59608af7bd51fe200e27baefb78159e -> 519838dbb91410544743b69c3d386fff9e4eb400 (pr19064.15 -> pr19064.16):

    • rebased due to the conflict with #18077
    • addressed @Empact's comment and dropped the last controversial commit
  85. DrahtBot removed the label Needs rebase on Jan 8, 2021
  86. DrahtBot cross-referenced this on Jan 8, 2021 from issue net: add RAII socket and use it instead of bare SOCKET by vasild
  87. DrahtBot cross-referenced this on Jan 27, 2021 from issue refactor: remove boost::thread_group usage by fanquake
  88. jonatack commented at 3:42 PM on January 27, 2021: contributor

    Concept ACK. Rebased to current master 15a9df070 and this debug builds cleanly. Now that we have moved on from C++ 11, can this pull also use lambdas in the place of std::bind and std::function?

  89. hebasto commented at 3:48 PM on January 27, 2021: member

    Concept ACK. Rebased to current master 15a9df0 and this debug builds cleanly. Now that we have moved on from C++ 11, can this pull also use lambdas in the place of std::bind and std::function?

    The dedicated commit was removed from this PR in the latest push, see #19064 (comment)

  90. jonatack commented at 3:53 PM on January 27, 2021: contributor

    It seems worthwhile to do. https://youtu.be/ZlHi8txU4aQ

  91. jonatack commented at 5:27 PM on January 27, 2021: contributor

    Also https://youtu.be/zt7ThwVfap0?t=1721 (I'm still going through it, though)

    Screenshot from 2021-01-27 18-21-18

    Screenshot from 2021-01-27 18-20-48

  92. DrahtBot cross-referenced this on Jan 29, 2021 from issue refactor: move load block thread into ChainstateManager by fanquake
  93. DrahtBot added the label Needs rebase on Feb 1, 2021
  94. hebasto force-pushed on Apr 13, 2021
  95. DrahtBot removed the label Needs rebase on Apr 13, 2021
  96. hebasto commented at 7:26 PM on April 13, 2021: member

    Updated 519838dbb91410544743b69c3d386fff9e4eb400 -> 33f9134f61d99903939c3048340927d051fd60f9 (pr19064.16 -> pr19064.17):

  97. DrahtBot cross-referenced this on Apr 14, 2021 from issue rpc/validation: enable packages through testmempoolaccept by glozow
  98. in src/validation.cpp:49 in 33f9134f61 outdated
      45 | @@ -46,6 +46,7 @@
      46 |  #include <util/rbf.h>
      47 |  #include <util/strencodings.h>
      48 |  #include <util/system.h>
      49 | +#include <util/threadnames.h>
    


    kiminuo commented at 8:54 AM on April 25, 2021:

    Why is this added? I can't find any util::* call.


    hebasto commented at 9:36 AM on April 25, 2021:

    Thanks! Updated.

  99. refactor: Make TraceThread a non-template free function
    Also it is moved into its own module.
    30e4448215
  100. hebasto force-pushed on Apr 25, 2021
  101. hebasto commented at 9:35 AM on April 25, 2021: member

    Updated 33f9134f61d99903939c3048340927d051fd60f9 -> 2a3ad116c8cf00ac44c9ad6c8585edf37054761d (pr19064.17 -> pr19064.18, diff):

  102. jonatack commented at 7:01 PM on April 25, 2021: contributor

    ACK 2a3ad116c8cf00ac44c9ad6c8585edf37054761d

    Recommend reviewers view the diff squashed to one commit as the second and third commit rewrite parts of the first one.

  103. in src/txmempool.cpp:21 in 2a3ad116c8 outdated
      17 | @@ -18,6 +18,7 @@
      18 |  #include <validation.h>
      19 |  #include <validationinterface.h>
      20 |  
      21 | +#include <cmath>
    


    jnewbery commented at 9:52 AM on April 26, 2021:

    Why are you adding this?


    jonatack commented at 10:15 AM on April 26, 2021:

    I asked myself this as well--it's a missing header for llround used in the file.


    hebasto commented at 2:54 PM on April 29, 2021:

    In a previous iteration of this PR this header was required to build successfully for the reason @jonatack pointed at.

    Currently, this change looks unrelated. Should I drop it?


    hebasto commented at 3:25 PM on April 29, 2021:
  104. in src/net.cpp:2550 in 2a3ad116c8 outdated
    2546 | @@ -2546,15 +2547,14 @@ bool CConnman::Start(CScheduler& scheduler, const Options& connOptions)
    2547 |          return false;
    2548 |      }
    2549 |      if (connOptions.m_use_addrman_outgoing || !connOptions.m_specified_outgoing.empty())
    2550 | -        threadOpenConnections = std::thread(&TraceThread<std::function<void()> >, "opencon", std::function<void()>(std::bind(&CConnman::ThreadOpenConnections, this, connOptions.m_specified_outgoing)));
    2551 | +        threadOpenConnections = std::thread(&util::TraceThread, "opencon", [this, connOptions] { ThreadOpenConnections(connOptions.m_specified_outgoing); });
    


    jnewbery commented at 9:54 AM on April 26, 2021:

    Consider just capturing what you need, rather than making a copy of the entire connOptions object:

         if (connOptions.m_use_addrman_outgoing || !connOptions.m_specified_outgoing.empty())
    -        threadOpenConnections = std::thread(&util::TraceThread, "opencon", [this, connOptions] { ThreadOpenConnections(connOptions.m_specified_outgoing); });
    +        threadOpenConnections = std::thread(&util::TraceThread,
    +                                            "opencon",
    +                                            [this, connect = connOptions.m_specified_outgoing] { ThreadOpenConnections(connect); });
     
         // Process messages
    

    hebasto commented at 3:46 PM on April 29, 2021:

    Thanks! Updated.

  105. in src/init.cpp:1270 in 2a3ad116c8 outdated
    1266 | @@ -1266,7 +1267,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
    1267 |      node.scheduler = std::make_unique<CScheduler>();
    1268 |  
    1269 |      // Start the lightweight task scheduler thread
    1270 | -    node.scheduler->m_service_thread = std::thread([&] { TraceThread("scheduler", [&] { node.scheduler->serviceQueue(); }); });
    1271 | +    node.scheduler->m_service_thread = std::thread([&] { util::TraceThread("scheduler", [&] { node.scheduler->serviceQueue(); }); });
    


    jnewbery commented at 10:01 AM on April 26, 2021:

    Perhaps:

        node.scheduler->m_service_thread = std::thread(util::TraceThread, "scheduler", [&] { node.scheduler->serviceQueue(); });
    

    To make this more readable and consistent with the other calls to std::thread ctor.


    hebasto commented at 3:46 PM on April 29, 2021:

    Thanks! Updated.

  106. in src/test/util/setup_common.cpp:137 in 2a3ad116c8 outdated
     133 | @@ -132,7 +134,7 @@ ChainTestingSetup::ChainTestingSetup(const std::string& chainName, const std::ve
     134 |      // We have to run a scheduler thread to prevent ActivateBestChain
     135 |      // from blocking due to queue overrun.
     136 |      m_node.scheduler = std::make_unique<CScheduler>();
     137 | -    m_node.scheduler->m_service_thread = std::thread([&] { TraceThread("scheduler", [&] { m_node.scheduler->serviceQueue(); }); });
     138 | +    m_node.scheduler->m_service_thread = std::thread([&] { util::TraceThread("scheduler", [&] { m_node.scheduler->serviceQueue(); }); });
    


    jnewbery commented at 10:01 AM on April 26, 2021:

    Perhaps:

        m_node.scheduler->m_service_thread = std::thread(&util::TraceThread("scheduler", [&] { m_node.scheduler->serviceQueue(); });
    

    To make this more readable and consistent with the other calls to std::thread ctor.


    hebasto commented at 3:47 PM on April 29, 2021:

    Thanks! Updated.

  107. jnewbery commented at 10:03 AM on April 26, 2021: member

    Looks good. A few suggestions inline.

    Perhaps thread.h and threadnames.h should be merged?

  108. DrahtBot cross-referenced this on Apr 28, 2021 from issue fuzz: Add fuzzing syscall sandbox: detect use of unexpected syscalls when fuzzing ("syscall sanitizer") by practicalswift
  109. DrahtBot cross-referenced this on Apr 28, 2021 from issue index: Avoid async shutdown on init error by MarcoFalke
  110. DrahtBot cross-referenced this on Apr 29, 2021 from issue mempool/validation: mempool ancestor/descendant limits for packages by glozow
  111. hebasto force-pushed on Apr 29, 2021
  112. hebasto marked this as a draft on Apr 29, 2021
  113. refactor: Use appropriate thread constructor a508f718f3
  114. refactor: Replace std::bind with lambdas
    Lambdas are shorter and more readable.
    Changes are limited to std::thread ctor calls only.
    792be53d3e
  115. hebasto force-pushed on Apr 29, 2021
  116. hebasto commented at 3:46 PM on April 29, 2021: member

    Updated 2a3ad116c8cf00ac44c9ad6c8585edf37054761d -> 792be53d3e9e366b9f6aeee7a1eeb912fa28062e (pr19064.18 -> pr19064.19, diff):

    Perhaps thread.h and threadnames.h should be merged?

    It will introduce a new circular dependency "logging -> util/threadnames -> logging".

  117. hebasto marked this as ready for review on Apr 29, 2021
  118. jnewbery commented at 3:50 PM on April 29, 2021: member

    utACK 792be53d3e9e366b9f6aeee7a1eeb912fa28062e

  119. jonatack commented at 6:56 PM on April 29, 2021: contributor

    Changes are even nicer now.

    tACK 792be53d3e9e366b9f6aeee7a1eeb912fa28062e

  120. in src/init.cpp:1270 in 792be53d3e
    1266 | @@ -1266,7 +1267,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
    1267 |      node.scheduler = std::make_unique<CScheduler>();
    1268 |  
    1269 |      // Start the lightweight task scheduler thread
    1270 | -    node.scheduler->m_service_thread = std::thread([&] { TraceThread("scheduler", [&] { node.scheduler->serviceQueue(); }); });
    1271 | +    node.scheduler->m_service_thread = std::thread(util::TraceThread, "scheduler", [&] { node.scheduler->serviceQueue(); });
    


    MarcoFalke commented at 6:50 AM on May 12, 2021:

    Is there a reason or even a difference between sometimes using &util::TraceThread and other times util::TraceThread?


    hebasto commented at 8:10 AM on May 12, 2021:

    No, there isn't. See https://en.cppreference.com/w/cpp/language/pointer#Pointers_to_functions

    It seems I overlooked that inconsistency.

  121. MarcoFalke commented at 6:51 AM on May 12, 2021: member

    cr ACK 792be53d3e9e366b9f6aeee7a1eeb912fa28062e

  122. MarcoFalke merged this on May 12, 2021
  123. MarcoFalke closed this on May 12, 2021

  124. hebasto deleted the branch on May 12, 2021
  125. sidhujag referenced this in commit 51e4bee06f on May 12, 2021
  126. gwillen referenced this in commit 99cc7a5245 on Jun 1, 2022
  127. bitcoin locked this on Aug 18, 2022

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: 2026-05-20 06:54 UTC