1 /*
   2  * Copyright (c) 2019, Red Hat, Inc. 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 
  27 #include "runtime/os.hpp"
  28 
  29 #include "gc/shenandoah/shenandoahLock.hpp"
  30 #include "runtime/atomic.hpp"
  31 #include "runtime/os.inline.hpp"
  32 #include "runtime/thread.hpp"
  33 
  34 ShenandoahSimpleLock::ShenandoahSimpleLock() {
  35   assert(os::mutex_init_done(), "Too early!");
  36 }
  37 
  38 void ShenandoahSimpleLock::lock() {
  39   _lock.lock();
  40 }
  41 
  42 void ShenandoahSimpleLock::unlock() {
  43   _lock.unlock();
  44 }
  45 
  46 bool ShenandoahSimpleLock::try_lock() {
  47   return _lock.try_lock();
  48 }
  49 
  50 ShenandoahReentrantLock::ShenandoahReentrantLock() :
  51   ShenandoahSimpleLock(), _owner(NULL), _count(0) {
  52   assert(os::mutex_init_done(), "Too early!");
  53 }
  54 
  55 ShenandoahReentrantLock::~ShenandoahReentrantLock() {
  56   assert(_count == 0, "Unbalance");
  57 }
  58 
  59 void ShenandoahReentrantLock::lock() {
  60   Thread* const thread = Thread::current();
  61   Thread* const owner = Atomic::load(&_owner);
  62 
  63   if (owner != thread) {
  64     ShenandoahSimpleLock::lock();
  65     Atomic::store(&_owner, thread);
  66   }
  67 
  68   _count++;
  69 }
  70 
  71 bool ShenandoahReentrantLock::try_lock() {
  72   Thread* const thread = Thread::current();
  73   Thread* const owner = Atomic::load(&_owner);
  74 
  75   if (owner != thread) {
  76     if (!ShenandoahSimpleLock::try_lock()) {
  77       return false;
  78     }
  79     Atomic::store(&_owner, thread);
  80   }
  81 
  82   _count++;
  83   return true;
  84 }
  85 
  86 void ShenandoahReentrantLock::unlock() {
  87   assert(owned_by_self(), "Invalid owner");
  88   assert(_count > 0, "Invalid count");
  89 
  90   _count--;
  91 
  92   if (_count == 0) {
  93     Atomic::store(&_owner, (Thread*)NULL);
  94     ShenandoahSimpleLock::unlock();
  95   }
  96 }
  97 
  98 bool ShenandoahReentrantLock::owned_by_self() const {
  99   Thread* const thread = Thread::current();
 100   Thread* const owner = Atomic::load(&_owner);
 101   return owner == thread;
 102 }