Search in sources :

Example 1 with Threads

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);
    }
}
Also used : Threads(lucee.runtime.type.scope.Threads) ChildThread(lucee.runtime.thread.ChildThread) ApplicationException(lucee.runtime.exp.ApplicationException)

Example 2 with Threads

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);
    }
}
Also used : Threads(lucee.runtime.type.scope.Threads) ThreadsImpl(lucee.runtime.thread.ThreadsImpl) ChildThreadImpl(lucee.runtime.thread.ChildThreadImpl) ApplicationException(lucee.runtime.exp.ApplicationException) ChildSpoolerTask(lucee.runtime.thread.ChildSpoolerTask) PageContextImpl(lucee.runtime.PageContextImpl) Key(lucee.runtime.type.Collection.Key)

Example 3 with Threads

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;
}
Also used : Threads(lucee.runtime.type.scope.Threads) PageContext(lucee.runtime.PageContext) PageContextImpl(lucee.runtime.PageContextImpl)

Example 4 with Threads

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;
}
Also used : Threads(lucee.runtime.type.scope.Threads) PageContext(lucee.runtime.PageContext) PageContextImpl(lucee.runtime.PageContextImpl)

Example 5 with Threads

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;
        }
    }
}
Also used : ChildThread(lucee.runtime.thread.ChildThread) Threads(lucee.runtime.type.scope.Threads) ApplicationException(lucee.runtime.exp.ApplicationException) Key(lucee.runtime.type.Collection.Key)

Aggregations

Threads (lucee.runtime.type.scope.Threads)5 PageContextImpl (lucee.runtime.PageContextImpl)3 ApplicationException (lucee.runtime.exp.ApplicationException)3 PageContext (lucee.runtime.PageContext)2 ChildThread (lucee.runtime.thread.ChildThread)2 Key (lucee.runtime.type.Collection.Key)2 ChildSpoolerTask (lucee.runtime.thread.ChildSpoolerTask)1 ChildThreadImpl (lucee.runtime.thread.ChildThreadImpl)1 ThreadsImpl (lucee.runtime.thread.ThreadsImpl)1