Changeset 11444 in josm for trunk/src/org


Ignore:
Timestamp:
2017-01-09T00:34:05+01:00 (3 years ago)
Author:
wiktorn
Message:

Fix disappearing download tasks.

When ThreadPoolExecutor is adapting it's size, it interrupts its threads from time to time. HostLimitQueue had a bug, that when interrupted when waiting to acquire lock, it didn't return the job back to the queue.

Added testcases that helped debug this and that verify, that we're actually doing host based limiting.

Sponsored by "Girl, interrupted" movie with Angelina Jolie.

See: #14166

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/cache/HostLimitQueue.java

    r11438 r11444  
    8484        job = pollFirst(timeout, unit);
    8585        if (job != null) {
    86             acquireSemaphore(job);
     86            try {
     87                boolean gotLock = tryAcquireSemaphore(job, timeout, unit);
     88                return gotLock ? job : null;
     89            } catch (InterruptedException e) {
     90                // acquire my got interrupted, first offer back what was taken
     91                offer(job);
     92                throw e;
     93            }
    8794        }
    8895        return job;
     
    96103        }
    97104        job = takeFirst();
    98         acquireSemaphore(job);
     105        try {
     106            acquireSemaphore(job);
     107        } catch (InterruptedException e) {
     108            // acquire my got interrupted, first offer back what was taken
     109            offer(job);
     110            throw e;
     111        }
    99112        return job;
    100113    }
     
    176189    }
    177190
     191    private boolean tryAcquireSemaphore(Runnable job, long timeout, TimeUnit unit) throws InterruptedException {
     192        boolean ret = true;
     193        if (job instanceof JCSCachedTileLoaderJob) {
     194            final JCSCachedTileLoaderJob<?, ?> jcsJob = (JCSCachedTileLoaderJob<?, ?>) job;
     195            Semaphore limit = getSemaphore(jcsJob);
     196            if (limit != null) {
     197                ret = limit.tryAcquire(timeout, unit);
     198                if (ret) {
     199                    jcsJob.setFinishedTask(() -> releaseSemaphore(jcsJob));
     200                }
     201            }
     202        }
     203        return ret;
     204    }
     205
    178206    private void releaseSemaphore(JCSCachedTileLoaderJob<?, ?> job) {
    179207        Semaphore limit = getSemaphore(job);
Note: See TracChangeset for help on using the changeset viewer.