enhance: Use std::shared_lock and std::unique_lock for mutexes (#44459)

issue: https://github.com/milvus-io/milvus/issues/44452

Signed-off-by: zhengbuqian <zhengbuqian@gmail.com>
Co-authored-by: buqian.zheng <buqian.zheng@zilliz.com>
This commit is contained in:
Buqian Zheng 2025-09-22 18:02:09 +08:00 committed by GitHub
parent 846cf52a95
commit 75557f3eb8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 13 additions and 9 deletions

View File

@ -24,6 +24,7 @@
#include <boost/uuid/random_generator.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <cstddef>
#include <shared_mutex>
#include <type_traits>
#include <vector>
#include "InvertedIndexTantivy.h"
@ -105,7 +106,7 @@ InvertedIndexTantivy<T>::finish() {
template <typename T>
BinarySet
InvertedIndexTantivy<T>::Serialize(const Config& config) {
folly::SharedMutex::ReadHolder lock(mutex_);
std::shared_lock<folly::SharedMutex> lock(mutex_);
auto index_valid_data_length = null_offset_.size() * sizeof(size_t);
std::shared_ptr<uint8_t[]> index_valid_data(
new uint8_t[index_valid_data_length]);
@ -293,7 +294,7 @@ InvertedIndexTantivy<T>::IsNull() {
};
if (is_growing_) {
folly::SharedMutex::ReadHolder lock(mutex_);
std::shared_lock<folly::SharedMutex> lock(mutex_);
fill_bitset();
} else {
fill_bitset();
@ -317,7 +318,7 @@ InvertedIndexTantivy<T>::IsNotNull() {
};
if (is_growing_) {
folly::SharedMutex::ReadHolder lock(mutex_);
std::shared_lock<folly::SharedMutex> lock(mutex_);
fill_bitset();
} else {
fill_bitset();
@ -365,7 +366,7 @@ InvertedIndexTantivy<T>::NotIn(size_t n, const T* values) {
};
if (is_growing_) {
folly::SharedMutex::ReadHolder lock(mutex_);
std::shared_lock<folly::SharedMutex> lock(mutex_);
fill_bitset();
} else {
fill_bitset();

View File

@ -11,6 +11,7 @@
#include "index/JsonInvertedIndex.h"
#include <string>
#include <shared_mutex>
#include <string_view>
#include <type_traits>
#include "common/EasyAssert.h"
@ -84,7 +85,7 @@ JsonInvertedIndex<T>::Exists() {
};
if (this->is_growing_) {
folly::SharedMutex::ReadHolder lock(this->mutex_);
std::shared_lock<folly::SharedMutex> lock(this->mutex_);
fill_bitset();
} else {
fill_bitset();

View File

@ -11,6 +11,7 @@
#pragma once
#include <cstdint>
#include <shared_mutex>
#include "common/Slice.h"
#include "common/FieldDataInterface.h"
#include "common/JsonCastFunction.h"
@ -134,7 +135,7 @@ class JsonInvertedIndex : public index::InvertedIndexTantivy<T> {
BinarySet
Serialize(const Config& config) override {
folly::SharedMutex::ReadHolder lock(this->mutex_);
std::shared_lock<folly::SharedMutex> lock(this->mutex_);
auto index_valid_data_length =
this->null_offset_.size() * sizeof(size_t);
std::shared_ptr<uint8_t[]> index_valid_data(

View File

@ -12,6 +12,7 @@
#include <boost/uuid/random_generator.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <memory>
#include <shared_mutex>
#include "index/TextMatchIndex.h"
#include "index/InvertedIndexUtil.h"
@ -211,7 +212,7 @@ TextMatchIndex::AddTextsGrowing(size_t n,
for (int i = 0; i < n; i++) {
auto offset = i + offset_begin;
if (!valids[i]) {
folly::SharedMutex::WriteHolder lock(mutex_);
std::unique_lock<folly::SharedMutex> lock(mutex_);
null_offset_.push_back(offset);
}
}
@ -233,14 +234,14 @@ TextMatchIndex::BuildIndexFromFieldData(
total += data->get_null_count();
}
{
folly::SharedMutex::WriteHolder lock(mutex_);
std::unique_lock<folly::SharedMutex> lock(mutex_);
null_offset_.reserve(total);
}
for (const auto& data : field_datas) {
auto n = data->get_num_rows();
for (int i = 0; i < n; i++) {
if (!data->is_valid(i)) {
folly::SharedMutex::WriteHolder lock(mutex_);
std::unique_lock<folly::SharedMutex> lock(mutex_);
null_offset_.push_back(i);
}
wrapper_->add_data(