shithub: npe

Download patch

ref: 7c9d7652b5a768d74376c95b773fc20db9bf9ce6
parent: 9c86d94662c9232add279d1240d825de7557e02a
author: qwx <qwx@sciops.net>
date: Mon Mar 9 05:59:47 EDT 2026

sdl2: audiostream: set lower priority for writer, fix termination issues

--- a/libnpe_sdl2/audiostm.c
+++ b/libnpe_sdl2/audiostm.c
@@ -54,6 +54,24 @@
 	[AUDIO_F32MSB] = {"F32", -1}, /* FIXME big endian f32 not supported by pcmconv */
 };
 
+static int
+setpri(int pri)
+{
+	int n, fd, pid;
+	char path[32];
+
+	if((pid = getpid()) == 0)
+		return -1;
+	snprint(path, sizeof path, "/proc/%d/ctl", pid);
+	if((fd = open(path, OWRITE)) < 0)
+		return -1;
+	n = fprint(fd, "pri %d\n", pri);
+	close(fd);
+	if(n < 0)
+		return -1;
+	return 0;
+}
+
 int
 SDL_AudioStreamAvailable(SDL_AudioStream *as)
 {
@@ -193,6 +211,8 @@
 	SDL_AudioStream *as;
 
 	threadsetname("awproc");
+	if(setpri(13) < 0)
+		fprint(2, "setpri: %r");
 	as = arg;
 	m = as->framesz;
 	p = as->buf;
@@ -250,22 +270,21 @@
 
 /* FIXME: bullshit */
 static void
-acbproc(void *arg)
+acbproc(void *)
 {
 	int n;
 	SDL_AudioStream *as, *ae;
-	Channel *c;
 
 	threadsetname("acbproc");
-	c = arg;
 	for(;;){
-		if(recv(c, &n) < 0)
+		if(recv(ach, &n) < 0)
 			break;
 		for(as=devs, ae=as+ndevs; as<ae; as++)
 			if(as->fd >= 0 && as->fn != nil)
 				as->fn(as->aux, as, n, agetl(&as->avail));
 	}
-	chanfree(c);
+	chanfree(ach);
+	ach = nil;
 }
 
 SDL_AudioStream* SDL_NewAudioStream(SDL_AudioFormat s, Uint8 sch, int srate, SDL_AudioFormat d, Uint8 dch, int drate)
@@ -425,8 +444,6 @@
 {
 	SDL_AudioStream *as, *ae;
 
-	if(ach != nil)
-		chanclose(ach);
 	for(as=devs, ae=as+ndevs; as<ae; as++)
 		SDL_FreeAudioStream(as);
 }
@@ -434,11 +451,13 @@
 int
 npe_sdl_init_audio(void)
 {
+	if(ach != nil)
+		return 0;
 	if(probe() < 0)
 		return -1;
 	if((ach = chancreate(sizeof(int), 16)) == nil)
 		return -1;
-	if(proccreate(acbproc, ach, 8192) < 0)
+	if(proccreate(acbproc, nil, 8192) < 0)
 		return -1;
 	return 0;
 }
--