< prev index next >

test/hotspot/gtest/utilities/test_waitBarrier.cpp

Print this page




  32 static volatile int wait_tag = 0;
  33 static volatile int valid_value = 0;
  34 
  35 template <typename WaitBarrierImpl>
  36 class WBThread : public JavaTestThread {
  37 public:
  38   static volatile bool _exit;
  39   WaitBarrierType<WaitBarrierImpl>* _wait_barrier;
  40   Semaphore* _wrt_start;
  41   volatile int _on_barrier;
  42 
  43   WBThread(Semaphore* post, WaitBarrierType<WaitBarrierImpl>* wb, Semaphore* wrt_start)
  44     : JavaTestThread(post), _wait_barrier(wb), _wrt_start(wrt_start) {};
  45   virtual ~WBThread(){}
  46   void main_run() {
  47     _wrt_start->signal();
  48     int vv, tag;
  49     // Similar to how a JavaThread would stop in a safepoint.
  50     while (!_exit) {
  51       // Load the published tag.
  52       tag = OrderAccess::load_acquire(&wait_tag);
  53       // Publish the tag this thread is going to wait for.
  54       OrderAccess::release_store(&_on_barrier, tag);
  55       if (_on_barrier == 0) {
  56         SpinPause();
  57         continue;
  58       }
  59       OrderAccess::storeload(); // Loads in WB must not float up.
  60       // Wait until we are woken.
  61       _wait_barrier->wait(tag);
  62       // Verify that we do not see an invalid value.
  63       vv = OrderAccess::load_acquire(&valid_value);
  64       ASSERT_EQ((vv & 0x1), 0);
  65       OrderAccess::release_store(&_on_barrier, 0);
  66     }
  67   }
  68 };
  69 
  70 template <typename WaitBarrierImpl>
  71 volatile bool WBThread<WaitBarrierImpl>::_exit = false;
  72 
  73 template <typename WaitBarrierImpl>
  74 class WBArmerThread : public JavaTestThread {
  75 public:
  76   WBArmerThread(Semaphore* post) : JavaTestThread(post) {
  77   };
  78   virtual ~WBArmerThread(){}
  79   void main_run() {
  80     static const int NUMBER_OF_READERS = 4;
  81     Semaphore post;
  82     Semaphore wrt_start;
  83     WaitBarrierType<WaitBarrierImpl> wb(this);
  84 
  85     WBThread<WaitBarrierImpl>* reader1 = new WBThread<WaitBarrierImpl>(&post, &wb, &wrt_start);


  87     WBThread<WaitBarrierImpl>* reader3 = new WBThread<WaitBarrierImpl>(&post, &wb, &wrt_start);
  88     WBThread<WaitBarrierImpl>* reader4 = new WBThread<WaitBarrierImpl>(&post, &wb, &wrt_start);
  89 
  90     reader1->doit();
  91     reader2->doit();
  92     reader3->doit();
  93     reader4->doit();
  94 
  95     int nw = NUMBER_OF_READERS;
  96     while (nw > 0) {
  97       wrt_start.wait();
  98       --nw;
  99     }
 100     jlong stop_ms = os::javaTimeMillis() + 1000; // 1 seconds max test time
 101     int next_tag = 1;
 102     // Similar to how the VM thread would use a WaitBarrier in a safepoint.
 103     while (stop_ms > os::javaTimeMillis()) {
 104       // Arm next tag.
 105       wb.arm(next_tag);
 106       // Publish tag.
 107       OrderAccess::release_store_fence(&wait_tag, next_tag);
 108 
 109       // Wait until threads picked up new tag.
 110       while (reader1->_on_barrier != wait_tag ||
 111              reader2->_on_barrier != wait_tag ||
 112              reader3->_on_barrier != wait_tag ||
 113              reader4->_on_barrier != wait_tag) {
 114         SpinPause();
 115       }
 116 
 117       // Set an invalid value.
 118       OrderAccess::release_store(&valid_value, valid_value + 1); // odd
 119       os::naked_yield();
 120       // Set a valid value.
 121       OrderAccess::release_store(&valid_value, valid_value + 1); // even
 122       // Publish inactive tag.
 123       OrderAccess::release_store_fence(&wait_tag, 0); // Stores in WB must not float up.
 124       wb.disarm();
 125 
 126       // Wait until threads done valid_value verification.
 127       while (reader1->_on_barrier != 0 ||
 128              reader2->_on_barrier != 0 ||
 129              reader3->_on_barrier != 0 ||
 130              reader4->_on_barrier != 0) {
 131         SpinPause();
 132       }
 133       ++next_tag;
 134     }
 135     WBThread<WaitBarrierImpl>::_exit = true;
 136     for (int i = 0; i < NUMBER_OF_READERS; i++) {
 137       post.wait();
 138     }
 139   }
 140 };
 141 
 142 TEST_VM(WaitBarrier, default_wb) {
 143   WBThread<WaitBarrierDefault>::_exit = false;


  32 static volatile int wait_tag = 0;
  33 static volatile int valid_value = 0;
  34 
  35 template <typename WaitBarrierImpl>
  36 class WBThread : public JavaTestThread {
  37 public:
  38   static volatile bool _exit;
  39   WaitBarrierType<WaitBarrierImpl>* _wait_barrier;
  40   Semaphore* _wrt_start;
  41   volatile int _on_barrier;
  42 
  43   WBThread(Semaphore* post, WaitBarrierType<WaitBarrierImpl>* wb, Semaphore* wrt_start)
  44     : JavaTestThread(post), _wait_barrier(wb), _wrt_start(wrt_start) {};
  45   virtual ~WBThread(){}
  46   void main_run() {
  47     _wrt_start->signal();
  48     int vv, tag;
  49     // Similar to how a JavaThread would stop in a safepoint.
  50     while (!_exit) {
  51       // Load the published tag.
  52       tag = Atomic::load_acquire(&wait_tag);
  53       // Publish the tag this thread is going to wait for.
  54       Atomic::release_store(&_on_barrier, tag);
  55       if (_on_barrier == 0) {
  56         SpinPause();
  57         continue;
  58       }
  59       OrderAccess::storeload(); // Loads in WB must not float up.
  60       // Wait until we are woken.
  61       _wait_barrier->wait(tag);
  62       // Verify that we do not see an invalid value.
  63       vv = Atomic::load_acquire(&valid_value);
  64       ASSERT_EQ((vv & 0x1), 0);
  65       Atomic::release_store(&_on_barrier, 0);
  66     }
  67   }
  68 };
  69 
  70 template <typename WaitBarrierImpl>
  71 volatile bool WBThread<WaitBarrierImpl>::_exit = false;
  72 
  73 template <typename WaitBarrierImpl>
  74 class WBArmerThread : public JavaTestThread {
  75 public:
  76   WBArmerThread(Semaphore* post) : JavaTestThread(post) {
  77   };
  78   virtual ~WBArmerThread(){}
  79   void main_run() {
  80     static const int NUMBER_OF_READERS = 4;
  81     Semaphore post;
  82     Semaphore wrt_start;
  83     WaitBarrierType<WaitBarrierImpl> wb(this);
  84 
  85     WBThread<WaitBarrierImpl>* reader1 = new WBThread<WaitBarrierImpl>(&post, &wb, &wrt_start);


  87     WBThread<WaitBarrierImpl>* reader3 = new WBThread<WaitBarrierImpl>(&post, &wb, &wrt_start);
  88     WBThread<WaitBarrierImpl>* reader4 = new WBThread<WaitBarrierImpl>(&post, &wb, &wrt_start);
  89 
  90     reader1->doit();
  91     reader2->doit();
  92     reader3->doit();
  93     reader4->doit();
  94 
  95     int nw = NUMBER_OF_READERS;
  96     while (nw > 0) {
  97       wrt_start.wait();
  98       --nw;
  99     }
 100     jlong stop_ms = os::javaTimeMillis() + 1000; // 1 seconds max test time
 101     int next_tag = 1;
 102     // Similar to how the VM thread would use a WaitBarrier in a safepoint.
 103     while (stop_ms > os::javaTimeMillis()) {
 104       // Arm next tag.
 105       wb.arm(next_tag);
 106       // Publish tag.
 107       Atomic::release_store_fence(&wait_tag, next_tag);
 108 
 109       // Wait until threads picked up new tag.
 110       while (reader1->_on_barrier != wait_tag ||
 111              reader2->_on_barrier != wait_tag ||
 112              reader3->_on_barrier != wait_tag ||
 113              reader4->_on_barrier != wait_tag) {
 114         SpinPause();
 115       }
 116 
 117       // Set an invalid value.
 118       Atomic::release_store(&valid_value, valid_value + 1); // odd
 119       os::naked_yield();
 120       // Set a valid value.
 121       Atomic::release_store(&valid_value, valid_value + 1); // even
 122       // Publish inactive tag.
 123       Atomic::release_store_fence(&wait_tag, 0); // Stores in WB must not float up.
 124       wb.disarm();
 125 
 126       // Wait until threads done valid_value verification.
 127       while (reader1->_on_barrier != 0 ||
 128              reader2->_on_barrier != 0 ||
 129              reader3->_on_barrier != 0 ||
 130              reader4->_on_barrier != 0) {
 131         SpinPause();
 132       }
 133       ++next_tag;
 134     }
 135     WBThread<WaitBarrierImpl>::_exit = true;
 136     for (int i = 0; i < NUMBER_OF_READERS; i++) {
 137       post.wait();
 138     }
 139   }
 140 };
 141 
 142 TEST_VM(WaitBarrier, default_wb) {
 143   WBThread<WaitBarrierDefault>::_exit = false;
< prev index next >