src/hotspot/share/utilities/concurrentHashTable.inline.hpp
author coleenp
Tue, 10 Jul 2018 11:13:33 -0400
changeset 50958 e0028bb6dd3d
parent 50637 359607017fb7
child 51334 cc2c79d22508
permissions -rw-r--r--
8206471: Race with ConcurrentHashTable deleting items on insert with cleanup thread Summary: Only fetch Node::next once and use that result. Reviewed-by: hseigel, dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50158
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
     1
/*
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
     4
 *
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
     7
 * published by the Free Software Foundation.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
     8
 *
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    13
 * accompanied this code).
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    14
 *
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    18
 *
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    21
 * questions.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    22
 *
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    23
 */
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    24
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    25
#ifndef SHARE_UTILITIES_CONCURRENT_HASH_TABLE_INLINE_HPP
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    26
#define SHARE_UTILITIES_CONCURRENT_HASH_TABLE_INLINE_HPP
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    27
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    28
#include "memory/allocation.inline.hpp"
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    29
#include "runtime/atomic.hpp"
50429
83aec1d357d4 8204301: Make OrderAccess functions available to hpp rather than inline.hpp files
coleenp
parents: 50158
diff changeset
    30
#include "runtime/orderAccess.hpp"
50158
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    31
#include "runtime/prefetch.inline.hpp"
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    32
#include "utilities/concurrentHashTable.hpp"
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    33
#include "utilities/globalCounter.inline.hpp"
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    34
#include "utilities/numberSeq.hpp"
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    35
#include "utilities/spinYield.hpp"
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    36
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    37
// 2^30 = 1G buckets
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    38
#define SIZE_BIG_LOG2 30
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    39
// 2^5  = 32 buckets
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    40
#define SIZE_SMALL_LOG2 5
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    41
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    42
// Number from spinYield.hpp. In some loops SpinYield would be unfair.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    43
#define SPINPAUSES_PER_YIELD 8192
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    44
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    45
#ifdef ASSERT
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    46
#ifdef _LP64
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    47
// Two low bits are not usable.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    48
static const void* POISON_PTR = (void*)UCONST64(0xfbadbadbadbadbac);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    49
#else
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    50
// Two low bits are not usable.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    51
static const void* POISON_PTR = (void*)0xffbadbac;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    52
#endif
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    53
#endif
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    54
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    55
// Node
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    56
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    57
inline typename ConcurrentHashTable<VALUE, CONFIG, F>::Node*
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    58
ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    59
  Node::next() const
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    60
{
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    61
  return OrderAccess::load_acquire(&_next);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    62
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    63
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    64
// Bucket
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    65
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    66
inline typename ConcurrentHashTable<VALUE, CONFIG, F>::Node*
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    67
ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    68
  Bucket::first_raw() const
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    69
{
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    70
  return OrderAccess::load_acquire(&_first);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    71
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    72
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    73
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    74
inline void ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    75
  Bucket::release_assign_node_ptr(
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    76
    typename ConcurrentHashTable<VALUE, CONFIG, F>::Node* const volatile * dst,
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    77
    typename ConcurrentHashTable<VALUE, CONFIG, F>::Node* node) const
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    78
{
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    79
  // Due to this assert this methods is not static.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    80
  assert(is_locked(), "Must be locked.");
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    81
  Node** tmp = (Node**)dst;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    82
  OrderAccess::release_store(tmp, clear_set_state(node, *dst));
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    83
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    84
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    85
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    86
inline typename ConcurrentHashTable<VALUE, CONFIG, F>::Node*
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    87
ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    88
  Bucket::first() const
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    89
{
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    90
  // We strip the states bit before returning the ptr.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    91
  return clear_state(OrderAccess::load_acquire(&_first));
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    92
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    93
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    94
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    95
inline bool ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    96
  Bucket::have_redirect() const
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    97
{
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    98
  return is_state(first_raw(), STATE_REDIRECT_BIT);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    99
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   100
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   101
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   102
inline bool ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   103
  Bucket::is_locked() const
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   104
{
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   105
  return is_state(first_raw(), STATE_LOCK_BIT);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   106
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   107
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   108
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   109
inline void ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   110
  Bucket::lock()
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   111
{
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   112
  int i = 0;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   113
  // SpinYield would be unfair here
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   114
  while (!this->trylock()) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   115
    if ((++i) == SPINPAUSES_PER_YIELD) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   116
      // On contemporary OS yielding will give CPU to another runnable thread if
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   117
      // there is no CPU available.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   118
      os::naked_yield();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   119
      i = 0;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   120
    } else {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   121
      SpinPause();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   122
    }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   123
  }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   124
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   125
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   126
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   127
inline void ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   128
  Bucket::release_assign_last_node_next(
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   129
     typename ConcurrentHashTable<VALUE, CONFIG, F>::Node* node)
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   130
{
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   131
  assert(is_locked(), "Must be locked.");
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   132
  Node* const volatile * ret = first_ptr();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   133
  while (clear_state(*ret) != NULL) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   134
    ret = clear_state(*ret)->next_ptr();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   135
  }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   136
  release_assign_node_ptr(ret, node);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   137
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   138
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   139
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   140
inline bool ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   141
  Bucket::cas_first(typename ConcurrentHashTable<VALUE, CONFIG, F>::Node* node,
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   142
                    typename ConcurrentHashTable<VALUE, CONFIG, F>::Node* expect
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   143
                    )
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   144
{
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   145
  if (is_locked()) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   146
    return false;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   147
  }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   148
  if (Atomic::cmpxchg(node, &_first, expect) == expect) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   149
    return true;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   150
  }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   151
  return false;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   152
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   153
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   154
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   155
inline bool ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   156
  Bucket::trylock()
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   157
{
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   158
  if (is_locked()) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   159
    return false;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   160
  }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   161
  // We will expect a clean first pointer.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   162
  Node* tmp = first();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   163
  if (Atomic::cmpxchg(set_state(tmp, STATE_LOCK_BIT), &_first, tmp) == tmp) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   164
    return true;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   165
  }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   166
  return false;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   167
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   168
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   169
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   170
inline void ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   171
  Bucket::unlock()
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   172
{
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   173
  assert(is_locked(), "Must be locked.");
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   174
  assert(!have_redirect(),
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   175
         "Unlocking a bucket after it has reached terminal state.");
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   176
  OrderAccess::release_store(&_first, clear_state(first()));
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   177
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   178
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   179
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   180
inline void ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   181
  Bucket::redirect()
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   182
{
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   183
  assert(is_locked(), "Must be locked.");
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   184
  OrderAccess::release_store(&_first, set_state(_first, STATE_REDIRECT_BIT));
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   185
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   186
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   187
// InternalTable
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   188
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   189
inline ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   190
  InternalTable::InternalTable(size_t log2_size)
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   191
    : _log2_size(log2_size), _size(((size_t)1ul) << _log2_size),
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   192
      _hash_mask(~(~((size_t)0) << _log2_size))
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   193
{
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   194
  assert(_log2_size >= SIZE_SMALL_LOG2 && _log2_size <= SIZE_BIG_LOG2,
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   195
         "Bad size");
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   196
  void* memory = NEW_C_HEAP_ARRAY(Bucket, _size, F);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   197
  _buckets = new (memory) Bucket[_size];
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   198
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   199
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   200
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   201
inline ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   202
  InternalTable::~InternalTable()
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   203
{
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   204
  FREE_C_HEAP_ARRAY(Bucket, _buckets);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   205
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   206
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   207
// ScopedCS
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   208
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   209
inline ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   210
  ScopedCS::ScopedCS(Thread* thread, ConcurrentHashTable<VALUE, CONFIG, F>* cht)
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   211
    : _thread(thread), _cht(cht)
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   212
{
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   213
  GlobalCounter::critical_section_begin(_thread);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   214
  // This version is published now.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   215
  if (OrderAccess::load_acquire(&_cht->_invisible_epoch) != NULL) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   216
    OrderAccess::release_store_fence(&_cht->_invisible_epoch, (Thread*)NULL);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   217
  }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   218
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   219
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   220
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   221
inline ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   222
  ScopedCS::~ScopedCS()
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   223
{
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   224
  GlobalCounter::critical_section_end(_thread);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   225
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   226
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   227
// BaseConfig
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   228
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   229
inline void* ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   230
  BaseConfig::allocate_node(size_t size, const VALUE& value)
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   231
{
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   232
  return AllocateHeap(size, F);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   233
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   234
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   235
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   236
inline void ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   237
  BaseConfig::free_node(void* memory, const VALUE& value)
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   238
{
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   239
  FreeHeap(memory);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   240
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   241
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   242
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   243
template <typename LOOKUP_FUNC>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   244
inline VALUE* ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   245
  MultiGetHandle::get(LOOKUP_FUNC& lookup_f, bool* grow_hint)
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   246
{
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   247
  return ScopedCS::_cht->internal_get(ScopedCS::_thread, lookup_f, grow_hint);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   248
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   249
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   250
// HaveDeletables
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   251
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   252
template <typename EVALUATE_FUNC>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   253
inline bool ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   254
  HaveDeletables<true, EVALUATE_FUNC>::have_deletable(Bucket* bucket,
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   255
                                                      EVALUATE_FUNC& eval_f,
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   256
                                                      Bucket* prefetch_bucket)
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   257
{
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   258
  // Instantiated for pointer type (true), so we can use prefetch.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   259
  // When visiting all Nodes doing this prefetch give around 30%.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   260
  Node* pref = prefetch_bucket != NULL ? prefetch_bucket->first() : NULL;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   261
  for (Node* next = bucket->first(); next != NULL ; next = next->next()) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   262
    if (pref != NULL) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   263
      Prefetch::read(*pref->value(), 0);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   264
      pref = pref->next();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   265
    }
50958
e0028bb6dd3d 8206471: Race with ConcurrentHashTable deleting items on insert with cleanup thread
coleenp
parents: 50637
diff changeset
   266
    // Read next() Node* once.  May be racing with a thread moving the next
e0028bb6dd3d 8206471: Race with ConcurrentHashTable deleting items on insert with cleanup thread
coleenp
parents: 50637
diff changeset
   267
    // pointers.
e0028bb6dd3d 8206471: Race with ConcurrentHashTable deleting items on insert with cleanup thread
coleenp
parents: 50637
diff changeset
   268
    Node* next_pref = next->next();
e0028bb6dd3d 8206471: Race with ConcurrentHashTable deleting items on insert with cleanup thread
coleenp
parents: 50637
diff changeset
   269
    if (next_pref != NULL) {
e0028bb6dd3d 8206471: Race with ConcurrentHashTable deleting items on insert with cleanup thread
coleenp
parents: 50637
diff changeset
   270
      Prefetch::read(*next_pref->value(), 0);
50158
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   271
    }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   272
    if (eval_f(next->value())) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   273
      return true;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   274
    }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   275
  }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   276
  return false;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   277
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   278
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   279
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   280
template <bool b, typename EVALUATE_FUNC>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   281
inline bool ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   282
  HaveDeletables<b, EVALUATE_FUNC>::have_deletable(Bucket* bucket,
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   283
                                                   EVALUATE_FUNC& eval_f,
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   284
                                                   Bucket* preb)
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   285
{
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   286
  for (Node* next = bucket->first(); next != NULL ; next = next->next()) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   287
    if (eval_f(next->value())) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   288
      return true;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   289
    }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   290
  }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   291
  return false;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   292
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   293
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   294
// ConcurrentHashTable
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   295
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   296
inline void ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   297
  write_synchonize_on_visible_epoch(Thread* thread)
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   298
{
50445
bd6b78feb6a3 8195097: Make it possible to process StringTable outside safepoint
rehn
parents: 50429
diff changeset
   299
  assert(_resize_lock_owner == thread, "Re-size lock not held");
50158
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   300
  OrderAccess::fence(); // Prevent below load from floating up.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   301
  // If no reader saw this version we can skip write_synchronize.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   302
  if (OrderAccess::load_acquire(&_invisible_epoch) == thread) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   303
    return;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   304
  }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   305
  assert(_invisible_epoch == NULL, "Two thread doing bulk operations");
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   306
  // We set this/next version that we are synchronizing for to not published.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   307
  // A reader will zero this flag if it reads this/next version.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   308
  OrderAccess::release_store(&_invisible_epoch, thread);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   309
  GlobalCounter::write_synchronize();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   310
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   311
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   312
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   313
inline bool ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   314
  try_resize_lock(Thread* locker)
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   315
{
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   316
  if (_resize_lock->try_lock()) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   317
    if (_resize_lock_owner != NULL) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   318
      assert(locker != _resize_lock_owner, "Already own lock");
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   319
      // We got mutex but internal state is locked.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   320
      _resize_lock->unlock();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   321
      return false;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   322
    }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   323
  } else {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   324
    return false;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   325
  }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   326
  _invisible_epoch = 0;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   327
  _resize_lock_owner = locker;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   328
  return true;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   329
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   330
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   331
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   332
inline void ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   333
  lock_resize_lock(Thread* locker)
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   334
{
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   335
  size_t i = 0;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   336
  // If lock is hold by some other thread, the chances that it is return quick
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   337
  // is low. So we will prefer yielding.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   338
  SpinYield yield(1, 512);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   339
  do {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   340
    _resize_lock->lock_without_safepoint_check();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   341
    // If holder of lock dropped mutex for safepoint mutex might be unlocked,
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   342
    // and _resize_lock_owner will contain the owner.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   343
    if (_resize_lock_owner != NULL) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   344
      assert(locker != _resize_lock_owner, "Already own lock");
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   345
      // We got mutex but internal state is locked.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   346
      _resize_lock->unlock();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   347
      yield.wait();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   348
    } else {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   349
      break;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   350
    }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   351
  } while(true);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   352
  _resize_lock_owner = locker;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   353
  _invisible_epoch = 0;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   354
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   355
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   356
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   357
inline void ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   358
  unlock_resize_lock(Thread* locker)
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   359
{
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   360
  _invisible_epoch = 0;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   361
  assert(locker == _resize_lock_owner, "Not unlocked by locker.");
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   362
  _resize_lock_owner = NULL;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   363
  _resize_lock->unlock();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   364
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   365
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   366
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   367
inline void ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   368
  free_nodes()
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   369
{
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   370
  // We assume we are not MT during freeing.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   371
  for (size_t node_it = 0; node_it < _table->_size; node_it++) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   372
    Bucket* bucket = _table->get_buckets() + node_it;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   373
    Node* node = bucket->first();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   374
    while (node != NULL) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   375
      Node* free_node = node;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   376
      node = node->next();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   377
      Node::destroy_node(free_node);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   378
    }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   379
  }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   380
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   381
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   382
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   383
inline typename ConcurrentHashTable<VALUE, CONFIG, F>::InternalTable*
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   384
ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   385
  get_table() const
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   386
{
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   387
  return OrderAccess::load_acquire(&_table);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   388
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   389
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   390
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   391
inline typename ConcurrentHashTable<VALUE, CONFIG, F>::InternalTable*
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   392
ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   393
  get_new_table() const
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   394
{
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   395
  return OrderAccess::load_acquire(&_new_table);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   396
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   397
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   398
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   399
inline typename ConcurrentHashTable<VALUE, CONFIG, F>::InternalTable*
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   400
ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   401
  set_table_from_new()
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   402
{
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   403
  InternalTable* old_table = _table;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   404
  // Publish the new table.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   405
  OrderAccess::release_store(&_table, _new_table);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   406
  // All must see this.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   407
  GlobalCounter::write_synchronize();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   408
  // _new_table not read any more.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   409
  _new_table = NULL;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   410
  DEBUG_ONLY(_new_table = (InternalTable*)POISON_PTR;)
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   411
  return old_table;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   412
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   413
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   414
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   415
inline void ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   416
  internal_grow_range(Thread* thread, size_t start, size_t stop)
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   417
{
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   418
  assert(stop <= _table->_size, "Outside backing array");
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   419
  assert(_new_table != NULL, "Grow not proper setup before start");
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   420
  // The state is also copied here. Hence all buckets in new table will be
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   421
  // locked. I call the siblings odd/even, where even have high bit 0 and odd
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   422
  // have high bit 1.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   423
  for (size_t even_index = start; even_index < stop; even_index++) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   424
    Bucket* bucket = _table->get_bucket(even_index);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   425
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   426
    bucket->lock();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   427
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   428
    size_t odd_index = even_index + _table->_size;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   429
    _new_table->get_buckets()[even_index] = *bucket;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   430
    _new_table->get_buckets()[odd_index] = *bucket;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   431
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   432
    // Moves lockers go to new table, where they will wait until unlock() below.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   433
    bucket->redirect(); /* Must release stores above */
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   434
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   435
    // When this is done we have separated the nodes into corresponding buckets
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   436
    // in new table.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   437
    if (!unzip_bucket(thread, _table, _new_table, even_index, odd_index)) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   438
      // If bucket is empty, unzip does nothing.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   439
      // We must make sure readers go to new table before we poison the bucket.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   440
      DEBUG_ONLY(GlobalCounter::write_synchronize();)
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   441
    }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   442
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   443
    // Unlock for writes into the new table buckets.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   444
    _new_table->get_bucket(even_index)->unlock();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   445
    _new_table->get_bucket(odd_index)->unlock();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   446
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   447
    DEBUG_ONLY(
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   448
       bucket->release_assign_node_ptr(
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   449
          _table->get_bucket(even_index)->first_ptr(), (Node*)POISON_PTR);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   450
    )
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   451
  }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   452
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   453
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   454
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   455
template <typename LOOKUP_FUNC, typename DELETE_FUNC>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   456
inline bool ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   457
  internal_remove(Thread* thread, LOOKUP_FUNC& lookup_f, DELETE_FUNC& delete_f)
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   458
{
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   459
  Bucket* bucket = get_bucket_locked(thread, lookup_f.get_hash());
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   460
  assert(bucket->is_locked(), "Must be locked.");
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   461
  Node* const volatile * rem_n_prev = bucket->first_ptr();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   462
  Node* rem_n = bucket->first();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   463
  bool have_dead = false;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   464
  while (rem_n != NULL) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   465
    if (lookup_f.equals(rem_n->value(), &have_dead)) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   466
      bucket->release_assign_node_ptr(rem_n_prev, rem_n->next());
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   467
      break;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   468
    } else {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   469
      rem_n_prev = rem_n->next_ptr();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   470
      rem_n = rem_n->next();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   471
    }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   472
  }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   473
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   474
  bucket->unlock();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   475
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   476
  if (rem_n == NULL) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   477
    return false;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   478
  }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   479
  // Publish the deletion.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   480
  GlobalCounter::write_synchronize();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   481
  delete_f(rem_n->value());
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   482
  Node::destroy_node(rem_n);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   483
  return true;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   484
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   485
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   486
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   487
template <typename EVALUATE_FUNC, typename DELETE_FUNC>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   488
inline void ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   489
  do_bulk_delete_locked_for(Thread* thread, size_t start_idx, size_t stop_idx,
50608
1609a43e77ae 8204857: ConcurrentHashTable: Fix parallel processing
rehn
parents: 50445
diff changeset
   490
                            EVALUATE_FUNC& eval_f, DELETE_FUNC& del_f, bool is_mt)
50158
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   491
{
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   492
  // Here we have resize lock so table is SMR safe, and there is no new
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   493
  // table. Can do this in parallel if we want.
50608
1609a43e77ae 8204857: ConcurrentHashTable: Fix parallel processing
rehn
parents: 50445
diff changeset
   494
  assert((is_mt && _resize_lock_owner != NULL) ||
1609a43e77ae 8204857: ConcurrentHashTable: Fix parallel processing
rehn
parents: 50445
diff changeset
   495
         (!is_mt && _resize_lock_owner == thread), "Re-size lock not held");
50158
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   496
  Node* ndel[BULK_DELETE_LIMIT];
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   497
  InternalTable* table = get_table();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   498
  assert(start_idx < stop_idx, "Must be");
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   499
  assert(stop_idx <= _table->_size, "Must be");
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   500
  // Here manual do critical section since we don't want to take the cost of
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   501
  // locking the bucket if there is nothing to delete. But we can have
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   502
  // concurrent single deletes. The _invisible_epoch can only be used by the
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   503
  // owner of _resize_lock, us here. There we should not changed it in our
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   504
  // own read-side.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   505
  GlobalCounter::critical_section_begin(thread);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   506
  for (size_t bucket_it = start_idx; bucket_it < stop_idx; bucket_it++) {
50445
bd6b78feb6a3 8195097: Make it possible to process StringTable outside safepoint
rehn
parents: 50429
diff changeset
   507
    Bucket* bucket = table->get_bucket(bucket_it);
50158
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   508
    Bucket* prefetch_bucket = (bucket_it+1) < stop_idx ?
50445
bd6b78feb6a3 8195097: Make it possible to process StringTable outside safepoint
rehn
parents: 50429
diff changeset
   509
                              table->get_bucket(bucket_it+1) : NULL;
50158
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   510
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   511
    if (!HaveDeletables<IsPointer<VALUE>::value, EVALUATE_FUNC>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   512
        have_deletable(bucket, eval_f, prefetch_bucket)) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   513
        // Nothing to remove in this bucket.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   514
        continue;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   515
    }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   516
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   517
    GlobalCounter::critical_section_end(thread);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   518
    // We left critical section but the bucket cannot be removed while we hold
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   519
    // the _resize_lock.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   520
    bucket->lock();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   521
    size_t nd = delete_check_nodes(bucket, eval_f, BULK_DELETE_LIMIT, ndel);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   522
    bucket->unlock();
50608
1609a43e77ae 8204857: ConcurrentHashTable: Fix parallel processing
rehn
parents: 50445
diff changeset
   523
    if (is_mt) {
1609a43e77ae 8204857: ConcurrentHashTable: Fix parallel processing
rehn
parents: 50445
diff changeset
   524
      GlobalCounter::write_synchronize();
1609a43e77ae 8204857: ConcurrentHashTable: Fix parallel processing
rehn
parents: 50445
diff changeset
   525
    } else {
1609a43e77ae 8204857: ConcurrentHashTable: Fix parallel processing
rehn
parents: 50445
diff changeset
   526
      write_synchonize_on_visible_epoch(thread);
1609a43e77ae 8204857: ConcurrentHashTable: Fix parallel processing
rehn
parents: 50445
diff changeset
   527
    }
50158
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   528
    for (size_t node_it = 0; node_it < nd; node_it++) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   529
      del_f(ndel[node_it]->value());
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   530
      Node::destroy_node(ndel[node_it]);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   531
      DEBUG_ONLY(ndel[node_it] = (Node*)POISON_PTR;)
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   532
    }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   533
    GlobalCounter::critical_section_begin(thread);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   534
  }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   535
  GlobalCounter::critical_section_end(thread);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   536
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   537
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   538
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   539
template <typename LOOKUP_FUNC>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   540
inline void ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   541
  delete_in_bucket(Thread* thread, Bucket* bucket, LOOKUP_FUNC& lookup_f)
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   542
{
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   543
  size_t dels = 0;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   544
  Node* ndel[BULK_DELETE_LIMIT];
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   545
  Node* const volatile * rem_n_prev = bucket->first_ptr();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   546
  Node* rem_n = bucket->first();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   547
  while (rem_n != NULL) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   548
    bool is_dead = false;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   549
    lookup_f.equals(rem_n->value(), &is_dead);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   550
    if (is_dead) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   551
      ndel[dels++] = rem_n;
50958
e0028bb6dd3d 8206471: Race with ConcurrentHashTable deleting items on insert with cleanup thread
coleenp
parents: 50637
diff changeset
   552
      Node* next_node = rem_n->next();
e0028bb6dd3d 8206471: Race with ConcurrentHashTable deleting items on insert with cleanup thread
coleenp
parents: 50637
diff changeset
   553
      bucket->release_assign_node_ptr(rem_n_prev, next_node);
e0028bb6dd3d 8206471: Race with ConcurrentHashTable deleting items on insert with cleanup thread
coleenp
parents: 50637
diff changeset
   554
      rem_n = next_node;
50158
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   555
      if (dels == BULK_DELETE_LIMIT) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   556
        break;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   557
      }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   558
    } else {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   559
      rem_n_prev = rem_n->next_ptr();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   560
      rem_n = rem_n->next();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   561
    }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   562
  }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   563
  if (dels > 0) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   564
    GlobalCounter::write_synchronize();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   565
    for (size_t node_it = 0; node_it < dels; node_it++) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   566
      Node::destroy_node(ndel[node_it]);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   567
      DEBUG_ONLY(ndel[node_it] = (Node*)POISON_PTR;)
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   568
    }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   569
  }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   570
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   571
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   572
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   573
inline typename ConcurrentHashTable<VALUE, CONFIG, F>::Bucket*
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   574
ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   575
  get_bucket(uintx hash) const
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   576
{
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   577
  InternalTable* table = get_table();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   578
  Bucket* bucket = get_bucket_in(table, hash);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   579
  if (bucket->have_redirect()) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   580
    table = get_new_table();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   581
    bucket = get_bucket_in(table, hash);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   582
  }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   583
  return bucket;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   584
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   585
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   586
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   587
inline typename ConcurrentHashTable<VALUE, CONFIG, F>::Bucket*
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   588
ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   589
  get_bucket_locked(Thread* thread, const uintx hash)
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   590
{
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   591
  Bucket* bucket;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   592
  int i = 0;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   593
  // SpinYield would be unfair here
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   594
  while(true) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   595
    {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   596
      // We need a critical section to protect the table itself. But if we fail
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   597
      // we must leave critical section otherwise we would deadlock.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   598
      ScopedCS cs(thread, this);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   599
      bucket = get_bucket(hash);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   600
      if (bucket->trylock()) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   601
        break; /* ends critical section */
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   602
      }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   603
    } /* ends critical section */
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   604
    if ((++i) == SPINPAUSES_PER_YIELD) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   605
      // On contemporary OS yielding will give CPU to another runnable thread if
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   606
      // there is no CPU available.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   607
      os::naked_yield();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   608
      i = 0;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   609
    } else {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   610
      SpinPause();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   611
    }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   612
  }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   613
  return bucket;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   614
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   615
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   616
// Always called within critical section
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   617
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   618
template <typename LOOKUP_FUNC>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   619
typename ConcurrentHashTable<VALUE, CONFIG, F>::Node*
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   620
ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   621
  get_node(const Bucket* const bucket, LOOKUP_FUNC& lookup_f,
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   622
           bool* have_dead, size_t* loops) const
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   623
{
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   624
  size_t loop_count = 0;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   625
  Node* node = bucket->first();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   626
  while (node != NULL) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   627
    bool is_dead = false;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   628
    ++loop_count;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   629
    if (lookup_f.equals(node->value(), &is_dead)) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   630
      break;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   631
    }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   632
    if (is_dead && !(*have_dead)) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   633
      *have_dead = true;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   634
    }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   635
    node = node->next();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   636
  }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   637
  if (loops != NULL) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   638
    *loops = loop_count;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   639
  }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   640
  return node;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   641
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   642
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   643
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   644
inline bool ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   645
  unzip_bucket(Thread* thread, InternalTable* old_table,
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   646
               InternalTable* new_table, size_t even_index, size_t odd_index)
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   647
{
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   648
  Node* aux = old_table->get_bucket(even_index)->first();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   649
  if (aux == NULL) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   650
    // This is an empty bucket and in debug we poison first ptr in bucket.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   651
    // Therefore we must make sure no readers are looking at this bucket.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   652
    // If we don't do a write_synch here, caller must do it.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   653
    return false;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   654
  }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   655
  Node* delete_me = NULL;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   656
  Node* const volatile * even = new_table->get_bucket(even_index)->first_ptr();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   657
  Node* const volatile * odd = new_table->get_bucket(odd_index)->first_ptr();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   658
  while (aux != NULL) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   659
    bool dead_hash = false;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   660
    size_t aux_hash = CONFIG::get_hash(*aux->value(), &dead_hash);
