From da247042c0dcb862d3d00529e3f5616f2c39338e Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Wed, 20 Jan 2021 16:41:22 +0100 Subject: [PATCH] Use c++11 std::thread instead of pthread. --- src/wrapper/java/kiwixicu.cpp | 6 +----- src/wrapper/java/utils.h | 20 ++++---------------- 2 files changed, 5 insertions(+), 21 deletions(-) diff --git a/src/wrapper/java/kiwixicu.cpp b/src/wrapper/java/kiwixicu.cpp index 0c604eb..a10b578 100644 --- a/src/wrapper/java/kiwixicu.cpp +++ b/src/wrapper/java/kiwixicu.cpp @@ -28,11 +28,7 @@ #include "utils.h" -#if __ANDROID__ -pthread_mutex_t globalLock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER; -#else -pthread_mutex_t globalLock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; -#endif +std::mutex globalLock; JNIEXPORT void JNICALL Java_org_kiwix_kiwixlib_JNIICU_setDataDirectory( JNIEnv* env, jclass kclass, jstring dirStr) diff --git a/src/wrapper/java/utils.h b/src/wrapper/java/utils.h index f922040..00b5855 100644 --- a/src/wrapper/java/utils.h +++ b/src/wrapper/java/utils.h @@ -24,7 +24,7 @@ #include -#include +#include #include #include #include @@ -36,7 +36,7 @@ #define LOG(...) #endif -extern pthread_mutex_t globalLock; +extern std::mutex globalLock; template void setPtr(JNIEnv* env, jobject thisObj, T* ptr) @@ -88,22 +88,10 @@ inline jobjectArray createArray(JNIEnv* env, size_t length, const std::string& t return env->NewObjectArray(length, c, NULL); } -class Lock +class Lock : public std::unique_lock { - protected: - pthread_mutex_t* lock; - public: - Lock() : lock(&globalLock) { pthread_mutex_lock(lock); } - Lock(const Lock&) = delete; - Lock& operator=(const Lock&) = delete; - Lock(Lock&& other) : lock(&globalLock) { other.lock = nullptr; } - virtual ~Lock() - { - if (lock) { - pthread_mutex_unlock(lock); - } - } + Lock() : std::unique_lock(globalLock) { } }; template