co.paralleluniverse.fibers.SchedulerLocal Maven / Gradle / Ivy
/*
* Quasar: lightweight threads and actors for the JVM.
* Copyright (c) 2013-2014, Parallel Universe Software Co. All rights reserved.
*
* This program and the accompanying materials are dual-licensed under
* either the terms of the Eclipse Public License v1.0 as published by
* the Eclipse Foundation
*
* or (per the licensee's choosing)
*
* under the terms of the GNU Lesser General Public License version 3.0
* as published by the Free Software Foundation.
*/
package co.paralleluniverse.fibers;
import co.paralleluniverse.strands.concurrent.ReentrantLock;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.locks.Lock;
/**
* Associates a value with the current {@link FiberScheduler}
*
* @author pron
*/
public class SchedulerLocal {
private final Lock lock = new ReentrantLock();
/**
* Computes the initial value for the current {@link FiberScheduler}.
* Returns {@code null} by default;
*
* @param scheduler the current {@link FiberScheduler}
* @return the initial value
*/
protected T initialValue(FiberScheduler scheduler) {
return null;
}
/**
* Returns the scheduler-local value of this {@code SchedulerLocal}.
*/
public final T get() throws SuspendExecution {
final FiberScheduler scheduler = currentScheduler();
final ConcurrentMap> map = scheduler.schedLocals;
Entry entry = (Entry) map.get(this);
if (entry == null) {
lock.lock();
try {
entry = (Entry) map.get(this);
if (entry == null) {
entry = new Entry<>();
entry.value = initialValue(scheduler);
Entry> old = map.putIfAbsent(this, entry);
assert old == null;
}
} finally {
lock.unlock();
}
}
return entry.value;
}
/**
* Sets the scheduler-local value of this {@code SchedulerLocal}.
*/
public final void set(T value) {
getEntry(getMap()).value = value;
}
/**
* Removes the association between the scheduler-local value and the current scheduler.
* The next call to {@link #get()} would return the initial value returned by a fresh call to {@link #initialValue(FiberScheduler) initialValue}.
*/
public final void remove() {
getMap().remove(this);
}
private static ConcurrentMap> getMap() {
return currentScheduler().schedLocals;
}
private Entry getEntry(ConcurrentMap> map) {
Entry entry = (Entry) map.get(this);
if (entry == null) {
entry = new Entry<>();
Entry> old = map.putIfAbsent(this, entry);
if (old != null)
entry = (Entry) old;
}
return entry;
}
private static FiberScheduler currentScheduler() {
final Fiber currentFiber = Fiber.currentFiber();
if (currentFiber == null)
throw new IllegalStateException("Method called not within a fiber");
return currentFiber.getScheduler();
}
static class Entry {
T value;
}
}