Skip to content

Commit d57d4c5

Browse files
committed
IOIO: lock now uses simpler mutex object for synchronisation
1 parent b872760 commit d57d4c5

File tree

13 files changed

+109
-86
lines changed

13 files changed

+109
-86
lines changed

ioio/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ FUNC_SIG lib_proc[] = {
251251
{0, 0, "HARDRESET", cmd_ioio_hardreset},
252252
{0, 0, "SOFTRESET", cmd_ioio_softreset},
253253
{0, 0, "SYNC", cmd_ioio_sync},
254-
{0, 0, "WAITFORCONNECT", cmd_ioio_waitforconnect},
254+
{1, 1, "WAITFORCONNECT", cmd_ioio_waitforconnect},
255255
{0, 0, "WAITFORDISCONNECT", cmd_ioio_waitfordisconnect},
256256
};
257257

ioio/samples/duino-1088AS.bas

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,20 @@ const dinPin = 2
4848
const csPin = 3
4949
const clkPin = 4
5050

51-
ioio.waitForConnect(10)
52-
5351
din = ioio.openDigitalOutput(dinPin)
5452
cs = ioio.openDigitalOutput(csPin)
5553
clk = ioio.openDigitalOutput(clkPin)
5654

55+
ioio.waitForConnect(10)
56+
5757
while 1
58-
shiftOut(0x55)
58+
shiftOut(0x55)
59+
delay 10000
5960
wend
6061

6162
sub shiftOut(_data)
6263
cs.write(false); ' Enable the chip
63-
for i = 7 to 0
64+
for i = 7 to 0 step -1
6465
clk.write(false) ' Start clock pulse
6566
din.write((_data band (1 lshift i)) != 0) ' Send bit
6667
clk.write(true) ' End clock pulse

ioio/samples/led.bas

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,3 @@ for i = 0 to 5
2020
next
2121

2222
print "done"
23-
24-
25-

ioio/samples/slider-pot.bas

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
11
import ioio
22

3+
rem
4+
rem PIN CONNECTIONS:
5+
rem GND -> GND
6+
rem VCC -> 3.3v
7+
rem OTA -> 46
8+
rem
9+
310
analogIn = ioio.openAnalogInput(46)
11+
out = ioio.openDigitalOutput(0)
12+
413
ioio.waitForConnect(10)
514

615
while 1
7-
print analogIn.read()
16+
n = analogIn.read()
17+
out.write(n > .5)
18+
print n
819
delay 10
920
wend

ioio/src/main/java/ioio/PausedInputStream.java

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,49 @@
11
package ioio;
22

3-
import java.io.BufferedInputStream;
43
import java.io.IOException;
54
import java.io.InputStream;
65

