Re: [RFC] Userspace RCU: (ab)using futexes to save cpu cycles and energy

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Mathieu Desnoyers
Date: Monday, October 5, 2009 - 3:21 pm

* Mathieu Desnoyers (mathieu.desnoyers@polymtl.ca) wrote:

Hrm, thinking about it, the pthread_cond_wait/pthread_cond_broadcast
scheme I proposed above is racy.

If a wait_gp executes concurrently with wake_up_gp, we can end up doing:

gp_wait = 0

*    wait_gp:       uatomic_dec(&gp_wait);
*    wait_gp:       smp_mb()
*    wait_gp:       if (!num_old_reader_gp()) { (false)
*    wait_gp:       pthread_mutex_lock(&rcumutex);
*    wait_gp:       if (uatomic_read(&gp_wait) == -1) { (true)
* wake_up_gp:  unlikely(uatomic_read(&gp_wait) == -1) { (true)
* wake_up_gp:  uatomic_set(&gp_wait, 0);
* wait_up_gp:  pthread_cond_broadcast(&rcucond);
*    wait_gp:       pthread_cond_wait(&rcucond, &rcumutex);

might wait forever (or until the next wakeup).

We would need an extra mutex in the wakeup, e.g.:

static inline void wake_up_gp(void)
{
	/* just released old reader gp */
	smp_mb();
        if (unlikely(uatomic_read(&gp_wait) == -1)) {
		pthread_mutex_lock(&rcumutex);
                uatomic_set(&gp_wait, 0);
                pthread_cond_broadcast(&rcucond);
		pthread_mutex_unlock(&rcumutex);
        }
}

static void wait_gp(void)
{
	uatomic_dec(&gp_wait);
        smp_mb();
	if (!num_old_reader_gp()) {
        	smp_mb();
		atomic_set(&gp_wait, 0);
		goto unlock;
	}
        /* Read reader_gp before read futex */
        smp_rmb();
	pthread_mutex_lock(&rcumutex);
        if (uatomic_read(&gp_wait) == -1) {
                pthread_cond_wait(&rcucond, &rcumutex);
		pthread_mutex_unlock(&rcumutex);
	}
}

This should work better, but I'll need to do a formal model to convince
myself.

Mathieu


-- 
Mathieu Desnoyers
OpenPGP key fingerprint: 8CD5 52C3 8E3C 4140 715F  BA06 3F25 A8FE 3BAE 9A68
--
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
[RFC] Userspace RCU: (ab)using futexes to save cpu cycles ..., Mathieu Desnoyers, (Wed Sep 23, 10:48 am)
Re: [RFC] Userspace RCU: (ab)using futexes to save cpu cyc ..., Mathieu Desnoyers, (Wed Sep 23, 12:03 pm)
Re: [RFC] Userspace RCU: (ab)using futexes to save cpu cyc ..., Mathieu Desnoyers, (Wed Sep 23, 3:32 pm)
Re: [RFC] Userspace RCU: (ab)using futexes to save cpu cyc ..., Mathieu Desnoyers, (Wed Sep 23, 4:28 pm)
Re: [RFC] Userspace RCU: (ab)using futexes to save cpu cyc ..., Mathieu Desnoyers, (Sat Sep 26, 12:05 am)
Re: [RFC] Userspace RCU: (ab)using futexes to save cpu cyc ..., Mathieu Desnoyers, (Mon Oct 5, 3:21 pm)