Path: blob/master/src/hotspot/share/jfr/utilities/jfrLinkedList.inline.hpp
41152 views
/*1* Copyright (c) 2020, 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_JFR_UTILITIES_JFRLINKEDLIST_INLINE_HPP25#define SHARE_JFR_UTILITIES_JFRLINKEDLIST_INLINE_HPP2627#include "jfr/utilities/jfrLinkedList.hpp"2829#include "runtime/atomic.hpp"3031template <typename NodeType, typename AllocPolicy>32JfrLinkedList<NodeType, AllocPolicy>::JfrLinkedList() : _head(NULL) {}3334template <typename NodeType, typename AllocPolicy>35bool JfrLinkedList<NodeType, AllocPolicy>::initialize() {36return true;37}3839template <typename NodeType, typename AllocPolicy>40inline NodeType* JfrLinkedList<NodeType, AllocPolicy>::head() const {41return (NodeType*)Atomic::load_acquire(&_head);42}4344template <typename NodeType, typename AllocPolicy>45inline bool JfrLinkedList<NodeType, AllocPolicy>::is_empty() const {46return NULL == head();47}4849template <typename NodeType, typename AllocPolicy>50inline bool JfrLinkedList<NodeType, AllocPolicy>::is_nonempty() const {51return !is_empty();52}5354template <typename NodeType, typename AllocPolicy>55inline void JfrLinkedList<NodeType, AllocPolicy>::add(NodeType* node) {56assert(node != NULL, "invariant");57NodePtr next;58do {59next = head();60node->_next = next;61} while (Atomic::cmpxchg(&_head, next, node) != next);62}6364template <typename NodeType, typename AllocPolicy>65inline NodeType* JfrLinkedList<NodeType, AllocPolicy>::remove() {66NodePtr node;67NodePtr next;68do {69node = head();70if (node == NULL) break;71next = (NodePtr)node->_next;72} while (Atomic::cmpxchg(&_head, node, next) != node);73return node;74}7576template <typename NodeType, typename AllocPolicy>77template <typename Callback>78void JfrLinkedList<NodeType, AllocPolicy>::iterate(Callback& cb) {79NodePtr current = head();80while (current != NULL) {81NodePtr next = (NodePtr)current->_next;82if (!cb.process(current)) {83return;84}85current = next;86}87}8889template <typename NodeType, typename AllocPolicy>90NodeType* JfrLinkedList<NodeType, AllocPolicy>::excise(NodeType* prev, NodeType* node) {91NodePtr next = (NodePtr)node->_next;92if (prev == NULL) {93prev = Atomic::cmpxchg(&_head, node, next);94if (prev == node) {95return NULL;96}97}98assert(prev != NULL, "invariant");99while (prev->_next != node) {100prev = (NodePtr)prev->_next;101}102assert(prev->_next == node, "invariant");103prev->_next = next;104return prev;105}106107template <typename NodeType, typename AllocPolicy>108bool JfrLinkedList<NodeType, AllocPolicy>::in_list(const NodeType* node) const {109assert(node != NULL, "invariant");110const NodeType* current = head();111while (current != NULL) {112if (current == node) {113return true;114}115current = (NodeType*)current->_next;116}117return false;118}119120#endif // SHARE_JFR_UTILITIES_JFRLINKEDLIST_INLINE_HPP121122123