50958
e0028bb6dd3d 8206471: Race with ConcurrentHashTable deleting items on insert with cleanup thread
coleenp
parents: 50637
diff changeset
   661
    Node* aux_next = aux->next();
50158
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   662
    if (dead_hash) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   663
      delete_me = aux;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   664
      // This item is dead, move both list to next
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   665
      new_table->get_bucket(odd_index)->release_assign_node_ptr(odd,
50958
e0028bb6dd3d 8206471: Race with ConcurrentHashTable deleting items on insert with cleanup thread
coleenp
parents: 50637
diff changeset
   666
                                                                aux_next);
50158
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   667
      new_table->get_bucket(even_index)->release_assign_node_ptr(even,
50958
e0028bb6dd3d 8206471: Race with ConcurrentHashTable deleting items on insert with cleanup thread
coleenp
parents: 50637
diff changeset
   668
                                                                 aux_next);
50158
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   669
    } else {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   670
      size_t aux_index = bucket_idx_hash(new_table, aux_hash);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   671
      if (aux_index == even_index) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   672
        // This is a even, so move odd to aux/even next
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   673
        new_table->get_bucket(odd_index)->release_assign_node_ptr(odd,
50958
e0028bb6dd3d 8206471: Race with ConcurrentHashTable deleting items on insert with cleanup thread
coleenp
parents: 50637
diff changeset
   674
                                                                  aux_next);
50158
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   675
        // Keep in even list
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   676
        even = aux->next_ptr();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   677
      } else if (aux_index == odd_index) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   678
        // This is a odd, so move odd to aux/odd next
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   679
        new_table->get_bucket(even_index)->release_assign_node_ptr(even,
50958
e0028bb6dd3d 8206471: Race with ConcurrentHashTable deleting items on insert with cleanup thread
coleenp
parents: 50637
diff changeset
   680
                                                                   aux_next);
