URCU and pthread mutex/cond variable hang
A message that is not in the archive (sent off-list or not yet received): [email protected]
----- Original Message -----
Quoted 13 lines
From: "Jeff Layton" <[email protected]>
To: [email protected]
Sent: Tuesday, March 3, 2015 3:40:14 PM
Subject: [lttng-dev] URCU and pthread mutex/cond variable hangI've been using urcu to develop some userland code, and I've run into a problem that I don't quite understand. If I register a thread and then have that thread block on a pthread condition variable, then it seems to cause
synchronize_rcuin another thread to hang.I've attached a testcase that demonstrates the problem. You have to build it and link it against -lpthread and -lurcu-qsbr:
Quoted 6 lines
$ gcc -Wall -o ./urcu_hang -lpthread -lurcu-qsbr urcu_hang.c...it will run fine. If you comment out the rcu_thread_offline/online calls though, it will hang.
Why? Is this expected behavior or a bug in urcu?
This is because you are using the urcu QSBR flavor. For this flavor, the default state of a registered thread is to be within a RCU read-side critical section, thus to block synchronize_rcu() until the next rcu_quiescent_state() call or until the next extended quiescent state (thread offline).
Therefore, you need to take care to put the threads in offline mode before you issue blocking operations that depend on completion of synchronize_rcu() to proceed further, which is the case of test_rcu() in your test program. Otherwise you create a deadlock, where main() is in a RCU read-side critical section while it blocks awaiting on the pthread cond var. Unfortuntately, the test_rcu() thread is unable to issue the pthread cond signal, because it is blocked on synchronize_rcu(), because main is itself in a RCU critical section.
Another alternative would be to use the other URCU flavors such as urcu-mb, urcu-signal or urcu-membarrier. Their default state is to be in a RCU quiescent state, which is IMHO more intuitive for the users. But QSBR is the fastest flavor, but it comes at the expense of a somewhat more complex API.
Hoping this explanation helps,
Thanks,
Mathieu
Quoted 10 lines
Thanks in advance!
--
Jeff Layton <[email protected]>_______________________________________________
lttng-dev mailing list
[email protected]
http://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. http://www.efficios.com
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 19 lines · On Tue, 10 Mar 2015 00:43:09 +0000 (UTC) Mathieu Desnoyers…
----- Original Message -----
Quoted 18 lines
From: "Jeff Layton" <[email protected]>
To: [email protected]
Sent: Tuesday, March 3, 2015 3:40:14 PM
Subject: [lttng-dev] URCU and pthread mutex/cond variable hangI've been using urcu to develop some userland code, and I've run into a problem that I don't quite understand. If I register a thread and then have that thread block on a pthread condition variable, then it seems to cause
synchronize_rcuin another thread to hang.I've attached a testcase that demonstrates the problem. You have to build it and link it against -lpthread and -lurcu-qsbr:
$ gcc -Wall -o ./urcu_hang -lpthread -lurcu-qsbr urcu_hang.c...it will run fine. If you comment out the rcu_thread_offline/online calls though, it will hang.
Quoted 8 lines
Quoted 1 line
Why? Is this expected behavior or a bug in urcu?
This is because you are using the urcu QSBR flavor. For this flavor, the default state of a registered thread is to be within a RCU read-side critical section, thus to block
synchronize_rcu()until the nextrcu_quiescent_state()call or until the next extended quiescent state (thread offline).
(facepalm)
Ahh ok. I totally missed that bit, but it makes sense now that you've pointed it out. So does that mean that rcu_read_lock/unlock are no-ops with QSBR?
Quoted 10 lines
Therefore, you need to take care to put the threads in offline mode before you issue blocking operations that depend on completion of
synchronize_rcu()to proceed further, which is the case oftest_rcu()in your test program. Otherwise you create a deadlock, wheremain()is in a RCU read-side critical section while it blocks awaiting on the pthread cond var. Unfortuntately, thetest_rcu()thread is unable to issue the pthread cond signal, because it is blocked onsynchronize_rcu(), because main is itself in a RCU critical section.
Yep, I've started doing that and everything is working well, but I'm a little worried that I could eventually end up missing someplace and hang everything. :)
Quoted 6 lines
Another alternative would be to use the other URCU flavors such as urcu-mb, urcu-signal or urcu-membarrier. Their default state is to be in a RCU quiescent state, which is IMHO more intuitive for the users. But QSBR is the fastest flavor, but it comes at the expense of a somewhat more complex API.
Yes, I may consider doing that instead.
Quoted 2 lines
Hoping this explanation helps,
It does -- many thanks!
Signature · 1 line
Jeff Layton <[email protected]>
A message that is not in the archive (sent off-list or not yet received): [email protected]
----- Original Message -----
Quoted 37 lines
From: "Jeff Layton" <[email protected]>
To: "Mathieu Desnoyers" <[email protected]>
Cc: [email protected]
Sent: Tuesday, March 10, 2015 3:25:39 PM
Subject: Re: [lttng-dev] URCU and pthread mutex/cond variable hangQuoted 19 lines · On Tue, 10 Mar 2015 00:43:09 +0000 (UTC) Mathieu Desnoyers…
----- Original Message -----
Quoted 18 lines
From: "Jeff Layton" <[email protected]>
To: [email protected]
Sent: Tuesday, March 3, 2015 3:40:14 PM
Subject: [lttng-dev] URCU and pthread mutex/cond variable hangI've been using urcu to develop some userland code, and I've run into a problem that I don't quite understand. If I register a thread and then have that thread block on a pthread condition variable, then it seems to cause
synchronize_rcuin another thread to hang.I've attached a testcase that demonstrates the problem. You have to build it and link it against -lpthread and -lurcu-qsbr:
$ gcc -Wall -o ./urcu_hang -lpthread -lurcu-qsbr urcu_hang.c...it will run fine. If you comment out the rcu_thread_offline/online calls though, it will hang.
Quoted 8 lines
Quoted 1 line
Why? Is this expected behavior or a bug in urcu?
This is because you are using the urcu QSBR flavor. For this flavor, the default state of a registered thread is to be within a RCU read-side critical section, thus to block
synchronize_rcu()until the nextrcu_quiescent_state()call or until the next extended quiescent state (thread offline).
Quoted 5 lines
(facepalm)
Ahh ok. I totally missed that bit, but it makes sense now that you've pointed it out. So does that mean that rcu_read_lock/unlock are no-ops with QSBR?
Yep, this is a copy-paste of their implementation:
static inline void rcu_read_lock(void)
{
}
static inline void rcu_read_unlock(void)
{
}
That's pretty much no-ops. I recommend that you keep them in your code for documentation purposes, and to facilitate porting to a different urcu flavor.
Glad to have helped :)
Thanks,
Mathieu
Quoted 33 lines
Quoted 11 lines
Therefore, you need to take care to put the threads in offline mode before you issue blocking operations that depend on completion of
synchronize_rcu()to proceed further, which is the case oftest_rcu()in your test program. Otherwise you create a deadlock, wheremain()is in a RCU read-side critical section while it blocks awaiting on the pthread cond var. Unfortuntately, thetest_rcu()thread is unable to issue the pthread cond signal, because it is blocked onsynchronize_rcu(), because main is itself in a RCU critical section.Yep, I've started doing that and everything is working well, but I'm a little worried that I could eventually end up missing someplace and hang everything. :)
Quoted 6 lines
Another alternative would be to use the other URCU flavors such as urcu-mb, urcu-signal or urcu-membarrier. Their default state is to be in a RCU quiescent state, which is IMHO more intuitive for the users. But QSBR is the fastest flavor, but it comes at the expense of a somewhat more complex API.
Yes, I may consider doing that instead.
Quoted 2 lines
Hoping this explanation helps,
It does -- many thanks!
--
Jeff Layton <[email protected]>
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. http://www.efficios.com