Skip to content

Commit

Permalink
Bugfix for concurrent garbage collection in Java API.
Browse files Browse the repository at this point in the history
Relates to Z3Prover#205 and Z3Prover#245
  • Loading branch information
Christoph M. Wintersteiger committed Oct 14, 2015
1 parent 2d2ec38 commit 6263252
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 19 deletions.
45 changes: 30 additions & 15 deletions src/api/java/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.microsoft.z3;

import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import com.microsoft.z3.enumerations.Z3_ast_print_mode;

Expand Down Expand Up @@ -3761,36 +3762,50 @@ public IDecRefQueue getOptimizeDRQ()
return m_Optimize_DRQ;
}

protected long m_refCount = 0;
protected AtomicInteger m_refCount = new AtomicInteger(0);

/**
* Finalizer.
* @throws Throwable
**/
protected void finalize()
protected void finalize() throws Throwable
{
dispose();

if (m_refCount == 0)
{
try
try {
dispose();

if (m_refCount.get() == 0)
{
Native.delContext(m_ctx);
} catch (Z3Exception e)
try
{
Native.delContext(m_ctx);
} catch (Z3Exception e)
{
// OK.
}
m_ctx = 0;
System.out.println("> disposed OK.");
}
else if (m_refCount.get() < 0)
System.out.println("XXX negative ref count");
else
{
// OK.
System.out.println("XXX context not disposed");
}
m_ctx = 0;
}
/*
else
CMW: re-queue the finalizer? */
}
catch (Throwable t) {
throw t;
}
finally {
super.finalize();
}
}

/**
* Disposes of the context.
**/
public void dispose()
{
System.out.println("> context disposing.");
m_AST_DRQ.clear(this);
m_ASTMap_DRQ.clear(this);
m_ASTVector_DRQ.clear(this);
Expand Down
8 changes: 4 additions & 4 deletions src/api/java/Z3Object.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public void dispose()
}

if (m_ctx != null)
{
m_ctx.m_refCount--;
{
m_ctx.m_refCount.decrementAndGet();
m_ctx = null;
}
}
Expand All @@ -54,13 +54,13 @@ public void dispose()

Z3Object(Context ctx)
{
ctx.m_refCount++;
ctx.m_refCount.incrementAndGet();
m_ctx = ctx;
}

Z3Object(Context ctx, long obj)
{
ctx.m_refCount++;
ctx.m_refCount.incrementAndGet();
m_ctx = ctx;
incRef(obj);
m_n_obj = obj;
Expand Down

0 comments on commit 6263252

Please sign in to comment.