Search in sources :

Example 1 with SchedTask

use of nars.task.NativeTask.SchedTask in project narchy by automenta.

the class Time method exeScheduled.

@Nullable
private List<SchedTask> exeScheduled() {
    // now = now();
    // SchedTask firstQuick = scheduled.peek(); //it's safe to call this outside synchronized block for speed
    // if (firstQuick == null || firstQuick.when > now)
    // return null; //too soon for the next one
    long now = now();
    long nextScheduled = scheduledNext.get();
    if ((now < nextScheduled) || !(scheduledNext.compareAndSet(nextScheduled, Long.MAX_VALUE)))
        return null;
    try {
        // preSched.drainTo(scheduled);
        List<SchedTask> pending = // new LinkedList();
        new FasterList(8);
        int s = 0;
        SchedTask p;
        while ((p = preSched.poll()) != null && s++ <= MAX_PRE_SCHED) {
            // limit by MAX in case the preSched continues filling while this is being processed
            if (p.when <= now)
                // bypass the queue
                pending.add(p);
            else
                scheduled.offer(p);
        }
        SchedTask next;
        while (((next = scheduled.peek()) != null) && (next.when <= now)) {
            SchedTask actualNext = scheduled.poll();
            assert (next == actualNext);
            pending.add(next);
        }
        // if emptied the priority queue, delay indefintely. otherwise delay until the time of the next item not dequeued now
        long nextNextWhen = next != null ? next.when : Long.MAX_VALUE;
        scheduledNext.updateAndGet(z -> Math.min(z, nextNextWhen));
        return pending.isEmpty() ? null : pending;
    } catch (Throwable t) {
        // logger.error() //TODO
        t.printStackTrace();
        // try again immediately
        scheduledNext.set(now);
        return null;
    }
}
Also used : SchedTask(nars.task.NativeTask.SchedTask) FasterList(jcog.list.FasterList) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

FasterList (jcog.list.FasterList)1 SchedTask (nars.task.NativeTask.SchedTask)1 Nullable (org.jetbrains.annotations.Nullable)1