1 /*
   2  * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 /*
  24  */
  25 
  26 
  27 package org.graalvm.compiler.jtt.threads;
  28 
  29 import org.graalvm.compiler.jtt.JTTTest;
  30 import org.junit.Rule;
  31 import org.junit.Test;
  32 import org.junit.rules.TestRule;
  33 
  34 /**
  35  * Inspired by {@code com.sun.media.sound.DirectAudioDevice$DirectDL.drain()}.
  36  *
  37  * Two loop exits hold a monitor while merging.
  38  *
  39  */
  40 public final class SynchronizedLoopExit01 extends JTTTest {
  41 
  42     @Rule public TestRule timeout = createTimeoutSeconds(20);
  43 
  44     protected Object object = new Object();
  45     protected volatile boolean drained = false;
  46     protected volatile boolean someBoolean = true;
  47     protected volatile int someInt = 3;
  48 
  49     public boolean test() {
  50         boolean b = true;
  51         while (!drained) {
  52             synchronized (object) {
  53                 boolean c = b = someBoolean;
  54                 if (c || drained) {
  55                     break;
  56                 }
  57             }
  58         }
  59         return b;
  60     }
  61 
  62     @Test
  63     public void run0() throws Throwable {
  64         runTest("test");
  65     }
  66 
  67     public synchronized boolean test1() {
  68         boolean b = true;
  69         while (!drained) {
  70             synchronized (object) {
  71                 boolean c = b = someBoolean;
  72                 if (c || drained) {
  73                     break;
  74                 }
  75             }
  76         }
  77         return b;
  78     }
  79 
  80     @Test
  81     public void run1() throws Throwable {
  82         runTest("test1");
  83     }
  84 
  85     public synchronized boolean test2() {
  86         boolean b = true;
  87         while (!drained) {
  88             synchronized (object) {
  89                 boolean c = b = someBoolean;
  90                 if (c || drained) {
  91                     break;
  92                 }
  93                 if (someInt > 0) {
  94                     throw new RuntimeException();
  95                 }
  96             }
  97             if (someInt < -10) {
  98                 throw new IndexOutOfBoundsException();
  99             }
 100         }
 101         if (someInt < -5) {
 102             throw new IllegalArgumentException();
 103         }
 104         return b;
 105     }
 106 
 107     @Test
 108     public void run2() throws Throwable {
 109         runTest("test2");
 110     }
 111 
 112 }