History

Use posix locks instead of pipes.

Russ Cox committed 21 years ago

Commit 74d480c1 parent 82b1d4d1 file · +21 −8Browse files

Unified · Split

kern/posix.c+21−8
@@ -2,6 +2,7 @@ · 1 unmodified line
2 * Posix generic OS implementation for drawterm.2 * Posix generic OS implementation for drawterm.
3 */3 */
4 4
5#define _XOPEN_SOURCE 500
5#include <pthread.h>6#include <pthread.h>
6#include <time.h>7#include <time.h>
7#include <sys/time.h>8#include <sys/time.h>
@@ -17,7 +18,10 @@ · 9 unmodified lines
17typedef struct Oproc Oproc;18typedef struct Oproc Oproc;
18struct Oproc19struct Oproc
19{20{
20 int p[2];21 int nsleep;
22 int nwakeup;
23 pthread_mutex_t mutex;
24 pthread_cond_t cond;
21};25};
22 26
23static pthread_key_t prdakey;27static pthread_key_t prdakey;
@@ -51,10 +55,14 @@ void · 27 unmodified lines
51osnewproc(Proc *p)55osnewproc(Proc *p)
52{56{
53 Oproc *op;57 Oproc *op;
58 pthread_mutexattr_t attr;
54 59
55 op = (Oproc*)p->oproc;60 op = (Oproc*)p->oproc;
56 if(pipe(op->p) < 0)61 pthread_mutexattr_init(&attr);
57 panic("cannot pipe");62 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
63 pthread_mutex_init(&op->mutex, &attr);
64 pthread_mutexattr_destroy(&attr);
65 pthread_cond_init(&op->cond, 0);
58}66}
59 67
60void68void
@@ -130,19 +138,24 @@ procsleep(void) · 69 unmodified lines
130 138
131 p = up;139 p = up;
132 op = (Oproc*)p->oproc;140 op = (Oproc*)p->oproc;
133 while(read(op->p[0], &c, 1) != 1)141 pthread_mutex_lock(&op->mutex);
134 ;142 op->nsleep++;
143 while(op->nsleep > op->nwakeup)
144 pthread_cond_wait(&op->cond, &op->mutex);
145 pthread_mutex_unlock(&op->mutex);
135}146}
136 147
137void148void
138procwakeup(Proc *p)149procwakeup(Proc *p)
139{150{
140 char c;
141 Oproc *op;151 Oproc *op;
142 152
143 op = (Oproc*)p->oproc;153 op = (Oproc*)p->oproc;
144 c = 'a';154 pthread_mutex_lock(&op->mutex);
145 write(op->p[1], &c, 1);155 op->nwakeup++;
156 if(op->nwakeup == op->nsleep)
157 pthread_cond_signal(&op->cond);
158 pthread_mutex_unlock(&op->mutex);
146}159}
147 160
148int randfd;161int randfd;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t