aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/mbien/opencl/QueueBarrier.java
blob: 733391920444ed5cc700023b63268e2f770617c8 (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
77
78
79
80
81
82
83
84
85
package com.mbien.opencl;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 *
 * @author Michael Bien
 */
public class QueueBarrier {

    private final CountDownLatch latch;
    private final Set<CLCommandQueue> queues;

    public QueueBarrier(int queueCount) {
        this.latch = new CountDownLatch(queueCount);
        this.queues = null;
    }

    public QueueBarrier(CLCommandQueue... allowedQueues) {
        this.latch = new CountDownLatch(allowedQueues.length);

        HashSet<CLCommandQueue> set = new HashSet<CLCommandQueue>(allowedQueues.length);
        for (CLCommandQueue queue : allowedQueues) {
            set.add(queue);
        }
        this.queues = Collections.unmodifiableSet(set);
    }

    /**
     * Blocks the current Thread until all commands on the CLCommandQueue finished excecution.
     * This method may be invoked concurrently without synchronization on the QueueBarrier object
     * as long each Thread passes a distinct CLCommandQueue as parameter to this method.
     */
    public QueueBarrier waitFor(CLCommandQueue queue) {
        checkQueue(queue);

        queue.putBarrier();
        latch.countDown();
        return this;
    }

    /**
     * Blocks the current Thread until the given events on the CLCommandQueue occurred.
     * This method may be invoked concurrently without synchronization on the QueueBarrier object
     * as long each Thread passes a distinct CLCommandQueue as parameter to this method.
     */
    public QueueBarrier waitFor(CLCommandQueue queue, CLEventList events) {
        checkQueue(queue);

        queue.putWaitForEvents(events);
        latch.countDown();
        return this;
    }

    /**
     * Blocks until all Threads which called {@link #waitFor}
     * continue excecution.
     * This method blocks only once, all subsequent calls are ignored.
     */
    public QueueBarrier await() throws InterruptedException {
        latch.await();
        return this;
    }
    
    /**
     * @see #await()
     * @param timeout the maximum time to wait
     * @param unit the time unit of the {@code timeout} argument
     */
    public QueueBarrier await(long timeout, TimeUnit unit) throws InterruptedException {
        latch.await(timeout, unit);
        return this;
    }

    private final void checkQueue(CLCommandQueue queue) throws IllegalArgumentException {
        if (queues != null && !queues.contains(queue)) {
            throw new IllegalArgumentException(queue + " is not in the allowedQueues Set: " + queues);
        }
    }

}