50158
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   681
        // Keep in odd list
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   682
        odd = aux->next_ptr();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   683
      } else {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   684
        fatal("aux_index does not match even or odd indices");
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   685
      }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   686
    }
50958
e0028bb6dd3d 8206471: Race with ConcurrentHashTable deleting items on insert with cleanup thread
coleenp
parents: 50637
diff changeset
   687
    aux = aux_next;
50158
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   688
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   689
    // We can only move 1 pointer otherwise a reader might be moved to the wrong
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   690
    // chain. E.g. looking for even hash value but got moved to the odd bucket
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   691
    // chain.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   692
    write_synchonize_on_visible_epoch(thread);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   693
    if (delete_me != NULL) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   694
      Node::destroy_node(delete_me);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   695
      delete_me = NULL;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   696
    }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   697
  }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   698
  return true;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   699
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   700
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   701
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   702
inline bool ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   703
  internal_shrink_prolog(Thread* thread, size_t log2_size)
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   704
{
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   705
  if (!try_resize_lock(thread)) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   706
    return false;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   707
  }
50445
bd6b78feb6a3 8195097: Make it possible to process StringTable outside safepoint
rehn
parents: 50429
diff changeset
   708
  assert(_resize_lock_owner == thread, "Re-size lock not held");
50158
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   709
  if (_table->_log2_size == _log2_start_size ||
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   710
      _table->_log2_size <= log2_size) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   711
    unlock_resize_lock(thread);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   712
    return false;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   713
  }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   714
  _new_table = new InternalTable(_table->_log2_size - 1);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   715
  return true;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   716
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   717
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   718
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   719
inline void ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   720
  internal_shrink_epilog(Thread* thread)
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   721
{
50445
bd6b78feb6a3 8195097: Make it possible to process StringTable outside safepoint
rehn
parents: 50429
diff changeset
   722
  assert(_resize_lock_owner == thread, "Re-size lock not held");
50158
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   723
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   724
  InternalTable* old_table = set_table_from_new();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   725
  _size_limit_reached = false;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   726
  unlock_resize_lock(thread);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   727
#ifdef ASSERT
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   728
  for (size_t i = 0; i < old_table->_size; i++) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   729
    assert(old_table->get_bucket(i++)->first() == POISON_PTR,
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   730
           "No poison found");
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   731
  }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   732
