Change uint_t to unsigned int in lib/replace
[ira/wip.git] / lib / tevent / tevent_immediate.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    common events code for immediate events
5
6    Copyright (C) Stefan Metzmacher 2009
7
8      ** NOTE! The following LGPL license applies to the tevent
9      ** library. This does NOT imply that all of Samba is released
10      ** under the LGPL
11
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.
16
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.
21
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/>.
24 */
25
26 #include "replace.h"
27 #include "tevent.h"
28 #include "tevent_internal.h"
29 #include "tevent_util.h"
30
31 static void tevent_common_immediate_cancel(struct tevent_immediate *im)
32 {
33         if (!im->event_ctx) {
34                 return;
35         }
36
37         tevent_debug(im->event_ctx, TEVENT_DEBUG_TRACE,
38                      "Cancel immediate event %p \"%s\"\n",
39                      im, im->handler_name);
40
41         /* let the backend free im->additional_data */
42         if (im->cancel_fn) {
43                 im->cancel_fn(im);
44         }
45
46         DLIST_REMOVE(im->event_ctx->immediate_events, im);
47         im->event_ctx           = NULL;
48         im->handler             = NULL;
49         im->private_data        = NULL;
50         im->handler_name        = NULL;
51         im->schedule_location   = NULL;
52         im->cancel_fn           = NULL;
53         im->additional_data     = NULL;
54
55         talloc_set_destructor(im, NULL);
56 }
57
58 /*
59   destroy an immediate event
60 */
61 static int tevent_common_immediate_destructor(struct tevent_immediate *im)
62 {
63         tevent_common_immediate_cancel(im);
64         return 0;
65 }
66
67 /*
68  * schedule an immediate event on
69  */
70 void tevent_common_schedule_immediate(struct tevent_immediate *im,
71                                       struct tevent_context *ev,
72                                       tevent_immediate_handler_t handler,
73                                       void *private_data,
74                                       const char *handler_name,
75                                       const char *location)
76 {
77         tevent_common_immediate_cancel(im);
78
79         if (!handler) {
80                 return;
81         }
82
83         im->event_ctx           = ev;
84         im->handler             = handler;
85         im->private_data        = private_data;
86         im->handler_name        = handler_name;
87         im->schedule_location   = location;
88         im->cancel_fn           = NULL;
89         im->additional_data     = NULL;
90
91         DLIST_ADD_END(ev->immediate_events, im, struct tevent_immediate *);
92         talloc_set_destructor(im, tevent_common_immediate_destructor);
93
94         tevent_debug(ev, TEVENT_DEBUG_TRACE,
95                      "Schedule immediate event \"%s\": %p\n",
96                      handler_name, im);
97 }
98
99 /*
100   trigger the first immediate event and return true
101   if no event was triggered return false
102 */
103 bool tevent_common_loop_immediate(struct tevent_context *ev)
104 {
105         struct tevent_immediate *im = ev->immediate_events;
106         tevent_immediate_handler_t handler;
107         void *private_data;
108
109         if (!im) {
110                 return false;
111         }
112
113         tevent_debug(ev, TEVENT_DEBUG_TRACE,
114                      "Run immediate event \"%s\": %p\n",
115                      im->handler_name, im);
116
117         /*
118          * remember the handler and then clear the event
119          * the handler might reschedule the event
120          */
121         handler = im->handler;
122         private_data = im->private_data;
123
124         DLIST_REMOVE(im->event_ctx->immediate_events, im);
125         im->event_ctx           = NULL;
126         im->handler             = NULL;
127         im->private_data        = NULL;
128         im->handler_name        = NULL;
129         im->schedule_location   = NULL;
130         im->cancel_fn           = NULL;
131         im->additional_data     = NULL;
132
133         talloc_set_destructor(im, NULL);
134
135         handler(ev, im, private_data);
136
137         return true;
138 }
139