Skip to content

Conversation

@ken2812221
Copy link
Contributor

@ken2812221 ken2812221 commented Oct 15, 2018

Based on #14464 and #14480. Add interruptible thread class to replace boost::thread

The remaining usage of libboost_thread is boost::shared_mutex

@ken2812221
Copy link
Contributor Author

Apparently I wrote a MSVC-only code. I am going to investigate it.

@ken2812221 ken2812221 changed the title refactor: Drop boost::thread and boost::chrono [WIP] refactor: Drop boost::thread and boost::chrono Oct 16, 2018
@meshcollider
Copy link
Contributor

Related #8631

@ken2812221 ken2812221 changed the title [WIP] refactor: Drop boost::thread and boost::chrono refactor: Drop boost::thread and boost::chrono Oct 16, 2018
@ken2812221
Copy link
Contributor Author

This is ready for review

Copy link
Contributor

Choose a reason for hiding this comment

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

Missing newline at end of file :-)

Copy link
Contributor

@practicalswift practicalswift Oct 17, 2018

Choose a reason for hiding this comment

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

Is the nonpublic inheritance here intentional? If so, make the inheritance explicitly private and add a comment to clarify that this is intentional. If not, make it public :-)

Background:

ThreadInterrupted won't be catched by say catch (const std::exception& e) since ThreadInterrupted has nonpublic inheritance from std::exception.

Example:

[cling]$ #include <iostream>
[cling]$ class PublicInheritanceException : public std::exception {};
[cling]$ try {
             throw PublicInheritanceException();
         } catch (const std::exception& e) {
             std::cout << "exception catched\n";
         }
exception catched
[cling]$ class PrivateByDefaultInheritanceException : std::exception {};
[cling]$ try {
             throw PrivateByDefaultInheritanceException();
         } catch (const std::exception& e) {
             std::cout << "exception catched\n";
         }
*program terminates*

@ken2812221
Copy link
Contributor Author

ken2812221 commented Oct 17, 2018

@donaloconnor It should use mutex because it's used by the condition_variable.

For reference, you can find boost implementation for pthread, it acquires lock before it reads interrupt_requested.

If it can drop InterruptibleSleep in the future, then it's fine to switch to atomic.

@donaloconnor
Copy link
Contributor

@ken2812221 ah yes for the sleep. Thanks for the link to boosts impl.

@ken2812221
Copy link
Contributor Author

Actually, I think it's fine to make it use both atomic and mutex at the same time. Then we don't have to lock mutex in InterruptionPoint.

@donaloconnor
Copy link
Contributor

In this case it might work but in practice it can cause race conditions. The value of the atomic can change from the moment the cond var is signalled and the predicate is checked (to work with spurious wakeups). Eg: false->true->notify+wakeup->false->predcheck.

The last false flag can never happen here (at least my understanding of the code) I guess so maybe it's okay.

@ken2812221
Copy link
Contributor Author

@donaloconnor I am not sure people would allow this solution. Now the flag contains two variable:

  • atomic bool read in InterruptionPoint
  • non-atomic bool read in InterruptionSleep

@donaloconnor
Copy link
Contributor

donaloconnor commented Oct 18, 2018

I think it's not worth the complexity tbh. Sorry. I don't know if the perf improvement is worth the extra complexity without measurements. Maybe we should just stick to what boost did as you had in original. I think using the atomic in the cond var is okay for this case because it's state only goes from false->true.

I'd prefer if others chimed in to give feedback on this..

Re: spurious wakeups in the sleep. Do you think that's an issue? The sleep could get ended prematurely. Probably not a big deal. I don't know how much the code relies on accurate sleeps.

@practicalswift
Copy link
Contributor

What about splitting the work in two parts?

Start with the low-risk Boost replacement: that change is likely worth it in itself as long as no deviations from what Boost was doing are introduced.

When that work is finished: continue with the risker performance improvement change you are considering.

Does that make sense?

@DrahtBot
Copy link
Contributor

DrahtBot commented Oct 20, 2018

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

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #14624 (Some simple improvements to the RNG code by sipa)
  • #14605 (Return of the Banman by dongcarl)
  • #14252 (build: Run functional tests and benchmarks under the undefined behaviour sanitizer (UBSan) by practicalswift)
  • #13910 (Trivial: Log progress while verifying blocks at level 4 by domob1812)
  • #13743 (refactor: Replace boost::bind with std::bind by ken2812221)
  • #13389 (Utils and libraries: Fix -sysperms=false (default) doesn't appear to do anything #13371 - move umask operation earlier in AppInit() by n2yen)
  • #13382 (util: Don't throw in GetTime{Millis,Micros}(). Mark as noexcept. by practicalswift)
  • #12980 (Allow quicker shutdowns during LoadBlockIndex() by jonasschnelli)
  • #12557 ([WIP] 64 bit iOs device support by Sjors)
  • #10102 ([experimental] Multiprocess bitcoin by ryanofsky)

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.

This commit add a Interrupt function for CCheckQueue that it can handle interrupt by itself instead of relying on boost thread interrupt
replace boost::mutex with debuggable Mutex
replace boost::condition_variable with std::condition_variable
add const specifier to fMaster and nBatchSize
add clang thread safety attributes
move init value of member of CCheckQueue from constructor to definition
Manage CCheckQueue workder threads by itself. Do not expose the threads outside the object.
@DrahtBot
Copy link
Contributor

DrahtBot commented Nov 5, 2018

Needs rebase

@maflcko
Copy link
Member

maflcko commented Mar 6, 2020

fyi, Boost::Chrono has been removed

@bitcoin bitcoin locked as resolved and limited conversation to collaborators Aug 16, 2022
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.

8 participants