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

Jan Echternach (echter@informatik.uni-rostock.de)
Wed, 1 Dec 1999 16:27:25 +0100


On Tue, Nov 30, 1999 at 10:50:56AM -0500, Richard B. Johnson wrote:
> int main()
> {
> PARS *pars = NULL;

Shouldn't this be

volatile PARS *pars = NULL;

> volatile unsigned int key;

I don't think volatile is necessary here. key isn't shared.

> pars->spin += key;
> if(pars->spin != key)
> {
> pars->spin -= key;
/* didn't get spinlock */
> usleep(rand() % 10000);
> continue;
> }
/* got spinlock; critical region */
> pars->spin -= key;

Let's assume process 1 has key == 50 and process 2 has key == 60.
The `+=' operator shall be implemented as load register, add to
register, store register.

P1 P2 spin
-------------------- -------------------- ----------
0
load spin -> 0
load spin -> 0
add key -> 60
store spin 60
add key -> 50
store spin 50
load spin -> 50
compare -> didn't get lock
load spin -> 50
subtract key -> -10
store spin -10
load spin -> -10
compare -> didn't get lock
load spin -> -10
subtract key -> -60
store spin -60

Do you see the problem? The algorithm requires atomic add/sub operations.
Otherwise undoing modifications to pars->spin is not possible. Atomic
load/store is not enough.

-- 
Jan

- 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/