This is the mail archive of the java@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: hrm...


Adam Megacz wrote:

In TinySSL, I generate some entropy like this:

   static class entropySpinner extends Thread {
       volatile boolean stop = false;
       byte counter = 0;
       entropySpinner() { start(); }
       public void run() {
           System.out.println("start!");
           while (!stop) counter++;
           System.out.println("stop!");
       }
   }

   entropySpinner[] spinners = new entropySpinner[10];
   for(int i=0; i<spinners.length; i++) spinners[i] = new entropySpinner();
   try { Thread.sleep(100); } catch (Exception e) { }
   for(int i=0; i<spinners.length; i++) {
       spinners[i].stop = true;
       randpool[i] = spinners[i].counter;
   }

But for some reason, under GCJ, the spinners keep spinning... the
thread never stops. Could it be possible that GCJ is overaggressively
optimizing out the while loop's test and turning it into an infinite
loop?

Yes, GCJ doesn't yet understand "volatile" so its possible that its optimizing the test away, or even that the memory seen by two different CPUs is inconsistent (on an MP system). One work-around would be to put a synchronized {} around the test.

regards

Bryce.



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]