made it so that zemaphore is used on mac, semaphore on linux, and other clean up
This commit is contained in:
37
threads-sema/zemaphore.h
Normal file
37
threads-sema/zemaphore.h
Normal 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__
|
||||
Reference in New Issue
Block a user