/***************************************************************************** Copyright (c) 2018, 2021, Alibaba and/or its affiliates. All rights reserved. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2.0, as published by the Free Software Foundation. This program is also distributed with certain software (including but not limited to OpenSSL) that is licensed under separate terms, as designated in a particular file or component or in included license documentation. The authors of MySQL/PolarDB-X Engine hereby grant you an additional permission to link the program and your derivative works with the separately licensed software that they have included with MySQL/PolarDB-X Engine. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *****************************************************************************/ /** @file include/lizard0iv.cc Lizard universe tools Created 2021-11-05 by Jianwei.zhao *******************************************************/ #include "lizard0tcn.h" #include "ut0new.h" namespace lizard { /** Constructor */ template Lru_list::Lru_list() : hash() { /** Unneccessary to memset m_elements if add into free list. */ UT_LIST_INIT(m_free_list, &Element_type::list); UT_LIST_INIT(m_lru_list, &Element_type::list); for (size_t i = 0; i < Prealloc; i++) { // new (&m_elements[i]) Element_type(); UT_LIST_ADD_LAST(m_free_list, m_elements + i); } DBUG_ASSERT(UT_LIST_GET_LEN(m_free_list) == Prealloc); } /** Destructor */ template Lru_list::~Lru_list() { /** 1.LOCK lru mutex */ /** 2.LOCK hash mutex */ /** Element_type *elem = UT_LIST_GET_FIRST(m_lru_list); while (elem != nullptr) { iv_hash_delete(&hash, elem); UT_LIST_REMOVE(m_lru_list, elem); elem = UT_LIST_GET_FIRST(m_lru_list); } */ /** 3.LOCK free mutex*/ /* elem = UT_LIST_GET_FIRST(m_free_list); while (elem != nullptr) { UT_LIST_REMOVE(m_free_list, elem); elem = UT_LIST_GET_FIRST(m_free_list); } for (size_t i = 0; i < Prealloc; i++) { m_elements[i].~Element_type(); } */ } template bool Lru_list::insert( Value_type value) { Element_type *elem = nullptr; bool result = false; /** Step 1. */ /** Lock free mutex to find available slot */ elem = get_free_item(); /** Release free mutex */ if (elem) { elem->assign(value); /** Lock LRU mutex */ /** Lock hash mutex */ result = iv_hash_insert(&hash, elem); if (!result) { UT_LIST_ADD_LAST(m_lru_list, elem); } else { put_free_item(elem); } return result; } else { return true; } } /** Search committed transaction information by trx_id. */ template Value_type Lru_list::search( Key_type key) { Element_type *elem; Value_type value; elem = iv_hash_search(&hash, key); if (elem) { elem->copy_to(value); } return value; } /** Template */ template tcn_t Lru_list::search( trx_id_t id); template bool Lru_list::insert( tcn_t tcn); template Lru_list::Lru_list(); template Lru_list::~Lru_list(); template tcn_t Lru_list::search( trx_id_t id); template bool Lru_list::insert( tcn_t tcn); template Lru_list::Lru_list(); template Lru_list::~Lru_list(); } // namespace lizard