-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
45 lines (40 loc) · 878 Bytes
/
test.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
#include "src/croutine.h"
#include "defer.h"
#include <unistd.h>
int sort(int n) {
sleep(n);
printf("%3d", n);
return n;
}
void sleep_sort(int size, int arr[static size]) {
typedef struct {
int n;
wg_t* wg;
} wrapper_args;
void wrapper(void* args) {
wrapper_args* arg = args;
defer({
wg_done(arg->wg);
free(arg);
});
sort(arg->n);
}
wg_t* wg = wg_init();
wg_add(wg, size);
defer({
wg_wait(wg);
printf("\n");
});
defer(wg_deinit(wg));
for (int i = 0; i < size; i++) {
wrapper_args* args = malloc(sizeof (wrapper_args));
args->n = arr[i];
args->wg = wg;
co(wrapper, args);
}
}
int main(void) {
int arr[] = {6,9,4,2,0};
sleep_sort(sizeof (arr) / sizeof (int), arr);
return 0;
}