1 /*
   2  * Copyright (c) 1999, 2010, 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 #ifndef SHARE_VM_PRIMS_JVMTIRAWMONITOR_HPP
  26 #define SHARE_VM_PRIMS_JVMTIRAWMONITOR_HPP
  27 
  28 #ifndef JVMTI_KERNEL
  29 #include "runtime/objectMonitor.hpp"
  30 #include "utilities/growableArray.hpp"
  31 #endif
  32 
  33 //
  34 // class JvmtiRawMonitor
  35 //
  36 // Used by JVMTI methods: All RawMonitor methods (CreateRawMonitor, EnterRawMonitor, etc.)
  37 //
  38 // Wrapper for ObjectMonitor class that saves the Monitor's name
  39 //
  40 
  41 class JvmtiRawMonitor : public ObjectMonitor  {
  42 private:
  43   int           _magic;
  44   char *        _name;
  45   // JVMTI_RM_MAGIC is set in contructor and unset in destructor.
  46   enum { JVMTI_RM_MAGIC = (int)(('T' << 24) | ('I' << 16) | ('R' << 8) | 'M') };
  47 
  48   int       SimpleEnter (Thread * Self) ;
  49   int       SimpleExit  (Thread * Self) ;
  50   int       SimpleWait  (Thread * Self, jlong millis) ;
  51   int       SimpleNotify (Thread * Self, bool All) ;
  52 
  53 public:
  54   JvmtiRawMonitor(const char *name);
  55   ~JvmtiRawMonitor();
  56   int       raw_enter(TRAPS);
  57   int       raw_exit(TRAPS);
  58   int       raw_wait(jlong millis, bool interruptable, TRAPS);
  59   int       raw_notify(TRAPS);
  60   int       raw_notifyAll(TRAPS);
  61   int            magic()   { return _magic;  }
  62   const char *get_name()   { return _name; }
  63   bool        is_valid();
  64 };
  65 
  66 // Onload pending raw monitors
  67 // Class is used to cache onload or onstart monitor enter
  68 // which will transition into real monitor when
  69 // VM is fully initialized.
  70 class JvmtiPendingMonitors : public AllStatic {
  71 
  72 private:
  73   static GrowableArray<JvmtiRawMonitor*> *_monitors; // Cache raw monitor enter
  74 
  75   inline static GrowableArray<JvmtiRawMonitor*>* monitors() { return _monitors; }
  76 
  77   static void dispose() {
  78     delete monitors();
  79   }
  80 
  81 public:
  82   static void enter(JvmtiRawMonitor *monitor) {
  83     monitors()->append(monitor);
  84   }
  85 
  86   static int count() {
  87     return monitors()->length();
  88   }
  89 
  90   static void destroy(JvmtiRawMonitor *monitor) {
  91     while (monitors()->contains(monitor)) {
  92       monitors()->remove(monitor);
  93     }
  94   }
  95 
  96   // Return false if monitor is not found in the list.
  97   static bool exit(JvmtiRawMonitor *monitor) {
  98     if (monitors()->contains(monitor)) {
  99       monitors()->remove(monitor);
 100       return true;
 101     } else {
 102       return false;
 103     }
 104   }
 105 
 106   static void transition_raw_monitors();
 107 };
 108 
 109 #endif // SHARE_VM_PRIMS_JVMTIRAWMONITOR_HPP