Skip to content

Commit

Permalink
Replaced platform-specific mutex implementations with std::recursive_…
Browse files Browse the repository at this point in the history
…mutex

Simplified various CMake files too, though they still need to look for pthread support because there is a call to pthread_atfork() in the codebase.

Also got rid of mutex.hpp and its workaround for old __MINGW32__ that did not support std:mutex.

Aside from being a general simplification, this fixes a clang -Wreserved-id-macro warning by no longer ever needing to #define _GNU_SOURCE.
  • Loading branch information
seanm committed Nov 3, 2022
1 parent 9d440ec commit 88d91f7
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 321 deletions.
21 changes: 4 additions & 17 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,10 @@ endif()
################################################################################
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
find_package(Threads)
if(Threads_FOUND AND CMAKE_USE_PTHREADS_INIT)
set(CMAKE_REQUIRED_LIBRARIES
"${CMAKE_REQUIRED_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}")
endif()

include(CheckCSourceCompiles)
if(MSVC)
Expand All @@ -259,23 +263,6 @@ else()
set(CMAKE_REQUIRED_FLAGS "${CMAKE_C_FLAGS} -Werror -Wall")
endif()

if(Threads_FOUND AND CMAKE_USE_PTHREADS_INIT)
set(CMAKE_REQUIRED_LIBRARIES
"${CMAKE_REQUIRED_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}")
check_c_source_compiles("
#include <pthread.h>
int main(int argc, char* argv[]) {
(void)PTHREAD_MUTEX_RECURSIVE;
(void)argv;
return argc;
}
" HAVE_PTHREAD_MUTEX_RECURSIVE_DEFN)
if(HAVE_PTHREAD_MUTEX_RECURSIVE_DEFN)
add_definitions(-DHAVE_PTHREAD_MUTEX_RECURSIVE=1)
endif()
endif()

# Set a default build type for single-configuration cmake generators if
# no build type is set.
if(NOT CMAKE_CONFIGURATION_TYPES AND NOT CMAKE_BUILD_TYPE)
Expand Down
61 changes: 0 additions & 61 deletions include/proj/internal/mutex.hpp

This file was deleted.

16 changes: 8 additions & 8 deletions src/iso19111/factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
#include "proj/internal/internal.hpp"
#include "proj/internal/io_internal.hpp"
#include "proj/internal/lru_cache.hpp"
#include "proj/internal/mutex.hpp"
#include "proj/internal/tracing.hpp"

#include "operation/coordinateoperation_internal.hpp"
Expand All @@ -60,6 +59,7 @@
#include <locale>
#include <map>
#include <memory>
#include <mutex>
#include <sstream> // std::ostringstream
#include <stdexcept>
#include <string>
Expand All @@ -78,16 +78,16 @@
// parallel. This is slightly faster
#define ENABLE_CUSTOM_LOCKLESS_VFS

#if defined(_WIN32) && defined(MUTEX_pthread)
#undef MUTEX_pthread
#if defined(_WIN32) && defined(PROJ_HAS_PTHREADS)
#undef PROJ_HAS_PTHREADS
#endif

/* SQLite3 might use seak()+read() or pread[64]() to read data */
/* The later allows the same SQLite handle to be safely used in forked */
/* children of a parent process, while the former doesn't. */
/* So we use pthread_atfork() to set a flag in forked children, to ask them */
/* to close and reopen their database handle. */
#if defined(MUTEX_pthread) && !defined(SQLITE_USE_PREAD)
#if defined(PROJ_HAS_PTHREADS) && !defined(SQLITE_USE_PREAD)
#include <pthread.h>
#define REOPEN_SQLITE_DB_AFTER_FORK
#endif
Expand Down Expand Up @@ -576,7 +576,7 @@ class SQLiteHandleCache {
bool firstTime_ = true;
#endif

NS_PROJ::mutex sMutex_{};
std::mutex sMutex_{};

// Map dbname to SQLiteHandle
lru11::Cache<std::string, std::shared_ptr<SQLiteHandle>> cache_{};
Expand Down Expand Up @@ -605,15 +605,15 @@ SQLiteHandleCache &SQLiteHandleCache::get() {
// ---------------------------------------------------------------------------

void SQLiteHandleCache::clear() {
NS_PROJ::lock_guard<NS_PROJ::mutex> lock(sMutex_);
std::lock_guard<std::mutex> lock(sMutex_);
cache_.clear();
}

// ---------------------------------------------------------------------------

std::shared_ptr<SQLiteHandle>
SQLiteHandleCache::getHandle(const std::string &path, PJ_CONTEXT *ctx) {
NS_PROJ::lock_guard<NS_PROJ::mutex> lock(sMutex_);
std::lock_guard<std::mutex> lock(sMutex_);

#ifdef REOPEN_SQLITE_DB_AFTER_FORK
if (firstTime_) {
Expand All @@ -636,7 +636,7 @@ SQLiteHandleCache::getHandle(const std::string &path, PJ_CONTEXT *ctx) {
// ---------------------------------------------------------------------------

void SQLiteHandleCache::invalidateHandles() {
NS_PROJ::lock_guard<NS_PROJ::mutex> lock(sMutex_);
std::lock_guard<std::mutex> lock(sMutex_);
const auto lambda =
[](const lru11::KeyValuePair<std::string, std::shared_ptr<SQLiteHandle>>
&kvp) { kvp.value->invalidate(); };
Expand Down
16 changes: 3 additions & 13 deletions src/lib_proj.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,9 @@ message(STATUS "Configuring proj library:")
option(BUILD_SHARED_LIBS
"Build PROJ library shared." ON)

option(USE_THREAD "Build libproj with thread/mutex support " ON)
if(NOT USE_THREAD)
add_definitions(-DMUTEX_stub)
endif()
find_package(Threads QUIET)
if(USE_THREAD AND Threads_FOUND AND CMAKE_USE_WIN32_THREADS_INIT)
add_definitions(-DMUTEX_win32)
elseif(USE_THREAD AND Threads_FOUND AND CMAKE_USE_PTHREADS_INIT)
add_definitions(-DMUTEX_pthread)
elseif(USE_THREAD AND NOT Threads_FOUND)
message(FATAL_ERROR
"No thread library found and thread/mutex support is "
"required by USE_THREAD option")
if(Threads_FOUND AND CMAKE_USE_PTHREADS_INIT)
add_definitions(-DPROJ_HAS_PTHREADS)
endif()

option(ENABLE_IPO
Expand Down Expand Up @@ -442,7 +432,7 @@ if(UNIX)
target_link_libraries(proj PRIVATE -ldl)
endif()
endif()
if(USE_THREAD AND Threads_FOUND AND CMAKE_USE_PTHREADS_INIT)
if(Threads_FOUND AND CMAKE_USE_PTHREADS_INIT)
target_link_libraries(proj PRIVATE ${CMAKE_THREAD_LIBS_INIT})
endif()

Expand Down
Loading

0 comments on commit 88d91f7

Please sign in to comment.