1 /*
   2  * Copyright (c) 2016, 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_GC_SHARED_REFERENCEPENDINGLISTLOCKER_HPP
  26 #define SHARE_VM_GC_SHARED_REFERENCEPENDINGLISTLOCKER_HPP
  27 
  28 #include "memory/allocation.hpp"
  29 #include "runtime/basicLock.hpp"
  30 #include "runtime/mutex.hpp"
  31 #include "runtime/thread.hpp"
  32 #include "utilities/exceptions.hpp"
  33 
  34 //
  35 // The ReferencePendingListLockerThread locks and unlocks the reference
  36 // pending list lock on behalf a non-Java thread, typically a concurrent
  37 // GC thread. This interface should not be directly accessed. All uses
  38 // should instead go through the ReferencePendingListLocker, which calls
  39 // this thread if needed.
  40 //
  41 class ReferencePendingListLockerThread : public JavaThread {
  42 private:
  43   enum Message {
  44     NONE,
  45     LOCK,
  46     UNLOCK
  47   };
  48 
  49   Monitor _monitor;
  50   Message _message;
  51 
  52   ReferencePendingListLockerThread();
  53 
  54   static void start(JavaThread* thread, TRAPS);
  55 
  56   void send_message(Message message);
  57   void receive_and_handle_messages();
  58 
  59 public:
  60   static ReferencePendingListLockerThread* create(TRAPS);
  61 
  62   virtual bool is_hidden_from_external_view() const;
  63 
  64   void lock();
  65   void unlock();
  66 };
  67 
  68 //
  69 // The ReferencePendingListLocker is the main interface for locking and
  70 // unlocking the reference pending list lock, which needs to be held by
  71 // the GC when adding references to the pending list. Since this is a
  72 // Java-level monitor it can only be locked/unlocked by a Java thread.
  73 // For this reason there is an option to spawn a helper thread, the
  74 // ReferencePendingListLockerThread, during initialization. If a helper
  75 // thread is spawned all lock operations from non-Java threads will be
  76 // delegated to the helper thread. The helper thread is typically needed
  77 // by concurrent GCs.
  78 //
  79 class ReferencePendingListLocker VALUE_OBJ_CLASS_SPEC {
  80 private:
  81   static bool                              _is_initialized;
  82   static ReferencePendingListLockerThread* _locker_thread;
  83   BasicLock                                _basic_lock;
  84 
  85 public:
  86   static void initialize(bool needs_locker_thread, TRAPS);
  87   static bool is_initialized();
  88 
  89   static bool is_locked_by_self();
  90 
  91   void lock();
  92   void unlock();
  93 };
  94 
  95 #endif // SHARE_VM_GC_SHARED_REFERENCEPENDINGLISTLOCKER_HPP