src/hotspot/share/utilities/concurrentHashTable.hpp
author naoto
Tue, 09 Jul 2019 08:05:38 -0700
changeset 55627 9c1885fb2a42
parent 54764 865ec913f916
child 55478 ae2e53e379cb
permissions -rw-r--r--
8227127: Era designator not displayed correctly using the COMPAT provider Reviewed-by: rriggs
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
/*
53149
259c36ef27df 8215731: Move forward class definitions out of globalDefinitions.hpp
coleenp
parents: 52717
diff changeset
     2
 * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
50158
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
53244
9807daeb47c4 8216167: Update include guards to reflect correct directories
coleenp
parents: 53239
diff changeset
    25
#ifndef SHARE_UTILITIES_CONCURRENTHASHTABLE_HPP
9807daeb47c4 8216167: Update include guards to reflect correct directories
coleenp
parents: 53239
diff changeset
    26
#define SHARE_UTILITIES_CONCURRENTHASHTABLE_HPP
50158
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    27
52332
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents: 51405
diff changeset
    28
#include "memory/allocation.hpp"
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents: 51405
diff changeset
    29
#include "utilities/globalCounter.hpp"
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents: 51405
diff changeset
    30
#include "utilities/globalDefinitions.hpp"
54764
865ec913f916 8185525: Add JFR event for DictionarySizes
gziemski
parents: 53555
diff changeset
    31
#include "utilities/tableStatistics.hpp"
52332
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents: 51405
diff changeset
    32
50158
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    33
// A mostly concurrent-hash-table where the read-side is wait-free, inserts are
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    34
// CAS and deletes mutual exclude each other on per bucket-basis. VALUE is the
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    35
// type kept inside each Node and CONFIG contains hash and allocation methods.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    36
// A CALLBACK_FUNC and LOOKUP_FUNC needs to be provided for get and insert.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    37
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    38
class Thread;
53149
259c36ef27df 8215731: Move forward class definitions out of globalDefinitions.hpp
coleenp
parents: 52717
diff changeset
    39
class Mutex;
50158
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    40
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    41
template <typename VALUE, typename CONFIG, MEMFLAGS F>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    42
class ConcurrentHashTable : public CHeapObj<F> {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    43
 private:
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    44
  // This is the internal node structure.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    45
  // Only constructed with placement new from memory allocated with MEMFLAGS of
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    46
  // the InternalTable or user-defined memory.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    47
  class Node {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    48
   private:
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    49
    Node * volatile _next;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    50
    VALUE _value;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    51
   public:
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    52
    Node(const VALUE& value, Node* next = NULL)
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    53
      : _next(next), _value(value) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    54
      assert((((uintptr_t)this) & ((uintptr_t)0x3)) == 0,
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    55
             "Must 16 bit aligned.");
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    56
    }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    57
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    58
    Node* next() const;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    59
    void set_next(Node* node)         { _next = node; }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    60
    Node* const volatile * next_ptr() { return &_next; }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    61
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    62
    VALUE* value()                    { return &_value; }
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
    // Creates a node.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    65
    static Node* create_node(const VALUE& value, Node* next = NULL) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    66
      return new (CONFIG::allocate_node(sizeof(Node), value)) Node(value, next);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    67
    }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    68
    // Destroys a node.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    69
    static void destroy_node(Node* node) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    70
      CONFIG::free_node((void*)node, node->_value);
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
    void print_on(outputStream* st) const {};
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    74
    void print_value_on(outputStream* st) const {};
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    75
  };
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    76
53344
cfc839f28b89 8216426: Usage of array placement new may lead to memory corruption
mdoerr
parents: 53244
diff changeset
    77
  // Only constructed with placement new from an array allocated with MEMFLAGS
50158
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    78
  // of InternalTable.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    79
  class Bucket {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    80
   private:
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    81
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    82
    // Embedded state in two low bits in first pointer is a spinlock with 3
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    83
    // states, unlocked, locked, redirect. You must never busy-spin on trylock()
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    84
    // or call lock() without _resize_lock, that would deadlock. Redirect can
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    85
    // only be installed by owner and is the final state of a bucket.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    86
    // The only two valid flows are:
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    87
    // unlocked -> locked -> unlocked
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    88
    // unlocked -> locked -> redirect
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    89
    // Locked state only applies to an updater.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    90
    // Reader only check for redirect.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    91
    Node * volatile _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
    static const uintptr_t STATE_LOCK_BIT     = 0x1;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    94
    static const uintptr_t STATE_REDIRECT_BIT = 0x2;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    95
    static const uintptr_t STATE_MASK         = 0x3;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    96
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    97
    // Get the first pointer unmasked.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
    98
    Node* first_raw() const;
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
    // Methods to manipulate the embedded.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   101
    static bool is_state(Node* node, uintptr_t bits) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   102
      return (bits & (uintptr_t)node) == bits;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   103
    }
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
    static Node* set_state(Node* n, uintptr_t bits) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   106
      return (Node*)(bits | (uintptr_t)n);
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
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   109
    static uintptr_t get_state(Node* node) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   110
      return (((uintptr_t)node) & STATE_MASK);
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
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   113
    static Node* clear_state(Node* node) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   114
      return (Node*)(((uintptr_t)node) & (~(STATE_MASK)));
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   115
    }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   116
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   117
    static Node* clear_set_state(Node* node, Node* state) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   118
      return (Node*)(((uintptr_t)clear_state(node)) ^ get_state(state));
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   119
    }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   120
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   121
   public:
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   122
    // A bucket is only one pointer with the embedded state.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   123
    Bucket() : _first(NULL) {};
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
    // Get the first pointer unmasked.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   126
    Node* first() const;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   127
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   128
    // Get a pointer to the const first pointer. Do not deference this
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   129
    // pointer, the pointer pointed to _may_ contain an embedded state. Such
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   130
    // pointer should only be used as input to release_assign_node_ptr.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   131
    Node* const volatile * first_ptr() { return &_first; }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   132
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   133
    // This is the only place where a pointer to a Node pointer that potentially
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   134
    // is _first should be changed. Otherwise we destroy the embedded state. We
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   135
    // only give out pointer to const Node pointer to avoid accidental
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   136
    // assignment, thus here we must cast const part away. Method is not static
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   137
    // due to an assert.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   138
    void release_assign_node_ptr(Node* const volatile * dst, Node* node) const;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   139
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   140
    // This method assigns this buckets last Node next ptr to input Node.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   141
    void release_assign_last_node_next(Node* node);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   142
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   143
    // Setting the first pointer must be done with CAS.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   144
    bool cas_first(Node *node, Node* expect);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   145
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   146
    // Returns true if this bucket is redirecting to a new table.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   147
    // Redirect is a terminal state and will never change.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   148
    bool have_redirect() const;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   149
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   150
    // Return true if this bucket is locked for updates.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   151
    bool is_locked() const;
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
    // Return true if this bucket was locked.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   154
    bool trylock();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   155
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   156
    // The bucket might be invalid, due to a concurrent resize. The lock()
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   157
    // method do no respect that and can deadlock if caller do not hold
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   158
    // _resize_lock.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   159
    void lock();
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
    // Unlocks this bucket.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   162
    void unlock();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   163
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   164
    // Installs redirect in this bucket.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   165
    // Prior to doing so you must have successfully locked this bucket.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   166
    void redirect();
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
  // The backing storage table holding the buckets and it's size and mask-bits.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   170
  // Table is always a power of two for two reasons:
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   171
  // - Re-size can only change the size into half or double
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   172
  //   (any pow 2 would also be possible).
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   173
  // - Use masking of hash for bucket index.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   174
  class InternalTable : public CHeapObj<F> {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   175
   private:
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   176
    Bucket* _buckets;        // Bucket array.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   177
   public:
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   178
    const size_t _log2_size; // Size in log2.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   179
    const size_t _size;      // Size in log10.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   180
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   181
    // The mask used on hash for selecting bucket.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   182
    // The masked value is guaranteed be to inside the buckets array.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   183
    const size_t _hash_mask;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   184
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   185
    // Create a backing table
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   186
    InternalTable(size_t log2_size);
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
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   189
    Bucket* get_buckets() { return _buckets; }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   190
    Bucket* get_bucket(size_t idx) { return &_buckets[idx]; }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   191
  };
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   192
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   193
  // Used as default functor when no functor supplied for some methods.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   194
  struct NoOp {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   195
    void operator()(VALUE*) {}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   196
    const VALUE& operator()() {}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   197
    void operator()(bool, VALUE*) {}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   198
  } noOp;
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
  // For materializing a supplied value.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   201
  class LazyValueRetrieve {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   202
   private:
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   203
    const VALUE& _val;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   204
   public:
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   205
    LazyValueRetrieve(const VALUE& val) : _val(val) {}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   206
    const VALUE& operator()() { return _val; }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   207
  };
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   208
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   209
  InternalTable* _table;      // Active table.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   210
  InternalTable* _new_table;  // Table we are resizing to.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   211
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   212
  // Default sizes
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   213
  static const size_t DEFAULT_MAX_SIZE_LOG2 = 21;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   214
  static const size_t DEFAULT_START_SIZE_LOG2 = 13;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   215
  static const size_t DEFAULT_GROW_HINT = 4; // Chain length
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   216
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   217
  const size_t _log2_size_limit;  // The biggest size.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   218
  const size_t _log2_start_size;  // Start size.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   219
  const size_t _grow_hint;        // Number of linked items
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   220
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   221
  volatile bool _size_limit_reached;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   222
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   223
  // We serialize resizers and other bulk operations which do not support
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   224
  // concurrent resize with this lock.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   225
  Mutex* _resize_lock;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   226
  // Since we need to drop mutex for safepoints, but stop other threads from
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   227
  // taking the mutex after a safepoint this bool is the actual state. After
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   228
  // acquiring the mutex you must check if this is already locked. If so you
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   229
  // must drop the mutex until the real lock holder grabs the mutex.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   230
  volatile Thread* _resize_lock_owner;
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 true if lock mutex/state succeeded.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   233
  bool try_resize_lock(Thread* locker);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   234
  // Returns when both mutex and state are proper locked.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   235
  void lock_resize_lock(Thread* locker);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   236
  // Unlocks mutex and state.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   237
  void unlock_resize_lock(Thread* locker);
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
  // This method sets the _invisible_epoch and do a write_synchronize.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   240
  // Subsequent calls check the state of _invisible_epoch and determine if the
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   241
  // write_synchronize can be avoided. If not, it sets the _invisible_epoch
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   242
  // again and do a write_synchronize.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   243
  void write_synchonize_on_visible_epoch(Thread* thread);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   244
  // To be-able to avoid write_synchronize in resize and other bulk operation,
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   245
  // this field keep tracks if a version of the hash-table was ever been seen.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   246
  // We the working thread pointer as tag for debugging. The _invisible_epoch
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   247
  // can only be used by the owner of _resize_lock.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   248
  volatile Thread* _invisible_epoch;
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
  // Scoped critical section, which also handles the invisible epochs.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   251
  // An invisible epoch/version do not need a write_synchronize().
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   252
  class ScopedCS: public StackObj {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   253
   protected:
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   254
    Thread* _thread;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   255
    ConcurrentHashTable<VALUE, CONFIG, F>* _cht;
52332
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents: 51405
diff changeset
   256
    GlobalCounter::CSContext _cs_context;
50158
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   257
   public:
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   258
    ScopedCS(Thread* thread, ConcurrentHashTable<VALUE, CONFIG, F>* cht);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   259
    ~ScopedCS();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   260
  };
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   261
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   262
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   263
  // Max number of deletes in one bucket chain during bulk delete.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   264
  static const size_t BULK_DELETE_LIMIT = 256;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   265
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   266
  // Simple getters and setters for the internal table.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   267
  InternalTable* get_table() const;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   268
  InternalTable* get_new_table() const;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   269
  InternalTable* set_table_from_new();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   270
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   271
  // Destroys all nodes.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   272
  void free_nodes();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   273
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   274
  // Mask away high bits of hash.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   275
  static size_t bucket_idx_hash(InternalTable* table, const uintx hash) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   276
    return ((size_t)hash) & table->_hash_mask;
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
  // Returns bucket for hash for that internal table.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   280
  Bucket* get_bucket_in(InternalTable* table, const uintx hash) const {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   281
    size_t bucket_index = bucket_idx_hash(table, hash);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   282
    return table->get_bucket(bucket_index);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   283
  }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   284
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   285
  // Return correct bucket for reading and handles resizing.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   286
  Bucket* get_bucket(const uintx hash) const;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   287
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   288
  // Return correct bucket for updates and handles resizing.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   289
  Bucket* get_bucket_locked(Thread* thread, const uintx hash);
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
  // Finds a node.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   292
  template <typename LOOKUP_FUNC>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   293
  Node* get_node(const Bucket* const bucket, LOOKUP_FUNC& lookup_f,
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   294
                 bool* have_dead, size_t* loops = NULL) const;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   295
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   296
  // Method for shrinking.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   297
  bool internal_shrink_prolog(Thread* thread, size_t log2_size);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   298
  void internal_shrink_epilog(Thread* thread);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   299
  void internal_shrink_range(Thread* thread, size_t start, size_t stop);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   300
  bool internal_shrink(Thread* thread, size_t size_limit_log2);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   301
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   302
  // Methods for growing.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   303
  bool unzip_bucket(Thread* thread, InternalTable* old_table,
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   304
                    InternalTable* new_table, size_t even_index,
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   305
                    size_t odd_index);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   306
  bool internal_grow_prolog(Thread* thread, size_t log2_size);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   307
  void internal_grow_epilog(Thread* thread);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   308
  void internal_grow_range(Thread* thread, size_t start, size_t stop);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   309
  bool internal_grow(Thread* thread, size_t log2_size);
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
  // Get a value.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   312
  template <typename LOOKUP_FUNC>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   313
  VALUE* internal_get(Thread* thread, LOOKUP_FUNC& lookup_f,
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   314
                      bool* grow_hint = NULL);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   315
52717
b22da519f2e3 8213791: StringTable: Use get and insert
rehn
parents: 52516
diff changeset
   316
  // Plain insert.
b22da519f2e3 8213791: StringTable: Use get and insert
rehn
parents: 52516
diff changeset
   317
  template <typename LOOKUP_FUNC>
b22da519f2e3 8213791: StringTable: Use get and insert
rehn
parents: 52516
diff changeset
   318
  bool internal_insert(Thread* thread, LOOKUP_FUNC& lookup_f, const VALUE& value,
b22da519f2e3 8213791: StringTable: Use get and insert
rehn
parents: 52516
diff changeset
   319
                       bool* grow_hint, bool* clean_hint);
50158
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   320
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   321
  // Returns true if an item matching LOOKUP_FUNC is removed.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   322
  // Calls DELETE_FUNC before destroying the node.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   323
  template <typename LOOKUP_FUNC, typename DELETE_FUNC>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   324
  bool internal_remove(Thread* thread, LOOKUP_FUNC& lookup_f,
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   325
                       DELETE_FUNC& delete_f);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   326
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   327
  // Visits nodes with FUNC.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   328
  template <typename FUNC>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   329
  static bool visit_nodes(Bucket* bucket, FUNC& visitor_f);
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
  // During shrink/grow we cannot guarantee that we only visit nodes once, with
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   332
  // current algorithm. To keep it simple caller will have locked
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   333
  // _resize_lock.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   334
  template <typename FUNC>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   335
  void do_scan_locked(Thread* thread, FUNC& scan_f);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   336
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   337
  // Check for dead items in a bucket.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   338
  template <typename EVALUATE_FUNC>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   339
  size_t delete_check_nodes(Bucket* bucket, EVALUATE_FUNC& eval_f,
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   340
                            size_t num_del, Node** ndel);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   341
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   342
  // Check for dead items in this table. During shrink/grow we cannot guarantee
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   343
  // that we only visit nodes once. To keep it simple caller will have locked
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   344
  // _resize_lock.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   345
  template <typename EVALUATE_FUNC, typename DELETE_FUNC>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   346
  void do_bulk_delete_locked(Thread* thread, EVALUATE_FUNC& eval_f
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   347
                             , DELETE_FUNC& del_f) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   348
    do_bulk_delete_locked_for(thread, 0, _table->_size, eval_f, del_f);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   349
  }
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
  // To have prefetching for a VALUE that is pointer during
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   352
  // do_bulk_delete_locked, we have this helper classes. One for non-pointer
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   353
  // case without prefect and one for pointer with prefect.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   354
  template <bool b, typename EVALUATE_FUNC>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   355
  struct HaveDeletables {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   356
    static bool have_deletable(Bucket* bucket, EVALUATE_FUNC& eval_f,
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   357
                               Bucket* prefetch_bucket);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   358
  };
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   359
  template<typename EVALUATE_FUNC>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   360
  struct HaveDeletables<true, EVALUATE_FUNC> {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   361
    static bool have_deletable(Bucket* bucket, EVALUATE_FUNC& eval_f,
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   362
                               Bucket* prefetch_bucket);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   363
  };
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
  // Check for dead items in this table with range. During shrink/grow we cannot
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   366
  // guarantee that we only visit nodes once. To keep it simple caller will
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   367
  // have locked _resize_lock.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   368
  template <typename EVALUATE_FUNC, typename DELETE_FUNC>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   369
  void do_bulk_delete_locked_for(Thread* thread, size_t start_idx,
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   370
                                 size_t stop_idx, EVALUATE_FUNC& eval_f,
50608
1609a43e77ae 8204857: ConcurrentHashTable: Fix parallel processing
rehn
parents: 50445
diff changeset
   371
                                 DELETE_FUNC& del_f, bool is_mt = false);
50158
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   372
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   373
  // Method to delete one items.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   374
  template <typename LOOKUP_FUNC>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   375
  void delete_in_bucket(Thread* thread, Bucket* bucket, LOOKUP_FUNC& lookup_f);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   376
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   377
 public:
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   378
  ConcurrentHashTable(size_t log2size = DEFAULT_START_SIZE_LOG2,
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   379
                      size_t log2size_limit = DEFAULT_MAX_SIZE_LOG2,
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   380
                      size_t grow_hint = DEFAULT_GROW_HINT);
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
  ~ConcurrentHashTable();
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   383
54764
865ec913f916 8185525: Add JFR event for DictionarySizes
gziemski
parents: 53555
diff changeset
   384
  TableRateStatistics _stats_rate;
865ec913f916 8185525: Add JFR event for DictionarySizes
gziemski
parents: 53555
diff changeset
   385
50158
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   386
  size_t get_size_log2(Thread* thread);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   387
  size_t get_node_size() const { return sizeof(Node); }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   388
  bool is_max_size_reached() { return _size_limit_reached; }
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
  // This means no paused bucket resize operation is going to resume
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   391
  // on this table.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   392
  bool is_safepoint_safe() { return _resize_lock_owner == NULL; }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   393
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   394
  // Re-size operations.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   395
  bool shrink(Thread* thread, size_t size_limit_log2 = 0);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   396
  bool grow(Thread* thread, size_t size_limit_log2 = 0);
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
  // All callbacks for get are under critical sections. Other callbacks may be
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   399
  // under critical section or may have locked parts of table. Calling any
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   400
  // methods on the table during a callback is not supported.Only MultiGetHandle
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   401
  // supports multiple gets.
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
  // Get methods return true on found item with LOOKUP_FUNC and FOUND_FUNC is
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   404
  // called.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   405
  template <typename LOOKUP_FUNC, typename FOUND_FUNC>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   406
  bool get(Thread* thread, LOOKUP_FUNC& lookup_f, FOUND_FUNC& foundf,
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   407
           bool* grow_hint = NULL);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   408
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   409
  // Returns true true if the item was inserted, duplicates are found with
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   410
  // LOOKUP_FUNC.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   411
  template <typename LOOKUP_FUNC>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   412
  bool insert(Thread* thread, LOOKUP_FUNC& lookup_f, const VALUE& value,
51405
8b23aa7cef47 8195100: Use a low latency hashtable for SymbolTable
gziemski
parents: 50608
diff changeset
   413
              bool* grow_hint = NULL, bool* clean_hint = NULL) {
52717
b22da519f2e3 8213791: StringTable: Use get and insert
rehn
parents: 52516
diff changeset
   414
    return internal_insert(thread, lookup_f, value, grow_hint, clean_hint);
50158
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   415
  }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   416
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   417
  // This does a fast unsafe insert and can thus only be used when there is no
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   418
  // risk for a duplicates and no other threads uses this table.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   419
  bool unsafe_insert(const VALUE& value);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   420
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   421
  // Returns true if items was deleted matching LOOKUP_FUNC and
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   422
  // prior to destruction DELETE_FUNC is called.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   423
  template <typename LOOKUP_FUNC, typename DELETE_FUNC>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   424
  bool remove(Thread* thread, LOOKUP_FUNC& lookup_f, DELETE_FUNC& del_f) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   425
    return internal_remove(thread, lookup_f, del_f);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   426
  }
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
  // Same without DELETE_FUNC.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   429
  template <typename LOOKUP_FUNC>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   430
  bool remove(Thread* thread, LOOKUP_FUNC& lookup_f) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   431
    return internal_remove(thread, lookup_f, noOp);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   432
  }
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   433
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   434
  // Visit all items with SCAN_FUNC if no concurrent resize. Takes the resize
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   435
  // lock to avoid concurrent resizes. Else returns false.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   436
  template <typename SCAN_FUNC>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   437
  bool try_scan(Thread* thread, SCAN_FUNC& scan_f);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   438
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   439
  // Visit all items with SCAN_FUNC when the resize lock is obtained.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   440
  template <typename SCAN_FUNC>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   441
  void do_scan(Thread* thread, SCAN_FUNC& scan_f);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   442
52516
d5eebe1e03fe 8213574: Deadlock in string table expansion when dumping lots of CDS classes
rehn
parents: 52332
diff changeset
   443
  // Visit all items with SCAN_FUNC without any protection.
d5eebe1e03fe 8213574: Deadlock in string table expansion when dumping lots of CDS classes
rehn
parents: 52332
diff changeset
   444
  // It will assume there is no other thread accessing this
d5eebe1e03fe 8213574: Deadlock in string table expansion when dumping lots of CDS classes
rehn
parents: 52332
diff changeset
   445
  // table during the safepoint. Must be called with VM thread.
d5eebe1e03fe 8213574: Deadlock in string table expansion when dumping lots of CDS classes
rehn
parents: 52332
diff changeset
   446
  template <typename SCAN_FUNC>
d5eebe1e03fe 8213574: Deadlock in string table expansion when dumping lots of CDS classes
rehn
parents: 52332
diff changeset
   447
  void do_safepoint_scan(SCAN_FUNC& scan_f);
d5eebe1e03fe 8213574: Deadlock in string table expansion when dumping lots of CDS classes
rehn
parents: 52332
diff changeset
   448
50158
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   449
  // Destroying items matching EVALUATE_FUNC, before destroying items
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   450
  // DELETE_FUNC is called, if resize lock is obtained. Else returns false.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   451
  template <typename EVALUATE_FUNC, typename DELETE_FUNC>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   452
  bool try_bulk_delete(Thread* thread, EVALUATE_FUNC& eval_f,
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   453
                       DELETE_FUNC& del_f);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   454
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   455
  // Destroying items matching EVALUATE_FUNC, before destroying items
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   456
  // DELETE_FUNC is called, when the resize lock is successfully obtained.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   457
  template <typename EVALUATE_FUNC, typename DELETE_FUNC>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   458
  void 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
   459
54764
865ec913f916 8185525: Add JFR event for DictionarySizes
gziemski
parents: 53555
diff changeset
   460
  // Calcuate statistics. Item sizes are calculated with VALUE_SIZE_FUNC.
865ec913f916 8185525: Add JFR event for DictionarySizes
gziemski
parents: 53555
diff changeset
   461
  template <typename VALUE_SIZE_FUNC>
865ec913f916 8185525: Add JFR event for DictionarySizes
gziemski
parents: 53555
diff changeset
   462
  TableStatistics statistics_calculate(Thread* thread, VALUE_SIZE_FUNC& vs_f);
865ec913f916 8185525: Add JFR event for DictionarySizes
gziemski
parents: 53555
diff changeset
   463
865ec913f916 8185525: Add JFR event for DictionarySizes
gziemski
parents: 53555
diff changeset
   464
  // Gets statistics if available, if not return old one. Item sizes are calculated with
865ec913f916 8185525: Add JFR event for DictionarySizes
gziemski
parents: 53555
diff changeset
   465
  // VALUE_SIZE_FUNC.
865ec913f916 8185525: Add JFR event for DictionarySizes
gziemski
parents: 53555
diff changeset
   466
  template <typename VALUE_SIZE_FUNC>
865ec913f916 8185525: Add JFR event for DictionarySizes
gziemski
parents: 53555
diff changeset
   467
  TableStatistics statistics_get(Thread* thread, VALUE_SIZE_FUNC& vs_f, TableStatistics old);
865ec913f916 8185525: Add JFR event for DictionarySizes
gziemski
parents: 53555
diff changeset
   468
50158
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   469
  // Writes statistics to the outputStream. Item sizes are calculated with
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   470
  // VALUE_SIZE_FUNC.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   471
  template <typename VALUE_SIZE_FUNC>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   472
  void statistics_to(Thread* thread, VALUE_SIZE_FUNC& vs_f, outputStream* st,
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   473
                     const char* table_name);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   474
50445
bd6b78feb6a3 8195097: Make it possible to process StringTable outside safepoint
rehn
parents: 50158
diff changeset
   475
  // Moves all nodes from this table to to_cht
bd6b78feb6a3 8195097: Make it possible to process StringTable outside safepoint
rehn
parents: 50158
diff changeset
   476
  bool try_move_nodes_to(Thread* thread, ConcurrentHashTable<VALUE, CONFIG, F>* to_cht);
bd6b78feb6a3 8195097: Make it possible to process StringTable outside safepoint
rehn
parents: 50158
diff changeset
   477
50158
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   478
  // This is a Curiously Recurring Template Pattern (CRPT) interface for the
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   479
  // specialization.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   480
  struct BaseConfig {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   481
   public:
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   482
    // Called when the hash table needs the hash for a VALUE.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   483
    static uintx get_hash(const VALUE& value, bool* dead) {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   484
      return CONFIG::get_hash(value, dead);
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
    // Default node allocation.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   487
    static void* allocate_node(size_t size, const VALUE& value);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   488
    // Default node reclamation.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   489
    static void free_node(void* memory, const VALUE& value);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   490
  };
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
  // Scoped multi getter.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   493
  class MultiGetHandle : private ScopedCS {
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   494
   public:
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   495
    MultiGetHandle(Thread* thread, ConcurrentHashTable<VALUE, CONFIG, F>* cht)
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   496
      : ScopedCS(thread, cht) {}
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   497
    // In the MultiGetHandle scope you can lookup items matching LOOKUP_FUNC.
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   498
    // The VALUEs are safe as long as you never save the VALUEs outside the
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   499
    // scope, e.g. after ~MultiGetHandle().
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   500
    template <typename LOOKUP_FUNC>
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   501
    VALUE* get(LOOKUP_FUNC& lookup_f, bool* grow_hint = NULL);
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   502
  };
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   503
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   504
 private:
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   505
  class BucketsOperation;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   506
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   507
 public:
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   508
  class BulkDeleteTask;
8e4fcfb4cfe4 8195098: Low latency hashtable for read-mostly scenarios
rehn
parents:
diff changeset
   509
  class GrowTask;
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
53244
9807daeb47c4 8216167: Update include guards to reflect correct directories
coleenp
parents: 53239
diff changeset
   512
#endif // SHARE_UTILITIES_CONCURRENTHASHTABLE_HPP