src/hotspot/share/gc/z/zSafeDelete.inline.hpp
author pliden
Thu, 28 Mar 2019 19:43:59 +0100
changeset 54330 69e80a82db9a
parent 54173 e6a92f2f37a9
child 58557 cf3205fdb6dc
permissions -rw-r--r--
8221540: ZGC: Reduce width of zForwardingEntry::from_index field Reviewed-by: stefank, eosterlund
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
54173
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
     1
/*
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
     4
 *
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
     7
 * published by the Free Software Foundation.
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
     8
 *
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    13
 * accompanied this code).
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    14
 *
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    18
 *
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    21
 * questions.
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    22
 */
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    23
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    24
#ifndef SHARE_GC_Z_ZSAFEDELETE_INLINE_HPP
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    25
#define SHARE_GC_Z_ZSAFEDELETE_INLINE_HPP
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    26
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    27
#include "gc/z/zArray.inline.hpp"
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    28
#include "gc/z/zSafeDelete.hpp"
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    29
#include "metaprogramming/isArray.hpp"
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    30
#include "utilities/debug.hpp"
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    31
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    32
template <typename T>
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    33
ZSafeDelete<T>::ZSafeDelete() :
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    34
    _lock(),
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    35
    _enabled(0),
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    36
    _deferred() {}
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    37
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    38
template <typename T>
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    39
bool ZSafeDelete<T>::deferred_delete(ItemT* item) {
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    40
  ZLocker<ZLock> locker(&_lock);
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    41
  if (_enabled > 0) {
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    42
    _deferred.add(item);
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    43
    return true;
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    44
  }
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    45
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    46
  return false;
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    47
}
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    48
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    49
template <typename T>
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    50
void ZSafeDelete<T>::immediate_delete(ItemT* item) {
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    51
  if (IsArray<T>::value) {
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    52
    delete [] item;
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    53
  } else {
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    54
    delete item;
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    55
  }
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    56
}
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    57
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    58
template <typename T>
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    59
void ZSafeDelete<T>::enable_deferred_delete() {
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    60
  ZLocker<ZLock> locker(&_lock);
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    61
  _enabled++;
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    62
}
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    63
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    64
template <typename T>
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    65
void ZSafeDelete<T>::disable_deferred_delete() {
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    66
  ZArray<ItemT*> deferred;
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    67
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    68
  {
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    69
    ZLocker<ZLock> locker(&_lock);
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    70
    assert(_enabled > 0, "Invalid state");
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    71
    if (--_enabled == 0) {
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    72
      deferred.transfer(&_deferred);
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    73
    }
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    74
  }
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    75
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    76
  ZArrayIterator<ItemT*> iter(&deferred);
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    77
  for (ItemT* item; iter.next(&item);) {
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    78
    immediate_delete(item);
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    79
  }
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    80
}
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    81
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    82
template <typename T>
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    83
void ZSafeDelete<T>::operator()(ItemT* item) {
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    84
  if (!deferred_delete(item)) {
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    85
    immediate_delete(item);
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    86
  }
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    87
}
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    88
e6a92f2f37a9 8220599: ZGC: Introduce ZSafeDelete
pliden
parents:
diff changeset
    89
#endif // SHARE_GC_Z_ZSAFEDELETE_INLINE_HPP