Path: blob/master/test/hotspot/gtest/utilities/test_singleWriterSynchronizer.cpp
41145 views
/*1* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "runtime/interfaceSupport.inline.hpp"26#include "runtime/atomic.hpp"27#include "runtime/os.hpp"28#include "runtime/thread.hpp"29#include "utilities/debug.hpp"30#include "utilities/globalCounter.inline.hpp"31#include "utilities/globalDefinitions.hpp"32#include "utilities/ostream.hpp"33#include "utilities/singleWriterSynchronizer.hpp"34#include "threadHelper.inline.hpp"35#include "unittest.hpp"3637class SingleWriterSynchronizerTestReader : public JavaTestThread {38SingleWriterSynchronizer* _synchronizer;39volatile uintx* _synchronized_value;40volatile int* _continue_running;4142static const uint reader_iterations = 10;4344public:45SingleWriterSynchronizerTestReader(Semaphore* post,46SingleWriterSynchronizer* synchronizer,47volatile uintx* synchronized_value,48volatile int* continue_running) :49JavaTestThread(post),50_synchronizer(synchronizer),51_synchronized_value(synchronized_value),52_continue_running(continue_running)53{}5455virtual void main_run() {56size_t iterations = 0;57size_t values_changed = 0;58while (Atomic::load_acquire(_continue_running) != 0) {59{ ThreadBlockInVM tbiv(this); } // Safepoint check outside critical section.60++iterations;61SingleWriterSynchronizer::CriticalSection cs(_synchronizer);62uintx value = Atomic::load_acquire(_synchronized_value);63uintx new_value = value;64for (uint i = 0; i < reader_iterations; ++i) {65new_value = Atomic::load_acquire(_synchronized_value);66// A reader can see either the value it first read after67// entering the critical section, or that value + 1. No other68// values are possible.69if (value != new_value) {70ASSERT_EQ((value + 1), new_value);71}72}73if (value != new_value) {74++values_changed;75}76}77tty->print_cr("reader iterations: " SIZE_FORMAT ", changes: " SIZE_FORMAT,78iterations, values_changed);79}80};8182class SingleWriterSynchronizerTestWriter : public JavaTestThread {83SingleWriterSynchronizer* _synchronizer;84volatile uintx* _synchronized_value;85volatile int* _continue_running;8687public:88SingleWriterSynchronizerTestWriter(Semaphore* post,89SingleWriterSynchronizer* synchronizer,90volatile uintx* synchronized_value,91volatile int* continue_running) :92JavaTestThread(post),93_synchronizer(synchronizer),94_synchronized_value(synchronized_value),95_continue_running(continue_running)96{}9798virtual void main_run() {99while (Atomic::load_acquire(_continue_running) != 0) {100++*_synchronized_value;101_synchronizer->synchronize();102{ ThreadBlockInVM tbiv(this); } // Safepoint check.103}104tty->print_cr("writer iterations: " UINTX_FORMAT, *_synchronized_value);105}106};107108const uint nreaders = 5;109const uint milliseconds_to_run = 1000;110111TEST_VM(TestSingleWriterSynchronizer, stress) {112Semaphore post;113SingleWriterSynchronizer synchronizer;114volatile uintx synchronized_value = 0;115volatile int continue_running = 1;116117JavaTestThread* readers[nreaders] = {};118for (uint i = 0; i < nreaders; ++i) {119readers[i] = new SingleWriterSynchronizerTestReader(&post,120&synchronizer,121&synchronized_value,122&continue_running);123readers[i]->doit();124}125126JavaTestThread* writer =127new SingleWriterSynchronizerTestWriter(&post,128&synchronizer,129&synchronized_value,130&continue_running);131132writer->doit();133134tty->print_cr("Stressing synchronizer for %u ms", milliseconds_to_run);135JavaThread* cur = JavaThread::current();136{137ThreadInVMfromNative invm(cur);138cur->sleep(milliseconds_to_run);139}140continue_running = 0;141for (uint i = 0; i < nreaders + 1; ++i) {142post.wait();143}144}145146147