Skip to content

Conversation

laanwj
Copy link
Member

@laanwj laanwj commented Apr 28, 2016

Use std::unique_ptr for handling http work items, and std::move to move them into and out of the queue.

This makes the code more RAII and, as mentioned in the comment, is what I planned when I wrote the code in the first place.

@@ -71,8 +71,7 @@ class WorkQueue
/** Mutex protects entire object */
CWaitableCriticalSection cs;
CConditionVariable cond;
/* XXX in C++11 we can use std::unique_ptr here and avoid manual cleanup */
std::deque<WorkItem*> queue;
std::deque<std::unique_ptr<WorkItem> > queue;
Copy link
Member

@sipa sipa Apr 28, 2016

Choose a reason for hiding this comment

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

Also, in C++11, you no longer need to avoid the '>>' in Template1<Template2<Template3>>.

Copy link
Contributor

Choose a reason for hiding this comment

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

I was just about to ask why the space is there...

Copy link
Member Author

Choose a reason for hiding this comment

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

@paveljanik because >> is an operator. But yes with c++11 this is apparently parsed differently so will remove the space.

@laanwj laanwj force-pushed the 2016_04_httpserver_c++11 branch from c0c7d8b to 3fc99f4 Compare April 28, 2016 19:42
@@ -106,19 +105,15 @@ class WorkQueue
*/
~WorkQueue()
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need an empty destructor?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll remove it.

Copy link
Member Author

Choose a reason for hiding this comment

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

On second thought, I'm going to keep it, the precondition comment is still relevant.

@laanwj laanwj force-pushed the 2016_04_httpserver_c++11 branch 3 times, most recently from fa1b71a to d16d992 Compare May 4, 2016 13:30
@laanwj
Copy link
Member Author

laanwj commented May 4, 2016

Decided to forget about using std::move to move items conditionally into the queue, the resulting code was too complex, the old code is cleaner.

@laanwj laanwj force-pushed the 2016_04_httpserver_c++11 branch 2 times, most recently from 827a36f to 867bc6d Compare May 4, 2016 14:19
@@ -118,7 +112,7 @@ class WorkQueue
if (queue.size() >= maxDepth) {
return false;
}
queue.push_back(item);
queue.push_back(std::unique_ptr<WorkItem>(item));
Copy link
Member

Choose a reason for hiding this comment

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

nit: queue.emplace_back(item);

@theuni
Copy link
Member

theuni commented May 5, 2016

ut ACK, other than the little nit

laanwj added 4 commits May 5, 2016 08:27
Use std::unique_ptr for handling work items.

This makes the code more RAII and, as mentioned in the comment, is what
I planned when I wrote the code in the first place.
@laanwj laanwj force-pushed the 2016_04_httpserver_c++11 branch from 867bc6d to f0188f9 Compare May 5, 2016 06:28
@laanwj
Copy link
Member Author

laanwj commented May 5, 2016

Fixed @theuni's nit, thanks

@laanwj laanwj merged commit f0188f9 into bitcoin:master May 5, 2016
laanwj added a commit that referenced this pull request May 5, 2016
f0188f9 http: use std::move to move HTTPRequest into HTTPWorkItem (Wladimir J. van der Laan)
37b2137 http: Change boost::scoped_ptr to std::unique_ptr in HTTPRequest (Wladimir J. van der Laan)
f97b410 http: Add log message when work queue is full (Wladimir J. van der Laan)
091d6e0 http: Do a pending c++11 simplification (Wladimir J. van der Laan)
@@ -118,7 +112,7 @@ class WorkQueue
if (queue.size() >= maxDepth) {
return false;
}
queue.push_back(item);
queue.emplace_back(std::unique_ptr<WorkItem>(item));
Copy link
Member

Choose a reason for hiding this comment

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

Creating the unique_ptr here defeats the purpose of the emplace :)
Passing it item directly would mean that the unique_ptr would be constructed in-place. As is, it's moved instead.

Not worth worrying about, just pointing it out.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes by all means feel free to improve this further.

codablock pushed a commit to codablock/dash that referenced this pull request Dec 21, 2017
…work items

f0188f9 http: use std::move to move HTTPRequest into HTTPWorkItem (Wladimir J. van der Laan)
37b2137 http: Change boost::scoped_ptr to std::unique_ptr in HTTPRequest (Wladimir J. van der Laan)
f97b410 http: Add log message when work queue is full (Wladimir J. van der Laan)
091d6e0 http: Do a pending c++11 simplification (Wladimir J. van der Laan)
sickpig pushed a commit to sickpig/BitcoinUnlimited that referenced this pull request Mar 12, 2018
HTTP Server cherries from Core:

bitcoin/bitcoin#6719 - Make HTTP server shutdown more graceful
bitcoin/bitcoin#6859 - http: Restrict maximum size of http + headers
bitcoin/bitcoin#6990 - http: speed up shutdown
bitcoin/bitcoin#7966 - http: Do a pending c++11 simplification handling work items
bitcoin/bitcoin#8421 - httpserver: drop boost (#8023 dependency)
bitcoin/bitcoin#11006 - Improve shutdown process
marlengit pushed a commit to marlengit/BitcoinUnlimited that referenced this pull request Sep 25, 2018
HTTP Server cherries from Core:

bitcoin/bitcoin#6719 - Make HTTP server shutdown more graceful
bitcoin/bitcoin#6859 - http: Restrict maximum size of http + headers
bitcoin/bitcoin#6990 - http: speed up shutdown
bitcoin/bitcoin#7966 - http: Do a pending c++11 simplification handling work items
bitcoin/bitcoin#8421 - httpserver: drop boost (#8023 dependency)
bitcoin/bitcoin#11006 - Improve shutdown process
zkbot added a commit to zcash/zcash that referenced this pull request Oct 1, 2020
Small httpserver.cpp backports

Also includes a change to the `uiInterface.NotifyBlockTip` signal API.
These remove merge conflicts from subsequent backports for `sync.h`.

Cherry-picked from the following upstream PRs:
- bitcoin/bitcoin#6859
- bitcoin/bitcoin#7112
  - Only the non-QT changes.
- bitcoin/bitcoin#7966
- bitcoin/bitcoin#8421
  - We already backported the second commit in #2555
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Sep 8, 2021
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.

5 participants