Re: Shared memory not SMP safe to user-mode code

Petr Vandrovec Ing. VTEI (VANDROVE@vc.cvut.cz)
Wed, 1 Dec 1999 00:03:47 MET-1


> typedef struct {
> unsigned int pad0;
> unsigned int pad1;
> unsigned int pad2;
> unsigned int i;
> unsigned int j;
> unsigned int spin;
You should mark at least 'spin' as volatile, otherwise bad optimization
can occur...
> static __inline__ unsigned int unique()
> {
> int ret;
> __asm__ __volatile__( "rdtsc\n"
> : "=eax" (ret)
> : /* No inputs */
> : "eax", "edx" );
> return ret;
And remove "eax" from list of used register to get it compile in gcc-2.95.2
Parent:
> while(pars->spin)
> key = unique();
> pars->spin += key;
> if(pars->spin != key)
> {
> }
> if(pars->i < pars->j)
> fprintf(stderr, "i = %08x, j = %08x\n", pars->i, pars->j);
> else
> fprintf(stderr, "Good\n");
> pars->spin -= key;

Son:
> while(pars->spin)
> key = unique();
> pars->spin += key;
> pars->i++;
> pars->j++;
> pars->spin -= key;
> }
Look into code. Son is for any reason started first. While it does

pars->spin -= key;
while (pars->spin) key = unique();
pars->spin += key;

Operations: read, sub, write, read, compare, read, add, write
So there is <------------------------->
this window to switch to parent. But parent does
while (pars->spin) key = unique();
and this includes 'rdtsc' which is not very fast (and without -O2 it does
also call/ret). If you add 'unique()' after 'pars->spin -= key;', it will
happilly print couple of 'Good' (after some time it will core dump).
Best regards,
Petr Vandrovec
vandrove@vc.cvut.cz

P.S.: I do not say that I'm right... But I think that I'm.
P.P.S.: dual PIII/450

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu
Please read the FAQ at http://www.tux.org/lkml/