76
/**
87
* Pause between read() to avoid excessive CPU usage
98
*/
10-
public class PausedInputStream extends BufferedInputStream {
9+
public class PausedInputStream extends InputStream {
1110
private long lastAccessMillis;
11+
private final InputStream wrapped;
12+
private boolean closed;
1213

1314
public PausedInputStream(InputStream inputStream) {
14-
super(inputStream);
15-
lastAccessMillis = System.currentTimeMillis();
15+
this.wrapped = inputStream;
16+
this.lastAccessMillis = System.currentTimeMillis();
17+
this.closed = false;
1618
}
1719

1820
@Override
19-
public synchronized int read(byte[] bytes, int i, int i1) throws IOException {
20-
pause();
21-
return super.read(bytes, i, i1);
21+
public void close() throws IOException {
22+
wrapped.close();
23+
closed = true;
24+
}
25+
26+
@Override
27+
public synchronized int read(byte[] bytes, int offset, int len) throws IOException {
28+
throw new UnsupportedOperationException();
2229
}
2330

2431
@Override
2532
public int read(byte[] bytes) throws IOException {
2633
pause();
27-
return super.read(bytes);
34+
int result = -1;
35+
while (!closed) {
36+
result = wrapped.read(bytes);
37+
if (result > 0) {
38+
break;
39+
}
40+
}
41+
return result;
2842
}
2943

3044
@Override
3145
public synchronized int read() throws IOException {
32-
pause();
33-
return super.read();
46+
throw new UnsupportedOperationException();
3447
}
3548

3649
private void pause() throws IOException {

ioio/src/main/java/ioio/lib/pc/SerialPortIOIOConnection.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@
2828
*/
2929
package ioio.lib.pc;
3030

31+
import java.io.BufferedOutputStream;
32+
import java.io.IOException;
33+
import java.io.InputStream;
34+
import java.io.OutputStream;
35+
3136
import ioio.PausedInputStream;
3237
import ioio.lib.api.IOIOConnection;
3338
import ioio.lib.api.exception.ConnectionLostException;
@@ -38,11 +43,6 @@
3843
import purejavacomm.NoSuchPortException;
3944
import purejavacomm.SerialPort;
4045

41-
import java.io.BufferedOutputStream;
42-
import java.io.IOException;
43-
import java.io.InputStream;
44-
import java.io.OutputStream;
45-
4646
class SerialPortIOIOConnection implements IOIOConnection {
4747
private final String name_;
4848
// private static final String TAG = "SerialPortIOIOConnection";

ioio/src/main/java/ioio/lib/pc/SerialPortIOIOConnectionBootstrap.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929

3030
package ioio.lib.pc;
3131

32+
import java.util.Collection;
33+
import java.util.Enumeration;
34+
import java.util.LinkedList;
35+
import java.util.List;
36+
3237
import ioio.lib.api.IOIOConnection;
3338
import ioio.lib.spi.IOIOConnectionBootstrap;
3439
import ioio.lib.spi.IOIOConnectionFactory;
@@ -37,11 +42,6 @@
3742
import purejavacomm.CommPortIdentifier;
3843
import purejavacomm.PortInUseException;
3944

40-
import java.util.Collection;
41-
import java.util.Enumeration;
42-
import java.util.LinkedList;
43-
import java.util.List;
44-
4545
public class SerialPortIOIOConnectionBootstrap implements IOIOConnectionBootstrap {
4646
private static final String TAG = "SerialPortIOIOConnectionBootstrap";
4747

ioio/src/main/java/net/sourceforge/smallbasic/ioio/DigitalOutputImpl.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@
55
import ioio.lib.api.exception.ConnectionLostException;
66
import ioio.lib.spi.Log;
77

8-
import java.util.concurrent.atomic.AtomicBoolean;
9-
108
public class DigitalOutputImpl extends IOTask implements DigitalOutput {
119
private static final String TAG = "DigitalOutput";
12-
private final AtomicBoolean value = new AtomicBoolean(false);
10+
private final IOLock<DigitalOutput> lock = new IOLock<>();
1311
private DigitalOutput output;
1412

1513
public DigitalOutputImpl() {
@@ -26,7 +24,7 @@ public void close() {
2624

2725
@Override
2826
public void loop() throws ConnectionLostException {
29-
output.write(value.get());
27+
lock.process(output);
3028
}
3129

3230
@Override
@@ -41,7 +39,10 @@ public void setup(IOIO ioio) {
4139
}
4240

4341
@Override
44-
public void write(boolean value) {
45-
this.value.set(value);
42+
public void write(final boolean value) {
43+
handleError();
44+
lock.invoke((i) -> {
45+
i.write(value);
46+
});
4647
}
4748
}

ioio/src/main/java/net/sourceforge/smallbasic/ioio/IOIOImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package net.sourceforge.smallbasic.ioio;
22

3+
import java.io.IOException;
4+
35
import ioio.TimerUtil;
46
import ioio.lib.api.IOIO;
57
import ioio.lib.spi.Log;
68

7-
import java.io.IOException;
8-
99
public class IOIOImpl extends IOTask {
1010
private static final String TAG = "IOIOImpl";
1111
private final IOLock<IOIO> lock = new IOLock<>();

ioio/src/main/java/net/sourceforge/smallbasic/ioio/IOLock.java

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,63 @@
11
package net.sourceforge.smallbasic.ioio;
22

3-
import ioio.lib.api.exception.ConnectionLostException;
4-
import ioio.lib.api.exception.IncompatibilityException;
5-
63
import java.util.concurrent.CountDownLatch;
74
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;
108

119
public class IOLock<I> {
12-
private final ReadWriteLock lock = new ReentrantReadWriteLock();
10+
private final Object mutex = new Object();
1311
private Consumer<I> consumer;
1412

15-
public float invoke(Function<Float, I> function) {
13+
public void invoke(Consumer<I> consumer) {
1614
CountDownLatch latch = beginLatch();
17-
lock.writeLock().lock();
18-
AtomicReference<Float> result = new AtomicReference<>();
19-
try {
15+
synchronized (mutex) {
2016
this.consumer = (i) -> {
21-
result.set(function.apply(i));
17+
consumer.accept(i);
2218
latch.countDown();
2319
};
2420
}
25-
finally {
26-
lock.writeLock().unlock();
27-
}
2821
endLatch(latch);
29-
return result.get();
3022
}
3123

32-
public void invoke(Consumer<I> consumer) {
24+
public float invoke(Function<Float, I> function) {
3325
CountDownLatch latch = beginLatch();
34-
lock.writeLock().lock();
35-
try {
26+
AtomicReference<Float> result = new AtomicReference<>();
27+
synchronized (mutex) {
3628
this.consumer = (i) -> {
37-
consumer.accept(i);
29+
result.set(function.apply(i));
3830
latch.countDown();
3931
};
4032
}
41-
finally {
42-
lock.writeLock().unlock();
43-
}
4433
endLatch(latch);
34+
return result.get();
4535
}
4636

4737
public int invokeInt(Function<Integer, I> function) {
4838
CountDownLatch latch = beginLatch();
49-
lock.writeLock().lock();
5039
AtomicReference<Integer> result = new AtomicReference<>();
51-
try {
40+
synchronized (mutex) {
5241
this.consumer = (i) -> {
5342
result.set(function.apply(i));
5443
latch.countDown();
5544
};
5645
}
57-
finally {
58-
lock.writeLock().unlock();
59-
}
6046
endLatch(latch);
6147
return result.get();
6248
}
6349

6450
public void process(I input) {
65-
lock.readLock().lock();
66-
try {
51+
synchronized (mutex) {
6752
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+
}
6958
consumer = null;
7059
}
7160
}
72-
catch (IncompatibilityException | ConnectionLostException | InterruptedException e) {
73-
throw new RuntimeException(e);
74-
}
75-
finally {
76-
lock.readLock().unlock();
77-
}
7861
}
7962

8063
/**

0 commit comments

Comments
 (0)