aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEven Rouault <even.rouault@spatialys.com>2021-06-02 19:06:15 +0200
committerGitHub <noreply@github.com>2021-06-02 19:06:15 +0200
commit29f849485bf2ea9caa7ca3da7d93ece1daf4d7d5 (patch)
treef0180f6659f9c060395a9eee010f18861dbad19b
parenta77df0d81bdfb12ef090bc53ed70219f293c04d1 (diff)
parente297475f0d05b3c8cfbc5cee90add7d7a37c6a02 (diff)
downloadPROJ-29f849485bf2ea9caa7ca3da7d93ece1daf4d7d5.tar.gz
PROJ-29f849485bf2ea9caa7ca3da7d93ece1daf4d7d5.zip
Merge pull request #2736 from rouault/cleanup_mutex
Cleanup: add proj/internal/mutex.hpp as compat layer for mingw32 for std::mutex
-rw-r--r--include/proj/internal/Makefile.am3
-rw-r--r--include/proj/internal/mutex.hpp61
-rw-r--r--src/networkfilemanager.cpp21
-rw-r--r--src/transformations/hgridshift.cpp26
-rw-r--r--src/transformations/vgridshift.cpp26
5 files changed, 75 insertions, 62 deletions
diff --git a/include/proj/internal/Makefile.am b/include/proj/internal/Makefile.am
index b3fb57eb..59325667 100644
--- a/include/proj/internal/Makefile.am
+++ b/include/proj/internal/Makefile.am
@@ -6,5 +6,6 @@ noinst_HEADERS = \
io_internal.hpp \
lru_cache.hpp \
include_nlohmann_json.hpp \
- tracing.hpp
+ tracing.hpp \
+ mutex.hpp
diff --git a/include/proj/internal/mutex.hpp b/include/proj/internal/mutex.hpp
new file mode 100644
index 00000000..a378be40
--- /dev/null
+++ b/include/proj/internal/mutex.hpp
@@ -0,0 +1,61 @@
+/******************************************************************************
+ *
+ * Project: PROJ
+ * Purpose: std::mutex emulation
+ * Author: Even Rouault <even dot rouault at spatialys dot com>
+ *
+ ******************************************************************************
+ * Copyright (c) 2021, Even Rouault <even dot rouault at spatialys dot com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included
+ * in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ ****************************************************************************/
+
+#include "proj/util.hpp"
+#include "proj_internal.h"
+
+#ifndef __MINGW32__
+#include <mutex>
+#endif
+
+NS_PROJ_START
+
+#ifdef __MINGW32__
+// mingw32-win32 doesn't implement std::mutex
+class mutex {
+ public:
+ // cppcheck-suppress functionStatic
+ void lock() { pj_acquire_lock(); }
+ // cppcheck-suppress functionStatic
+ void unlock() { pj_release_lock(); }
+};
+
+template <class Lock> struct lock_guard {
+ Lock &lock_;
+ lock_guard(Lock &lock) : lock_(lock) { lock_.lock(); }
+ ~lock_guard() { lock_.unlock(); }
+};
+
+#else
+
+typedef std::mutex mutex;
+template <class Mutex> using lock_guard = std::lock_guard<Mutex>;
+
+#endif
+
+NS_PROJ_END
diff --git a/src/networkfilemanager.cpp b/src/networkfilemanager.cpp
index fa6375b8..d331f4d6 100644
--- a/src/networkfilemanager.cpp
+++ b/src/networkfilemanager.cpp
@@ -40,25 +40,10 @@
#include "proj.h"
#include "proj/internal/internal.hpp"
#include "proj/internal/lru_cache.hpp"
+#include "proj/internal/mutex.hpp"
#include "proj_internal.h"
#include "sqlite3_utils.hpp"
-#ifdef __MINGW32__
-// mingw32-win32 doesn't implement std::mutex
-namespace {
-class MyMutex {
- public:
- // cppcheck-suppress functionStatic
- void lock() { pj_acquire_lock(); }
- // cppcheck-suppress functionStatic
- void unlock() { pj_release_lock(); }
-};
-} // namespace
-#else
-#include <mutex>
-#define MyMutex std::mutex
-#endif
-
#ifdef CURL_ENABLED
#include <curl/curl.h>
#include <sqlite3.h> // for sqlite3_snprintf
@@ -152,7 +137,7 @@ class NetworkChunkCache {
};
lru11::Cache<
- Key, std::shared_ptr<std::vector<unsigned char>>, MyMutex,
+ Key, std::shared_ptr<std::vector<unsigned char>>, NS_PROJ::mutex,
std::unordered_map<
Key,
typename std::list<lru11::KeyValuePair<
@@ -176,7 +161,7 @@ class NetworkFilePropertiesCache {
void clearMemoryCache();
private:
- lru11::Cache<std::string, FileProperties, MyMutex> cache_{};
+ lru11::Cache<std::string, FileProperties, NS_PROJ::mutex> cache_{};
};
// ---------------------------------------------------------------------------
diff --git a/src/transformations/hgridshift.cpp b/src/transformations/hgridshift.cpp
index 326bbb13..96b77c20 100644
--- a/src/transformations/hgridshift.cpp
+++ b/src/transformations/hgridshift.cpp
@@ -6,31 +6,16 @@
#include <time.h>
#include "proj_internal.h"
+#include "proj/internal/mutex.hpp"
#include "grids.hpp"
PROJ_HEAD(hgridshift, "Horizontal grid shift");
-using namespace NS_PROJ;
-
-#ifdef __MINGW32__
-// mingw32-win32 doesn't implement std::mutex
-namespace {
-class MyMutex {
- public:
- // cppcheck-suppress functionStatic
- void lock() { pj_acquire_lock(); }
- // cppcheck-suppress functionStatic
- void unlock() { pj_release_lock(); }
-};
-}
-#else
-#include <mutex>
-#define MyMutex std::mutex
-#endif
-
-static MyMutex gMutex{};
+static NS_PROJ::mutex gMutex{};
static std::set<std::string> gKnownGrids{};
+using namespace NS_PROJ;
+
namespace { // anonymous namespace
struct hgridshiftData {
double t_final = 0;
@@ -208,7 +193,6 @@ PJ *TRANSFORMATION(hgridshift,0) {
}
void pj_clear_hgridshift_knowngrids_cache() {
- gMutex.lock();
+ NS_PROJ::lock_guard<NS_PROJ::mutex> lock(gMutex);
gKnownGrids.clear();
- gMutex.unlock();
}
diff --git a/src/transformations/vgridshift.cpp b/src/transformations/vgridshift.cpp
index 7b234517..527fe1e8 100644
--- a/src/transformations/vgridshift.cpp
+++ b/src/transformations/vgridshift.cpp
@@ -6,32 +6,15 @@
#include <time.h>
#include "proj_internal.h"
+#include "proj/internal/mutex.hpp"
#include "grids.hpp"
PROJ_HEAD(vgridshift, "Vertical grid shift");
-using namespace NS_PROJ;
-
-
-#ifdef __MINGW32__
-// mingw32-win32 doesn't implement std::mutex
-namespace {
-class MyMutex {
- public:
- // cppcheck-suppress functionStatic
- void lock() { pj_acquire_lock(); }
- // cppcheck-suppress functionStatic
- void unlock() { pj_release_lock(); }
-};
-}
-#else
-#include <mutex>
-#define MyMutex std::mutex
-#endif
-
-static MyMutex gMutex{};
+static NS_PROJ::mutex gMutex{};
static std::set<std::string> gKnownGrids{};
+using namespace NS_PROJ;
namespace { // anonymous namespace
struct vgridshiftData {
@@ -251,7 +234,6 @@ PJ *TRANSFORMATION(vgridshift,0) {
}
void pj_clear_vgridshift_knowngrids_cache() {
- gMutex.lock();
+ NS_PROJ::lock_guard<NS_PROJ::mutex> lock(gMutex);
gKnownGrids.clear();
- gMutex.unlock();
}