9
0
mirror of https://github.com/VolmitSoftware/Iris.git synced 2025-12-30 04:29:05 +00:00
This commit is contained in:
Daniel Mills
2020-08-06 16:43:02 -04:00
parent 4207330dcf
commit 5be89f3f31
2 changed files with 123 additions and 72 deletions

View File

@@ -9,11 +9,16 @@ public class AtomicCache<T>
{
private transient volatile T t;
private transient volatile long a;
private boolean nullSupport;
private transient volatile int validations;
private final IrisLock check;
private final IrisLock time;
private final IrisLock write;
private final boolean nullSupport;
public AtomicCache()
{
this(false);
}
public AtomicCache(boolean nullSupport)
{
@@ -26,11 +31,6 @@ public class AtomicCache<T>
t = null;
}
public AtomicCache()
{
this(false);
}
public void reset()
{
check.lock();
@@ -43,7 +43,51 @@ public class AtomicCache<T>
check.unlock();
}
public T aquireNullex(Supplier<T> t)
public T aquire(Supplier<T> t)
{
if(nullSupport)
{
return aquireNull(t);
}
if(this.t != null && validations > 1000)
{
return this.t;
}
if(this.t != null && M.ms() - a > 1000)
{
if(this.t != null)
{
validations++;
}
return this.t;
}
check.lock();
if(this.t == null)
{
write.lock();
this.t = t.get();
time.lock();
if(a == -1)
{
a = M.ms();
}
time.unlock();
write.unlock();
}
check.unlock();
return this.t;
}
public T aquireNull(Supplier<T> t)
{
if(validations > 1000)
{
@@ -72,48 +116,4 @@ public class AtomicCache<T>
check.unlock();
return this.t;
}
public T aquire(Supplier<T> t)
{
if(nullSupport)
{
return aquireNullex(t);
}
if(this.t != null && validations > 1000)
{
return this.t;
}
if(this.t != null && M.ms() - a > 1000)
{
if(this.t != null)
{
validations++;
}
return this.t;
}
check.lock();
if(this.t != null)
{
write.lock();
this.t = t.get();
time.lock();
if(a == -1)
{
a = M.ms();
}
time.unlock();
write.unlock();
}
check.unlock();
return this.t;
}
}