shithub: drawterm

Download patch

ref: 74d480c1e8fcf5817fa2963d504d90ccc478ffc2
parent: 82b1d4d6c238074e42e8721a4b8f0d3f7e62756d
author: Russ Cox <[email protected]>
date: Tue Oct 18 09:45:38 EDT 2005

Use posix locks instead of pipes.

--- a/kern/posix.c
+++ b/kern/posix.c
@@ -2,6 +2,7 @@
  * Posix generic OS implementation for drawterm.
  */
 
+#define _XOPEN_SOURCE 500
 #include <pthread.h>
 #include <time.h>
 #include <sys/time.h>
@@ -17,7 +18,10 @@
 typedef struct Oproc Oproc;
 struct Oproc
 {
-	int p[2];
+	int nsleep;
+	int nwakeup;
+	pthread_mutex_t mutex;
+	pthread_cond_t cond;
 };
 
 static pthread_key_t prdakey;
@@ -51,10 +55,14 @@
 osnewproc(Proc *p)
 {
 	Oproc *op;
+	pthread_mutexattr_t attr;
 
 	op = (Oproc*)p->oproc;
-	if(pipe(op->p) < 0)
-		panic("cannot pipe");
+	pthread_mutexattr_init(&attr);
+	pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
+	pthread_mutex_init(&op->mutex, &attr);
+	pthread_mutexattr_destroy(&attr);
+	pthread_cond_init(&op->cond, 0);
 }
 
 void
@@ -130,19 +138,24 @@
 
 	p = up;
 	op = (Oproc*)p->oproc;
-	while(read(op->p[0], &c, 1) != 1)
-		;
+	pthread_mutex_lock(&op->mutex);
+	op->nsleep++;
+	while(op->nsleep > op->nwakeup)
+		pthread_cond_wait(&op->cond, &op->mutex);
+	pthread_mutex_unlock(&op->mutex);
 }
 
 void
 procwakeup(Proc *p)
 {
-	char c;
 	Oproc *op;
 
 	op = (Oproc*)p->oproc;
-	c = 'a';
-	write(op->p[1], &c, 1);
+	pthread_mutex_lock(&op->mutex);
+	op->nwakeup++;
+	if(op->nwakeup == op->nsleep)
+		pthread_cond_signal(&op->cond);
+	pthread_mutex_unlock(&op->mutex);
 }
 
 int randfd;