2 Unix SMB/CIFS implementation.
4 common events code for immediate events
6 Copyright (C) Stefan Metzmacher 2009
8 ** NOTE! The following LGPL license applies to the tevent
9 ** library. This does NOT imply that all of Samba is released
12 This library is free software; you can redistribute it and/or
13 modify it under the terms of the GNU Lesser General Public
14 License as published by the Free Software Foundation; either
15 version 3 of the License, or (at your option) any later version.
17 This library is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 Lesser General Public License for more details.
22 You should have received a copy of the GNU Lesser General Public
23 License along with this library; if not, see <http://www.gnu.org/licenses/>.
27 #define TEVENT_DEPRECATED 1
29 #include "tevent_internal.h"
30 #include "tevent_util.h"
32 static void tevent_common_immediate_cancel(struct tevent_immediate *im)
34 const char *create_location = im->create_location;
38 tevent_abort(im->event_ctx, "tevent_immediate use after free");
46 if (im->handler_name != NULL) {
47 tevent_debug(im->event_ctx, TEVENT_DEBUG_TRACE,
48 "Cancel immediate event %p \"%s\"\n",
49 im, im->handler_name);
52 /* let the backend free im->additional_data */
57 DLIST_REMOVE(im->event_ctx->immediate_events, im);
59 *im = (struct tevent_immediate) {
60 .create_location = create_location,
65 talloc_set_destructor(im, NULL);
70 destroy an immediate event
72 static int tevent_common_immediate_destructor(struct tevent_immediate *im)
75 tevent_common_check_double_free(im,
76 "tevent_immediate double free");
80 tevent_common_immediate_cancel(im);
93 * schedule an immediate event on
95 void tevent_common_schedule_immediate(struct tevent_immediate *im,
96 struct tevent_context *ev,
97 tevent_immediate_handler_t handler,
99 const char *handler_name,
100 const char *location)
102 const char *create_location = im->create_location;
103 bool busy = im->busy;
104 struct tevent_wrapper_glue *glue = im->wrapper;
106 tevent_common_immediate_cancel(im);
112 *im = (struct tevent_immediate) {
116 .private_data = private_data,
117 .handler_name = handler_name,
118 .create_location = create_location,
119 .schedule_location = location,
123 DLIST_ADD_END(ev->immediate_events, im);
124 talloc_set_destructor(im, tevent_common_immediate_destructor);
126 tevent_debug(ev, TEVENT_DEBUG_TRACE,
127 "Schedule immediate event \"%s\": %p\n",
131 int tevent_common_invoke_immediate_handler(struct tevent_immediate *im,
134 struct tevent_context *handler_ev = im->event_ctx;
135 struct tevent_context *ev = im->event_ctx;
136 struct tevent_immediate cur = *im;
138 if (removed != NULL) {
142 tevent_debug(ev, TEVENT_DEBUG_TRACE,
143 "Run immediate event \"%s\": %p\n",
144 im->handler_name, im);
147 * remember the handler and then clear the event
148 * the handler might reschedule the event
152 im->handler_name = NULL;
153 tevent_common_immediate_cancel(im);
154 if (cur.wrapper != NULL) {
155 handler_ev = cur.wrapper->wrap_ev;
157 tevent_wrapper_push_use_internal(handler_ev, cur.wrapper);
158 cur.wrapper->ops->before_immediate_handler(
159 cur.wrapper->wrap_ev,
160 cur.wrapper->private_state,
161 cur.wrapper->main_ev,
164 cur.schedule_location);
166 cur.handler(handler_ev, im, cur.private_data);
167 if (cur.wrapper != NULL) {
168 cur.wrapper->ops->after_immediate_handler(
169 cur.wrapper->wrap_ev,
170 cur.wrapper->private_state,
171 cur.wrapper->main_ev,
174 cur.schedule_location);
175 tevent_wrapper_pop_use_internal(handler_ev, cur.wrapper);
180 talloc_set_destructor(im, NULL);
182 if (removed != NULL) {
191 trigger the first immediate event and return true
192 if no event was triggered return false
194 bool tevent_common_loop_immediate(struct tevent_context *ev)
196 struct tevent_immediate *im = ev->immediate_events;
203 ret = tevent_common_invoke_immediate_handler(im, NULL);
205 tevent_abort(ev, "tevent_common_invoke_immediate_handler() failed");