Skip to content

Commit c29bda8

Browse files
committedMay 16, 2016
Made buffer changes thread-safe.
1 parent 12fbfed commit c29bda8

File tree

3 files changed

+44
-34
lines changed

3 files changed

+44
-34
lines changed
 

‎src/main/java/pl/asie/computronics/audio/SoundCardPacketClientHandler.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525

2626
import java.io.ByteArrayOutputStream;
2727
import java.io.IOException;
28+
import java.util.ArrayDeque;
2829
import java.util.HashMap;
29-
import java.util.LinkedList;
3030
import java.util.Map;
3131
import java.util.Queue;
3232

@@ -52,7 +52,7 @@ protected void readData(Packet packet, int packetId, int codecId) throws IOExcep
5252
String address = packet.readString();
5353
int delay = packet.readInt();
5454
int size = packet.readInt();
55-
Queue<Instruction> buffer = new LinkedList<Instruction>();
55+
Queue<Instruction> buffer = new ArrayDeque<Instruction>();
5656
for(int i = 0; i < size; i++) {
5757
int type = packet.readByte();
5858
switch(type) {

‎src/main/java/pl/asie/computronics/oc/driver/DriverCardSound.java

+40-30
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@
4444
import pl.asie.computronics.util.sound.Instruction.SetVolume;
4545
import pl.asie.computronics.util.sound.Instruction.SetWave;
4646

47+
import java.util.ArrayDeque;
4748
import java.util.Collections;
4849
import java.util.HashMap;
49-
import java.util.LinkedList;
5050
import java.util.Locale;
5151
import java.util.Queue;
5252
import java.util.Set;
@@ -69,9 +69,8 @@ public DriverCardSound(EnvironmentHost host) {
6969

7070
if(host.world().isRemote) {
7171
SyncHandler.envs.add(this);
72-
} else {
73-
buildBuffer = new LinkedList<Instruction>();
7472
}
73+
buildBuffer = new ArrayDeque<Instruction>();
7574
}
7675

7776
private final IAudioReceiver internalSpeaker = new IAudioReceiver() {
@@ -112,7 +111,7 @@ public void receivePacket(AudioPacket packet, ForgeDirection direction) {
112111
};
113112

114113
private AudioUtil.AudioProcess process;
115-
private Queue<Instruction> buildBuffer;
114+
private final ArrayDeque<Instruction> buildBuffer;
116115
private Queue<Instruction> nextBuffer;
117116

118117
private int buildDelay = 0;
@@ -163,7 +162,7 @@ public boolean canUpdate() {
163162
@Override
164163
public void update() {
165164
if(nextBuffer != null && System.currentTimeMillis() >= timeout - 100) {
166-
sendSound(nextDelay, nextBuffer);
165+
sendSound(nextBuffer);
167166
timeout = timeout + nextDelay;
168167
nextBuffer = null;
169168
} else if(codecId != null && System.currentTimeMillis() >= timeout + soundTimeoutMS) {
@@ -188,11 +187,14 @@ public void load(NBTTagCompound nbt) {
188187
}
189188
} else {
190189
if(nbt.hasKey("bbuffer")) {
191-
buildBuffer = Instruction.fromNBT(nbt.getTagList("bbuffer", Constants.NBT.TAG_COMPOUND));
192-
buildDelay = 0;
193-
for(Instruction inst : buildBuffer) {
194-
if(inst instanceof Delay) {
195-
buildDelay += ((Delay) inst).delay;
190+
synchronized(buildBuffer) {
191+
buildBuffer.clear();
192+
buildBuffer.addAll(Instruction.fromNBT(nbt.getTagList("bbuffer", Constants.NBT.TAG_COMPOUND)));
193+
buildDelay = 0;
194+
for(Instruction inst : buildBuffer) {
195+
if(inst instanceof Delay) {
196+
buildDelay += ((Delay) inst).delay;
197+
}
196198
}
197199
}
198200
}
@@ -220,7 +222,9 @@ public void save(NBTTagCompound nbt) {
220222
process.save(processNBT);
221223
if(buildBuffer != null) {
222224
NBTTagList buildNBT = new NBTTagList();
223-
Instruction.toNBT(buildNBT, buildBuffer);
225+
synchronized(buildBuffer) {
226+
Instruction.toNBT(buildNBT, buildBuffer);
227+
}
224228
nbt.setTag("bbuffer", buildNBT);
225229
}
226230
if(nextBuffer != null) {
@@ -233,10 +237,12 @@ public void save(NBTTagCompound nbt) {
233237
}
234238

235239
private Object[] tryAdd(Instruction inst) {
236-
if(buildBuffer.size() >= maxInstructions) {
237-
return new Object[] { false, "too many instructions" };
240+
synchronized(buildBuffer) {
241+
if(buildBuffer.size() >= maxInstructions) {
242+
return new Object[] { false, "too many instructions" };
243+
}
244+
buildBuffer.add(inst);
238245
}
239-
buildBuffer.add(inst);
240246
return new Object[] { true };
241247
}
242248

@@ -289,7 +295,9 @@ public Object[] setTotalVolume(Context context, Arguments args) {
289295
@Callback(doc = "function(); Clears the instruction queue.", direct = true)
290296
@Optional.Method(modid = Mods.OpenComputers)
291297
public Object[] clear(Context context, Arguments args) {
292-
buildBuffer.clear();
298+
synchronized(buildBuffer) {
299+
buildBuffer.clear();
300+
}
293301
buildDelay = 0;
294302
return new Object[] {};
295303
}
@@ -376,20 +384,22 @@ public Object[] setVolume(Context context, Arguments args) {
376384
@Callback(doc = "function(); Starts processing the queue; Returns true is processing began, false if there is still a queue being processed.", direct = true)
377385
@Optional.Method(modid = Mods.OpenComputers)
378386
public Object[] process(Context context, Arguments args) {
379-
if(buildBuffer.size() == 0) {
380-
return new Object[] { true };
381-
}
382-
if(nextBuffer == null) {
383-
nextBuffer = buildBuffer;
384-
nextDelay = buildDelay;
385-
buildBuffer = new LinkedList<Instruction>();
386-
buildDelay = 0;
387-
if(System.currentTimeMillis() > timeout) {
388-
timeout = System.currentTimeMillis();
387+
synchronized(buildBuffer) {
388+
if(buildBuffer.size() == 0) {
389+
return new Object[] { true };
390+
}
391+
if(nextBuffer == null) {
392+
nextBuffer = new ArrayDeque<Instruction>(buildBuffer);
393+
nextDelay = buildDelay;
394+
buildBuffer.clear();
395+
buildDelay = 0;
396+
if(System.currentTimeMillis() > timeout) {
397+
timeout = System.currentTimeMillis();
398+
}
399+
return new Object[] { true };
400+
} else {
401+
return new Object[] { false, System.currentTimeMillis(), timeout };
389402
}
390-
return new Object[] { true };
391-
} else {
392-
return new Object[] { false, System.currentTimeMillis(), timeout };
393403
}
394404
}
395405

@@ -403,9 +413,9 @@ private void sendMusicPacket(int delay, Queue<Instruction> instructions) {
403413
pkt.sendPacket();
404414
}
405415

406-
protected void sendSound(int delay, Queue<Instruction> buffer) {
416+
protected void sendSound(Queue<Instruction> buffer) {
407417
int counter = 0;
408-
Queue<Instruction> sendBuffer = new LinkedList<Instruction>();
418+
Queue<Instruction> sendBuffer = new ArrayDeque<Instruction>();
409419
while(!buffer.isEmpty() || process.delay > 0) {
410420
if(process.delay > 0) {
411421
if(counter + process.delay < packetSizeMS) {

‎src/main/java/pl/asie/computronics/util/sound/Instruction.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ public SetADSR(NBTTagCompound tag) {
215215

216216
@Override
217217
public void encounter(AudioProcess process, State state) {
218-
if (state.envelope != null) {
218+
if(state.envelope != null) {
219219
this.envelope.progress = state.envelope.progress;
220220
this.envelope.phase = state.envelope.phase;
221221
}
@@ -380,7 +380,7 @@ public static Queue<Instruction> fromNBT(NBTTagList l) {
380380
}
381381

382382
public static void toNBT(NBTTagList l, Queue<Instruction> instructions) {
383-
for (Instruction instruction : instructions) {
383+
for(Instruction instruction : instructions) {
384384
NBTTagCompound tag = new NBTTagCompound();
385385
save(tag, instruction);
386386
l.appendTag(tag);

0 commit comments

Comments
 (0)
Please sign in to comment.