-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpsem.c
59 lines (52 loc) · 1.05 KB
/
psem.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include "psem.h"
#include "mycommon.h"
#define _GNU_SOURCE
#include <sys/syscall.h>
#define gettid() \
syscall(SYS_gettid ,NULL, NULL, NULL, NULL, NULL)
void
psem_init(psem_t *x, int count) {
x->count = count;
}
psem_t *
psem_create(char *name, int count) {
psem_t *x;
x = (psem_t *)malloc(sizeof(psem_t));
if (!x) {
return x;
}
x->count = count;
pthread_cond_init(&x->sem_cond, NULL);
pthread_mutex_init(&x->sem_mutex, NULL);
return x;
}
void
psem_up(psem_t *x) {
pthread_mutex_lock(&x->sem_mutex);
x->count++;
print_debug("thread %d sem_count %d\n", gettid(), x->count);
pthread_mutex_unlock(&x->sem_mutex);
pthread_cond_broadcast(&x->sem_cond);
}
void
psem_down(psem_t *x) {
pthread_mutex_lock(&x->sem_mutex);
while (1) {
if (x->count > 0) {
x->count--;
break;
}
else {
pthread_cond_wait(&x->sem_cond, &x->sem_mutex);
}
}
print_debug("thread %d sem_count %d\n", gettid(), x->count);
pthread_mutex_unlock(&x->sem_mutex);
}
void
psem_destroy(psem_t *ps) {
free(ps);
}