aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/jogamp/opencl/util/concurrent/CLTaskCompletionService.java
blob: 630ee1c769cc4c4a17b3723a86cfcd86adbee24e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
 * Created on Tuesday, July 05 2011 00:26
 */
package com.jogamp.opencl.util.concurrent;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

/**
 * A {@link CompletionService} for {@link CLTask}s executed in a {@link CLCommandQueuePool}.
 * It simplifies asynchronous execution of tasks with the same result type in a potentially shared pool.
 * @see CompletionService
 * @author Michael Bien
 */
public class CLTaskCompletionService<R> {

    private final ExecutorCompletionService<R> service;
    private final CLCommandQueuePool pool;

    /**
     * Creates an CLTaskCompletionService using the supplied pool for base
     * task execution and a LinkedBlockingQueue with the capacity of {@link Integer#MAX_VALUE}
     * as a completion queue.
     */
    public CLTaskCompletionService(CLCommandQueuePool pool) {
        this.service = new ExecutorCompletionService<R>(pool.getExcecutor());
        this.pool = pool;
    }

    /**
     * Creates an CLTaskCompletionService using the supplied pool for base
     * task execution the supplied queue as its completion queue.
     */
    public CLTaskCompletionService(CLCommandQueuePool pool, BlockingQueue queue) {
        this.service = new ExecutorCompletionService<R>(pool.getExcecutor(), queue);
        this.pool = pool;
    }

    /**
     * Submits a CLTask for execution and returns a Future representing the pending
     * results of the task. Upon completion, this task may be taken or polled.
     * @see CompletionService#submit(java.util.concurrent.Callable)
     */
    public Future<R> submit(CLTask<? extends CLQueueContext, R> task) {
        return service.submit(pool.wrapTask(task));
    }

    /**
     * Retrieves and removes the Future representing the next completed task, waiting if none are yet present.
     * @see CompletionService#take()
     */
    public Future<R> take() throws InterruptedException {
        return service.take();
    }

    /**
     * Retrieves and removes the Future representing the next completed task or null if none are present.
     * @see CompletionService#poll()
     */
    public Future<R> poll() {
        return service.poll();
    }

    /**
     * Retrieves and removes the Future representing the next completed task, waiting if necessary
     * up to the specified wait time if none are yet present.
     * @see CompletionService#poll(long, java.util.concurrent.TimeUnit)
     */
    public Future<R> poll(long timeout, TimeUnit unit) throws InterruptedException {
        return service.poll(timeout, unit);
    }

}