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
22 * Posix generic OS implementation for drawterm.
33 */
44
5+#define _XOPEN_SOURCE 500
56 #include <pthread.h>
67 #include <time.h>
78 #include <sys/time.h>
@@ -17,7 +18,10 @@ · 9 unmodified lines
1718 typedef struct Oproc Oproc;
1819 struct Oproc
1920 {
20− int p[2];
21+ int nsleep;
22+ int nwakeup;
23+ pthread_mutex_t mutex;
24+ pthread_cond_t cond;
2125 };
2226
2327 static pthread_key_t prdakey;
@@ -51,10 +55,14 @@ void · 27 unmodified lines
5155 osnewproc(Proc *p)
5256 {
5357 Oproc *op;
58+ pthread_mutexattr_t attr;
5459
5560 op = (Oproc*)p->oproc;
56− if(pipe(op->p) < 0)
57− panic("cannot pipe");
61+ pthread_mutexattr_init(&attr);
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);
5866 }
5967
6068 void
@@ -130,19 +138,24 @@ procsleep(void) · 69 unmodified lines
130138
131139 p = up;
132140 op = (Oproc*)p->oproc;
133− while(read(op->p[0], &c, 1) != 1)
134− ;
141+ pthread_mutex_lock(&op->mutex);
142+ op->nsleep++;
143+ while(op->nsleep > op->nwakeup)
144+ pthread_cond_wait(&op->cond, &op->mutex);
145+ pthread_mutex_unlock(&op->mutex);
135146 }
136147
137148 void
138149 procwakeup(Proc *p)
139150 {
140− char c;
141151 Oproc *op;
142152
143153 op = (Oproc*)p->oproc;
144− c = 'a';
145− write(op->p[1], &c, 1);
154+ pthread_mutex_lock(&op->mutex);
155+ op->nwakeup++;
156+ if(op->nwakeup == op->nsleep)
157+ pthread_cond_signal(&op->cond);
158+ pthread_mutex_unlock(&op->mutex);
146159 }
147160
148161 int 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