1 /*
   2  * Copyright (c) 2019, 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_RUNTIME_MUTEX_INLINE_HPP
  26 #define SHARE_RUNTIME_MUTEX_INLINE_HPP
  27 
  28 #include "runtime/mutex.hpp"
  29 #include "runtime/os.inline.hpp"
  30 #include "runtime/thread.hpp"
  31 
  32 inline bool Mutex::try_lock(Thread* self) {
  33   // Some safepoint_check_always locks use try_lock, so cannot check
  34   // safepoint state, but can check blocking state.
  35   check_block_state(self);
  36   if (_lock.try_lock()) {
  37     assert_owner(NULL);
  38     set_owner(self);
  39     return true;
  40   }
  41   return false;
  42 }
  43 
  44 inline bool Mutex::try_lock() {
  45   return this->try_lock(Thread::current());
  46 }
  47 
  48 inline void Mutex::lock(Thread* self) {
  49   if (try_lock(self)) {
  50     return;
  51   }
  52   lock_slow(self);
  53 }
  54 
  55 inline void Mutex::lock() {
  56   this->lock(Thread::current());
  57 }
  58 
  59 // JavaThreads when it is known to be safe to not check for a safepoint when
  60 // acquiring this lock. If the thread blocks acquiring the lock it is not
  61 // safepoint-safe and so will prevent a safepoint from being reached. If used
  62 // in the wrong way this can lead to a deadlock with the safepoint code.
  63 inline void Mutex::lock_without_safepoint_check(Thread * self) {
  64   check_no_safepoint_state(self);
  65   assert(_owner != self, "invariant");
  66   _lock.lock();
  67   assert_owner(NULL);
  68   set_owner(self);
  69 }
  70 
  71 inline void Mutex::lock_without_safepoint_check() {
  72   lock_without_safepoint_check(Thread::current());
  73 }
  74 
  75 inline void Mutex::release_for_safepoint() {
  76   assert_owner(NULL);
  77   _lock.unlock();
  78 }
  79 
  80 inline void Mutex::unlock() {
  81   assert_owner(Thread::current());
  82   set_owner(NULL);
  83   _lock.unlock();
  84 }
  85 
  86 // Lock without safepoint check - a degenerate variant of lock() for use by
  87 
  88 #endif // SHARE_RUNTIME_MUTEX_INLINE_HPP