Skip to content

Commit

Permalink
Quickfix for sosy-lab/java-smt#347
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-raffler committed Dec 28, 2023
1 parent 8701a55 commit ee6fab5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/api/java/jni/api_utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <cvc5/cvc5_parser.h>
#include <jni.h>

#include <mutex>
#include <string>
#include <vector>

Expand Down Expand Up @@ -149,8 +150,15 @@ jobject getBooleanObject(JNIEnv* env, bool value);
* a map from solver pointers to global references that need to be freed when
* the java Solver.deletePointer method is called
*/

inline std::map<jlong, std::vector<jobject> > globalReferences;

/**
* a lock to protect globalReferences
*/

inline std::mutex globalLock;

/**
* @param env jni environment
* @param solverRef a global reference to java Solver object
Expand Down
9 changes: 9 additions & 0 deletions src/api/java/jni/solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ JNIEXPORT void JNICALL Java_io_github_cvc5_Solver_deletePointer(JNIEnv* env,
jobject,
jlong pointer)
{
std::lock_guard<std::mutex> guard(globalLock);
const std::vector<jobject>& refs = globalReferences[pointer];
for (jobject ref : refs)
{
Expand Down Expand Up @@ -2437,6 +2438,7 @@ Java_io_github_cvc5_Solver_declareOracleFun(JNIEnv* env,
jobject oracle)
{
CVC5_JAVA_API_TRY_CATCH_BEGIN;
std::lock_guard<std::mutex> guard(globalLock);
jobject oracleReference = env->NewGlobalRef(oracle);
globalReferences[pointer].push_back(oracleReference);
Solver* solver = reinterpret_cast<Solver*>(pointer);
Expand All @@ -2446,6 +2448,13 @@ Java_io_github_cvc5_Solver_declareOracleFun(JNIEnv* env,
std::vector<Sort> sorts = getObjectsFromPointers<Sort>(env, sortPointers);
std::function<Term(std::vector<Term>)> fn =
[env, oracleReference](std::vector<Term> input) {
// FIXME:
// This is likely still broken if multiple threads are
// used as the captured JEnv ("env") is only valid on the
// current thread. However, the callback might come from
// any thread that encouters a term for the oracle.
//
// See https://github.com/sosy-lab/java-smt/pull/345
Term term = applyOracle(env, oracleReference, input);
return term;
};
Expand Down

0 comments on commit ee6fab5

Please sign in to comment.