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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
#define _GNU_SOURCE
#include <stdio.h>
#include <pthread.h>
#include <time.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <afb/afb-req-itf.h>
#include "../afb-thread.h"
#include "../jobs.h"
struct foo {
int value;
int refcount;
};
void addref(void *closure)
{
struct foo *foo = closure;
foo->refcount++;
}
void unref(void *closure)
{
struct foo *foo = closure;
if(!--foo->refcount) {
printf("%06d FREE\n", foo->value);
free(foo);
}
}
void fail(void *closure, const char *status, const char *info)
{
struct foo *foo = closure;
printf("%06d ERROR %s\n", foo->value, status);
}
struct afb_req_itf itf = {
.json = NULL,
.get = NULL,
.success = NULL,
.fail = fail,
.raw = NULL,
.send = NULL,
.context_get = NULL,
.context_set = NULL,
.addref = addref,
.unref = unref,
.session_close = NULL,
.session_set_LOA = NULL,
.subscribe = NULL,
.unsubscribe = NULL,
.subcall = NULL
};
void process(struct afb_req req)
{
struct timespec ts;
struct foo *foo = req.closure;
printf("%06d PROCESS T%d\n", foo->value, (int)syscall(SYS_gettid));
ts.tv_sec = 0;
ts.tv_nsec = foo->value * 1000;
// nanosleep(&ts, NULL);
}
int main()
{
int i;
struct foo *foo;
struct afb_req req;
struct timespec ts;
req.itf = &itf;
jobs_init(4, 0, 20000);
for (i = 0 ; i < 10000 ; i++) {
req.closure = foo = malloc(sizeof *foo);
foo->value = i;
foo->refcount = 1;
afb_thread_req_call(req, process, 5, (&ts) + (i % 7));
unref(foo);
ts.tv_sec = 0;
ts.tv_nsec = 1000000;
// nanosleep(&ts, NULL);
}
ts.tv_sec = 1;
ts.tv_nsec = 0;
nanosleep(&ts, NULL);
jobs_terminate();
}
|