hotspot/src/share/vm/prims/jvmtiImpl.hpp
changeset 8110 c992c8d52344
parent 7918 ce1e4ae77aea
child 8475 c2b97a27943a
--- a/hotspot/src/share/vm/prims/jvmtiImpl.hpp	Wed Feb 02 18:38:40 2011 -0500
+++ b/hotspot/src/share/vm/prims/jvmtiImpl.hpp	Wed Feb 02 14:38:01 2011 -0500
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -433,6 +433,149 @@
 
 #endif // !JVMTI_KERNEL
 
+/**
+ * When a thread (such as the compiler thread or VM thread) cannot post a
+ * JVMTI event itself because the event needs to be posted from a Java
+ * thread, then it can defer the event to the Service thread for posting.
+ * The information needed to post the event is encapsulated into this class
+ * and then enqueued onto the JvmtiDeferredEventQueue, where the Service
+ * thread will pick it up and post it.
+ *
+ * This is currently only used for posting compiled-method-load and unload
+ * events, which we don't want posted from the compiler thread.
+ */
+class JvmtiDeferredEvent VALUE_OBJ_CLASS_SPEC {
+  friend class JvmtiDeferredEventQueue;
+ private:
+  typedef enum {
+    TYPE_NONE,
+    TYPE_COMPILED_METHOD_LOAD,
+    TYPE_COMPILED_METHOD_UNLOAD,
+    TYPE_FLUSH // pseudo-event used to implement flush_queue()
+  } Type;
+
+  Type _type;
+  union {
+    nmethod* compiled_method_load;
+    struct {
+      jmethodID method_id;
+      const void* code_begin;
+    } compiled_method_unload;
+    int* flush_state_addr;
+  } _event_data;
+
+  JvmtiDeferredEvent(Type t) : _type(t) {}
+
+  void set_compiled_method_load(nmethod* nm) {
+    assert(_type == TYPE_COMPILED_METHOD_LOAD, "must be");
+    _event_data.compiled_method_load = nm;
+  }
+
+  nmethod* compiled_method_load() const {
+    assert(_type == TYPE_COMPILED_METHOD_LOAD, "must be");
+    return _event_data.compiled_method_load;
+  }
+
+  void set_compiled_method_unload(jmethodID id, const void* code) {
+    assert(_type == TYPE_COMPILED_METHOD_UNLOAD, "must be");
+    _event_data.compiled_method_unload.method_id = id;
+    _event_data.compiled_method_unload.code_begin = code;
+  }
+
+  jmethodID compiled_method_unload_method_id() const {
+    assert(_type == TYPE_COMPILED_METHOD_UNLOAD, "must be");
+    return _event_data.compiled_method_unload.method_id;
+  }
+
+  const void* compiled_method_unload_code_begin() const {
+    assert(_type == TYPE_COMPILED_METHOD_UNLOAD, "must be");
+    return _event_data.compiled_method_unload.code_begin;
+  }
+
+  bool is_flush_event() const { return _type == TYPE_FLUSH; }
+
+  int* flush_state_addr() const {
+    assert(is_flush_event(), "must be");
+    return _event_data.flush_state_addr;
+  }
+
+  void set_flush_state_addr(int* flag) {
+    assert(is_flush_event(), "must be");
+    _event_data.flush_state_addr = flag;
+  }
+
+ public:
+
+  JvmtiDeferredEvent() : _type(TYPE_NONE) {}
+
+  // Factory methods
+  static JvmtiDeferredEvent compiled_method_load_event(nmethod* nm)
+    KERNEL_RETURN_(JvmtiDeferredEvent());
+  static JvmtiDeferredEvent compiled_method_unload_event(
+      jmethodID id, const void* code) KERNEL_RETURN_(JvmtiDeferredEvent());
+
+  // Actually posts the event.
+  void post() KERNEL_RETURN;
+};
+
+/**
+ * Events enqueued on this queue wake up the Service thread which dequeues
+ * and posts the events.  The Service_lock is required to be held
+ * when operating on the queue (except for the "pending" events).
+ */
+class JvmtiDeferredEventQueue : AllStatic {
+  friend class JvmtiDeferredEvent;
+ private:
+  class QueueNode : public CHeapObj {
+   private:
+    JvmtiDeferredEvent _event;
+    QueueNode* _next;
+
+   public:
+    QueueNode(const JvmtiDeferredEvent& event)
+      : _event(event), _next(NULL) {}
+
+    const JvmtiDeferredEvent& event() const { return _event; }
+    QueueNode* next() const { return _next; }
+
+    void set_next(QueueNode* next) { _next = next; }
+  };
+
+  static QueueNode* _queue_head;             // Hold Service_lock to access
+  static QueueNode* _queue_tail;             // Hold Service_lock to access
+  static volatile QueueNode* _pending_list;  // Uses CAS for read/update
+
+  // Transfers events from the _pending_list to the _queue.
+  static void process_pending_events() KERNEL_RETURN;
+
+  static void flush_complete(int* flush_state) KERNEL_RETURN;
+
+ public:
+  // Must be holding Service_lock when calling these
+  static bool has_events() KERNEL_RETURN_(false);
+  static void enqueue(const JvmtiDeferredEvent& event) KERNEL_RETURN;
+  static JvmtiDeferredEvent dequeue() KERNEL_RETURN_(JvmtiDeferredEvent());
+
+  // This call blocks until all events enqueued prior to this call
+  // have been posted.  The Service_lock is acquired and waited upon.
+  //
+  // Implemented by creating a "flush" event and placing it in the queue.
+  // When the flush event is "posted" it will call flush_complete(), which
+  // will release the caller.
+  //
+  // Can be called by any thread (maybe even the service thread itself).
+  // Not necessary for the caller to be a JavaThread.
+  static void flush_queue(Thread* current) KERNEL_RETURN;
+
+  // Used to enqueue events without using a lock, for times (such as during
+  // safepoint) when we can't or don't want to lock the Service_lock.
+  //
+  // Events will be held off to the side until there's a call to
+  // dequeue(), enqueue(), or process_pending_events() (all of which require
+  // the holding of the Service_lock), and will be enqueued at that time.
+  static void add_pending_event(const JvmtiDeferredEvent&) KERNEL_RETURN;
+};
+
 // Utility macro that checks for NULL pointers:
 #define NULL_CHECK(X, Y) if ((X) == NULL) { return (Y); }