shithub: riscv

Download patch

ref: c10e21b491937315a096e3e83840886fd7ba9ae8
parent: e45bd6814a125e7a175ff19e23252608a060d847
author: cinap_lenrek <[email protected]>
date: Mon Dec 7 09:31:02 EST 2020

libthread: reduce stack usage of threadkill*(), open /proc/n/ctl with OCEXEC flag

--- a/sys/src/libthread/kill.c
+++ b/sys/src/libthread/kill.c
@@ -76,24 +76,31 @@
 	threadxxx(id, 0);
 }
 
-static void
-tinterrupt(Proc *p, Thread *t)
+static int
+writeprocctl(int pid, char *ctl)
 {
-	char buf[64];
+	char buf[32];
 	int fd;
 
+	snprint(buf, sizeof(buf), "/proc/%lud/ctl", (ulong)pid);
+	fd = open(buf, OWRITE|OCEXEC);
+	if(fd < 0)
+		return -1;
+	if(write(fd, ctl, strlen(ctl)) < 0){
+		close(fd);
+		return -1;
+	}
+	close(fd);
+	return 0;
+}
+
+static void
+tinterrupt(Proc *p, Thread *t)
+{
 	switch(t->state){
 	case Running:
-		snprint(buf, sizeof(buf), "/proc/%d/ctl", p->pid);
-		fd = open(buf, OWRITE|OCEXEC);
-		if(fd >= 0){
-			if(write(fd, "interrupt", 9) == 9){
-				close(fd);
-				break;
-			}
-			close(fd);
-		}
-		postnote(PNPROC, p->pid, "threadint");
+		if(writeprocctl(p->pid, "interrupt") < 0)
+			postnote(PNPROC, p->pid, "threadint");
 		break;
 	case Rendezvous:
 		_threadflagrendez(t);