Path: blob/master/test/hotspot/gtest/utilities/test_globalCounter.cpp
41144 views
/*1* Copyright (c) 2018, 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*/2223#include "precompiled.hpp"24#include "runtime/atomic.hpp"25#include "runtime/os.hpp"26#include "utilities/globalCounter.hpp"27#include "utilities/globalCounter.inline.hpp"28#include "threadHelper.inline.hpp"2930#define GOOD_VALUE 133731#define BAD_VALUE 47113233struct TestData {34long test_value;35};3637class RCUReaderThread : public JavaTestThread {38public:39static volatile bool _exit;40volatile TestData** _test;41Semaphore* _wrt_start;42RCUReaderThread(Semaphore* post, volatile TestData** test, Semaphore* wrt_start)43: JavaTestThread(post), _test(test), _wrt_start(wrt_start) {};44virtual ~RCUReaderThread(){}45void main_run() {46_wrt_start->signal();47while (!_exit) {48GlobalCounter::CSContext cs_context = GlobalCounter::critical_section_begin(this);49volatile TestData* test = Atomic::load_acquire(_test);50long value = Atomic::load_acquire(&test->test_value);51ASSERT_EQ(value, GOOD_VALUE);52GlobalCounter::critical_section_end(this, cs_context);53{54GlobalCounter::CriticalSection cs(this);55volatile TestData* test = Atomic::load_acquire(_test);56long value = Atomic::load_acquire(&test->test_value);57ASSERT_EQ(value, GOOD_VALUE);58}59}60}61};6263volatile bool RCUReaderThread::_exit = false;6465class RCUWriterThread : public JavaTestThread {66public:67RCUWriterThread(Semaphore* post) : JavaTestThread(post) {68};69virtual ~RCUWriterThread(){}70void main_run() {71static const int NUMBER_OF_READERS = 4;72Semaphore post;73Semaphore wrt_start;74volatile TestData* test = NULL;7576RCUReaderThread* reader1 = new RCUReaderThread(&post, &test, &wrt_start);77RCUReaderThread* reader2 = new RCUReaderThread(&post, &test, &wrt_start);78RCUReaderThread* reader3 = new RCUReaderThread(&post, &test, &wrt_start);79RCUReaderThread* reader4 = new RCUReaderThread(&post, &test, &wrt_start);8081TestData* tmp = new TestData();82tmp->test_value = GOOD_VALUE;83Atomic::release_store_fence(&test, tmp);8485reader1->doit();86reader2->doit();87reader3->doit();88reader4->doit();8990int nw = NUMBER_OF_READERS;91while (nw > 0) {92wrt_start.wait();93--nw;94}95jlong stop_ms = os::javaTimeMillis() + 1000; // 1 seconds max test time96for (int i = 0; i < 100000 && stop_ms > os::javaTimeMillis(); i++) {97volatile TestData* free_tmp = test;98tmp = new TestData();99tmp->test_value = GOOD_VALUE;100Atomic::release_store(&test, tmp);101GlobalCounter::write_synchronize();102free_tmp->test_value = BAD_VALUE;103delete free_tmp;104}105RCUReaderThread::_exit = true;106for (int i = 0; i < NUMBER_OF_READERS; i++) {107post.wait();108}109}110};111112TEST_VM(GlobalCounter, critical_section) {113RCUReaderThread::_exit = false;114mt_test_doer<RCUWriterThread>();115}116117118