Re: atomic RAM ?

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Arnd Bergmann
Date: Thursday, April 8, 2010 - 7:15 am

On Thursday 08 April 2010, Michael Schnell wrote:

One really expensive but safe way to do atomic operations is to always
have them done on one CPU only, and provide a mechanism for other CPUs
to ask for an atomic operation using an inter-processor-interrupt.


Why can't you do a hash by memory address for this?

I would guess you can define an instruction to atomically set and check
a bit in a shared array of implementation-specific size, by passing
a token in that by convention is the memory address you want to lock.

Given two priviledged instructions

/* returns one if we got the lock, zero if someone else holds it */
bool hashlock_addr(volatile void *addr);
void hashunlock_addr(volatile void *addr);

you can do

int atomic_add_return(int i, atomic_t *v)
{
	int temp;

	while (!hashlock_addr(v))
		;
	smp_rmb();
	temp = v->counter;
	temp += i;
	v->counter = temp;
	smp_wmb();
	hashunlock_addr(v);
}

static inline unsigned long __cmpxchg(volatile unsigned long *m,
                                      unsigned long old, unsigned long new)
{
        unsigned long retval;
        unsigned long flags;

        while (!hashlock_addr(m))
		;
	smp_rmb()
        retval = *m;
        if (retval == old) {
                *m = new;
		smp_wmb();
	}
        hashunlock_addr(m);
        return retval;
}

Anything else you can build on top of these two, including the system calls
that are used from user applications. Since you never hold that bit lock for
more than a few cycles, you could do with much less than 1K bits, in theory
a single global mutex (ignoring the address entirely) would be enough.

That said, a real load-locked/store-conditional would be much more powerful,
in particular because it can also be used from user space, and it is typically
more efficient because it uses the same mechanisms as the cache coherency
protocol.

	Arnd
--
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
atomic RAM ?, Michael Schnell, (Thu Apr 8, 12:32 am)
Re: atomic RAM ?, Michael Schnell, (Thu Apr 8, 2:52 am)
Re: atomic RAM ?, Michael Schnell, (Thu Apr 8, 3:12 am)
Re: atomic RAM ?, Alan Cox, (Thu Apr 8, 3:45 am)
Re: atomic RAM ?, Michael Schnell, (Thu Apr 8, 5:11 am)
Re: atomic RAM ?, David Miller, (Thu Apr 8, 5:14 am)
Re: atomic RAM ?, Michael Schnell, (Thu Apr 8, 5:30 am)
Re: atomic RAM ?, Alan Cox, (Thu Apr 8, 6:37 am)
Re: atomic RAM ?, Arnd Bergmann, (Thu Apr 8, 7:15 am)
Re: atomic RAM ?, Mike Frysinger, (Thu Apr 8, 6:36 pm)
Re: atomic RAM ?, Michael Schnell, (Fri Apr 9, 2:23 am)
Re: atomic RAM ?, Michael Schnell, (Fri Apr 9, 3:54 am)
Re: atomic RAM ?, Michael Schnell, (Fri Apr 9, 3:55 am)
Re: atomic RAM ?, Alan Cox, (Fri Apr 9, 4:54 am)
Re: atomic RAM ?, Michael Schnell, (Fri Apr 9, 5:53 am)
Re: atomic RAM ?, Michael Schnell, (Fri Apr 9, 6:14 am)
Re: atomic RAM ?, Alan Cox, (Fri Apr 9, 6:15 am)
Re: atomic RAM ?, Michael Schnell, (Fri Apr 9, 6:32 am)
Re: atomic RAM ?, Michael Schnell, (Mon Apr 12, 2:58 am)
Re: atomic RAM ?, Pavel Machek, (Mon Apr 12, 5:54 am)
Re: atomic RAM ?, Arnd Bergmann, (Mon Apr 12, 8:02 am)
Re: [Nios2-dev] atomic RAM ?, Michael Schnell, (Tue Apr 13, 1:39 am)
Re: atomic RAM ?, Michael Schnell, (Tue Apr 13, 3:11 am)
Re: atomic RAM ?, Michael Schnell, (Wed Apr 14, 1:42 am)
Re: atomic RAM ?, Michael Schnell, (Wed Apr 14, 5:46 am)
Re: atomic RAM ?, Alan Cox, (Wed Apr 14, 5:57 am)
Re: atomic RAM ?, Michael Schnell, (Wed Apr 14, 7:38 am)