#endif
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   733
  // ABA safe, old_table not visible to any other threads.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   734
  delete old_table;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   735
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   736
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   737
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   738
inline void ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   739
  internal_shrink_range(Thread* thread, size_t start, size_t stop)
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   740
{
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   741
  // The state is also copied here.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   742
  // Hence all buckets in new table will be locked.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   743
  for (size_t bucket_it = start; bucket_it < stop; bucket_it++) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   744
    size_t even_hash_index = bucket_it; // High bit 0
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   745
    size_t odd_hash_index = bucket_it + _new_table->_size; // High bit 1
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   746
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   747
    Bucket* b_old_even = _table->get_bucket(even_hash_index);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   748
    Bucket* b_old_odd  = _table->get_bucket(odd_hash_index);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   749
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   750
    b_old_even->lock();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   751
    b_old_odd->lock();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   752
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   753
    _new_table->get_buckets()[bucket_it] = *b_old_even;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   754
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   755
    // Put chains together.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   756
    _new_table->get_bucket(bucket_it)->
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   757
      release_assign_last_node_next(*(b_old_odd->first_ptr()));
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   758
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   759
    b_old_even->redirect();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   760
    b_old_odd->redirect();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   761
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   762
    write_synchonize_on_visible_epoch(thread);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   763
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   764
    // Unlock for writes into new smaller table.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   765
    _new_table->get_bucket(bucket_it)->unlock();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   766
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   767
    DEBUG_ONLY(b_old_even->release_assign_node_ptr(b_old_even->first_ptr(),
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   768
                                                   (Node*)POISON_PTR);)
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   769
    DEBUG_ONLY(b_old_odd->release_assign_node_ptr(b_old_odd->first_ptr(),
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   770
                                                  (Node*)POISON_PTR);)
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   771
  }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   772
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   773
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   774
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   775
inline bool ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   776
  internal_shrink(Thread* thread, size_t log2_size)
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   777
{
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   778
  if (!internal_shrink_prolog(thread, log2_size)) {
50445
bd6b78feb6a3 8195097: Make it possible to process StringTable outside safepoint
rehn
parents: 50429
diff changeset
   779
    assert(_resize_lock_owner != thread, "Re-size lock held");
50158
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   780
    return false;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   781
  }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   782
  assert(_resize_lock_owner == thread, "Should be locked by me");
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   783
  internal_shrink_range(thread, 0, _new_table->_size);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   784
  internal_shrink_epilog(thread);
50445
bd6b78feb6a3 8195097: Make it possible to process StringTable outside safepoint
rehn
parents: 50429
diff changeset
   785
  assert(_resize_lock_owner != thread, "Re-size lock held");
50158
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   786
  return true;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   787
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   788
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   789
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   790
inline bool ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   791
  internal_grow_prolog(Thread* thread, size_t log2_size)
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   792
{
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   793
  // This double checking of _size_limit_reached/is_max_size_reached()
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   794
  //  we only do in grow path, since grow means high load on table
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   795
  // while shrink means low load.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   796
  if (is_max_size_reached()) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   797
    return false;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   798
  }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   799
  if (!try_resize_lock(thread)) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   800
    // Either we have an ongoing resize or an operation which doesn't want us
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   801
    // to resize now.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   802
    return false;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   803
  }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   804
  if (is_max_size_reached() || _table->_log2_size >= log2_size) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   805
    unlock_resize_lock(thread);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   806
    return false;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   807
  }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   808
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   809
  _new_table = new InternalTable(_table->_log2_size + 1);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   810
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   811
  if (_new_table->_log2_size == _log2_size_limit) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   812
    _size_limit_reached = true;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   813
  }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   814
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   815
  return true;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   816
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   817
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   818
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   819
inline void ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   820
  internal_grow_epilog(Thread* thread)
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   821
{
50445
bd6b78feb6a3 8195097: Make it possible to process StringTable outside safepoint
rehn
parents: 50429
diff changeset
   822
  assert(_resize_lock_owner == thread, "Should be locked");
50158
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   823
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   824
  InternalTable* old_table = set_table_from_new();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   825
  unlock_resize_lock(thread);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   826
#ifdef ASSERT
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   827
  for (size_t i = 0; i < old_table->_size; i++) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   828
    assert(old_table->get_bucket(i++)->first() == POISON_PTR,
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   829
           "No poison found");
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   830
  }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   831
