r13842: Make some more functions public.
[kai/samba-autobuild/.git] / source4 / lib / events / events.c
1 /* 
2    Unix SMB/CIFS implementation.
3    main select loop and event handling
4    Copyright (C) Andrew Tridgell 2003
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 /*
22   PLEASE READ THIS BEFORE MODIFYING!
23
24   This module is a general abstraction for the main select loop and
25   event handling. Do not ever put any localised hacks in here, instead
26   register one of the possible event types and implement that event
27   somewhere else.
28
29   There are 2 types of event handling that are handled in this module:
30
31   1) a file descriptor becoming readable or writeable. This is mostly
32      used for network sockets, but can be used for any type of file
33      descriptor. You may only register one handler for each file
34      descriptor/io combination or you will get unpredictable results
35      (this means that you can have a handler for read events, and a
36      separate handler for write events, but not two handlers that are
37      both handling read events)
38
39   2) a timed event. You can register an event that happens at a
40      specific time.  You can register as many of these as you
41      like. They are single shot - add a new timed event in the event
42      handler to get another event.
43
44   To setup a set of events you first need to create a event_context
45   structure using the function event_context_init(); This returns a
46   'struct event_context' that you use in all subsequent calls.
47
48   After that you can add/remove events that you are interested in
49   using event_add_*() and talloc_free()
50
51   Finally, you call event_loop_wait_once() to block waiting for one of the
52   events to occor or event_loop_wait() which will loop
53   forever.
54
55 */
56
57 #include "includes.h"
58 #include "lib/events/events.h"
59 #include "lib/events/events_internal.h"
60
61 /*
62   create a event_context structure for a specific implemementation.
63   This must be the first events call, and all subsequent calls pass
64   this event_context as the first element. Event handlers also
65   receive this as their first argument.
66
67   This function is for allowing third-party-applications to hook in gluecode
68   to their own event loop code, so that they can make async usage of our client libs
69
70   NOTE: use event_context_init() inside of samba!
71 */
72 struct event_context *event_context_init_ops(TALLOC_CTX *mem_ctx, const struct event_ops *ops, void *private_data)
73 {
74         struct event_context *ev;
75         int ret;
76
77         ev = talloc_zero(mem_ctx, struct event_context);
78         if (!ev) return NULL;
79
80         ev->ops = ops;
81
82         ret = ev->ops->context_init(ev, private_data);
83         if (ret != 0) {
84                 talloc_free(ev);
85                 return NULL;
86         }
87
88         return ev;
89 }
90
91 /*
92   create a event_context structure. This must be the first events
93   call, and all subsequent calls pass this event_context as the first
94   element. Event handlers also receive this as their first argument.
95 */
96 struct event_context *event_context_init(TALLOC_CTX *mem_ctx)
97 {
98         const struct event_ops *ops = event_standard_get_ops();
99         return event_context_init_ops(mem_ctx, ops, NULL);
100 }
101
102 /*
103   add a fd based event
104   return NULL on failure (memory allocation error)
105 */
106 struct fd_event *event_add_fd(struct event_context *ev, TALLOC_CTX *mem_ctx,
107                               int fd, uint16_t flags, event_fd_handler_t handler,
108                               void *private_data)
109 {
110         return ev->ops->add_fd(ev, mem_ctx, fd, flags, handler, private_data);
111 }
112
113 /*
114   return the fd event flags
115 */
116 uint16_t event_get_fd_flags(struct fd_event *fde)
117 {
118         if (!fde) return 0;
119         return fde->event_ctx->ops->get_fd_flags(fde);
120 }
121
122 /*
123   set the fd event flags
124 */
125 void event_set_fd_flags(struct fd_event *fde, uint16_t flags)
126 {
127         if (!fde) return;
128         fde->event_ctx->ops->set_fd_flags(fde, flags);
129 }
130
131 /*
132   add a timed event
133   return NULL on failure
134 */
135 struct timed_event *event_add_timed(struct event_context *ev, TALLOC_CTX *mem_ctx,
136                                     struct timeval next_event, 
137                                     event_timed_handler_t handler, 
138                                     void *private_data)
139 {
140         return ev->ops->add_timed(ev, mem_ctx, next_event, handler, private_data);
141 }
142
143 /*
144   do a single event loop using the events defined in ev 
145 */
146 _PUBLIC_ int event_loop_once(struct event_context *ev)
147 {
148         return ev->ops->loop_once(ev);
149 }
150
151 /*
152   return on failure or (with 0) if all fd events are removed
153 */
154 int event_loop_wait(struct event_context *ev)
155 {
156         return ev->ops->loop_wait(ev);
157 }
158
159 /*
160   find an event context that is a parent of the given memory context,
161   or create a new event context as a child of the given context if
162   none is found
163
164   This should be used in preference to event_context_init() in places
165   where you would prefer to use the existing event context if possible
166   (which is most situations)
167 */
168 struct event_context *event_context_find(TALLOC_CTX *mem_ctx)
169 {
170         struct event_context *ev = talloc_find_parent_bytype(mem_ctx, struct event_context);
171         if (ev == NULL) {
172                 ev = event_context_init(mem_ctx);
173         }
174         return ev;
175 }