refactor: Misc scheduler cleanups #19090

pull MarcoFalke wants to merge 7 commits into bitcoin:master from MarcoFalke:2005-schedulerCleanup changing 5 files +72 −73
  1. MarcoFalke commented at 1:07 PM on May 28, 2020: member

    This accumulates a bunch of cleanup that was long overdue, but I haven't yet gotten around to address. Specifically, but not limited to:

    • Remove unused code, documentation and includes
    • Upgrade to doxygen documentation

    Please refer to the individual commits for more details.

  2. scheduler: Remove unused REVERSE_LOCK
    No longer needed after commit d61f2bb076d8f
    fa609c4f76
  3. test: Remove unused scheduler.h include from the common setup
    The common setup is included in virtually all tests, so it should be
    as slim as possible.
    fa9819695a
  4. MarcoFalke added the label Refactoring on May 28, 2020
  5. practicalswift commented at 1:16 PM on May 28, 2020: contributor

    Concept ACK

  6. in src/scheduler.h:79 in fafaf24446 outdated
      77 | -    // done servicing whatever task they're currently servicing (drain=false)
      78 | +    // Tell any threads running serviceQueue to stop as soon as they are
      79 | +    // done servicing whatever task they are currently servicing (drain=false)
      80 |      // or when there is no work left to be done (drain=true)
      81 | -    void stop(bool drain=false);
      82 | +    void stop(bool drain);
    


    ajtowns commented at 1:17 PM on May 28, 2020:

    Might be better to have something like:

        // Tell any threads running serviceQueue to stop as soon as they are
        // done servicing whatever task they are currently servicing.
        void stop_immediately();
    
        // Tell any threads running serviceQueue to stop as soon as
        // there is no work left to be done.
        void stop_when_drained();
    

    MarcoFalke commented at 1:25 PM on May 28, 2020:

    Sure, will do that


    MarcoFalke commented at 2:39 PM on May 28, 2020:

    Done

  7. in src/scheduler.h:70 in fa52400257 outdated
      60 | @@ -62,8 +61,6 @@ class CScheduler
      61 |       */
      62 |      void MockForward(std::chrono::seconds delta_seconds);
      63 |  
      64 | -    // To keep things as simple as possible, there is no unschedule.
    


    ajtowns commented at 1:19 PM on May 28, 2020:

    Hmm, I kind-of think that comment's helpful.


    MarcoFalke commented at 1:25 PM on May 28, 2020:

    This should be clear from our general policy that everything should be as minimal as possible and that unused features should not be implemented.

    If the comment is brought back, I'd prefer the alternative version:

         // To keep things as simple as possible, there is no unschedule and the scheduler can not schedule events on your electronic barista
    
  8. ajtowns commented at 1:20 PM on May 28, 2020: member

    Concept ACK

  9. doc: Switch boost::thread to std::thread in scheduler
    After commit d0ebd93 the scheduler itself no longer cares if the
    serviceQueue is run in a std::thread or boost::thread. Change the
    documentation to std::thread because we switched to C++11.
    fab2950d70
  10. doc: Remove unused documentation about unimplemented features fa9cca0550
  11. MarcoFalke force-pushed on May 28, 2020
  12. in src/scheduler.h:21 in fae1ef5e7b outdated
      39 | + * Simple class for background tasks that should be run
      40 | + * periodically or once "after a while"
      41 | + *
      42 | + * Usage:
      43 | + *
      44 | + * CScheduler* s = new CScheduler();
    


    jnewbery commented at 3:23 PM on May 28, 2020:

    If you're changing this, perhaps make it a unique_ptr to indicate unique ownership?


    MarcoFalke commented at 4:27 PM on May 28, 2020:

    Not sure how to preserve this part of the comment then:

    delete s; // Must be done after thread is interrupted/joined.
    

    If this is changed, I'd rather just

    CScheduler s{};
    s.scheduleFromNow(doSomething, std::chrono::milliseconds{11}); // Assuming a: void doSomething() { }
    s.scheduleFromNow([=] { this->func(argument); }, std::chrono::milliseconds{3});
    std::thread t{[&] { s.serviceQueue(); }};
     
     ... then at program shutdown, make sure to call stop() to clean up the thread(s) running serviceQueue:
    s.stop();
    t.join();
    

    jnewbery commented at 5:18 PM on May 28, 2020:

    That looks reasonable to me, although I don't think it's that important. If you don't think it matters, leave it as is to avoid bikeshedding.

    However, I do think that you should s/stop()/StopImmediately()/ in this comment now that you're removing the stop() function.


    MarcoFalke commented at 6:26 PM on May 28, 2020:

    Left as is for now.

  13. in src/scheduler.h:77 in fae1ef5e7b outdated
      80 | +    {
      81 | +        WITH_LOCK(newTaskMutex, stopRequested = true);
      82 | +        newTaskScheduled.notify_all();
      83 | +    }
      84 | +    /** Tell any threads running serviceQueue to stop when there is no work left to be done */
      85 | +    void StopWhenDrained()
    


    jnewbery commented at 3:55 PM on May 28, 2020:

    This is a really good change. Explicitly naming these functions makes it much clearer at the call site.

    Do you think a follow-up should remove the StopWhenDrained() function entirely? It's only used in tests.


    MarcoFalke commented at 6:26 PM on May 28, 2020:

    Done

  14. in src/scheduler.h:87 in fae1ef5e7b outdated
      94 | -                        std::chrono::system_clock::time_point &last) const;
      95 | +    /**
      96 | +     * Returns number of tasks waiting to be serviced,
      97 | +     * and first and last task times
      98 | +     */
      99 | +    size_t getQueueInfo(std::chrono::system_clock::time_point& first, std::chrono::system_clock::time_point& last) const;
    


    jnewbery commented at 4:17 PM on May 28, 2020:

    I find it more readable to to have the arguments split over multiple lines, though I guess that means I should open a PR to remove AllowAllParametersOfDeclarationOnNextLine: false from .clang-format?


    MarcoFalke commented at 4:53 PM on May 28, 2020:

    In this case splitting over multiple lines is allowed because BinPack... is already set to false. However, unless AlignAfterOpenBracket is changed to Align I find it less readable.

    In any case changing .clang-format should be done in a separate pr, and I might object changing AlignAfterOpenBracket to Align because it wastes so much whitespace.


    jnewbery commented at 5:38 PM on May 28, 2020:

    MarcoFalke commented at 10:06 AM on June 21, 2020:

    thx fixed

  15. jnewbery commented at 4:19 PM on May 28, 2020: member

    Code review ACK fae1ef5e7

  16. MarcoFalke force-pushed on May 28, 2020
  17. MarcoFalke commented at 6:26 PM on May 28, 2020: member

    Removed some more code as requested by @jnewbery

  18. scheduler: Replace stop(true) with StopWhenDrained()
    This helps understanding the code at the call site without having to
    look up the name of the argument or the default value.
    fac43f9889
  19. doc: Switch scheduler to doxygen comments fa3d41b5ab
  20. MarcoFalke force-pushed on May 28, 2020
  21. MarcoFalke commented at 11:31 PM on May 28, 2020: member

    Reverted back to the previous version for a smaller diff

  22. jnewbery commented at 2:17 AM on May 29, 2020: member

    reACK fa197f1d1119346b8980cf1a8b34ceee07f30a6a

    only change is renaming StopImmediately() to stop() (presumably so StopWhenDrained() can be removed in a follow-up PR.

    Reverted back to the previous version for a smaller diff

    Good. My original suggestion was to remove StopWhenDrained() in a follow-up. No need to do everything in one PR.

  23. MarcoFalke referenced this in commit e6f807f87c on Jun 21, 2020
  24. clang-format scheduler
    Can easily be reviewed with the git options
    --ignore-all-space --word-diff-regex=. -U0
    fa8337fcdb
  25. MarcoFalke force-pushed on Jun 21, 2020
  26. MarcoFalke commented at 10:07 AM on June 21, 2020: member

    Force pushed last commit, but should be trivial to re-review with git range-diff bitcoin-core/master --word-diff-regex=. A B

  27. jnewbery commented at 1:27 PM on June 22, 2020: member

    utACK fa8337fcdbcab8368d5bf111c4b2e5acf25e6e22

  28. MarcoFalke merged this on Jun 25, 2020
  29. MarcoFalke closed this on Jun 25, 2020

  30. MarcoFalke deleted the branch on Jun 25, 2020
  31. sidhujag referenced this in commit a9c4279d65 on Jul 7, 2020
  32. sidhujag referenced this in commit 6e5f8607cd on Jul 8, 2020
  33. PastaPastaPasta referenced this in commit c45dda6bbb on Jun 27, 2021
  34. PastaPastaPasta referenced this in commit 089ff0f52e on Jun 28, 2021
  35. PastaPastaPasta referenced this in commit 170818d645 on Jun 29, 2021
  36. PastaPastaPasta referenced this in commit 5c297fe8ae on Jul 1, 2021
  37. PastaPastaPasta referenced this in commit 849a583664 on Jul 1, 2021
  38. PastaPastaPasta referenced this in commit 3f3bdafd4e on Jul 15, 2021
  39. Fabcien referenced this in commit c85ed4043d on Aug 27, 2021
  40. Fabcien referenced this in commit ff7bc6bc59 on Aug 27, 2021
  41. Fabcien referenced this in commit cdc0cfb74c on Aug 27, 2021
  42. deadalnix referenced this in commit 16a035e7e7 on Aug 27, 2021
  43. deadalnix referenced this in commit 4526add539 on Aug 27, 2021
  44. deadalnix referenced this in commit 42914f4cf6 on Aug 27, 2021
  45. PastaPastaPasta referenced this in commit fe8e1c9d9b on Sep 28, 2021
  46. PastaPastaPasta referenced this in commit c37e1ae5e7 on Sep 28, 2021
  47. kittywhiskers referenced this in commit cc2c318afe on Oct 12, 2021
  48. DrahtBot locked this on Feb 15, 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-04-17 06:14 UTC

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