2 Unix SMB/CIFS implementation.
4 Coroutine abstraction for tevent.
6 Copyright (C) Stefan Metzmacher 2009
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "tevent_coroutine.h"
29 typedef void *coroutine_t;
32 coroutine_t co_create(void (*func)(void *), void *data, void *stack, int size);
33 void co_delete(coroutine_t coro);
34 void co_call(coroutine_t coro);
36 void co_exit_to(coroutine_t coro);
38 coroutine_t co_current(void);
41 struct tevent_coroutine;
43 struct tevent_coroutine_result {
45 TEVENT_COROUTINE_INIT = 0,
46 TEVENT_COROUTINE_IN_PROGRESS,
47 TEVENT_COROUTINE_YIELD,
48 TEVENT_COROUTINE_DONE,
49 TEVENT_COROUTINE_NO_MEMORY,
50 TEVENT_COROUTINE_USER_ERROR
54 const char *return_location;
57 struct tevent_coroutine {
58 struct tevent_req *req;
59 struct tevent_context *ev;
60 const char *create_location;
61 tevent_coroutine_fn_t fn;
62 struct tevent_coroutine_result result;
67 static void tevent_coroutine_body(void *data);
69 struct tevent_coroutine *_tevent_coroutine_create(
70 struct tevent_req *req,
71 struct tevent_context *ev,
72 tevent_coroutine_fn_t fn,
75 void *state = _tevent_req_data(req);
76 struct tevent_coroutine *tco;
78 tco = talloc(state, struct tevent_coroutine);
85 tco->create_location = location;
87 ZERO_STRUCT(tco->result);
88 tco->coro = co_create(tevent_coroutine_body, tco,
89 tco->stack, sizeof(tco->stack));
90 if (tco->coro == NULL) {
98 static void tevent_coroutine_body(void *data)
100 struct tevent_coroutine *tco = talloc_get_type_abort(data,
101 struct tevent_coroutine);
102 void *fn_state = _tevent_req_data(tco->req);
103 struct tevent_coroutine_result *res;
105 /* now call the real implementation */
106 res = tco->fn(tco, tco->ev, fn_state);
109 * now destroy the coroutine_t handle and jump back to
110 * tevent_coroutine_run() after co_call().
116 void tevent_coroutine_run(struct tevent_coroutine *tco)
118 coroutine_t cur = co_current();
119 if (tco->coro == cur) {
120 /* this should never happen */
124 tco->result.state = TEVENT_COROUTINE_IN_PROGRESS;
126 * now start or continue the given coroutine
127 * on its own stack. co_call() returns when
128 * the coroutine has called co_resume() (via tevent_coroutine_yield())
129 * or co_exit() (via tevent_coroutine_return()
132 /* now we're back on the original stack */
134 switch (tco->result.state) {
135 case TEVENT_COROUTINE_INIT:
136 case TEVENT_COROUTINE_IN_PROGRESS:
137 /* this should never be reached */
140 case TEVENT_COROUTINE_YIELD:
141 /* we'll resume later */
144 case TEVENT_COROUTINE_DONE:
145 _tevent_req_done(tco->req, tco->result.location);
148 case TEVENT_COROUTINE_NO_MEMORY:
149 _tevent_req_nomem(NULL, tco->req, tco->result.location);
152 case TEVENT_COROUTINE_USER_ERROR:
153 _tevent_req_error(tco->req, tco->result.error,
154 tco->result.location);
158 /* this should never be reached */
162 static void tevent_coroutine_yield_callback(struct tevent_req *subreq);
164 void _tevent_coroutine_yield(struct tevent_coroutine *tco,
165 struct tevent_req *subreq,
166 const char *location)
168 if (subreq == NULL) {
169 tco->result.state = TEVENT_COROUTINE_NO_MEMORY;
170 tco->result.location = location;
171 tco->result.return_location = location;
173 * this jumps back to tevent_coroutine_run() after
181 tco->result.state = TEVENT_COROUTINE_YIELD;
182 tco->result.location = location;
184 tevent_req_set_callback(subreq,
185 tevent_coroutine_yield_callback,
189 * this jumps back to tevent_coroutine_run() after
195 static void tevent_coroutine_yield_callback(struct tevent_req *subreq)
197 struct tevent_coroutine *tco = tevent_req_callback_data(subreq,
198 struct tevent_coroutine);
199 tevent_coroutine_run(tco);
202 void _tevent_coroutine_done(struct tevent_coroutine *tco,
203 const char *location)
205 tco->result.state = TEVENT_COROUTINE_DONE;
206 tco->result.location = location;
209 bool _tevent_coroutine_nomem(const void *ptr,
210 struct tevent_coroutine *tco,
211 const char *location)
217 tco->result.state = TEVENT_COROUTINE_NO_MEMORY;
218 tco->result.location = location;
222 bool _tevent_coroutine_error(struct tevent_coroutine *tco,
224 const char *location)
230 tco->result.state = TEVENT_COROUTINE_USER_ERROR;
231 tco->result.error = error;
232 tco->result.location = location;
236 struct tevent_coroutine_result *_tevent_coroutine_return(
237 struct tevent_coroutine *tco,
238 const char *location)
240 tco->result.return_location = location;