Class EolContextParallel
java.lang.Object
org.eclipse.epsilon.eol.execute.context.EolContext
org.eclipse.epsilon.eol.execute.context.concurrent.EolContextParallel
- All Implemented Interfaces:
IEolContextParallel
,IEolContext
- Direct Known Subclasses:
ErlContextParallel
,EwlContextParallel
Skeletal implementation of a parallel IEolContext. This class takes care of
the common structures which are affected by multi-threading using
ThreadLocals. For use cases where these thread-local values are
required, a PersistentThreadLocal is used, so that the
context will be consistent with a sequential implementation once
#endParallel() is invoked.
For optimal performance, it is recommend that parallel tasks obtain a sequential "snapshot" of this context to avoid frequent retrieval of ThreadLocal values using the
For optimal performance, it is recommend that parallel tasks obtain a sequential "snapshot" of this context to avoid frequent retrieval of ThreadLocal values using the
getShadow()
method.- Since:
- 1.6
- Author:
- Sina Madani
-
Field Summary
Fields inherited from class org.eclipse.epsilon.eol.execute.context.EolContext
assertionsEnabled, asyncStatementsQueue, classpathNativeTypeDelegate, errorStream, executorFactory, extendedProperties, frameStack, introspectionManager, methodContributorRegistry, modelRepository, module, nativeTypeDelegates, operationFactory, outputStream, prettyPrinterManager, profilingEnabled, userInput, warningStream
Fields inherited from interface org.eclipse.epsilon.eol.execute.context.concurrent.IEolContextParallel
NUM_THREADS_CONFIG
-
Constructor Summary
ModifierConstructorDescriptionEolContextParallel
(int parallelism) protected
EolContextParallel
(IEolContext other) Copy constructor. -
Method Summary
Modifier and TypeMethodDescriptionbeginParallelTask
(ModuleElement entryPoint, boolean shortCircuiting) Registers the beginning of parallel task on the default ExecutorService.protected void
protected void
static IEolContextParallel
convertToParallel
(IEolContext context) Utility method for converting a sequential context to a parallel one, if it is not already parallel.protected IEolContext
protected ExecutorFactory
protected FrameStack
protected OperationContributorRegistry
void
dispose()
void
Must be called once parallel processing has finished.executeJob
(Object job) Evaluates the job using this context's parallel execution facilities.final EolThreadPoolExecutor
A re-usable ExecutorService.final int
Indicates the scalability of this Context when more processing nodes are added.Can be used to obtain an optimal execution context while executing in parallel.protected <T extends BaseDelegate<T>>
DelegatePersistentThreadLocal<T>initDelegateThreadLocal
(Supplier<? extends T> constructor) protected void
boolean
This method will return true if#beginParallelTask()
has been called and false ifIEolContextParallel.endParallelTask()
has been called, or if#beginParallelTask()
has not been called yet.protected EolThreadPoolExecutor
protected void
protected final <R> R
parallelGet
(ThreadLocal<? extends R> threadLocal, Supplier<? extends R> originalValueGetter) Utility method used to appropriately return either a thread-local or the original value, depending on whether this context isParallel().protected final <R> R
parallelGet
(ThreadLocal<? extends R> threadLocal, R originalValue) Utility method used to appropriately return either a thread-local or the original value, depending on whether this context isParallel().protected final <T> void
parallelSet
(T value, ThreadLocal<? super T> threadLocal, Consumer<? super T> originalValueSetter) Utility method used to appropriately set either a thread-local or the original value, depending on whether this context isParallel().protected void
removeAll
(ThreadLocal<?>... threadLocals) void
setExecutorFactory
(ExecutorFactory executorFactory) void
setFrameStack
(FrameStack frameStack) void
setParallelism
(int threads) Attempts to set the parallelism of this context and its associated executor.toString()
protected boolean
Determines whether callingparallelGet(ThreadLocal, Object)
orparallelSet(Object, ThreadLocal, Consumer)
should use the ThreadLocal value or if the alternative value should be returned.Methods inherited from class org.eclipse.epsilon.eol.execute.context.EolContext
getAsyncStatementsQueue, getErrorStream, getExtendedProperties, getIntrospectionManager, getModelRepository, getModule, getNativeTypeDelegates, getOperationContributorRegistry, getOperationFactory, getOutputStream, getPrettyPrinterManager, getUserInput, getWarningStream, isAssertionsEnabled, isProfilingEnabled, setAssertionsEnabled, setErrorStream, setExtendedProperties, setIntrospectionManager, setModelRepository, setModule, setNativeTypeDelegates, setOperationFactory, setOutputStream, setPrettyPrinterManager, setProfilingEnabled, setUserInput, setWarningStream
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.eclipse.epsilon.eol.execute.context.IEolContext
getAsyncStatementsQueue, getErrorStream, getExtendedProperties, getIntrospectionManager, getModelRepository, getModule, getNativeTypeDelegates, getOperationContributorRegistry, getOperationFactory, getOutputStream, getPrettyPrinterManager, getUserInput, getWarningStream, isAssertionsEnabled, isProfilingEnabled, setAssertionsEnabled, setErrorStream, setExtendedProperties, setIntrospectionManager, setModelRepository, setModule, setNativeTypeDelegates, setOperationFactory, setOutputStream, setPrettyPrinterManager, setProfilingEnabled, setUserInput, setWarningStream
Methods inherited from interface org.eclipse.epsilon.eol.execute.context.concurrent.IEolContextParallel
beginParallelTask, ensureNotNested, executeAll, executeAny, isParallelisationLegal
-
Field Details
-
executorService
-
-
Constructor Details
-
EolContextParallel
public EolContextParallel() -
EolContextParallel
public EolContextParallel(int parallelism) - Parameters:
parallelism
- The number of threads to use.
-
EolContextParallel
Copy constructor.- Parameters:
other
- The parent context to copy from. Must not be null.
-
-
Method Details
-
initThreadLocals
protected void initThreadLocals() -
initDelegateThreadLocal
protected <T extends BaseDelegate<T>> DelegatePersistentThreadLocal<T> initDelegateThreadLocal(Supplier<? extends T> constructor) -
useThreadLocalValue
protected boolean useThreadLocalValue()Determines whether callingparallelGet(ThreadLocal, Object)
orparallelSet(Object, ThreadLocal, Consumer)
should use the ThreadLocal value or if the alternative value should be returned.- Returns:
true
if the ThreadLocal should be used,false
otherwise.
-
parallelGet
protected final <R> R parallelGet(ThreadLocal<? extends R> threadLocal, Supplier<? extends R> originalValueGetter) Utility method used to appropriately return either a thread-local or the original value, depending on whether this context isParallel().- Type Parameters:
R
- The value type- Parameters:
threadLocal
- The ThreadLocal value (returned if parallel).originalValueGetter
- The non-thread-local value (returned if not parallel).- Returns:
- The appropriate value for the current thread.
-
parallelGet
Utility method used to appropriately return either a thread-local or the original value, depending on whether this context isParallel().- Type Parameters:
R
- The value type- Parameters:
threadLocal
- The thread-local wrapper for the valueoriginalValue
- The main, persistent variable- Returns:
- The appropriate value for the current thread.
-
parallelSet
protected final <T> void parallelSet(T value, ThreadLocal<? super T> threadLocal, Consumer<? super T> originalValueSetter) Utility method used to appropriately set either a thread-local or the original value, depending on whether this context isParallel().- Parameters:
value
- The value to set.threadLocal
- The ThreadLocal value (will be set if parallel).originalValueSetter
- The non-thread-local value (will be set if not parallel).
-
removeAll
-
clearThreadLocals
protected void clearThreadLocals() -
nullifyThreadLocals
protected void nullifyThreadLocals() -
clearExecutor
protected void clearExecutor() -
newExecutorService
-
beginParallelTask
public ExecutorService beginParallelTask(ModuleElement entryPoint, boolean shortCircuiting) throws EolNestedParallelismException Description copied from interface:IEolContextParallel
Registers the beginning of parallel task on the default ExecutorService. TheIEolContextParallel.endParallelTask()
method must be called once finished.- Specified by:
beginParallelTask
in interfaceIEolContextParallel
- Parameters:
entryPoint
- The AST to associate with this task. May be null, in which case a default value (e.g. IEolContext.getModule()) should be used.shortCircuiting
- Whether the task may be terminated abruptly.- Returns:
IEolContextParallel.getExecutorService()
- Throws:
EolNestedParallelismException
- If there was already a parallel task in progress.
-
endParallelTask
Description copied from interface:IEolContextParallel
Must be called once parallel processing has finished.- Specified by:
endParallelTask
in interfaceIEolContextParallel
- Throws:
EolRuntimeException
- if the status completed exceptionally.- See Also:
-
setParallelism
Description copied from interface:IEolContextParallel
Attempts to set the parallelism of this context and its associated executor. Note that this may not take effect immediately and is intended to be called during parallel execution. Implementations may ignore this operation or throw an UnsupportedOperationException. It is recommended that this method is only called during initialisation, and is present for convenience only.- Specified by:
setParallelism
in interfaceIEolContextParallel
- Parameters:
threads
- The new value. Must be positive.- Throws:
IllegalStateException
- If this method is called at an inconvenient time.IllegalArgumentException
- If the new value is out of bounds.
-
isParallel
public boolean isParallel()Description copied from interface:IEolContextParallel
This method will return true if#beginParallelTask()
has been called and false ifIEolContextParallel.endParallelTask()
has been called, or if#beginParallelTask()
has not been called yet.- Specified by:
isParallel
in interfaceIEolContextParallel
- Returns:
- Whether this Context is currently executing in parallel mode.
-
getParallelism
public final int getParallelism()Description copied from interface:IEolContextParallel
Indicates the scalability of this Context when more processing nodes are added.- Specified by:
getParallelism
in interfaceIEolContextParallel
- Returns:
- the number of threads.
-
getExecutorService
Description copied from interface:IEolContextParallel
A re-usable ExecutorService.- Specified by:
getExecutorService
in interfaceIEolContextParallel
- Returns:
- a cached
ExecutorService
.
-
dispose
public void dispose()- Specified by:
dispose
in interfaceIEolContext
- Overrides:
dispose
in classEolContext
-
getFrameStack
- Specified by:
getFrameStack
in interfaceIEolContext
- Overrides:
getFrameStack
in classEolContext
-
getExecutorFactory
- Specified by:
getExecutorFactory
in interfaceIEolContext
- Overrides:
getExecutorFactory
in classEolContext
-
setFrameStack
- Specified by:
setFrameStack
in interfaceIEolContext
- Overrides:
setFrameStack
in classEolContext
-
setExecutorFactory
- Specified by:
setExecutorFactory
in interfaceIEolContext
- Overrides:
setExecutorFactory
in classEolContext
-
toString
-
createThreadLocalExecutorFactory
-
createThreadLocalFrameStack
-
createThreadLocalOperationContributorRegistry
-
createShadowThreadLocalContext
-
getShadow
Can be used to obtain an optimal execution context while executing in parallel. If execution is currently not parallel, then this context itself is returned.- Returns:
- A ThreadLocal copy of this context if in parallel, or this context otherwise.
-
convertToParallel
public static IEolContextParallel convertToParallel(IEolContext context) throws EolNestedParallelismException Utility method for converting a sequential context to a parallel one, if it is not already parallel.- Parameters:
context
- The current execution context to check or convert.- Returns:
- The context if it is already parallel, or a new
EolContextParallel
with state copied over from the context parameter. - Throws:
EolNestedParallelismException
- If the context is already derived from a parallel context.
-
executeJob
Evaluates the job using this context's parallel execution facilities. Subclasses may override this to support additional job types, calling the super method as the last resort for unknown cases. Supported types include multi-valued types (e.g. arrays, Iterable / Iterator, Stream etc.) as well as common concurrency units such as Runnable, Callable and Future.- Parameters:
job
- The job (or jobs) to evaluate.- Returns:
- The result of evaluating the job, if any.
- Throws:
IllegalArgumentException
- If the job type is not recognised.EolRuntimeException
- If an exception is thrown whilst evaluating the job(s).
-