Remove thread lock.

This make the JNI wrapping *somehow* NOT threadsafe.

Few things are threadsafe "by nature":
- A lot of native method in libzim are threadsafe.
- Wrapping internal are threadsafe (shared_ptr).

What is not threadsafe is accessing the SAME java object from different
thread. But accessing the same wrapped cpp object using two different java
wrapper in two different thread is ok (assuming that the called cpp method
is threadsafe itself).
This commit is contained in:
Matthieu Gautier 2023-01-24 17:10:08 +01:00
parent b2b7dad84f
commit 06638d46b8
9 changed files with 0 additions and 25 deletions

View File

@ -10,7 +10,6 @@ add_library(
zim_wrapper zim_wrapper
SHARED SHARED
common.cpp
libzim/archive.cpp libzim/archive.cpp
libzim/entry.cpp libzim/entry.cpp
libzim/entry_iterator.cpp libzim/entry_iterator.cpp

View File

@ -1,4 +0,0 @@
#include <thread>
std::mutex globalLock;

View File

@ -27,12 +27,9 @@
#include "utils.h" #include "utils.h"
#include "zim/tools.h" #include "zim/tools.h"
std::mutex globalLock;
JNIEXPORT void JNICALL Java_org_kiwix_kiwixlib_JNIICU_setDataDirectory( JNIEXPORT void JNICALL Java_org_kiwix_kiwixlib_JNIICU_setDataDirectory(
JNIEnv* env, jclass kclass, jstring dirStr) JNIEnv* env, jclass kclass, jstring dirStr)
{ {
Lock l;
try { try {
zim::setICUDataDirectory(TO_C(dirStr)); zim::setICUDataDirectory(TO_C(dirStr));
} catch (...) { } catch (...) {

View File

@ -35,7 +35,6 @@
METHOD(void, setNativeServer, jobject jLibrary) METHOD(void, setNativeServer, jobject jLibrary)
{ {
LOG("Attempting to create server"); LOG("Attempting to create server");
Lock l;
try { try {
auto library = getPtr<kiwix::Library>(env, jLibrary); auto library = getPtr<kiwix::Library>(env, jLibrary);
SET_PTR(std::make_shared<NATIVE_TYPE>(library.get())); SET_PTR(std::make_shared<NATIVE_TYPE>(library.get()));

View File

@ -41,7 +41,6 @@ METHOD(void, setNativeArchive, jstring filename)
std::string cPath = TO_C(filename); std::string cPath = TO_C(filename);
LOG("Attempting to create reader with: %s", cPath.c_str()); LOG("Attempting to create reader with: %s", cPath.c_str());
Lock l;
try { try {
auto archive = std::make_shared<zim::Archive>(cPath); auto archive = std::make_shared<zim::Archive>(cPath);
SET_PTR(archive); SET_PTR(archive);
@ -78,7 +77,6 @@ JNIEXPORT void JNICALL Java_org_kiwix_libzim_Archive_setNativeArchiveByFD(
int fd = jni2fd(fdObj, env); int fd = jni2fd(fdObj, env);
LOG("Attempting to create reader with fd: %d", fd); LOG("Attempting to create reader with fd: %d", fd);
Lock l;
try { try {
auto archive = std::make_shared<zim::Archive>(fd); auto archive = std::make_shared<zim::Archive>(fd);
SET_PTR(archive); SET_PTR(archive);
@ -99,7 +97,6 @@ JNIEXPORT void JNICALL Java_org_kiwix_libzim_Archive_setNativeArchiveEmbedded(
int fd = jni2fd(fdObj, env); int fd = jni2fd(fdObj, env);
LOG("Attempting to create reader with fd: %d", fd); LOG("Attempting to create reader with fd: %d", fd);
Lock l;
try { try {
auto archive = std::make_shared<zim::Archive>(fd, offset, size); auto archive = std::make_shared<zim::Archive>(fd, offset, size);
SET_PTR(archive); SET_PTR(archive);

View File

@ -36,7 +36,6 @@
METHOD(void, setNativeQuery, jstring query) METHOD(void, setNativeQuery, jstring query)
{ {
auto cQuery = TO_C(query); auto cQuery = TO_C(query);
Lock l;
try { try {
auto query = std::make_shared<NATIVE_TYPE>(cQuery); auto query = std::make_shared<NATIVE_TYPE>(cQuery);
SET_PTR(query); SET_PTR(query);

View File

@ -35,8 +35,6 @@
METHOD(void, setNativeSearcher, jobject archive) METHOD(void, setNativeSearcher, jobject archive)
{ {
Lock l;
auto cArchive = getPtr<zim::Archive>(env, archive); auto cArchive = getPtr<zim::Archive>(env, archive);
try { try {
auto searcher = std::make_shared<zim::Searcher>(*cArchive); auto searcher = std::make_shared<zim::Searcher>(*cArchive);

View File

@ -35,8 +35,6 @@
METHOD(void, setNativeSearcher, jobject archive) METHOD(void, setNativeSearcher, jobject archive)
{ {
Lock l;
auto cArchive = getPtr<zim::Archive>(env, archive); auto cArchive = getPtr<zim::Archive>(env, archive);
try { try {
auto searcher = std::make_shared<zim::SuggestionSearcher>(*cArchive); auto searcher = std::make_shared<zim::SuggestionSearcher>(*cArchive);

View File

@ -37,7 +37,6 @@
#define LOG(...) #define LOG(...)
#endif #endif
extern std::mutex globalLock;
using std::shared_ptr; using std::shared_ptr;
// Here is the wrapping structure. // Here is the wrapping structure.
@ -131,13 +130,6 @@ inline jobject buildWrapper(JNIEnv* env, const char* class_name, T&& obj, const
#define BUILD_WRAPPER(CLASSNAME, OBJ) buildWrapper(env, CLASSNAME, std::move(OBJ)) #define BUILD_WRAPPER(CLASSNAME, OBJ) buildWrapper(env, CLASSNAME, std::move(OBJ))
// A mixin class which will lock the globalLock when a instance is created
// This avoid the cration of two instance inheriting from Lock in the same time.
class Lock : public std::unique_lock<std::mutex>
{
public:
Lock() : std::unique_lock<std::mutex>(globalLock) { }
};
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// Convert things to JAVA // Convert things to JAVA