|
1 | 1 | package net.sourceforge.smallbasic.ioio; |
2 | 2 |
|
3 | | -import ioio.lib.api.exception.ConnectionLostException; |
4 | | -import ioio.lib.api.exception.IncompatibilityException; |
5 | | - |
6 | 3 | import java.util.concurrent.CountDownLatch; |
7 | 4 | import java.util.concurrent.atomic.AtomicReference; |
8 | | -import java.util.concurrent.locks.ReadWriteLock; |
9 | | -import java.util.concurrent.locks.ReentrantReadWriteLock; |
| 5 | + |
| 6 | +import ioio.lib.api.exception.ConnectionLostException; |
| 7 | +import ioio.lib.api.exception.IncompatibilityException; |
10 | 8 |
|
11 | 9 | public class IOLock<I> { |
12 | | - private final ReadWriteLock lock = new ReentrantReadWriteLock(); |
| 10 | + private final Object mutex = new Object(); |
13 | 11 | private Consumer<I> consumer; |
14 | 12 |
|
15 | | - public float invoke(Function<Float, I> function) { |
| 13 | + public void invoke(Consumer<I> consumer) { |
16 | 14 | CountDownLatch latch = beginLatch(); |
17 | | - lock.writeLock().lock(); |
18 | | - AtomicReference<Float> result = new AtomicReference<>(); |
19 | | - try { |
| 15 | + synchronized (mutex) { |
20 | 16 | this.consumer = (i) -> { |
21 | | - result.set(function.apply(i)); |
| 17 | + consumer.accept(i); |
22 | 18 | latch.countDown(); |
23 | 19 | }; |
24 | 20 | } |
25 | | - finally { |
26 | | - lock.writeLock().unlock(); |
27 | | - } |
28 | 21 | endLatch(latch); |
29 | | - return result.get(); |
30 | 22 | } |
31 | 23 |
|
32 | | - public void invoke(Consumer<I> consumer) { |
| 24 | + public float invoke(Function<Float, I> function) { |
33 | 25 | CountDownLatch latch = beginLatch(); |
34 | | - lock.writeLock().lock(); |
35 | | - try { |
| 26 | + AtomicReference<Float> result = new AtomicReference<>(); |
| 27 | + synchronized (mutex) { |
36 | 28 | this.consumer = (i) -> { |
37 | | - consumer.accept(i); |
| 29 | + result.set(function.apply(i)); |
38 | 30 | latch.countDown(); |
39 | 31 | }; |
40 | 32 | } |
41 | | - finally { |
42 | | - lock.writeLock().unlock(); |
43 | | - } |
44 | 33 | endLatch(latch); |
| 34 | + return result.get(); |
45 | 35 | } |
46 | 36 |
|
47 | 37 | public int invokeInt(Function<Integer, I> function) { |
48 | 38 | CountDownLatch latch = beginLatch(); |
49 | | - lock.writeLock().lock(); |
50 | 39 | AtomicReference<Integer> result = new AtomicReference<>(); |
51 | | - try { |
| 40 | + synchronized (mutex) { |
52 | 41 | this.consumer = (i) -> { |
53 | 42 | result.set(function.apply(i)); |
54 | 43 | latch.countDown(); |
55 | 44 | }; |
56 | 45 | } |
57 | | - finally { |
58 | | - lock.writeLock().unlock(); |
59 | | - } |
60 | 46 | endLatch(latch); |
61 | 47 | return result.get(); |
62 | 48 | } |
63 | 49 |
|
64 | 50 | public void process(I input) { |
65 | | - lock.readLock().lock(); |
66 | | - try { |
| 51 | + synchronized (mutex) { |
67 | 52 | if (input != null && consumer != null) { |
68 | | - consumer.accept(input); |
| 53 | + try { |
| 54 | + consumer.accept(input); |
| 55 | + } catch (ConnectionLostException | InterruptedException | IncompatibilityException e) { |
| 56 | + throw new RuntimeException(e); |
| 57 | + } |
69 | 58 | consumer = null; |
70 | 59 | } |
71 | 60 | } |
72 | | - catch (IncompatibilityException | ConnectionLostException | InterruptedException e) { |
73 | | - throw new RuntimeException(e); |
74 | | - } |
75 | | - finally { |
76 | | - lock.readLock().unlock(); |
77 | | - } |
78 | 61 | } |
79 | 62 |
|
80 | 63 | /** |
|
0 commit comments