Fencing Sanity Check

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Fencing Sanity Check

JSR166 Concurrency mailing list
I asked the following question on StackOverflow.
https://stackoverflow.com/questions/48754065/re-ordering-of-assignments-and-adding-a-fence 
Please respond there.

The following code looks a little strange because I have simplified it
down to the bare essentials.  I think the code has an ordering problem. 
I am looking at the first table in
http://g.oswego.edu/dl/jmm/cookbook.html and it seems the normal store
can be reordered with the volatile store in change().

Can the assignment to m_normal in change() move ahead of the assignment
of m_volatile?  In other words, can get() return null?

How should I solve this?  Would adding "value = m_volatile;" after
"m_volatile = value;" prevent the assignment of m_normal happening
before the assignment of m_volatile?

private volatile Object m_volatile;
private          Object m_normal;

....

public void change()
{
    Object value;

    value = m_normal;

    if (value == null)
       return;

    m_volatile = value;
    m_normal   = null;
}

public Object get()
{
    Object value;

    value = m_normal;

    if (value == null)
       value = m_volatile;
   
    return(value);
}

-- -Nathan
_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: Fencing Sanity Check

JSR166 Concurrency mailing list
No, it won’t help the API as a whole. get() is racy whatever you do to change().

Alex


> On 12 Feb 2018, at 19:27, Nathan and Ila Reynolds via Concurrency-interest <[hidden email]> wrote:
>
> I asked the following question on StackOverflow. https://stackoverflow.com/questions/48754065/re-ordering-of-assignments-and-adding-a-fence Please respond there.
>
> The following code looks a little strange because I have simplified it down to the bare essentials.  I think the code has an ordering problem.  I am looking at the first table in http://g.oswego.edu/dl/jmm/cookbook.html and it seems the normal store can be reordered with the volatile store in change().
>
> Can the assignment to m_normal in change() move ahead of the assignment of m_volatile?  In other words, can get() return null?
>
> How should I solve this?  Would adding "value = m_volatile;" after "m_volatile = value;" prevent the assignment of m_normal happening before the assignment of m_volatile?
>
> private volatile Object m_volatile;
> private          Object m_normal;
>
> ....
>
> public void change()
> {
>   Object value;
>
>   value = m_normal;
>
>   if (value == null)
>      return;
>
>   m_volatile = value;
>   m_normal   = null;
> }
>
> public Object get()
> {
>   Object value;
>
>   value = m_normal;
>
>   if (value == null)
>      value = m_volatile;
>      return(value);
> }
>
> -- -Nathan
> _______________________________________________
> Concurrency-interest mailing list
> [hidden email]
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest