1 /*
   2  * Copyright (c) 2016, 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 #include "precompiled.hpp"
  26 #include "code/dependencyContext.hpp"
  27 #include "code/nmethod.hpp"
  28 #include "unittest.hpp"
  29 
  30 class TestDependencyContext {
  31  public:
  32   nmethod _nmethods[3];
  33 
  34   nmethodBucket* volatile _dependency_context;
  35   volatile uint64_t _last_cleanup;
  36 
  37   DependencyContext dependencies() {
  38     DependencyContext depContext(&_dependency_context, &_last_cleanup);
  39     return depContext;
  40   }
  41 
  42   TestDependencyContext()
  43     : _dependency_context(NULL),
  44       _last_cleanup(0) {
  45     CodeCache_lock->lock_without_safepoint_check();
  46 
  47     _nmethods[0].clear_unloading_state();
  48     _nmethods[1].clear_unloading_state();
  49     _nmethods[2].clear_unloading_state();
  50 
  51     dependencies().add_dependent_nmethod(&_nmethods[2]);
  52     dependencies().add_dependent_nmethod(&_nmethods[1]);
  53     dependencies().add_dependent_nmethod(&_nmethods[0]);
  54   }
  55 
  56   ~TestDependencyContext() {
  57     wipe();
  58     CodeCache_lock->unlock();
  59   }
  60 
  61   void wipe() {
  62     DependencyContext ctx(&_dependency_context, &_last_cleanup);
  63     nmethodBucket* b = ctx.dependencies();
  64     ctx.set_dependencies(NULL);
  65     while (b != NULL) {
  66       nmethodBucket* next = b->next();
  67       delete b;
  68       b = next;
  69     }
  70   }
  71 };
  72 
  73 static void test_remove_dependent_nmethod(int id) {
  74   TestDependencyContext c;
  75   DependencyContext depContext = c.dependencies();
  76 
  77   nmethod* nm = &c._nmethods[id];
  78   depContext.remove_dependent_nmethod(nm);
  79 
  80   NOT_PRODUCT(ASSERT_FALSE(depContext.is_dependent_nmethod(nm)));
  81 }
  82 
  83 TEST_VM(code, dependency_context) {
  84   test_remove_dependent_nmethod(0);
  85   test_remove_dependent_nmethod(1);
  86   test_remove_dependent_nmethod(2);
  87 }