[parisc-linux] Re: [parisc-linux-cvs] linux grundler

Matthew Wilcox willy@debian.org
Sun, 9 Feb 2003 02:03:25 +0000


On Sat, Feb 08, 2003 at 04:56:08PM -0800, Randolph Chung wrote:
> > lock(l1)
> > do something
> > unlock(l1)
> > 
> > lock(l2)
> > do something else
> > unlock(l2)
> > 
> > will gcc ever move the lock(l2) before the unlock(l1)? 
> 
> well, bad example, since l1 and l2 are both volatile, but in general,
> nothing will prevent part of "do something else" to happen inside the l1
> lock, right?

I think it's even worse than that.  What stops gcc reordering:

typedef struct {
        spinlock_t lock;
        volatile int counter;
} rwlock_t;

static  __inline__ void _raw_read_lock(rwlock_t *rw)
{
        while (__ldcw (&(x)->lock) == 0) \
                while (((x)->lock) == 0) ; } while (0)
        rw->counter++;
        do { (x)->lock = 1; } while(0)
}

to:

        while (__ldcw (&(x)->lock) == 0) \
                while (((x)->lock) == 0) ; } while (0)
        do { (x)->lock = 1; } while(0)
        rw->counter++;

-- 
"It's not Hollywood.  War is real, war is primarily not about defeat or
victory, it is about death.  I've seen thousands and thousands of dead bodies.
Do you think I want to have an academic debate on this subject?" -- Robert Fisk