RE: JavaMemoryModel: We need your litmus tests

From: Mike Marchywka (mmarchywka@eyewonder.com)
Date: Mon Sep 24 2001 - 12:24:56 EDT


I don't have a litmus test but I do have some code in search of
a "counter example."
I have a class that creates a primitive type array as
a circular queue. It writes to it in one thread, reads from another,and
works without synchronized or volatile.
I was trying to find out if there are ANY machines it doesn't run on.
I developed this while trying to write applet code that was unable to
hang a browser. I was rather worried about any synchronized
blocks/methods
for a number of reasons related to error/fault tolerance. I imagine the
peformance is better too since there is no monitor acquire/release
overhead.
Any thoughts/comments?
Thanks.

public class q2 implements Runnable
{
        // Mike Marchywka
        // mmarchywka@eyewonder.com, marchywka@hotmail.com
        // Test program for un-synchronized Thread communication
        // Attempt to see if JVM ever makes (relevant) out-of-order
writes
        //
        public static final int len=10;
        public static final int sz=(1<<len);
        public static final int mask=sz-1;
        // At least these should be volatile....
        // pointers to queue, rmw by one thread only
        public int r; //,w;
        public static int w;
        // shared info queue- read one, write by other
        public static int[] que= new int[sz];
        public int serial;
        public static int last;
public q2()
{
        serial=last;
        last+=1;
}
public void randomWait()
{
        long t1= System.currentTimeMillis();
        slowDown((int)t1&255);
}
public void slowDown(int i)
{
        try { Thread.sleep(i); } catch (Exception e) {}
}
public void doSomethingElse()
{
        try { Thread.sleep(10); } catch (Exception e) {}
}
// Consumer Thread runs here...
public void run()
{
        int i=1;
        int count=0;
        int errorCount=0;
        while (true)
        {
                if (r!=w)
                {
                        int j= que[r];
                        i=prn(i);
                        // did we get what we expect?
                        if (j!=i)
                        {
                                //System.out.println("instance="+serial+
" Didn't work: "+j+" instead of "+i);
                                errorCount++;
                        }
                        r=(r+1)&mask;
                        count++;
                        if (0==(count%100000))
                        {
                                System.out.println
                                        ("instance="+serial+ " i="+i+ "
count = "+count+" errors:"+errorCount);
                                // introduce some added timing variation
                                randomWait();
                        }
                }
                else
                        doSomethingElse();
        }
}
public static int prn(int n)
{
        n=(n)<<1; // 31 bit sr.
        if (n<0) // I picked this at "random"
                return ((n^0x024401)|1);
        else
                return (n);
}
public static void producerWait(int i)
{

        try { Thread.sleep(i); } catch (Exception e) {}
}
public static void main(String[] args)
{
        // create demand for our "product"
        q2 qa= new q2();
        Thread ta = new Thread(qa);
        ta.start();
        q2 qb= new q2();
        Thread tb = new Thread(qb);
        tb.start();
        int i=1;
        // Producer runs here
        while (true)
        {
                boolean ba=(0!=((qa.r-w-1)&mask));
                boolean bb=(0!=((qb.r-w-1)&mask));
                
                if (ba&&bb)
                {
                        i=prn(i);
                        que[w]=i;
                        w=(w+1)&mask;
                }
                else
                        producerWait(i&31);
        }
}
}

-----Original Message-----
From: Bill Pugh [mailto:pugh@cs.umd.edu]
Sent: 2001,September.24.Monday 11:43 AM
To: javamemorymodel@cs.umd.edu
Subject: JavaMemoryModel: We need your litmus tests

We are currently working on a model checker for our semantics. Given
a multithreaded example, it can tell you what behaviors are legal.

We hope to release this within a month or so. But for now, if you
could mail any example litmus tests to Jeremy Manson
<jmanson@cs.umd.edu>, it would be greatly appreciated. We will be
putting together a web page of all the litmus tests and their results.

Don't worry too much about the format; for right now, we will just
recode them in the input format we need. Please keep the examples
small (rather than full Java applications) and remember that at the
moment we only handle integers and object references (no arrays). We
do handle final fields and volatile variables.

        Bill
-------------------------------
JavaMemoryModel mailing list -
http://www.cs.umd.edu/~pugh/java/memoryModel
-------------------------------
JavaMemoryModel mailing list - http://www.cs.umd.edu/~pugh/java/memoryModel



This archive was generated by hypermail 2b29 : Thu Oct 13 2005 - 07:00:35 EDT