shithub: qk1

Download patch

ref: db9b193ab08b9e033e80887e31ac7cca8203d906
parent: e65eec36dd5aa7223691f9557e8236ee78fbd1a9
author: qwx <qwx@sciops.net>
date: Sat Oct 12 10:53:39 EDT 2024

plan9/snd: fix race condition

reported only on amd64 and 386, did not occur on arm64.

--- a/snd_plan9.c
+++ b/snd_plan9.c
@@ -1,9 +1,13 @@
 #include "quakedef.h"
 #include <thread.h>
 
+typedef struct Mixbuf Mixbuf;
+struct Mixbuf{
+	byte *buf;
+	long sz;
+};
+
 static int afd;
-static QLock alock;
-static uchar *mixbuf;
 static Channel *ach;
 
 long
@@ -20,15 +24,14 @@
 static void
 auproc(void *p)
 {
-	long sz, n;
+	Mixbuf m;
+	long n;
 
 	for(;;){
-		if((sz = recvul(p)) < 1)
+		if(recv(p, &m) < 0)
 			break;
-		qlock(&alock);
-		n = write(afd, mixbuf, sz);
-		qunlock(&alock);
-		if(n != sz){
+		n = write(afd, m.buf, m.sz);
+		if(n != m.sz){
 			Con_DPrintf("sndwrite: %r\n");
 			break;
 		}
@@ -44,12 +47,12 @@
 void
 sndwrite(uchar *buf, long sz)
 {
+	Mixbuf m;
+
 	if(ach == nil)
 		return;
-	qlock(&alock);
-	sendul(ach, sz);
-	mixbuf = buf;
-	qunlock(&alock);
+	m = (Mixbuf){buf, sz};
+	send(ach, &m);
 }
 
 void
@@ -67,7 +70,7 @@
 {
 	if((afd = open("/dev/audio", OWRITE)) < 0)
 		return -1;
-	ach = chancreate(sizeof(ulong), 0);
+	ach = chancreate(sizeof(Mixbuf), 1);
 	proccreate(auproc, ach, 4096);
 	return 0;
 }