Unified · Split
| @@ -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 | |||
| 17 | typedef struct Oproc Oproc; | 18 | typedef struct Oproc Oproc; |
| 18 | struct Oproc | 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 | static pthread_key_t prdakey; | 27 | static pthread_key_t prdakey; |
| @@ -51,10 +55,14 @@ void · 27 unmodified lines | |||
| 51 | osnewproc(Proc *p) | 55 | osnewproc(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 | ||
| 60 | void | 68 | void |
| @@ -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 | ||
| 137 | void | 148 | void |
| 138 | procwakeup(Proc *p) | 149 | procwakeup(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 | ||
| 148 | int randfd; | 161 | int randfd; |