Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions be/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,9 @@ if (COMPILER_CLANG)
-Wunused-template
-Wunused-member-function
-Wunused-macros
-Wconversion)
add_compile_options( -Wno-gnu-statement-expression
-Wconversion
-Wthread-safety)
add_compile_options(-Wno-gnu-statement-expression
-Wno-implicit-float-conversion
-Wno-sign-conversion
)
Expand Down
171 changes: 171 additions & 0 deletions be/src/common/thread_safety_annotations.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

// Thread safety annotation macros and annotated mutex wrappers for
// Clang's -Wthread-safety static analysis.
// Reference: https://clang.llvm.org/docs/ThreadSafetyAnalysis.html

#pragma once

#include <mutex>

#ifdef BE_TEST
namespace doris {
void mock_random_sleep();
} // namespace doris
#endif

// Enable thread safety attributes only with clang.
// The attributes can be safely erased when compiling with other compilers.
#if defined(__clang__) && (!defined(SWIG))
#define THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x))
#else
#define THREAD_ANNOTATION_ATTRIBUTE__(x) // no-op
#endif

#define CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(capability(x))

#define SCOPED_CAPABILITY THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable)

#define GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x))

#define PT_GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_by(x))

#define ACQUIRED_BEFORE(...) THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(__VA_ARGS__))

#define ACQUIRED_AFTER(...) THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(__VA_ARGS__))

#define REQUIRES(...) THREAD_ANNOTATION_ATTRIBUTE__(requires_capability(__VA_ARGS__))

#define REQUIRES_SHARED(...) THREAD_ANNOTATION_ATTRIBUTE__(requires_shared_capability(__VA_ARGS__))

#define ACQUIRE(...) THREAD_ANNOTATION_ATTRIBUTE__(acquire_capability(__VA_ARGS__))

#define ACQUIRE_SHARED(...) THREAD_ANNOTATION_ATTRIBUTE__(acquire_shared_capability(__VA_ARGS__))

#define RELEASE(...) THREAD_ANNOTATION_ATTRIBUTE__(release_capability(__VA_ARGS__))

#define RELEASE_SHARED(...) THREAD_ANNOTATION_ATTRIBUTE__(release_shared_capability(__VA_ARGS__))

#define TRY_ACQUIRE(...) THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_capability(__VA_ARGS__))

#define TRY_ACQUIRE_SHARED(...) \
THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_shared_capability(__VA_ARGS__))

#define EXCLUDES(...) THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(__VA_ARGS__))

#define ASSERT_CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(assert_capability(x))

#define ASSERT_SHARED_CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(assert_shared_capability(x))

#define RETURN_CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x))

#define NO_THREAD_SAFETY_ANALYSIS THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis)

// Annotated mutex wrapper for use with Clang thread safety analysis.
// Wraps std::mutex and provides the CAPABILITY annotation so that
// GUARDED_BY / REQUIRES / etc. annotations can reference it.
class CAPABILITY("mutex") AnnotatedMutex {
public:
void lock() ACQUIRE() { _mutex.lock(); }
void unlock() RELEASE() { _mutex.unlock(); }
bool try_lock() TRY_ACQUIRE(true) { return _mutex.try_lock(); }

// Access the underlying std::mutex (e.g., for std::condition_variable).
// Use with care — this bypasses thread safety annotations.
std::mutex& native_handle() { return _mutex; }

private:
std::mutex _mutex;
};

// RAII scoped lock guard annotated for thread safety analysis.
// In BE_TEST builds, injects a random sleep before acquiring and after
// releasing the lock to exercise concurrent code paths.
template <typename MutexType>
class SCOPED_CAPABILITY LockGuard {
public:
explicit LockGuard(MutexType& mu) ACQUIRE(mu) : _mu(mu) {
#ifdef BE_TEST
doris::mock_random_sleep();
#endif
_mu.lock();
}
~LockGuard() RELEASE() {
_mu.unlock();
#ifdef BE_TEST
doris::mock_random_sleep();
#endif
}

LockGuard(const LockGuard&) = delete;
LockGuard& operator=(const LockGuard&) = delete;

private:
MutexType& _mu;
};

