made it so that zemaphore is used on mac, semaphore on linux, and other clean up

This commit is contained in:
Remzi Arpaci-Dusseau
2019-05-16 16:07:33 -05:00
parent bb639b166c
commit 3faa28e520
12 changed files with 146 additions and 35 deletions

37
threads-sema/zemaphore.h Normal file
View File

@@ -0,0 +1,37 @@
#ifndef __zemaphore_h__
#define __zemaphore_h__
typedef struct __Zem_t {
int value;
pthread_cond_t cond;
pthread_mutex_t lock;
} Zem_t;
void Zem_init(Zem_t *z, int value) {
z->value = value;
Cond_init(&z->cond);
Mutex_init(&z->lock);
}
void Zem_wait(Zem_t *z) {
Mutex_lock(&z->lock);
while (z->value <= 0)
Cond_wait(&z->cond, &z->lock);
z->value--;
Mutex_unlock(&z->lock);
}
void Zem_post(Zem_t *z) {
Mutex_lock(&z->lock);
z->value++;
Cond_signal(&z->cond);
Mutex_unlock(&z->lock);
}
typedef Zem_t sem_t;
#define Sem_wait(s) Zem_wait(s)
#define Sem_post(s) Zem_post(s)
#define Sem_init(s, v) Zem_init(s, v)
#endif // __zemaphore_h__