Repository navigation
Conversation
|
O yeah, I remember this PR. It was really awesome, the inline instead of the do while seems a modern approach to do while preprocessor magic. Hopefully @saghul has more time to review it this time! |
|
@ahippo doh, this fell through the cracks, I'm afraid! Yeah, please rebase on top of master since we're now getting started with v2 stuff. |
Thank you!
No worries, it was quite intrusive for a stable branch. |
Make QUEUE a struct instead of an array of pointers.
This adds some minimal type-checking to QUEUE_*() macros.
Also, it makes the purpose of fields a bit clearer
(it's not that obvious that "void* active_reqs[2]" is not an arbitrary array
of void* pointers but is, in fact, a QUEUE).
Also, this aids in debugging queue-related issues.
As a consequence, factor out public uv-queue.h header with QUEUE
definition from queue.h.
queue.h still contains the actual implementation.
Drop now unneeded QUEUE_{NEXT,PREV}() macros and friends as
q->next or q->prev is shorter and clearer (especially, when used as lvalue).
PR-URL: libuv#620
Inline functions provide more type-checking, are as fast as macros, and are easier to debug. PR-URL: libuv#620
These macros/functions should be used to remove elements from a queue while iterating over it. This turns out to be a common use-case for QUEUE. For example, calling uv_close() on a handle from the handle's callback ends up calling QUEUE_REMOVE() from within QUEUE_FOREACH(), which is unsafe if special care is not taken. Win backend open-codes a similar thing in src/win/loop-watcher.c, for example. PR-URL: libuv#620
It's bad to leave dangling pointers in QUEUE operations. Most QUEUE operations update all involved queue elements' prev/next pointers to be valid, except for QUEUE_ADD() and QUEUE_REMOVE(). These operations leave dangling pointers behind. So, explicitly poison pointers that shouldn't be used after QUEUE_ADD() and QUEUE_REMOVE() calls. This greatly aids in debugging as it causes an immediate segfault upon dereference of these pointers instead of making havoc down the road (like infinite loops over the queue, at best). PR-URL: libuv#620
Introduce QUEUE_DESTROY() as a complementary operation to QUEUE_INIT(). For now, it just poisons prev/next pointers as the QUEUE is statically allocated. PR-URL: libuv#620
…event callback Revert 4634376 unix: fix uv_fs_event_stop() from fs_event_cb Instead of using an ad-hoc trick with additional variable to indicate if a queue is being iterated over, and actually implementing QUEUE_REMOVE_SAFE() macro for this particular queue, use generic QUEUE_FOREACH_SAFE() and QUEUE_REMOVE_SAFE() pair of macros to handle queue modifications from within user callbacks while iterating over the queue. The callstack is as follows: uv__inotify_read() | \/ QUEUE_FOREACH() <--- [1] | \/ user's callback | \/ uv_close() (or uv_fs_event_stop()) | \/ uv__fs_event_close() | \/ uv_fs_event_stop() | \/ QUEUE_REMOVE() <--- [2] Eventually, QUEUE_REMOVE() [2] is called from within QUEUE_FOREACH() [1], which leads the next loop iteration to dereference a dangling pointer. Use "safe" versions of appropriate macros with a shared iterator, so that QUEUE_REMOVE_SAFE() can update it in case someone is removing an element from under out feet. The use of an elements "next" pointer after the element is removed from a queue is found using "next" pointer poisoning in QUEUE_REMOVE(). PR-URL: libuv#620
Use QUEUE_FOREACH_SAFE() instead of open-coding the same thing using careful while() and q->next. PR-URL: libuv#620
We have a careful QUEUE implementation, so use it instead of open-coding a similar functionality. PR-URL: libuv#620
…llback Revert 4634376 unix: fix uv_fs_event_stop() from fs_event_cb Instead of using an ad-hoc trick with additional variable to indicate if a queue is being iterated over, and actually implementing QUEUE_REMOVE_SAFE() macro for this particular queue, use generic QUEUE_FOREACH_SAFE() and QUEUE_REMOVE_SAFE() pair of macros to handle queue modifications from within user callbacks while iterating over the queue. The callstack is as follows: uv__inotify_read() | \/ QUEUE_FOREACH() <--- [1] | \/ user's callback | \/ uv_close() (or uv_fs_event_stop()) | \/ uv__fs_event_close() | \/ uv_fs_event_stop() | \/ QUEUE_REMOVE() <--- [2] Eventually, QUEUE_REMOVE() [2] is called from within QUEUE_FOREACH() [1], which leads the next loop iteration to dereference a dangling pointer. Use "safe" versions of appropriate macros with a shared iterator, so that QUEUE_REMOVE_SAFE() can update it in case someone is removing an element from under out feet. The use of an elements "next" pointer after the element is removed from a queue is found using "next" pointer poisoning in QUEUE_REMOVE(). PR-URL: libuv#620
Use QUEUE_FOREACH_SAFE() instead of open-coding the same thing using careful while() and q->next. PR-URL: libuv#620
We have a careful QUEUE implementation, so use it instead of open-coding a similar functionality. PR-URL: libuv#620
5608904 to
d6ec3e6
Compare
|
@saghul, I've just rebased my changes on top of master branch. Comments are welcome! |
|
@ahippo are you interested in picking this back up? It has a number of conflicts at the moment. |
|
Yes, I think that's a good place to start. |
OK, I'll work on rebasing the PR on top of current master, and will update the PR. |
Make QUEUE a struct instead of an array of pointers.
This adds some minimal type-checking to QUEUE_*() macros.
Also, it makes the purpose of fields a bit clearer
(it's not that obvious that "void* active_reqs[2]" is not an arbitrary array
of void* pointers but is, in fact, a QUEUE).
Also, this aids in debugging queue-related issues.
As a consequence, factor out public uv-queue.h header with QUEUE
definition from queue.h.
queue.h still contains the actual implementation.
Drop now unneeded QUEUE_{NEXT,PREV}() macros and friends as
q->next or q->prev is shorter and clearer (especially, when used as lvalue).
PR-URL: libuv#620
Inline functions provide more type-checking, are as fast as macros, and are easier to debug. PR-URL: libuv#620
These macros/functions should be used to remove elements from a queue while iterating over it. This turns out to be a common use-case for QUEUE. For example, calling uv_close() on a handle from the handle's callback ends up calling QUEUE_REMOVE() from within QUEUE_FOREACH(), which is unsafe if special care is not taken. Win backend open-codes a similar thing in src/win/loop-watcher.c, for example. PR-URL: libuv#620
It's bad to leave dangling pointers in QUEUE operations. Most QUEUE operations update all involved queue elements' prev/next pointers to be valid, except for QUEUE_ADD() and QUEUE_REMOVE(). These operations leave dangling pointers behind. So, explicitly poison pointers that shouldn't be used after QUEUE_ADD() and QUEUE_REMOVE() calls. This greatly aids in debugging as it causes an immediate segfault upon dereference of these pointers instead of making havoc down the road (like infinite loops over the queue, at best). PR-URL: libuv#620
Introduce QUEUE_DESTROY() as a complementary operation to QUEUE_INIT(). For now, it just poisons prev/next pointers as the QUEUE is statically allocated. PR-URL: libuv#620
…llback Revert 4634376 unix: fix uv_fs_event_stop() from fs_event_cb Instead of using an ad-hoc trick with additional variable to indicate if a queue is being iterated over, and actually implementing QUEUE_REMOVE_SAFE() macro for this particular queue, use generic QUEUE_FOREACH_SAFE() and QUEUE_REMOVE_SAFE() pair of macros to handle queue modifications from within user callbacks while iterating over the queue. The callstack is as follows: uv__inotify_read() | \/ QUEUE_FOREACH() <--- [1] | \/ user's callback | \/ uv_close() (or uv_fs_event_stop()) | \/ uv__fs_event_close() | \/ uv_fs_event_stop() | \/ QUEUE_REMOVE() <--- [2] Eventually, QUEUE_REMOVE() [2] is called from within QUEUE_FOREACH() [1], which leads the next loop iteration to dereference a dangling pointer. Use "safe" versions of appropriate macros with a shared iterator, so that QUEUE_REMOVE_SAFE() can update it in case someone is removing an element from under out feet. Similarly, use QUEUE_FOREACH_SAFE() in uv__inotify_fork(). The use of an elements "next" pointer after the element is removed from a queue was found using "next" pointer poisoning in QUEUE_REMOVE(). PR-URL: libuv#620
Use QUEUE_FOREACH_SAFE() instead of open-coding the same thing using careful while() and q->next. PR-URL: libuv#620
We have a careful QUEUE implementation, so use it instead of open-coding a similar functionality. PR-URL: libuv#620
d6ec3e6 to
320d673
Compare
|
@cjihrig , I've rebased my changes on top of the current master. |
|
@cjihrig , are there any comments/questions about the changes? |
cjihrig
left a comment
There was a problem hiding this comment.
Left a couple comments. To be honest, I'm still not entirely sure what the end goal of this PR is. Would it make more sense to split it up into smaller, more easily digestible PRs?
I'd also like @bnoordhuis and @santigimeno to weigh in.
| #ifndef UV_QUEUE_H_ | ||
| #define UV_QUEUE_H_ | ||
|
|
||
| #ifdef __cplusplus |
There was a problem hiding this comment.
Are these blocks necessary?
There was a problem hiding this comment.
Not really most likely.
uv/queue.h can be included from C++ code via uv.h only, which has these extern "C" guards.
Also, uv/queue.h only defines a POD struct now, so there should be no difference C vs C++.
I just put extern "C" to be on a safer side as they don't hurt.
I'll remove them.
| (q) != (h); \ | ||
| (q) = (n), (n) = (n)->next) | ||
|
|
||
| static inline int QUEUE_EMPTY(const QUEUE *q) { |
There was a problem hiding this comment.
Can you align the pointer stars to the left throughout.
There was a problem hiding this comment.
Sure thing.
I'll post an updated version after gathering some more comments.
GitHub shows commits in a wrong order, which makes it even harder to understand what's going on. There are four goals overall:
|
|
Closing because this PR is unfortunately very stale. |
This is an improved version of #577, which instead of exposing the whole queue.h to a user and making it a part of the public API, exposes only the absolutely necessary part of queue.h (
struct QUEUEdeclaration).This pull request also incorporates comments raised as part of review request #577.
Everything else is identical to #577.
I didn't force-push new changesets to #577 as this would wipe many comments there because they are tied to particular changesets.