Path: blob/master/test/hotspot/gtest/runtime/test_mutex.cpp
41144 views
/*1* Copyright (c) 2020, 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/interfaceSupport.inline.hpp"25#include "runtime/mutex.hpp"26#include "runtime/mutexLocker.hpp"27#include "runtime/thread.hpp"28#include "utilities/formatBuffer.hpp"29#include "threadHelper.inline.hpp"30#include "unittest.hpp"3132const int iterations = 10;33static Mutex* m[iterations];34static int i = 0;3536static void create_mutex(Thread* thr) {37m[i] = new Mutex(Mutex::leaf, FormatBuffer<128>("MyLock lock #%u", i), true, Mutex::_safepoint_check_never);38i++;39}4041TEST_VM(MutexName, mutex_name) {42// Create mutexes in threads, where the names are created on the thread43// stacks and then check that their names are correct.44for (int i = 0; i < iterations; i++) {45nomt_test_doer(create_mutex);46}47for (int i = 0; i < iterations; i++) {48FormatBuffer<128> f("MyLock lock #%u", i);49ASSERT_STREQ(m[i]->name(), f.buffer()) << "Wrong name!";50}51}5253#ifdef ASSERT5455const int rankA = 50;5657TEST_OTHER_VM(MutexRank, mutex_lock_rank_in_order) {58JavaThread* THREAD = JavaThread::current();59ThreadInVMfromNative invm(THREAD);6061Mutex* mutex_rankA = new Mutex(rankA, "mutex_rankA", false, Mutex::_safepoint_check_always);62Mutex* mutex_rankA_plus_one = new Mutex(rankA + 1, "mutex_rankA_plus_one", false, Mutex::_safepoint_check_always);6364mutex_rankA_plus_one->lock();65mutex_rankA->lock();66mutex_rankA->unlock();67mutex_rankA_plus_one->unlock();68}6970TEST_VM_ASSERT_MSG(MutexRank, mutex_lock_rank_out_of_orderA,71".* Attempting to acquire lock mutex_rankA_plus_one/51 out of order with lock mutex_rankA/50 -- possible deadlock") {72JavaThread* THREAD = JavaThread::current();73ThreadInVMfromNative invm(THREAD);7475Mutex* mutex_rankA = new Mutex(rankA, "mutex_rankA", false, Mutex::_safepoint_check_always);76Mutex* mutex_rankA_plus_one = new Mutex(rankA + 1, "mutex_rankA_plus_one", false, Mutex::_safepoint_check_always);7778mutex_rankA->lock();79mutex_rankA_plus_one->lock();80mutex_rankA_plus_one->unlock();81mutex_rankA->unlock();82}8384TEST_VM_ASSERT_MSG(MutexRank, mutex_lock_rank_out_of_orderB,85".* Attempting to acquire lock mutex_rankB/50 out of order with lock mutex_rankA/50 -- possible deadlock") {86JavaThread* THREAD = JavaThread::current();87ThreadInVMfromNative invm(THREAD);8889Mutex* mutex_rankA = new Mutex(rankA, "mutex_rankA", false, Mutex::_safepoint_check_always);90Mutex* mutex_rankB = new Mutex(rankA, "mutex_rankB", false, Mutex::_safepoint_check_always);9192mutex_rankA->lock();93mutex_rankB->lock();94mutex_rankB->unlock();95mutex_rankA->unlock();96}9798TEST_OTHER_VM(MutexRank, mutex_trylock_rank_out_of_orderA) {99JavaThread* THREAD = JavaThread::current();100ThreadInVMfromNative invm(THREAD);101102Mutex* mutex_rankA = new Mutex(rankA, "mutex_rankA", false, Mutex::_safepoint_check_always);103Mutex* mutex_rankA_plus_one = new Mutex(rankA + 1, "mutex_rankA_plus_one", false, Mutex::_safepoint_check_always);104Mutex* mutex_rankA_plus_two = new Mutex(rankA + 2, "mutex_rankA_plus_two", false, Mutex::_safepoint_check_always);105106mutex_rankA_plus_one->lock();107mutex_rankA_plus_two->try_lock_without_rank_check();108mutex_rankA->lock();109mutex_rankA->unlock();110mutex_rankA_plus_two->unlock();111mutex_rankA_plus_one->unlock();112}113114TEST_VM_ASSERT_MSG(MutexRank, mutex_trylock_rank_out_of_orderB,115".* Attempting to acquire lock mutex_rankA_plus_one/51 out of order with lock mutex_rankA/50 -- possible deadlock") {116JavaThread* THREAD = JavaThread::current();117ThreadInVMfromNative invm(THREAD);118119Mutex* mutex_rankA = new Mutex(rankA, "mutex_rankA", false, Mutex::_safepoint_check_always);120Mutex* mutex_rankA_plus_one = new Mutex(rankA + 1, "mutex_rankA_plus_one", false, Mutex::_safepoint_check_always);121122mutex_rankA->lock();123mutex_rankA_plus_one->try_lock_without_rank_check();124mutex_rankA_plus_one->unlock();125mutex_rankA_plus_one->try_lock();126mutex_rankA_plus_one->unlock();127mutex_rankA->unlock();128}129130TEST_VM_ASSERT_MSG(MutexRank, mutex_lock_access_leaf,131".* Attempting to acquire lock mutex_rank_leaf/11 out of order with lock mutex_rank_access/1 "132"-- possible deadlock") {133JavaThread* THREAD = JavaThread::current();134ThreadInVMfromNative invm(THREAD);135136Mutex* mutex_rank_access = new Mutex(Mutex::access, "mutex_rank_access", false, Mutex::_safepoint_check_never);137Mutex* mutex_rank_leaf = new Mutex(Mutex::leaf, "mutex_rank_leaf", false, Mutex::_safepoint_check_never);138139mutex_rank_access->lock_without_safepoint_check();140mutex_rank_leaf->lock_without_safepoint_check();141mutex_rank_leaf->unlock();142mutex_rank_access->unlock();143}144145TEST_VM_ASSERT_MSG(MutexRank, mutex_lock_tty_special,146".* Attempting to acquire lock mutex_rank_special/6 out of order with lock mutex_rank_tty/3 "147"-- possible deadlock") {148JavaThread* THREAD = JavaThread::current();149ThreadInVMfromNative invm(THREAD);150151Mutex* mutex_rank_tty = new Mutex(Mutex::tty, "mutex_rank_tty", false, Mutex::_safepoint_check_never);152Mutex* mutex_rank_special = new Mutex(Mutex::special, "mutex_rank_special", false, Mutex::_safepoint_check_never);153154mutex_rank_tty->lock_without_safepoint_check();155mutex_rank_special->lock_without_safepoint_check();156mutex_rank_special->unlock();157mutex_rank_tty->unlock();158}159160TEST_OTHER_VM(MutexRank, monitor_wait_rank_in_order) {161JavaThread* THREAD = JavaThread::current();162ThreadInVMfromNative invm(THREAD);163164Monitor* monitor_rankA = new Monitor(rankA, "monitor_rankA", false, Mutex::_safepoint_check_always);165Monitor* monitor_rankA_plus_one = new Monitor(rankA + 1, "monitor_rankA_plus_one", false, Mutex::_safepoint_check_always);166167monitor_rankA_plus_one->lock();168monitor_rankA->lock();169monitor_rankA->wait(1);170monitor_rankA->unlock();171monitor_rankA_plus_one->unlock();172}173174TEST_VM_ASSERT_MSG(MutexRank, monitor_wait_rank_out_of_order,175".* Attempting to wait on monitor monitor_rankA_plus_one/51 while holding lock monitor_rankA/50 "176"-- possible deadlock. Should wait on the least ranked monitor from all owned locks.") {177JavaThread* THREAD = JavaThread::current();178ThreadInVMfromNative invm(THREAD);179180Monitor* monitor_rankA = new Monitor(rankA, "monitor_rankA", false, Mutex::_safepoint_check_always);181Monitor* monitor_rankA_plus_one = new Monitor(rankA + 1, "monitor_rankA_plus_one", false, Mutex::_safepoint_check_always);182183monitor_rankA_plus_one->lock();184monitor_rankA->lock();185monitor_rankA_plus_one->wait(1);186monitor_rankA_plus_one->unlock();187monitor_rankA->unlock();188}189190TEST_VM_ASSERT_MSG(MutexRank, monitor_wait_rank_out_of_order_trylock,191".* Attempting to wait on monitor monitor_rankA_plus_one/51 while holding lock monitor_rankA/50 "192"-- possible deadlock. Should wait on the least ranked monitor from all owned locks.") {193JavaThread* THREAD = JavaThread::current();194ThreadInVMfromNative invm(THREAD);195196Monitor* monitor_rankA = new Monitor(rankA, "monitor_rankA", false, Mutex::_safepoint_check_always);197Monitor* monitor_rankA_plus_one = new Monitor(rankA + 1, "monitor_rankA_plus_one", false, Mutex::_safepoint_check_always);198199monitor_rankA->lock();200monitor_rankA_plus_one->try_lock_without_rank_check();201monitor_rankA_plus_one->wait();202monitor_rankA_plus_one->unlock();203monitor_rankA->unlock();204}205206TEST_VM_ASSERT_MSG(MutexRank, monitor_wait_rank_special,207".* Attempting to wait on monitor monitor_rank_special_minus_one/5 while holding lock monitor_rank_special/6 "208"-- possible deadlock. Should not block\\(wait\\) while holding a lock of rank special.") {209JavaThread* THREAD = JavaThread::current();210ThreadInVMfromNative invm(THREAD);211212Monitor* monitor_rank_special = new Monitor(Mutex::special, "monitor_rank_special", false, Mutex::_safepoint_check_never);213Monitor* monitor_rank_special_minus_one = new Monitor(Mutex::special - 1, "monitor_rank_special_minus_one", false, Mutex::_safepoint_check_never);214215monitor_rank_special->lock_without_safepoint_check();216monitor_rank_special_minus_one->lock_without_safepoint_check();217monitor_rank_special_minus_one->wait_without_safepoint_check(1);218monitor_rank_special_minus_one->unlock();219monitor_rank_special->unlock();220}221222TEST_VM_ASSERT_MSG(MutexRank, monitor_wait_access_leaf,223".* Attempting to wait on monitor monitor_rank_access/1 while holding lock monitor_rank_tty/3 "224"-- possible deadlock. Should not block\\(wait\\) while holding a lock of rank special.") {225JavaThread* THREAD = JavaThread::current();226ThreadInVMfromNative invm(THREAD);227228Monitor* monitor_rank_tty = new Monitor(Mutex::tty, "monitor_rank_tty", false, Mutex::_safepoint_check_never);229Monitor* monitor_rank_access = new Monitor(Mutex::access, "monitor_rank_access", false, Mutex::_safepoint_check_never);230231monitor_rank_tty->lock_without_safepoint_check();232monitor_rank_access->lock_without_safepoint_check();233monitor_rank_access->wait_without_safepoint_check(1);234monitor_rank_access->unlock();235monitor_rank_tty->unlock();236}237238TEST_VM_ASSERT_MSG(MutexRank, monitor_wait_tty_special,239".* Attempting to wait on monitor monitor_rank_tty/3 while holding lock monitor_rank_special/6 "240"-- possible deadlock. Should not block\\(wait\\) while holding a lock of rank special.") {241JavaThread* THREAD = JavaThread::current();242ThreadInVMfromNative invm(THREAD);243244Monitor* monitor_rank_special = new Monitor(Mutex::special, "monitor_rank_special", false, Mutex::_safepoint_check_never);245Monitor* monitor_rank_tty = new Monitor(Mutex::tty, "monitor_rank_tty", false, Mutex::_safepoint_check_never);246247monitor_rank_special->lock_without_safepoint_check();248monitor_rank_tty->lock_without_safepoint_check();249monitor_rank_tty->wait_without_safepoint_check(1);250monitor_rank_tty->unlock();251monitor_rank_special->unlock();252}253#endif // ASSERT254255256