Search in sources :

Example 1 with Guard

use of jetbrains.exodus.core.execution.locks.Guard in project xodus by JetBrains.

the class JobProcessorQueueAdapter method queueLowest.

@Override
protected boolean queueLowest(@NotNull Job job) {
    if (isFinished())
        return false;
    if (job.getProcessor() == null) {
        job.setProcessor(this);
    }
    try (Guard ignored = queue.lock()) {
        final Pair<Priority, Job> pair = queue.floorPair();
        final Priority priority = pair == null ? Priority.highest : pair.getFirst();
        if (queue.push(priority, job) != null) {
            return false;
        }
    }
    awake.release();
    return true;
}
Also used : Guard(jetbrains.exodus.core.execution.locks.Guard)

Example 2 with Guard

use of jetbrains.exodus.core.execution.locks.Guard in project xodus by JetBrains.

the class JobProcessorQueueAdapter method queueLowestTimed.

@Override
protected boolean queueLowestTimed(@NotNull final Job job) {
    if (isFinished()) {
        return false;
    }
    if (job.getProcessor() == null) {
        job.setProcessor(this);
    }
    try (Guard ignored = timeQueue.lock()) {
        final Pair<Long, Job> pair = timeQueue.floorPair();
        final long priority = pair == null ? Long.MAX_VALUE - System.currentTimeMillis() : pair.getFirst();
        if (timeQueue.push(priority, job) != null) {
            return false;
        }
    }
    awake.release();
    return true;
}
Also used : Guard(jetbrains.exodus.core.execution.locks.Guard)

Example 3 with Guard

use of jetbrains.exodus.core.execution.locks.Guard in project xodus by JetBrains.

the class PriorityQueue method moveQueue.

// Returns size of the destination (and obviously of the source) queue
public static <P extends Comparable<? super P>, E> int moveQueue(@NotNull final PriorityQueue<P, E> source, @NotNull final PriorityQueue<P, E> dest) {
    try (Guard ignored = source.lock()) {
        try (Guard ignore = dest.lock()) {
            while (true) {
                final Pair<P, E> pair = source.peekPair();
                if (pair == null)
                    break;
                dest.push(pair.getFirst(), pair.getSecond());
                source.pop();
            }
            return dest.size();
        }
    }
}
Also used : Guard(jetbrains.exodus.core.execution.locks.Guard)

Example 4 with Guard

use of jetbrains.exodus.core.execution.locks.Guard in project xodus by JetBrains.

the class StablePriorityQueue method clear.

@Override
public void clear() {
    try (Guard ignored = lock()) {
        theQueue.clear();
        priorities.clear();
        size.set(0);
    }
}
Also used : Guard(jetbrains.exodus.core.execution.locks.Guard)

Example 5 with Guard

use of jetbrains.exodus.core.execution.locks.Guard in project xodus by JetBrains.

the class JobProcessorQueueAdapter method pushAt.

@Override
protected Job pushAt(final Job job, final long millis) {
    if (isFinished()) {
        return null;
    }
    if (job.getProcessor() == null) {
        job.setProcessor(this);
    }
    Job oldJob;
    final Pair<Long, Job> pair;
    final long priority = Long.MAX_VALUE - millis;
    try (Guard ignored = timeQueue.lock()) {
        oldJob = timeQueue.push(priority, job);
        pair = timeQueue.peekPair();
    }
    if (pair != null && pair.getFirst() != priority) {
        return oldJob;
    }
    awake.release();
    return oldJob;
}
Also used : Guard(jetbrains.exodus.core.execution.locks.Guard)

Aggregations

Guard (jetbrains.exodus.core.execution.locks.Guard)9 ArrayList (java.util.ArrayList)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 IntHashSet (jetbrains.exodus.core.dataStructures.hash.IntHashSet)1 Test (org.junit.Test)1