42 lines
590 B
C
42 lines
590 B
C
#include "count.h"
|
|
|
|
void count_init(count_t *c) {
|
|
c->cnt = 0;
|
|
|
|
pthread_mutex_init(&c->mutex, NULL);
|
|
}
|
|
|
|
void count_destroy(count_t *c) {
|
|
pthread_mutex_destroy(&c->mutex);
|
|
}
|
|
|
|
int count_inc(count_t *c) {
|
|
int i;
|
|
|
|
pthread_mutex_lock(&c->mutex);
|
|
i = ++c->cnt;
|
|
pthread_mutex_unlock(&c->mutex);
|
|
|
|
return i;
|
|
}
|
|
|
|
int count_dec(count_t *c) {
|
|
int i;
|
|
|
|
pthread_mutex_lock(&c->mutex);
|
|
i = --c->cnt;
|
|
pthread_mutex_unlock(&c->mutex);
|
|
|
|
return i;
|
|
}
|
|
|
|
int count_get(count_t *c) {
|
|
int i;
|
|
|
|
pthread_mutex_lock(&c->mutex);
|
|
i = c->cnt;
|
|
pthread_mutex_unlock(&c->mutex);
|
|
|
|
return i;
|
|
}
|