#endif
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   832
  // ABA safe, old_table not visible to any other threads.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   833
  delete old_table;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   834
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   835
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   836
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   837
inline bool ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   838
  internal_grow(Thread* thread, size_t log2_size)
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   839
{
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   840
  if (!internal_grow_prolog(thread, log2_size)) {
50445
bd6b78feb6a3 8195097: Make it possible to process StringTable outside safepoint
rehn
parents: 50429
diff changeset
   841
    assert(_resize_lock_owner != thread, "Re-size lock held");
50158
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   842
    return false;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   843
  }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   844
  assert(_resize_lock_owner == thread, "Should be locked by me");
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   845
  internal_grow_range(thread, 0, _table->_size);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   846
  internal_grow_epilog(thread);
50445
bd6b78feb6a3 8195097: Make it possible to process StringTable outside safepoint
rehn
parents: 50429
diff changeset
   847
  assert(_resize_lock_owner != thread, "Re-size lock held");
50158
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   848
  return true;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   849
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   850
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   851
// Always called within critical section
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   852
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   853
template <typename LOOKUP_FUNC>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   854
inline VALUE* ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   855
  internal_get(Thread* thread, LOOKUP_FUNC& lookup_f, bool* grow_hint)
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   856
{
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   857
  bool clean = false;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   858
  size_t loops = 0;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   859
  VALUE* ret = NULL;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   860
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   861
  const Bucket* bucket = get_bucket(lookup_f.get_hash());
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   862
  Node* node = get_node(bucket, lookup_f, &clean, &loops);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   863
  if (node != NULL) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   864
    ret = node->value();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   865
  }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   866
  if (grow_hint != NULL) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   867
    *grow_hint = loops > _grow_hint;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   868
  }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   869
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   870
  return ret;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   871
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   872
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   873
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   874
template <typename LOOKUP_FUNC, typename VALUE_FUNC, typename CALLBACK_FUNC>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   875
inline bool ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   876
  internal_insert(Thread* thread, LOOKUP_FUNC& lookup_f, VALUE_FUNC& value_f,
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   877
                  CALLBACK_FUNC& callback, bool* grow_hint)
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   878
{
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   879
  bool ret = false;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   880
  bool clean = false;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   881
  bool locked;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   882
  size_t loops = 0;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   883
  size_t i = 0;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   884
  Node* new_node = NULL;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   885
  uintx hash = lookup_f.get_hash();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   886
  while (true) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   887
    {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   888
      ScopedCS cs(thread, this); /* protected the table/bucket */
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   889
      Bucket* bucket = get_bucket(hash);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   890
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   891
      Node* first_at_start = bucket->first();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   892
      Node* old = get_node(bucket, lookup_f, &clean, &loops);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   893
      if (old == NULL) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   894
        // No duplicate found.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   895
        if (new_node == NULL) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   896
          new_node = Node::create_node(value_f(), first_at_start);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   897
        } else {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   898
          new_node->set_next(first_at_start);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   899
        }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   900
        if (bucket->cas_first(new_node, first_at_start)) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   901
          callback(true, new_node->value());
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   902
          new_node = NULL;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   903
          ret = true;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   904
          break; /* leave critical section */
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   905
        }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   906
        // CAS failed we must leave critical section and retry.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   907
        locked = bucket->is_locked();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   908
      } else {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   909
        // There is a duplicate.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   910
        callback(false, old->value());
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   911
        break; /* leave critical section */
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   912
      }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   913
    } /* leave critical section */
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   914
    i++;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   915
    if (locked) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   916
      os::naked_yield();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   917
    } else {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   918
      SpinPause();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   919
    }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   920
  }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   921
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   922
  if (new_node != NULL) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   923
    // CAS failed and a duplicate was inserted, we must free this node.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   924
    Node::destroy_node(new_node);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   925
  } else if (i == 0 && clean) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   926
    // We only do cleaning on fast inserts.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   927
    Bucket* bucket = get_bucket_locked(thread, lookup_f.get_hash());
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   928
    assert(bucket->is_locked(), "Must be locked.");
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   929
    delete_in_bucket(thread, bucket, lookup_f);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   930
    bucket->unlock();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   931
  }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   932
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   933
  if (grow_hint != NULL) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   934
    *grow_hint = loops > _grow_hint;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   935
  }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   936
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   937
  return ret;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   938
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   939
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   940
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   941
template <typename FUNC>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   942
inline bool ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   943
  visit_nodes(Bucket* bucket, FUNC& visitor_f)
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   944
{
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   945
  Node* current_node = bucket->first();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   946
  while (current_node != NULL) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   947
    if (!visitor_f(current_node->value())) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   948
      return false;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   949
    }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   950
    current_node = current_node->next();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   951
  }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   952
  return true;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   953
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   954
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   955
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   956
template <typename FUNC>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   957
inline void ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   958
  do_scan_locked(Thread* thread, FUNC& scan_f)
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   959
{
50445
bd6b78feb6a3 8195097: Make it possible to process StringTable outside safepoint
rehn
parents: 50429
diff changeset
   960
  assert(_resize_lock_owner == thread, "Re-size lock not held");
50158
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   961
  // We can do a critical section over the entire loop but that would block
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   962
  // updates for a long time. Instead we choose to block resizes.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   963
  InternalTable* table = get_table();
50445
bd6b78feb6a3 8195097: Make it possible to process StringTable outside safepoint
rehn
parents: 50429
diff changeset
   964
  for (size_t bucket_it = 0; bucket_it < table->_size; bucket_it++) {
50158
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   965
    ScopedCS cs(thread, this);
50445
bd6b78feb6a3 8195097: Make it possible to process StringTable outside safepoint
rehn
parents: 50429
diff changeset
   966
    if (!visit_nodes(table->get_bucket(bucket_it), scan_f)) {
50158
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   967
      break; /* ends critical section */
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   968
    }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   969
  } /* ends critical section */
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   970
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   971
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   972
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   973
template <typename EVALUATE_FUNC>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   974
inline size_t ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   975
  delete_check_nodes(Bucket* bucket, EVALUATE_FUNC& eval_f,
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   976
                     size_t num_del, Node** ndel)
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   977
{
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   978
  size_t dels = 0;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   979
  Node* const volatile * rem_n_prev = bucket->first_ptr();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   980
  Node* rem_n = bucket->first();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   981
  while (rem_n != NULL) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   982
    if (eval_f(rem_n->value())) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   983
      ndel[dels++] = rem_n;
50958
e0028bb6dd3d 8206471: Race with ConcurrentHashTable deleting items on insert with cleanup thread
coleenp
parents: 50637
diff changeset
   984
      Node* next_node = rem_n->next();
e0028bb6dd3d 8206471: Race with ConcurrentHashTable deleting items on insert with cleanup thread
coleenp
parents: 50637
diff changeset
   985
      bucket->release_assign_node_ptr(rem_n_prev, next_node);
e0028bb6dd3d 8206471: Race with ConcurrentHashTable deleting items on insert with cleanup thread
coleenp
parents: 50637
diff changeset
   986
      rem_n = next_node;
50158
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   987
      if (dels == num_del) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   988
        break;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   989
      }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   990
    } else {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   991
      rem_n_prev = rem_n->next_ptr();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   992
      rem_n = rem_n->next();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   993
    }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   994
  }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   995
  return dels;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   996
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   997
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   998
// Constructor
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   999
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1000
inline ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1001
  ConcurrentHashTable(size_t log2size, size_t log2size_limit, size_t grow_hint)
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1002
    : _new_table(NULL), _log2_start_size(log2size),
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1003
       _log2_size_limit(log2size_limit), _grow_hint(grow_hint),
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1004
       _size_limit_reached(false), _resize_lock_owner(NULL),
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1005
       _invisible_epoch(0)
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1006
{
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1007
  _resize_lock =
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1008
    new Mutex(Mutex::leaf, "ConcurrentHashTable", false,
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1009
              Monitor::_safepoint_check_never);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1010
  _table = new InternalTable(log2size);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1011
  assert(log2size_limit >= log2size, "bad ergo");
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1012
  _size_limit_reached = _table->_log2_size == _log2_size_limit;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1013
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1014
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1015
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1016
inline ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1017
  ~ConcurrentHashTable()
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1018
{
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1019
  delete _resize_lock;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1020
  free_nodes();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1021
  delete _table;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1022
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1023
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1024
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1025
inline size_t ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1026
  get_size_log2(Thread* thread)
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1027
{
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1028
  ScopedCS cs(thread, this);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1029
  return _table->_log2_size;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1030
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1031
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1032
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1033
inline bool ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1034
  shrink(Thread* thread, size_t size_limit_log2)
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1035
{
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1036
  size_t tmp = size_limit_log2 == 0 ? _log2_start_size : size_limit_log2;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1037
  bool ret = internal_shrink(thread, tmp);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1038
  return ret;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1039
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1040
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1041
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1042
inline bool ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1043
  grow(Thread* thread, size_t size_limit_log2)
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1044
{
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1045
  size_t tmp = size_limit_log2 == 0 ? _log2_size_limit : size_limit_log2;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1046
  return internal_grow(thread, tmp);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1047
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1048
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1049
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1050
template <typename LOOKUP_FUNC, typename FOUND_FUNC>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1051
inline bool ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1052
  get(Thread* thread, LOOKUP_FUNC& lookup_f, FOUND_FUNC& found_f, bool* grow_hint)
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1053
{
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1054
  bool ret = false;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1055
  ScopedCS cs(thread, this);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1056
  VALUE* val = internal_get(thread, lookup_f, grow_hint);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1057
  if (val != NULL) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1058
    found_f(val);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1059
    ret = true;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1060
  }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1061
  return ret;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1062
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1063
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1064
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1065
template <typename LOOKUP_FUNC>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1066
inline VALUE ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1067
  get_copy(Thread* thread, LOOKUP_FUNC& lookup_f, bool* grow_hint)
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1068
{
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1069
  ScopedCS cs(thread, this);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1070
  VALUE* val = internal_get(thread, lookup_f, grow_hint);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1071
  return val != NULL ? *val : CONFIG::notfound();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1072
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1073
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1074
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1075
inline bool ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1076
  unsafe_insert(const VALUE& value) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1077
  bool dead_hash = false;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1078
  size_t hash = CONFIG::get_hash(value, &dead_hash);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1079
  if (dead_hash) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1080
    return false;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1081
  }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1082
  // This is an unsafe operation.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1083
  InternalTable* table = get_table();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1084
  Bucket* bucket = get_bucket_in(table, hash);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1085
  assert(!bucket->have_redirect() && !bucket->is_locked(), "bad");
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1086
  Node* new_node = Node::create_node(value, bucket->first());
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1087
  if (!bucket->cas_first(new_node, bucket->first())) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1088
    assert(false, "bad");
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1089
  }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1090
  return true;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1091
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1092
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1093
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1094
template <typename SCAN_FUNC>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1095
inline bool ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1096
  try_scan(Thread* thread, SCAN_FUNC& scan_f)
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1097
{
50445
bd6b78feb6a3 8195097: Make it possible to process StringTable outside safepoint
rehn
parents: 50429
diff changeset
  1098
  if (!try_resize_lock(thread)) {
50158
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1099
    return false;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1100
  }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1101
  do_scan_locked(thread, scan_f);
50445
bd6b78feb6a3 8195097: Make it possible to process StringTable outside safepoint
rehn
parents: 50429
diff changeset
  1102
  unlock_resize_lock(thread);
50158
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1103
  return true;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1104
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1105
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1106
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1107
template <typename SCAN_FUNC>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1108
inline void ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1109
  do_scan(Thread* thread, SCAN_FUNC& scan_f)
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1110
{
50445
bd6b78feb6a3 8195097: Make it possible to process StringTable outside safepoint
rehn
parents: 50429
diff changeset
  1111
  assert(_resize_lock_owner != thread, "Re-size lock held");
50158
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1112
  lock_resize_lock(thread);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1113
  do_scan_locked(thread, scan_f);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1114
  unlock_resize_lock(thread);
50445
bd6b78feb6a3 8195097: Make it possible to process StringTable outside safepoint
rehn
parents: 50429
diff changeset
  1115
  assert(_resize_lock_owner != thread, "Re-size lock held");
50158
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1116
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1117
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1118
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1119
template <typename EVALUATE_FUNC, typename DELETE_FUNC>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1120
inline bool ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1121
  try_bulk_delete(Thread* thread, EVALUATE_FUNC& eval_f, DELETE_FUNC& del_f)
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1122
{
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1123
  if (!try_resize_lock(thread)) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1124
    return false;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1125
  }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1126
  do_bulk_delete_locked(thread, eval_f, del_f);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1127
  unlock_resize_lock(thread);
50445
bd6b78feb6a3 8195097: Make it possible to process StringTable outside safepoint
rehn
parents: 50429
diff changeset
  1128
  assert(_resize_lock_owner != thread, "Re-size lock held");
50158
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1129
  return true;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1130
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1131
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1132
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1133
template <typename EVALUATE_FUNC, typename DELETE_FUNC>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1134
inline void ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1135
  bulk_delete(Thread* thread, EVALUATE_FUNC& eval_f, DELETE_FUNC& del_f)
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1136
{
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1137
  lock_resize_lock(thread);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1138
  do_bulk_delete_locked(thread, eval_f, del_f);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1139
  unlock_resize_lock(thread);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1140
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1141
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1142
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1143
template <typename VALUE_SIZE_FUNC>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1144
inline void ConcurrentHashTable<VALUE, CONFIG, F>::
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1145
  statistics_to(Thread* thread, VALUE_SIZE_FUNC& vs_f,
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1146
                outputStream* st, const char* table_name)
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1147
{
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1148
  NumberSeq summary;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1149
  size_t literal_bytes = 0;
50445
bd6b78feb6a3 8195097: Make it possible to process StringTable outside safepoint
rehn
parents: 50429
diff changeset
  1150
  if (!try_resize_lock(thread)) {
50158
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1151
    st->print_cr("statistics unavailable at this moment");
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1152
    return;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1153
  }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1154
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1155
  InternalTable* table = get_table();
50445
bd6b78feb6a3 8195097: Make it possible to process StringTable outside safepoint
rehn
parents: 50429
diff changeset
  1156
  for (size_t bucket_it = 0; bucket_it < table->_size; bucket_it++) {
50158
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1157
    ScopedCS cs(thread, this);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1158
    size_t count = 0;
50445
bd6b78feb6a3 8195097: Make it possible to process StringTable outside safepoint
rehn
parents: 50429
diff changeset
  1159
    Bucket* bucket = table->get_bucket(bucket_it);
50158
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1160
    if (bucket->have_redirect() || bucket->is_locked()) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1161
        continue;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1162
    }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1163
    Node* current_node = bucket->first();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1164
    while (current_node != NULL) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1165
      ++count;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1166
      literal_bytes += vs_f(current_node->value());
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1167
      current_node = current_node->next();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1168
    }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1169
    summary.add((double)count);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1170
  }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1171
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1172
  double num_buckets = summary.num();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1173
  double num_entries = summary.sum();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1174
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1175
  size_t bucket_bytes = num_buckets * sizeof(Bucket);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1176
  size_t entry_bytes  = num_entries * sizeof(Node);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1177
  size_t total_bytes = literal_bytes +  bucket_bytes + entry_bytes;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1178
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1179
  size_t bucket_size  = (num_buckets <= 0) ? 0 : (bucket_bytes  / num_buckets);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1180
  size_t entry_size   = (num_entries <= 0) ? 0 : (entry_bytes   / num_entries);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1181
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1182
  st->print_cr("%s statistics:", table_name);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1183
  st->print_cr("Number of buckets       : %9" PRIuPTR " = %9" PRIuPTR
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1184
               " bytes, each " SIZE_FORMAT,
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1185
               (size_t)num_buckets, bucket_bytes,  bucket_size);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1186
  st->print_cr("Number of entries       : %9" PRIuPTR " = %9" PRIuPTR
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1187
               " bytes, each " SIZE_FORMAT,
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1188
               (size_t)num_entries, entry_bytes,   entry_size);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1189
  if (literal_bytes != 0) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1190
    double literal_avg = (num_entries <= 0) ? 0 : (literal_bytes / num_entries);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1191
    st->print_cr("Number of literals      : %9" PRIuPTR " = %9" PRIuPTR
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1192
                 " bytes, avg %7.3f",
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1193
                 (size_t)num_entries, literal_bytes, literal_avg);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1194
  }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1195
  st->print_cr("Total footprsize_t         : %9s = %9" PRIuPTR " bytes", ""
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1196
               , total_bytes);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1197
  st->print_cr("Average bucket size     : %9.3f", summary.avg());
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1198
  st->print_cr("Variance of bucket size : %9.3f", summary.variance());
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1199
  st->print_cr("Std. dev. of bucket size: %9.3f", summary.sd());
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1200
  st->print_cr("Maximum bucket size     : %9" PRIuPTR,
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1201
               (size_t)summary.maximum());
50445
bd6b78feb6a3 8195097: Make it possible to process StringTable outside safepoint
rehn
parents: 50429
diff changeset
  1202
  unlock_resize_lock(thread);
bd6b78feb6a3 8195097: Make it possible to process StringTable outside safepoint
rehn
parents: 50429
diff changeset
  1203
}
bd6b78feb6a3 8195097: Make it possible to process StringTable outside safepoint
rehn
parents: 50429
diff changeset
  1204
bd6b78feb6a3 8195097: Make it possible to process StringTable outside safepoint
rehn
parents: 50429
diff changeset
  1205
template <typename VALUE, typename CONFIG, MEMFLAGS F>
bd6b78feb6a3 8195097: Make it possible to process StringTable outside safepoint
rehn
parents: 50429
diff changeset
  1206
inline bool ConcurrentHashTable<VALUE, CONFIG, F>::
bd6b78feb6a3 8195097: Make it possible to process StringTable outside safepoint
rehn
parents: 50429
diff changeset
  1207
  try_move_nodes_to(Thread* thread, ConcurrentHashTable<VALUE, CONFIG, F>* to_cht)
bd6b78feb6a3 8195097: Make it possible to process StringTable outside safepoint
rehn
parents: 50429
diff changeset
  1208
{
bd6b78feb6a3 8195097: Make it possible to process StringTable outside safepoint
rehn
parents: 50429
diff changeset
  1209
  if (!try_resize_lock(thread)) {
bd6b78feb6a3 8195097: Make it possible to process StringTable outside safepoint
rehn
parents: 50429
diff changeset
  1210
    return false;
50158
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1211
  }
50637
359607017fb7 8205006: Assertion failure when rehashing stringtable
rehn
parents: 50608
diff changeset
  1212
  assert(_new_table == NULL || _new_table == POISON_PTR, "Must be NULL");
50445
bd6b78feb6a3 8195097: Make it possible to process StringTable outside safepoint
rehn
parents: 50429
diff changeset
  1213
  for (size_t bucket_it = 0; bucket_it < _table->_size; bucket_it++) {
bd6b78feb6a3 8195097: Make it possible to process StringTable outside safepoint
rehn
parents: 50429
diff changeset
  1214
    Bucket* bucket = _table->get_bucket(bucket_it);
bd6b78feb6a3 8195097: Make it possible to process StringTable outside safepoint
rehn
parents: 50429
diff changeset
  1215
    assert(!bucket->have_redirect() && !bucket->is_locked(), "Table must be uncontended");
bd6b78feb6a3 8195097: Make it possible to process StringTable outside safepoint
rehn
parents: 50429
diff changeset
  1216
    while (bucket->first() != NULL) {
bd6b78feb6a3 8195097: Make it possible to process StringTable outside safepoint
rehn
parents: 50429
diff changeset
  1217
      Node* move_node = bucket->first();
bd6b78feb6a3 8195097: Make it possible to process StringTable outside safepoint
rehn
parents: 50429
diff changeset
  1218
      bool ok = bucket->cas_first(move_node->next(), move_node);
bd6b78feb6a3 8195097: Make it possible to process StringTable outside safepoint
rehn
parents: 50429
diff changeset
  1219
      assert(ok, "Uncontended cas must work");
bd6b78feb6a3 8195097: Make it possible to process StringTable outside safepoint
rehn
parents: 50429
diff changeset
  1220
      bool dead_hash = false;
bd6b78feb6a3 8195097: Make it possible to process StringTable outside safepoint
rehn
parents: 50429
diff changeset
  1221
      size_t insert_hash = CONFIG::get_hash(*move_node->value(), &dead_hash);
bd6b78feb6a3 8195097: Make it possible to process StringTable outside safepoint
rehn
parents: 50429
diff changeset
  1222
      if (!dead_hash) {
bd6b78feb6a3 8195097: Make it possible to process StringTable outside safepoint
rehn
parents: 50429
diff changeset
  1223
        Bucket* insert_bucket = to_cht->get_bucket(insert_hash);
bd6b78feb6a3 8195097: Make it possible to process StringTable outside safepoint
rehn
parents: 50429
diff changeset
  1224
        assert(!bucket->have_redirect() && !bucket->is_locked(), "Not bit should be present");
bd6b78feb6a3 8195097: Make it possible to process StringTable outside safepoint
rehn
parents: 50429
diff changeset
  1225
        move_node->set_next(insert_bucket->first());
bd6b78feb6a3 8195097: Make it possible to process StringTable outside safepoint
rehn
parents: 50429
diff changeset
  1226
        ok = insert_bucket->cas_first(move_node, insert_bucket->first());
bd6b78feb6a3 8195097: Make it possible to process StringTable outside safepoint
rehn
parents: 50429
diff changeset
  1227
        assert(ok, "Uncontended cas must work");
bd6b78feb6a3 8195097: Make it possible to process StringTable outside safepoint
rehn
parents: 50429
diff changeset
  1228
      }
bd6b78feb6a3 8195097: Make it possible to process StringTable outside safepoint
rehn
parents: 50429
diff changeset
  1229
    }
bd6b78feb6a3 8195097: Make it possible to process StringTable outside safepoint
rehn
parents: 50429
diff changeset
  1230
  }
bd6b78feb6a3 8195097: Make it possible to process StringTable outside safepoint
rehn
parents: 50429
diff changeset
  1231
  unlock_resize_lock(thread);
bd6b78feb6a3 8195097: Make it possible to process StringTable outside safepoint
rehn
parents: 50429
diff changeset
  1232
  return true;
50158
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1233
}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1234
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
  1235
#endif // include guard