import
This commit is contained in:
0
libxasync/.dirstamp
Normal file
0
libxasync/.dirstamp
Normal file
43
libxasync/waitgroup.c
Normal file
43
libxasync/waitgroup.c
Normal file
@@ -0,0 +1,43 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
//#include <unistd.h>
|
||||
//#include <semaphore.h>
|
||||
//#include <pthread.h>
|
||||
//#include <stdatomic.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include <waitgroup.h>
|
||||
|
||||
wg_t* new_gw(void) {
|
||||
wg_t* wg = malloc(sizeof(wg_t));
|
||||
wg->num = 0;
|
||||
sem_init(&(wg->sem), 1, 0);
|
||||
return wg;
|
||||
}
|
||||
|
||||
void wg_init(wg_t* wg) {
|
||||
wg->num = 0;
|
||||
sem_init(&(wg->sem), 1, 0);
|
||||
}
|
||||
|
||||
void wg_add(wg_t* wg) {
|
||||
wg->num++;
|
||||
}
|
||||
|
||||
void wg_done(wg_t* wg) {
|
||||
if ((--wg->num) == 0) sem_post(&(wg->sem));
|
||||
}
|
||||
|
||||
void wg_wait(wg_t* wg) {
|
||||
if ((wg->num) < 1) sem_post(&(wg->sem));
|
||||
sem_wait(&(wg->sem));
|
||||
}
|
||||
|
||||
void wg_destroy(wg_t* wg) {
|
||||
sem_destroy(&(wg->sem));
|
||||
}
|
||||
|
||||
void wg_free(wg_t* wg) {
|
||||
sem_destroy(&(wg->sem));
|
||||
free(wg);
|
||||
}
|
||||
23
libxasync/waitgroup.h
Normal file
23
libxasync/waitgroup.h
Normal file
@@ -0,0 +1,23 @@
|
||||
|
||||
#ifndef WAITGROUP_H_QWERTY
|
||||
#define WAITGROUP_H_QWERTY
|
||||
|
||||
#include <semaphore.h>
|
||||
#include <pthread.h>
|
||||
#include <stdatomic.h>
|
||||
#include <stdio.h>
|
||||
|
||||
typedef struct {
|
||||
sem_t sem;
|
||||
atomic_int num;
|
||||
} wg_t;
|
||||
|
||||
wg_t* new_gw(void);
|
||||
void wg_init(wg_t* wg);
|
||||
void wg_add(wg_t* wg);
|
||||
void wg_done(wg_t* wg);
|
||||
void wg_wait(wg_t* wg);
|
||||
void wg_destroy(wg_t* wg);
|
||||
void wg_free(wg_t* wg);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user