doc: Document locks - increase LOCK(...) comment coverage from 2 % to 77 % #11369

pull practicalswift wants to merge 1 commits into bitcoin:master from practicalswift:lock-comments changing 41 files +406 −402
  1. practicalswift commented at 1:00 PM on September 19, 2017: contributor

    Currently only a tiny fraction of all LOCK(...):s are documented with information on why each lock is held.

    In the absence of such comments it can sometimes be hard to see why a specific lock is held from merely reading the code. This risks leading to situations where redundant LOCK(...):s are kept by mistake or "just to be safe" after refactoring, etc.

    This PR improves the situation by adding comments with information on why a specific lock is being held by adding a comment using the form:

    LOCK(cs_lockName); // variableBeingGuardedByThisLock, FunctionRequiringHoldingThisLock(...)
    …
    variableBeingGuardedByThisLock += 1;
    …
    FunctionRequiringHoldingThisLock(someThing);
    

    Before this patch there were 9 documented and 514 undocumented LOCK/LOCK2:s –

    $ git grep -E "[^a-zA-Z_](LOCK|LOCK2)\(" -- "*.h" "*.cpp" | grep -v sync.h | grep "//" | wc -l
    9
    $ git grep -E "[^a-zA-Z_](LOCK|LOCK2)\(" -- "*.h" "*.cpp" | grep -v sync.h | grep -v "//" | wc -l
    514
    

    After this patch there are 405 documented and 118 undocumented LOCK/LOCK2:s –

    $ git grep -E "[^a-zA-Z_](LOCK|LOCK2)\(" -- "*.h" "*.cpp" | grep -v sync.h | grep "//" | wc -l
    405
    $ git grep -E "[^a-zA-Z_](LOCK|LOCK2)\(" -- "*.h" "*.cpp" | grep -v sync.h | grep -v "//" | wc -l
    118
    

    Note that there are still some locks left that are undocumented. I need help identifying the variable that is ultimately being guarded by each of these locks (it could be that some of these locks are unnecessary - if so, let me know!).

    Please help by browsing the remaining undocumented locks using ...

    $ git grep -3 -n -E '^[^/#]*[^A-Z_](LOCK|LOCK2)\([^/]*$' -- "*.cpp" "*.h"
    

    ... and see if you can help bring clarity. Each undocumented guarded variable that can be identified helps a lot - just leave a comment on the missing lock/guarded variable pair you've found and I'll track down all instances of that combination and add the appropriate comments :-)

    That will be of great help for this comment PR and also for the related thread-safety annotations PR that I'm working on (see #11226).

  2. fanquake added the label Docs and Output on Sep 19, 2017
  3. meshcollider commented at 1:23 PM on September 19, 2017: contributor

    Big concept ACK :+1:

  4. jnewbery commented at 6:05 PM on September 19, 2017: member

    concept ACK. I think this is especially helpful for new contributors to the project.

  5. theuni commented at 7:45 PM on September 19, 2017: member

    mmm, I like the idea, but I don't agree with annotating each lock with a comment. They will eventually go stale and be misleading or completely wrong.

    Instead, I'd rather annotate everything with GUARDED_BY (ala #11226), so that it's easy to see what things a mutex protects, as well as having it enforceable via static analysis.

  6. practicalswift commented at 9:24 PM on September 19, 2017: contributor

    @theuni You have a point regarding the risk for staleness unless we are careful to keep them up-to-date. If there is any interest I can commit to keeping these comments up-to-date - including removing no longer needed locks (due to refactoring, etc.).

    FWIW, I have written a script that generates these comments automatically by using the GUARDED_BY annotations added in #11226. The comments added in this PR were created by running that script.

  7. doc: Document locks - increase LOCK(...) comment coverage from 2 % to 77 % 916e0b2f90
  8. practicalswift force-pushed on Sep 21, 2017
  9. practicalswift commented at 12:35 PM on September 21, 2017: contributor

    Rebased! :-)

  10. jonasschnelli commented at 5:36 AM on September 22, 2017: contributor

    Seems NACKish to me. Will quickly run out of date and – if you ask me – you should follow/read the code to understand the lock rather then trusting a comment.

  11. MarcoFalke commented at 9:15 AM on September 22, 2017: member

    Agree with Cory. We might not need those annotations after #11226 is merged, as that pull already serves as documentation.

  12. fanquake commented at 1:41 AM on September 25, 2017: member

    Needs a rebase, but going to close this in favour of #11226. Seems like this sort of change could end up with a similar situation to what happened after enabling -Wshadow. Also seems like the EXCLUSIVE_LOCKS_REQUIRED and GUARDED_BY will work better as documentation themselves. @laanwj Any thoughts?

  13. fanquake closed this on Sep 25, 2017

  14. laanwj commented at 8:22 AM on September 28, 2017: member

    @fanquake I agree, annotations that can be checked/enforced by the compiler are preferable. This is a nice initiative in principle but keeping it up to date is unrealistic.

    On Sep 25, 2017 03:41, "fanquake" notifications@github.com wrote:

    Needs a rebase, but going to close this in favour of #11226 https://github.com/bitcoin/bitcoin/pull/11226. Seems like this sort of change could end up with a similar situation to what happened after enabling -Wshadow. Also seems like the EXCLUSIVE_LOCKS_REQUIRED and GUARDED_BY will work better as documentation themselves. @laanwj https://github.com/laanwj Any thoughts?

    — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/bitcoin/bitcoin/pull/11369#issuecomment-331756739, or mute the thread https://github.com/notifications/unsubscribe-auth/AAHutp9YP7AZlLBpBiM5h4TWuX26XOLSks5slwTIgaJpZM4PcWq4 .

  15. practicalswift deleted the branch on Apr 10, 2021
  16. DrahtBot 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-04-16 15:15 UTC

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