Use posix locks instead of pipes.

This commit is contained in:
Russ Cox 2005-10-18 13:45:38 +00:00
parent 82b1d4d6c2
commit 74d480c1e8

View File

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