Main Page | Class List | Directories | File List | Class Members | File Members

backend.h

Go to the documentation of this file.
00001 /*
00002 || This file is part of Pike. For copyright information see COPYRIGHT.
00003 || Pike is distributed under GPL, LGPL and MPL. See the file COPYING
00004 || for more information.
00005 || $Id: backend.h,v 1.33 2005/01/28 18:21:13 grubba Exp $
00006 */
00007 
00008 #ifndef BACKEND_H
00009 #define BACKEND_H
00010 
00011 #include "global.h"
00012 #include "time_stuff.h"
00013 #include "callback.h"
00014 
00015 /*
00016  * POLL/SELECT selection
00017  */
00018 
00019 #if defined(HAVE_POLL) && defined(HAVE_AND_USE_POLL)
00020 /* We have poll(2), and it isn't simulated. */
00021 #if defined(HAVE_SYS_DEVPOLL_H) && defined(PIKE_POLL_DEVICE)
00022 /*
00023  * Backend using /dev/poll-style poll device.
00024  *
00025  * Used on:
00026  *   Solaris 7 + patches and above.
00027  *   OSF/1 + patches and above.
00028  *   IRIX 5.6.15m and above.
00029  */
00030 #define BACKEND_USES_POLL_DEVICE
00031 #define BACKEND_USES_DEVPOLL
00032 #elif defined(HAVE_SYS_EPOLL_H) && defined(PIKE_POLL_DEVICE)
00033 /*
00034  * Backend using /dev/epoll-style poll device.
00035  *
00036  * Used on:
00037  *   Linux 2.6 and above.
00038  * Note:
00039  *   Some libc's are missing wrappers for the system calls, so
00040  *   we include the appropriate wrappers below.
00041  */
00042 #define BACKEND_USES_POLL_DEVICE
00043 #define BACKEND_USES_DEVEPOLL
00044 #else /* !BACKEND_USES_POLL_DEVICE */
00045 /*
00046  * Backend using poll(2).
00047  *
00048  * This is used on most older SVR4- or POSIX-style systems.
00049  */
00050 #define BACKEND_USES_POLL
00051 #endif /* HAVE_SYS_DEVPOLL_H || HAVE_SYS_EPOLL_H */
00052 #elif defined(HAVE_SYS_EVENT_H) && defined(HAVE_KQUEUE) /* && !HAVE_POLL */
00053 /*
00054  * Backend using kqueue-style poll device.
00055  *
00056  * FIXME: Not fully implemented yet! Out of band data handling is missing.
00057  *
00058  * Used on
00059  *   FreeBSD 4.1 and above.
00060  *   MacOS X/Darwin 7.x and above.
00061  *   Various other BSDs.
00062  */
00063 #define BACKEND_USES_KQUEUE
00064 /* Currently kqueue doesn't differentiate between in-band and out-of-band
00065  * data.
00066  */
00067 #define BACKEND_OOB_IS_SIMULATED
00068 #else  /* !HAVE_POLL && !HAVE_KQUEUE */
00069 /*
00070  * Backend using select(2)
00071  *
00072  * This is used on most older BSD-style systems, and WIN32.
00073  */
00074 #define BACKEND_USES_SELECT
00075 #endif  /* HAVE_POLL || BACKEND_USES_KQUEUE */
00076 
00077 struct Backend_struct;
00078 struct selectors;
00079 
00080 PMOD_EXPORT extern struct timeval current_time;
00081 PMOD_EXPORT extern struct timeval next_timeout;
00082 PMOD_EXPORT extern struct Backend_struct *default_backend;
00083 extern struct callback_list do_debug_callbacks;
00084 PMOD_EXPORT extern struct program *Backend_program;
00085 
00086 PMOD_EXPORT void debug_check_fd_not_in_use (int fd);
00087 #if 1
00088 struct Backend_struct *get_backend_for_fd(int fd);
00089 PMOD_EXPORT struct object *get_backend_obj_for_fd (int fd);
00090 PMOD_EXPORT void set_backend_for_fd (int fd, struct Backend_struct *new);
00091 #endif
00092 
00093 PMOD_EXPORT struct object *get_backend_obj (struct Backend_struct *b);
00094 PMOD_EXPORT void wake_up_backend(void);
00095 void init_backend(void);
00096 void do_debug(void);
00097 void backend(void);
00098 void exit_backend(void);
00099 PMOD_EXPORT int write_to_stderr(char *a, size_t len);
00100 PMOD_EXPORT struct callback *debug_add_backend_callback(callback_func call,
00101                                                         void *arg,
00102                                                         callback_func free_func);
00103 
00104 /*
00105  * New style callback interface.
00106  */
00107 
00108 struct fd_callback_box;
00109 
00110 typedef int (*fd_box_callback) (struct fd_callback_box *box, int event);
00111 
00112 /* The callback user should keep an instance of this struct around for as long
00113  * as callbacks are wanted. Use hook_fd_callback_box and
00114  * unhook_fd_callback_box to connect/disconnect it to/from a backend. */
00115 struct fd_callback_box
00116 {
00117   struct Backend_struct *backend; /* Not refcounted. Cleared when the backend
00118                                    * is destructed or the box is unhooked. */
00119   struct object *ref_obj;       /* If set, it's the object containing the box.
00120                                  * It then acts as the ref from the backend to
00121                                  * the object and is refcounted by the backend
00122                                  * whenever any event is wanted. */
00123   struct fd_callback_box *next; /* Circular list of active fds in a backend.
00124                                  * NULL if the fd is not active in some
00125                                  * backend. Note: ref_obj MUST be freed if
00126                                  * the box is unlinked. */
00127   int fd;                       /* Use change_fd_for_box to change this. May
00128                                  * be negative, in which case only the ref
00129                                  * magic on backend and ref_obj is done. The
00130                                  * backend might change a negative value to a
00131                                  * different negative value. */
00132   int events;                   /* Bitfield with wanted events. Always use
00133                                  * set_fd_callback_events to change this. It's
00134                                  * ok to have hooked boxes where no events are
00135                                  * wanted. */
00136   int revents;                  /* Bitfield with active events. Always clear
00137                                  * the corresponding event if you perform an
00138                                  * action that might affect it. */
00139   fd_box_callback callback;     /* Function to call. Assumed to be valid if
00140                                  * any event is wanted. */
00141 };
00142 
00143 #define INIT_FD_CALLBACK_BOX(BOX, BACKEND, REF_OBJ, FD, EVENTS, CALLBACK) do { \
00144     struct fd_callback_box *box__ = (BOX);                              \
00145     box__->backend = (BACKEND);                                         \
00146     box__->ref_obj = (REF_OBJ);                                         \
00147     box__->next = NULL;                                                 \
00148     box__->fd = (FD);                                                   \
00149     box__->events = (EVENTS);                                           \
00150     box__->revents = 0;                                                 \
00151     box__->callback = (CALLBACK);                                       \
00152     hook_fd_callback_box (box__);                                       \
00153   } while (0)
00154 
00155 /* The event types. */
00156 #define PIKE_FD_READ            0
00157 #define PIKE_FD_WRITE           1
00158 #define PIKE_FD_READ_OOB        2
00159 #define PIKE_FD_WRITE_OOB       3
00160 #define PIKE_FD_ERROR           4
00161 #define PIKE_FD_NUM_EVENTS      5
00162 
00163 /* Flags for event bitfields. */
00164 #define PIKE_BIT_FD_READ        (1 << PIKE_FD_READ)
00165 #define PIKE_BIT_FD_WRITE       (1 << PIKE_FD_WRITE)
00166 #define PIKE_BIT_FD_READ_OOB    (1 << PIKE_FD_READ_OOB)
00167 #define PIKE_BIT_FD_WRITE_OOB   (1 << PIKE_FD_WRITE_OOB)
00168 #define PIKE_BIT_FD_ERROR       (1 << PIKE_FD_ERROR)
00169 
00170 /* If an error condition occurs then all events except
00171  * PIKE_BIT_FD_ERROR are cleared from fd_callback_box.events. */
00172 
00173 /* Note: If ref_obj is used, both unhook_fd_callback_box and
00174  * set_fd_callback_events may free the object containing the box.
00175  * They may be used from within the gc recurse passes. */
00176 PMOD_EXPORT void hook_fd_callback_box (struct fd_callback_box *box);
00177 PMOD_EXPORT void unhook_fd_callback_box (struct fd_callback_box *box);
00178 PMOD_EXPORT void set_fd_callback_events (struct fd_callback_box *box, int events);
00179 PMOD_EXPORT void change_backend_for_box (struct fd_callback_box *box,
00180                                          struct Backend_struct *new);
00181 PMOD_EXPORT void change_fd_for_box (struct fd_callback_box *box, int new_fd);
00182 
00183 /* Old style callback interface. This only accesses the default backend. It
00184  * can't be mixed with the new style interface above for the same fd. */
00185 
00186 typedef int (*file_callback)(int,void *);
00187 
00188 #if 1
00189 PMOD_EXPORT void set_read_callback(int fd,file_callback cb,void *data);
00190 PMOD_EXPORT void set_write_callback(int fd,file_callback cb,void *data);
00191 PMOD_EXPORT void set_read_oob_callback(int fd,file_callback cb,void *data);
00192 PMOD_EXPORT void set_write_oob_callback(int fd,file_callback cb,void *data);
00193 PMOD_EXPORT file_callback query_read_callback(int fd);
00194 PMOD_EXPORT file_callback query_write_callback(int fd);
00195 PMOD_EXPORT file_callback query_read_oob_callback(int fd);
00196 PMOD_EXPORT file_callback query_write_oob_callback(int fd);
00197 PMOD_EXPORT void *query_read_callback_data(int fd);
00198 PMOD_EXPORT void *query_write_callback_data(int fd);
00199 PMOD_EXPORT void *query_read_oob_callback_data(int fd);
00200 PMOD_EXPORT void *query_write_oob_callback_data(int fd);
00201 #endif
00202 
00203 #define add_backend_callback(X,Y,Z) \
00204   dmalloc_touch(struct callback *,debug_add_backend_callback((X),(Y),(Z)))
00205 
00206 #endif

Generated on Fri Jul 22 23:44:24 2005 for Pike by  doxygen 1.3.9.1