use of lucee.runtime.type.scope.Threads in project Lucee by lucee.
the class ThreadTag method doTerminate.
private void doTerminate() throws ApplicationException {
// PageContextImpl mpc=(PageContextImpl)getMainPageContext(pc);
// mpc.getThreadScope(nameAsString(false));
Threads ts = ThreadTag.getThreadScope(pc, KeyImpl.init(nameAsString(false)), ThreadTag.LEVEL_CURRENT + ThreadTag.LEVEL_KIDS);
if (ts == null)
throw new ApplicationException("there is no thread running with the name [" + nameAsString(false) + "]");
ChildThread ct = ts.getChildThread();
if (ct.isAlive()) {
ct.terminated();
SystemUtil.stop(ct);
}
}
use of lucee.runtime.type.scope.Threads in project Lucee by lucee.
the class ThreadTag method register.
public void register(Page currentPage, int threadIndex) throws PageException {
if (ACTION_RUN != action)
return;
Key name = name(true);
try {
// pc.getThreadScope(name);
Threads ts = ThreadTag.getThreadScope(pc, name, ThreadTag.LEVEL_ALL);
if (type == TYPE_DAEMON) {
if (ts != null)
throw new ApplicationException("could not create a thread with the name [" + name.getString() + "]. name must be unique within a request");
ChildThreadImpl ct = new ChildThreadImpl((PageContextImpl) pc, currentPage, name.getString(), threadIndex, attrs, false);
pc.setThreadScope(name, new ThreadsImpl(ct));
ct.setPriority(priority);
ct.setDaemon(false);
ct.start();
} else {
ChildThreadImpl ct = new ChildThreadImpl((PageContextImpl) pc, currentPage, name.getString(), threadIndex, attrs, true);
ct.setPriority(priority);
((ConfigImpl) pc.getConfig()).getSpoolerEngine().add(new ChildSpoolerTask(ct, plans));
}
} catch (Throwable t) {
ExceptionUtil.rethrowIfNecessary(t);
throw Caster.toPageException(t);
} finally {
// this method is not called from template when type is run, a call from template is to early,
((PageContextImpl) pc).reuse(this);
}
}
use of lucee.runtime.type.scope.Threads in project Lucee by lucee.
the class ThreadTag method getThreadScope.
public static Threads getThreadScope(PageContext pc, Key name, int level) {
Threads t = null;
// current
if ((level & LEVEL_CURRENT) > 0) {
t = pc.getThreadScope(name);
if (t != null)
return t;
}
// parent
if ((level & LEVEL_PARENTS) > 0) {
PageContext parent = pc.getParentPageContext();
while (parent != null) {
t = parent.getThreadScope(name);
if (t != null)
return t;
parent = parent.getParentPageContext();
}
}
// children
if ((level & LEVEL_KIDS) > 0 && pc.hasFamily()) {
t = getKidsThreadScope(((PageContextImpl) pc).getChildPageContexts(), name);
if (t != null)
return t;
}
return t;
}
use of lucee.runtime.type.scope.Threads in project Lucee by lucee.
the class ThreadTag method getKidsThreadScope.
private static Threads getKidsThreadScope(List<PageContext> pageContexts, Key name) {
if (pageContexts == null || pageContexts.isEmpty())
return null;
Threads t;
Iterator<PageContext> it = pageContexts.iterator();
PageContext pc;
while (it.hasNext()) {
pc = it.next();
t = pc.getThreadScope(name);
if (t != null)
return t;
t = getKidsThreadScope(((PageContextImpl) pc).getChildPageContexts(), name);
return t;
}
return null;
}
use of lucee.runtime.type.scope.Threads in project Lucee by lucee.
the class ThreadTag method doJoin.
private void doJoin() throws ApplicationException {
// PageContextImpl mpc=(PageContextImpl)getMainPageContext(pc);
String[] names, all = null;
Key name = name(false);
if (name == null) {
// mpc.getThreadScopeNames();
all = names = ListUtil.toStringArray(ThreadTag.getThreadScopeNames(pc, ThreadTag.LEVEL_CURRENT + ThreadTag.LEVEL_KIDS));
} else
names = ListUtil.listToStringArray(name.getLowerString(), ',');
ChildThread ct;
Threads ts;
long start = System.currentTimeMillis(), _timeout = timeout > 0 ? timeout : -1;
for (int i = 0; i < names.length; i++) {
if (StringUtil.isEmpty(names[i], true))
continue;
// PageContextImpl mpc=(PageContextImpl)getMainPageContext(pc);
// mpc.getThreadScope(names[i]);
ts = ThreadTag.getThreadScope(pc, KeyImpl.init(names[i]), ThreadTag.LEVEL_CURRENT + ThreadTag.LEVEL_KIDS);
if (ts == null) {
// mpc.getThreadScopeNames();
if (all == null)
all = ListUtil.toStringArray(ThreadTag.getThreadScopeNames(pc, ThreadTag.LEVEL_CURRENT + ThreadTag.LEVEL_KIDS));
throw new ApplicationException("there is no thread running with the name [" + names[i] + "], only the following threads existing [" + ListUtil.arrayToList(all, ", ") + "]");
}
ct = ts.getChildThread();
if (ct.isAlive()) {
try {
if (_timeout != -1)
ct.join(_timeout);
else
ct.join();
} catch (InterruptedException e) {
}
}
if (_timeout != -1) {
_timeout = _timeout - (System.currentTimeMillis() - start);
if (_timeout < 1)
break;
}
}
}
Aggregations