package org.kne.opencl64;

import java.io.Closeable;
import java.util.concurrent.atomic.AtomicBoolean;

public abstract class Releaser<R> implements Runnable,Closeable,AutoCloseable{
	private AtomicBoolean released=new AtomicBoolean();
	private R resource;
	public Releaser(R resource) {
		super();
		this.resource = resource;
	}

	public AtomicBoolean getReleased() {
		return released;
	}

	@Override
	public void run() {
		if(released.compareAndSet(false, true)) {
		release(resource);
		}
	}

	public boolean isReleased() {
		return released.get();
	}
	
	@Override
	public void close() {
		run();
	}
	
	protected abstract void release(R resource);
	
}
