Skip to content

Commit

Permalink
Use std::mutex instead of base lock_* functions
Browse files Browse the repository at this point in the history
Use `std::lock_guard` instead of `CLockScope`.

Rename `lock` variables to `mutex`.
  • Loading branch information
Robyt3 committed Nov 9, 2023
1 parent fa8640b commit fd4c9d7
Show file tree
Hide file tree
Showing 20 changed files with 127 additions and 343 deletions.
19 changes: 7 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,11 @@ if(NOT MSVC AND NOT HAIKU)
add_cxx_compiler_flag_if_supported(OUR_FLAGS_LINK -Wl,--no-insert-timestamp)
endif()

if(TARGET_OS STREQUAL "mac")
add_cxx_compiler_flag_if_supported(OUR_FLAGS -stdlib=libc++)
endif()
# Force usage of libc++ to ensure thread-safety annotations are supported for std::mutex
add_cxx_compiler_flag_if_supported(OUR_FLAGS -stdlib=libc++)
add_linker_flag_if_supported(OUR_FLAGS_LINK -stdlib=libc++)
add_linker_flag_if_supported(OUR_FLAGS_LINK -static)
add_linker_flag_if_supported(OUR_FLAGS_LINK -no-pie)

if(EXCEPTION_HANDLING)
add_cxx_compiler_flag_if_supported(OUR_FLAGS -DCONF_EXCEPTION_HANDLING)
Expand All @@ -323,7 +325,7 @@ if(NOT MSVC AND NOT HAIKU)
add_cxx_compiler_flag_if_supported(OUR_FLAGS_OWN -Wshadow-all) # clang
add_cxx_compiler_flag_if_supported(OUR_FLAGS_OWN -Wshadow=global) # gcc
add_cxx_compiler_flag_if_supported(OUR_FLAGS_OWN -Wthread-safety)
add_cxx_compiler_flag_if_supported(OUR_FLAGS_OWN -Wthread-safety-negative)
# add_cxx_compiler_flag_if_supported(OUR_FLAGS_OWN -Wthread-safety-negative) # Causes false positives at the moment
add_cxx_compiler_flag_if_supported(OUR_FLAGS_OWN -Wsuggest-override)
add_linker_flag_if_supported(OUR_FLAGS_LINK -Wno-alloc-size-larger-than) # save.cpp with LTO
# add_cxx_compiler_flag_if_supported(OUR_FLAGS_OWN -Wdouble-promotion) # Many occurrences
Expand Down Expand Up @@ -1844,7 +1846,6 @@ set_src(BASE GLOB_RECURSE src/base
hash_ctxt.h
hash_libtomcrypt.cpp
hash_openssl.cpp
lock_scope.h
log.cpp
log.h
logger.h
Expand Down Expand Up @@ -3285,22 +3286,16 @@ foreach(target ${TARGETS})
if(ENABLE_IPO)
set_property(TARGET ${target} PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
endif()
target_compile_definitions(${target} PRIVATE _LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS) # Enable thread-safety annotations for std::mutex
endforeach()

foreach(target ${TARGETS_LINK})
if(MSVC)
set_property(TARGET ${target} APPEND PROPERTY LINK_FLAGS /SAFESEH:NO) # Disable SafeSEH because the shipped libraries don't support it (would cause error LNK2026 otherwise).
endif()
if(TARGET_OS STREQUAL "mac")
target_link_libraries(${target} -stdlib=libc++)
target_link_libraries(${target} "-framework SystemConfiguration") # Required by curl 7.79.0
endif()
if((MINGW OR TARGET_OS STREQUAL "linux") AND PREFER_BUNDLED_LIBS)
# Statically link the standard libraries with on MinGW/Linux so we don't
# have to ship them as DLLs.
target_link_libraries(${target} -static-libgcc)
target_link_libraries(${target} -static-libstdc++)
endif()
endforeach()

foreach(target ${TARGETS_OWN})
Expand Down
24 changes: 0 additions & 24 deletions src/base/lock_scope.h

This file was deleted.

10 changes: 4 additions & 6 deletions src/base/log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,9 +447,9 @@ std::unique_ptr<ILogger> log_logger_windows_debugger()

void CFutureLogger::Set(std::shared_ptr<ILogger> pLogger)
{
std::shared_ptr<ILogger> null;
m_PendingLock.lock();
if(!std::atomic_compare_exchange_strong_explicit(&m_pLogger, &null, pLogger, std::memory_order_acq_rel, std::memory_order_acq_rel))
const std::lock_guard<std::mutex> LockGuard(m_PendingLock);
std::shared_ptr<ILogger> pNullLogger;
if(!std::atomic_compare_exchange_strong_explicit(&m_pLogger, &pNullLogger, pLogger, std::memory_order_acq_rel, std::memory_order_acq_rel))
{
dbg_assert(false, "future logger has already been set and can only be set once");
}
Expand All @@ -461,7 +461,6 @@ void CFutureLogger::Set(std::shared_ptr<ILogger> pLogger)
}
m_vPending.clear();
m_vPending.shrink_to_fit();
m_PendingLock.unlock();
}

void CFutureLogger::Log(const CLogMessage *pMessage)
Expand All @@ -472,15 +471,14 @@ void CFutureLogger::Log(const CLogMessage *pMessage)
pLogger->Log(pMessage);
return;
}
m_PendingLock.lock();
const std::lock_guard<std::mutex> LockGuard(m_PendingLock);
pLogger = std::atomic_load_explicit(&m_pLogger, std::memory_order_relaxed);
if(pLogger)
{
pLogger->Log(pMessage);
return;
}
m_vPending.push_back(*pMessage);
m_PendingLock.unlock();
}

void CFutureLogger::GlobalFinish()
Expand Down
6 changes: 4 additions & 2 deletions src/base/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#define BASE_LOGGER_H

#include "log.h"
#include "system.h"

#include <atomic>
#include <memory>
#include <mutex>
Expand Down Expand Up @@ -233,8 +235,8 @@ class CFutureLogger : public ILogger
* Replace the `CFutureLogger` instance with the given logger. It'll
* receive all log messages sent to the `CFutureLogger` so far.
*/
void Set(std::shared_ptr<ILogger> pLogger);
void Log(const CLogMessage *pMessage) override;
void Set(std::shared_ptr<ILogger> pLogger) EXCLUDES(m_PendingLock);
void Log(const CLogMessage *pMessage) override EXCLUDES(m_PendingLock);
void GlobalFinish() override;
void OnFilterChange() override;
};
Expand Down
Loading

0 comments on commit fd4c9d7

Please sign in to comment.