Path: blob/master/src/hotspot/share/utilities/concurrentHashTableTasks.inline.hpp
40949 views
/*1* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_UTILITIES_CONCURRENTHASHTABLETASKS_INLINE_HPP25#define SHARE_UTILITIES_CONCURRENTHASHTABLETASKS_INLINE_HPP2627// No concurrentHashTableTasks.hpp2829#include "runtime/atomic.hpp"30#include "utilities/globalDefinitions.hpp"31#include "utilities/concurrentHashTable.inline.hpp"3233// This inline file contains BulkDeleteTask and GrowTasks which are both bucket34// operations, which they are serialized with each other.3536// Base class for pause and/or parallel bulk operations.37template <typename CONFIG, MEMFLAGS F>38class ConcurrentHashTable<CONFIG, F>::BucketsOperation {39protected:40ConcurrentHashTable<CONFIG, F>* _cht;4142// Default size of _task_size_log243static const size_t DEFAULT_TASK_SIZE_LOG2 = 12;4445// The table is split into ranges, every increment is one range.46volatile size_t _next_to_claim;47size_t _task_size_log2; // Number of buckets.48size_t _stop_task; // Last task49size_t _size_log2; // Table size.50bool _is_mt;5152BucketsOperation(ConcurrentHashTable<CONFIG, F>* cht, bool is_mt = false)53: _cht(cht), _next_to_claim(0), _task_size_log2(DEFAULT_TASK_SIZE_LOG2),54_stop_task(0), _size_log2(0), _is_mt(is_mt) {}5556// Returns true if you succeeded to claim the range start -> (stop-1).57bool claim(size_t* start, size_t* stop) {58size_t claimed = Atomic::fetch_and_add(&_next_to_claim, 1u);59if (claimed >= _stop_task) {60return false;61}62*start = claimed * (((size_t)1) << _task_size_log2);63*stop = ((*start) + (((size_t)1) << _task_size_log2));64return true;65}6667// Calculate starting values.68void setup(Thread* thread) {69thread_owns_resize_lock(thread);70_size_log2 = _cht->_table->_log2_size;71_task_size_log2 = MIN2(_task_size_log2, _size_log2);72size_t tmp = _size_log2 > _task_size_log2 ?73_size_log2 - _task_size_log2 : 0;74_stop_task = (((size_t)1) << tmp);75}7677// Returns false if all ranges are claimed.78bool have_more_work() {79return Atomic::load_acquire(&_next_to_claim) >= _stop_task;80}8182void thread_owns_resize_lock(Thread* thread) {83assert(BucketsOperation::_cht->_resize_lock_owner == thread,84"Should be locked by me");85assert(BucketsOperation::_cht->_resize_lock->owned_by_self(),86"Operations lock not held");87}88void thread_owns_only_state_lock(Thread* thread) {89assert(BucketsOperation::_cht->_resize_lock_owner == thread,90"Should be locked by me");91assert(!BucketsOperation::_cht->_resize_lock->owned_by_self(),92"Operations lock held");93}94void thread_do_not_own_resize_lock(Thread* thread) {95assert(!BucketsOperation::_cht->_resize_lock->owned_by_self(),96"Operations lock held");97assert(BucketsOperation::_cht->_resize_lock_owner != thread,98"Should not be locked by me");99}100101public:102// Pauses for safepoint103void pause(Thread* thread) {104// This leaves internal state locked.105this->thread_owns_resize_lock(thread);106BucketsOperation::_cht->_resize_lock->unlock();107this->thread_owns_only_state_lock(thread);108}109110// Continues after safepoint.111void cont(Thread* thread) {112this->thread_owns_only_state_lock(thread);113// If someone slips in here directly after safepoint.114while (!BucketsOperation::_cht->_resize_lock->try_lock())115{ /* for ever */ };116this->thread_owns_resize_lock(thread);117}118};119120// For doing pausable/parallel bulk delete.121template <typename CONFIG, MEMFLAGS F>122class ConcurrentHashTable<CONFIG, F>::BulkDeleteTask :123public BucketsOperation124{125public:126BulkDeleteTask(ConcurrentHashTable<CONFIG, F>* cht, bool is_mt = false)127: BucketsOperation(cht, is_mt) {128}129// Before start prepare must be called.130bool prepare(Thread* thread) {131bool lock = BucketsOperation::_cht->try_resize_lock(thread);132if (!lock) {133return false;134}135this->setup(thread);136return true;137}138139// Does one range destroying all matching EVALUATE_FUNC and140// DELETE_FUNC is called be destruction. Returns true if there is more work.141template <typename EVALUATE_FUNC, typename DELETE_FUNC>142bool do_task(Thread* thread, EVALUATE_FUNC& eval_f, DELETE_FUNC& del_f) {143size_t start, stop;144assert(BucketsOperation::_cht->_resize_lock_owner != NULL,145"Should be locked");146if (!this->claim(&start, &stop)) {147return false;148}149BucketsOperation::_cht->do_bulk_delete_locked_for(thread, start, stop,150eval_f, del_f,151BucketsOperation::_is_mt);152assert(BucketsOperation::_cht->_resize_lock_owner != NULL,153"Should be locked");154return true;155}156157// Must be called after ranges are done.158void done(Thread* thread) {159this->thread_owns_resize_lock(thread);160BucketsOperation::_cht->unlock_resize_lock(thread);161this->thread_do_not_own_resize_lock(thread);162}163};164165template <typename CONFIG, MEMFLAGS F>166class ConcurrentHashTable<CONFIG, F>::GrowTask :167public BucketsOperation168{169public:170GrowTask(ConcurrentHashTable<CONFIG, F>* cht) : BucketsOperation(cht) {171}172// Before start prepare must be called.173bool prepare(Thread* thread) {174if (!BucketsOperation::_cht->internal_grow_prolog(175thread, BucketsOperation::_cht->_log2_size_limit)) {176return false;177}178this->setup(thread);179return true;180}181182// Re-sizes a portion of the table. Returns true if there is more work.183bool do_task(Thread* thread) {184size_t start, stop;185assert(BucketsOperation::_cht->_resize_lock_owner != NULL,186"Should be locked");187if (!this->claim(&start, &stop)) {188return false;189}190BucketsOperation::_cht->internal_grow_range(thread, start, stop);191assert(BucketsOperation::_cht->_resize_lock_owner != NULL,192"Should be locked");193return true;194}195196// Must be called after do_task returns false.197void done(Thread* thread) {198this->thread_owns_resize_lock(thread);199BucketsOperation::_cht->internal_grow_epilog(thread);200this->thread_do_not_own_resize_lock(thread);201}202};203204#endif // SHARE_UTILITIES_CONCURRENTHASHTABLETASKS_INLINE_HPP205206207