// RAII unique lock annotated for thread safety analysis.
// Supports manual lock/unlock while preserving capability tracking.
template <typename MutexType>
class SCOPED_CAPABILITY UniqueLock {
public:
explicit UniqueLock(MutexType& mu) ACQUIRE(mu) : _mu(&mu), _locked(true) {
#ifdef BE_TEST
doris::mock_random_sleep();
#endif
_mu->lock();
}

UniqueLock(MutexType& mu, std::adopt_lock_t) REQUIRES(mu) : _mu(&mu), _locked(true) {}

UniqueLock(MutexType& mu, std::defer_lock_t) EXCLUDES(mu) : _mu(&mu), _locked(false) {}

~UniqueLock() RELEASE() {
if (_locked) {
_mu->unlock();
#ifdef BE_TEST
doris::mock_random_sleep();
#endif
}
}

void lock() ACQUIRE() {
#ifdef BE_TEST
doris::mock_random_sleep();
#endif
_mu->lock();
_locked = true;
}

void unlock() RELEASE() {
_mu->unlock();
_locked = false;
#ifdef BE_TEST
doris::mock_random_sleep();
#endif
}

bool owns_lock() const { return _locked; }

UniqueLock(const UniqueLock&) = delete;
UniqueLock& operator=(const UniqueLock&) = delete;

private:
MutexType* _mu;
bool _locked;
};
4 changes: 2 additions & 2 deletions be/src/exec/operator/analytic_sink_operator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ void AnalyticSinkLocalState::_init_result_columns() {
void AnalyticSinkLocalState::_refresh_buffer_and_dependency_state(Block* block) {
size_t buffer_size = 0;
{
std::unique_lock<std::mutex> lc(_shared_state->buffer_mutex);
LockGuard lc(_shared_state->buffer_mutex);
_shared_state->blocks_buffer.push(std::move(*block));
buffer_size = _shared_state->blocks_buffer.size();
}
Expand Down Expand Up @@ -756,7 +756,7 @@ Status AnalyticSinkOperatorX::sink(doris::RuntimeState* state, Block* input_bloc
RETURN_IF_ERROR(_add_input_block(state, input_block));
RETURN_IF_ERROR(local_state._execute_impl());
if (local_state._input_eos) {
std::unique_lock<std::mutex> lc(local_state._shared_state->sink_eos_lock);
LockGuard lc(local_state._shared_state->sink_eos_lock);
local_state._shared_state->sink_eos = true;
local_state._dependency->set_ready_to_read(); // ready for source to read
}
Expand Down
9 changes: 4 additions & 5 deletions be/src/exec/operator/analytic_source_operator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,27 +53,26 @@ Status AnalyticSourceOperatorX::get_block(RuntimeState* state, Block* output_blo
output_block->clear_column_data();
size_t output_rows = 0;
{
std::lock_guard<std::mutex> lock(local_state._shared_state->buffer_mutex);
LockGuard lock(local_state._shared_state->buffer_mutex);
if (!local_state._shared_state->blocks_buffer.empty()) {
local_state._shared_state->blocks_buffer.front().swap(*output_block);
local_state._shared_state->blocks_buffer.pop();
output_rows = output_block->rows();
//if buffer have no data and sink not eos, block reading and wait for signal again
RETURN_IF_ERROR(local_state.filter_block(local_state._conjuncts, output_block,
output_block->columns()));
if (local_state._shared_state->blocks_buffer.empty() &&
!local_state._shared_state->sink_eos) {
if (local_state._shared_state->blocks_buffer.empty()) {
// add this mutex to check, as in some case maybe is doing block(), and the sink is doing set eos.
// so have to hold mutex to set block(), avoid to sink have set eos and set ready, but here set block() by mistake
std::unique_lock<std::mutex> lc(local_state._shared_state->sink_eos_lock);
LockGuard lc(local_state._shared_state->sink_eos_lock);
if (!local_state._shared_state->sink_eos) {
local_state._dependency->block(); // block self source
local_state._dependency->set_ready_to_write(); // ready for sink write
}
}
} else {
//iff buffer have no data and sink eos, set eos
std::unique_lock<std::mutex> lc(local_state._shared_state->sink_eos_lock);
LockGuard lc(local_state._shared_state->sink_eos_lock);
*eos = local_state._shared_state->sink_eos;
}
}
Expand Down
Loading
Loading