1tevent_data(3) tevent tevent_data(3)
2
3
4
6 tevent_data - Chapter 3: Accessing data
7
9 A tevent request is (usually) created together with a structure for
10 storing the data necessary for an asynchronous computation. For these
11 private data, tevent library uses void (generic) pointers, therefore
12 any data type can be very simply pointed at. However, this attitude
13 requires clear and guaranteed knowledge of the data type that will be
14 handled, in advance. Private data can be of 2 types: connected with a
15 request itself or given as an individual argument to a callback. It is
16 necessary to differentiate these types, because there is a slightly
17 different method of data access for each. There are two possibilities
18 how to access data that is given as an argument directly to a callback.
19 The difference lies in the pointer that is returned. In one case it is
20 the data type specified in the function’s argument, in another void* is
21 returned.
22
23 void tevent_req_callback_data (struct tevent_req *req, #type)
24 void tevent_req_callback_data_void (struct tevent_req *req)
25
26 To obtain data that are strictly bound to a request, this function is
27 the only direct procedure.
28
29 void *tevent_req_data (struct tevent_req *req, #type)
30
31 Example with both calls which differs between private data within
32 tevent request and data handed over as an argument.
33
34 #include <stdio.h>
35 #include <unistd.h>
36 #include <tevent.h>
37
38 struct foo_state {
39 int x;
40 };
41
42 struct testA {
43 int y;
44 };
45
46
47 static void foo_done(struct tevent_req *req) {
48 // a->x contains 10 since it came from foo_send
49 struct foo_state *a = tevent_req_data(req, struct foo_state);
50
51 // b->y contains 9 since it came from run
52 struct testA *b = tevent_req_callback_data(req, struct testA);
53
54 // c->y contains 9 since it came from run we just used a different way
55 // of getting it.
56 struct testA *c = (struct testA *)tevent_req_callback_data_void(req);
57
58 printf('a->x: %d0, a->x);
59 printf('b->y: %d0, b->y);
60 printf('c->y: %d0, c->y);
61 }
62
63
64 struct tevent_req * foo_send(TALLOC_CTX *mem_ctx, struct tevent_context *event_ctx) {
65
66 printf('_send0);
67 struct tevent_req *req;
68 struct foo_state *state;
69
70 req = tevent_req_create(event_ctx, &state, struct foo_state);
71 state->x = 10;
72
73 return req;
74 }
75
76 static void run(struct tevent_context *ev, struct tevent_timer *te,
77 struct timeval current_time, void *private_data) {
78 struct tevent_req *req;
79 struct testA *tmp = talloc(ev, struct testA);
80
81 // Note that we did not use the private data passed in
82
83 tmp->y = 9;
84 req = foo_send(ev, ev);
85
86 tevent_req_set_callback(req, foo_done, tmp);
87 tevent_req_done(req);
88
89 }
90
91 int main (int argc, char **argv) {
92
93 struct tevent_context *event_ctx;
94 struct testA *data;
95 TALLOC_CTX *mem_ctx;
96 struct tevent_timer *time_event;
97
98 mem_ctx = talloc_new(NULL); //parent
99 if (mem_ctx == NULL)
100 return EXIT_FAILURE;
101
102 event_ctx = tevent_context_init(mem_ctx);
103 if (event_ctx == NULL)
104 return EXIT_FAILURE;
105
106 data = talloc(mem_ctx, struct testA);
107 data->y = 11;
108
109 time_event = tevent_add_timer(event_ctx,
110 mem_ctx,
111 tevent_timeval_current(),
112 run,
113 data);
114 if (time_event == NULL) {
115 fprintf(stderr, ' FAILED0);
116 return EXIT_FAILURE;
117 }
118
119 tevent_loop_once(event_ctx);
120
121 talloc_free(mem_ctx);
122
123 printf('Quit0);
124 return EXIT_SUCCESS;
125 }
126
127 Output of this example is:
128
129 a->x: 10
130 b->y: 9
131 c->y: 9
132
133Version 0.9.8 12 Apr 2016 tevent_data(3)