Unified · Split
| @@ -2,6 +2,7 @@ · 1 unmodified line | ||
| 2 | 2 | * Posix generic OS implementation for drawterm. |
| 3 | 3 | */ |
| 4 | 4 | |
| 5 | +#define _XOPEN_SOURCE 500 | |
| 5 | 6 | #include <pthread.h> |
| 6 | 7 | #include <time.h> |
| 7 | 8 | #include <sys/time.h> |
| @@ -17,7 +18,10 @@ · 9 unmodified lines | ||
| 17 | 18 | typedef struct Oproc Oproc; |
| 18 | 19 | struct 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 | |
| 23 | 27 | static pthread_key_t prdakey; |
| @@ -51,10 +55,14 @@ void · 27 unmodified lines | ||
| 51 | 55 | osnewproc(Proc *p) |
| 52 | 56 | { |
| 53 | 57 | Oproc *op; |
| 58 | + pthread_mutexattr_t attr; | |
| 54 | 59 | |
| 55 | 60 | 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); | |
| 58 | 66 | } |
| 59 | 67 | |
| 60 | 68 | void |
| @@ -130,19 +138,24 @@ procsleep(void) · 69 unmodified lines | ||
| 130 | 138 | |
| 131 | 139 | p = up; |
| 132 | 140 | 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); | |
| 135 | 146 | } |
| 136 | 147 | |
| 137 | 148 | void |
| 138 | 149 | procwakeup(Proc *p) |
| 139 | 150 | { |
| 140 | − char c; | |
| 141 | 151 | Oproc *op; |
| 142 | 152 | |
| 143 | 153 | 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); | |
| 146 | 159 | } |
| 147 | 160 | |
| 148 | 161 | int randfd; |