lkml.org 
[lkml]   [1999]   [Dec]   [1]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
From
SubjectRe: Shared memory not SMP safe to user-mode code.
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/

\
 
 \ /
  Last update: 2005-03-22 13:55    [W:0.067 / U:0.172 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site