src/hotspot/share/gc/shenandoah/shenandoahLock.cpp
author zgu
Wed, 27 Nov 2019 11:52:57 -0500
changeset 59296 9186be5c78ba
permissions -rw-r--r--
8228720: Shenandoah: Implementation of concurrent class unloading Reviewed-by: rkennke
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
59296
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
     1
/*
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
     2
 * Copyright (c) 2019, Red Hat, Inc. All rights reserved.
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
     3
 *
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
     6
 * published by the Free Software Foundation.
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
     7
 *
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
     8
 * This code is distributed in the hope that it will be useful, but WITHOUT
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
     9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    10
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    11
 * version 2 for more details (a copy is included in the LICENSE file that
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    12
 * accompanied this code).
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    13
 *
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License version
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    15
 * 2 along with this work; if not, write to the Free Software Foundation,
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    16
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    17
 *
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    18
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    19
 * or visit www.oracle.com if you need additional information or have any
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    20
 * questions.
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    21
 *
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    22
 */
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    23
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    24
#include "precompiled.hpp"
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    25
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    26
#include "runtime/os.hpp"
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    27
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    28
#include "gc/shenandoah/shenandoahLock.hpp"
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    29
#include "runtime/atomic.hpp"
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    30
#include "runtime/os.inline.hpp"
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    31
#include "runtime/thread.hpp"
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    32
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    33
ShenandoahSimpleLock::ShenandoahSimpleLock() {
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    34
  assert(os::mutex_init_done(), "Too early!");
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    35
}
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    36
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    37
void ShenandoahSimpleLock::lock() {
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    38
  _lock.lock();
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    39
}
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    40
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    41
void ShenandoahSimpleLock::unlock() {
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    42
  _lock.unlock();
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    43
}
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    44
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    45
ShenandoahReentrantLock::ShenandoahReentrantLock() :
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    46
  ShenandoahSimpleLock(), _owner(NULL), _count(0) {
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    47
  assert(os::mutex_init_done(), "Too early!");
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    48
}
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    49
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    50
ShenandoahReentrantLock::~ShenandoahReentrantLock() {
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    51
  assert(_count == 0, "Unbalance");
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    52
}
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    53
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    54
void ShenandoahReentrantLock::lock() {
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    55
  Thread* const thread = Thread::current();
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    56
  Thread* const owner = Atomic::load(&_owner);
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    57
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    58
  if (owner != thread) {
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    59
    ShenandoahSimpleLock::lock();
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    60
    Atomic::store(&_owner, thread);
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    61
  }
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    62
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    63
  _count++;
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    64
}
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    65
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    66
void ShenandoahReentrantLock::unlock() {
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    67
  assert(owned_by_self(), "Invalid owner");
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    68
  assert(_count > 0, "Invalid count");
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    69
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    70
  _count--;
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    71
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    72
  if (_count == 0) {
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    73
    Atomic::store(&_owner, (Thread*)NULL);
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    74
    ShenandoahSimpleLock::unlock();
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    75
  }
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    76
}
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    77
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    78
bool ShenandoahReentrantLock::owned_by_self() const {
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    79
  Thread* const thread = Thread::current();
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    80
  Thread* const owner = Atomic::load(&_owner);
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    81
  return owner == thread;
9186be5c78ba 8228720: Shenandoah: Implementation of concurrent class unloading
zgu
parents:
diff changeset
    82
}