e576bb2002-10-11Martin Nilsson /* || This file is part of Pike. For copyright information see COPYRIGHT. || Pike is distributed under GPL, LGPL and MPL. See the file COPYING || for more information. */
24ddc71998-03-28Henrik Grubbström (Grubba) 
5267b71995-08-09Fredrik Hübinette (Hubbe) #ifndef BACKEND_H #define BACKEND_H
ee1be21995-11-02Fredrik Hübinette (Hubbe) #include "global.h"
52e0751996-06-21Fredrik Hübinette (Hubbe) #include "callback.h"
ee1be21995-11-02Fredrik Hübinette (Hubbe) 
e2d69a2005-01-28Henrik Grubbström (Grubba) /* * POLL/SELECT selection */ #if defined(HAVE_SYS_DEVPOLL_H) && defined(PIKE_POLL_DEVICE) /* * Backend using /dev/poll-style poll device. * * Used on: * Solaris 7 + patches and above. * OSF/1 + patches and above. */ #define BACKEND_USES_POLL_DEVICE #define BACKEND_USES_DEVPOLL
c42ea42006-12-15Henrik Grubbström (Grubba) #elif defined(HAVE_SYS_EPOLL_H) && defined(WITH_EPOLL)
e2d69a2005-01-28Henrik Grubbström (Grubba) /* * Backend using /dev/epoll-style poll device. * * Used on: * Linux 2.6 and above. */ #define BACKEND_USES_POLL_DEVICE #define BACKEND_USES_DEVEPOLL #elif defined(HAVE_SYS_EVENT_H) && defined(HAVE_KQUEUE) /* && !HAVE_POLL */ /* * Backend using kqueue-style poll device. * * FIXME: Not fully implemented yet! Out of band data handling is missing. * * Used on * FreeBSD 4.1 and above. * MacOS X/Darwin 7.x and above. * Various other BSDs. */ #define BACKEND_USES_KQUEUE /* Currently kqueue doesn't differentiate between in-band and out-of-band * data. */ #define BACKEND_OOB_IS_SIMULATED
0ce67d2012-02-15Bill Welliver 
fcca612016-02-11Martin Nilsson #ifdef HAVE_CFRUNLOOPRUNINMODE
0ce67d2012-02-15Bill Welliver /* Have kqueue+CFRunLoop variant (Mac OSX, iOS) */ #define BACKEND_USES_CFRUNLOOP #endif /* HAVE_CFRUNLOOPRUNINMODE */
da4a742013-11-03Henrik Grubbström (Grubba) #elif defined(HAVE_POLL) && defined(HAVE_AND_USE_POLL) /* We have poll(2), and it isn't simulated. */ /* * Backend using poll(2). * * This is used on most older SVR4- or POSIX-style systems. */ #define BACKEND_USES_POLL #else /* !HAVE_POLL */
e2d69a2005-01-28Henrik Grubbström (Grubba) /* * Backend using select(2) * * This is used on most older BSD-style systems, and WIN32. */ #define BACKEND_USES_SELECT
da4a742013-11-03Henrik Grubbström (Grubba) #endif /* HAVE_SYS_DEVPOLL_H && PIKE_POLL_DEVICE */
e2d69a2005-01-28Henrik Grubbström (Grubba) 
9b66692003-10-28Martin Stjernholm struct Backend_struct;
036ca62004-04-04Martin Stjernholm PMOD_EXPORT extern struct Backend_struct *default_backend;
4aba511997-10-22Fredrik Hübinette (Hubbe) extern struct callback_list do_debug_callbacks;
7e63662003-10-24Martin Stjernholm PMOD_EXPORT extern struct program *Backend_program;
5267b71995-08-09Fredrik Hübinette (Hubbe) 
7db3162012-03-08Henrik Grubbström (Grubba) void count_memory_in_compat_cb_boxs(size_t *num, size_t *size);
3b54e52014-08-21Martin Nilsson void free_all_compat_cb_box_blocks(void);
41061b2007-05-26Henrik Grubbström (Grubba) 
036ca62004-04-04Martin Stjernholm PMOD_EXPORT void debug_check_fd_not_in_use (int fd);
e817332004-04-05Martin Stjernholm #if 1
9b66692003-10-28Martin Stjernholm struct Backend_struct *get_backend_for_fd(int fd);
7e63662003-10-24Martin Stjernholm PMOD_EXPORT struct object *get_backend_obj_for_fd (int fd);
98945e2008-06-29Martin Nilsson PMOD_EXPORT void set_backend_for_fd (int fd, struct Backend_struct *new_be);
e817332004-04-05Martin Stjernholm #endif
d59cdc2004-04-04Martin Stjernholm  PMOD_EXPORT struct object *get_backend_obj (struct Backend_struct *b); PMOD_EXPORT void wake_up_backend(void); void init_backend(void); void do_debug(void); void backend(void); void exit_backend(void); PMOD_EXPORT int write_to_stderr(char *a, size_t len); PMOD_EXPORT struct callback *debug_add_backend_callback(callback_func call, void *arg, callback_func free_func);
e2d69a2005-01-28Henrik Grubbström (Grubba) /* * New style callback interface. */
d59cdc2004-04-04Martin Stjernholm  struct fd_callback_box; typedef int (*fd_box_callback) (struct fd_callback_box *box, int event);
653d8b2005-09-10Marek Habersack /** The callback user should keep an instance of this struct around for as long
d59cdc2004-04-04Martin Stjernholm  * as callbacks are wanted. Use hook_fd_callback_box and * unhook_fd_callback_box to connect/disconnect it to/from a backend. */ struct fd_callback_box {
653d8b2005-09-10Marek Habersack  struct Backend_struct *backend; /**< Not refcounted. Cleared when the backend
157ebe2004-04-04Martin Stjernholm  * is destructed or the box is unhooked. */
653d8b2005-09-10Marek Habersack  struct object *ref_obj; /**< If set, it's the object containing the box.
036ca62004-04-04Martin Stjernholm  * It then acts as the ref from the backend to * the object and is refcounted by the backend
c23f712014-05-08Henrik Grubbström (Grubba)  * whenever any event is wanted. * * It receives a ref for each when next and/or * events are non-zero. */
653d8b2005-09-10Marek Habersack  struct fd_callback_box *next; /**< Circular list of active fds in a backend.
9fc2032005-01-20Henrik Grubbström (Grubba)  * NULL if the fd is not active in some
c23f712014-05-08Henrik Grubbström (Grubba)  * backend. Note: ref_obj MUST be freed when
9fc2032005-01-20Henrik Grubbström (Grubba)  * the box is unlinked. */
653d8b2005-09-10Marek Habersack  int fd; /**< Use change_fd_for_box to change this. May
036ca62004-04-04Martin Stjernholm  * be negative, in which case only the ref * magic on backend and ref_obj is done. The
658c182005-01-20Martin Stjernholm  * backend might change a negative value to a * different negative value. */
653d8b2005-09-10Marek Habersack  int events; /**< Bitfield with wanted events. Always use
d59cdc2004-04-04Martin Stjernholm  * set_fd_callback_events to change this. It's * ok to have hooked boxes where no events are * wanted. */
653d8b2005-09-10Marek Habersack  int revents; /**< Bitfield with active events. Always clear
c16f9e2005-01-19Henrik Grubbström (Grubba)  * the corresponding event if you perform an * action that might affect it. */
ca6bd02012-05-01Bill Welliver  int flags; /** < Bitfield used for system dependent flags, such as for kqueue(2) events */ int rflags; /** < Bitfield used for active flags, such as for kqueue(2) events */
653d8b2005-09-10Marek Habersack  fd_box_callback callback; /**< Function to call. Assumed to be valid if
036ca62004-04-04Martin Stjernholm  * any event is wanted. */
d59cdc2004-04-04Martin Stjernholm };
ca6bd02012-05-01Bill Welliver #define INIT_FD_CALLBACK_BOX(BOX, BACKEND, REF_OBJ, FD, EVENTS, CALLBACK, FLAGS) do { \
d59cdc2004-04-04Martin Stjernholm  struct fd_callback_box *box__ = (BOX); \ box__->backend = (BACKEND); \ box__->ref_obj = (REF_OBJ); \
9fc2032005-01-20Henrik Grubbström (Grubba)  box__->next = NULL; \
d59cdc2004-04-04Martin Stjernholm  box__->fd = (FD); \
036ca62004-04-04Martin Stjernholm  box__->events = (EVENTS); \
c16f9e2005-01-19Henrik Grubbström (Grubba)  box__->revents = 0; \
ca6bd02012-05-01Bill Welliver  box__->callback = (CALLBACK); \ box__->flags = (FLAGS); \
7939a62008-06-13Henrik Grubbström (Grubba)  if (box__->backend) hook_fd_callback_box (box__); \
d59cdc2004-04-04Martin Stjernholm  } while (0)
036ca62004-04-04Martin Stjernholm /* The event types. */ #define PIKE_FD_READ 0 #define PIKE_FD_WRITE 1 #define PIKE_FD_READ_OOB 2 #define PIKE_FD_WRITE_OOB 3
e817332004-04-05Martin Stjernholm #define PIKE_FD_ERROR 4
ca6bd02012-05-01Bill Welliver /* FS_EVENT is currently only implemented in kqueue backends. */ #define PIKE_FD_FS_EVENT 5 #define PIKE_FD_NUM_EVENTS 6
036ca62004-04-04Martin Stjernholm  /* Flags for event bitfields. */ #define PIKE_BIT_FD_READ (1 << PIKE_FD_READ) #define PIKE_BIT_FD_WRITE (1 << PIKE_FD_WRITE) #define PIKE_BIT_FD_READ_OOB (1 << PIKE_FD_READ_OOB) #define PIKE_BIT_FD_WRITE_OOB (1 << PIKE_FD_WRITE_OOB)
e817332004-04-05Martin Stjernholm #define PIKE_BIT_FD_ERROR (1 << PIKE_FD_ERROR)
ca6bd02012-05-01Bill Welliver /* FS_EVENT is currently only implemented in kqueue backends. */ #define PIKE_BIT_FD_FS_EVENT (1 << PIKE_FD_FS_EVENT)
d59cdc2004-04-04Martin Stjernholm 
10a7e72004-09-27Martin Stjernholm /* If an error condition occurs then all events except * PIKE_BIT_FD_ERROR are cleared from fd_callback_box.events. */
157ebe2004-04-04Martin Stjernholm /* Note: If ref_obj is used, both unhook_fd_callback_box and
c16f9e2005-01-19Henrik Grubbström (Grubba)  * set_fd_callback_events may free the object containing the box.
d5ecf82004-04-05Martin Stjernholm  * They may be used from within the gc recurse passes. */
d59cdc2004-04-04Martin Stjernholm PMOD_EXPORT void hook_fd_callback_box (struct fd_callback_box *box); PMOD_EXPORT void unhook_fd_callback_box (struct fd_callback_box *box);
ca6bd02012-05-01Bill Welliver PMOD_EXPORT void set_fd_callback_events (struct fd_callback_box *box, int events, int flags);
036ca62004-04-04Martin Stjernholm PMOD_EXPORT void change_backend_for_box (struct fd_callback_box *box,
98945e2008-06-29Martin Nilsson  struct Backend_struct *new_be);
036ca62004-04-04Martin Stjernholm PMOD_EXPORT void change_fd_for_box (struct fd_callback_box *box, int new_fd);
d59cdc2004-04-04Martin Stjernholm 
bbfcfd2012-11-19Per Hedbor PMOD_EXPORT struct fd_callback_box *get_fd_callback_box_for_fd (struct Backend_struct *me, int fd);
d59cdc2004-04-04Martin Stjernholm /* Old style callback interface. This only accesses the default backend. It * can't be mixed with the new style interface above for the same fd. */ typedef int (*file_callback)(int,void *);
e817332004-04-05Martin Stjernholm #if 1
7e63662003-10-24Martin Stjernholm PMOD_EXPORT void set_read_callback(int fd,file_callback cb,void *data); PMOD_EXPORT void set_write_callback(int fd,file_callback cb,void *data);
73300d2000-12-18Fredrik Hübinette (Hubbe) PMOD_EXPORT void set_read_oob_callback(int fd,file_callback cb,void *data); PMOD_EXPORT void set_write_oob_callback(int fd,file_callback cb,void *data);
ca6bd02012-05-01Bill Welliver PMOD_EXPORT void set_fs_event_callback(int fd,file_callback cb,void *data, int flags);
f31ced2000-12-16Marcus Comstedt PMOD_EXPORT file_callback query_read_callback(int fd); PMOD_EXPORT file_callback query_write_callback(int fd); PMOD_EXPORT file_callback query_read_oob_callback(int fd); PMOD_EXPORT file_callback query_write_oob_callback(int fd);
ca6bd02012-05-01Bill Welliver PMOD_EXPORT file_callback query_fs_event_callback(int fd);
f31ced2000-12-16Marcus Comstedt PMOD_EXPORT void *query_read_callback_data(int fd); PMOD_EXPORT void *query_write_callback_data(int fd); PMOD_EXPORT void *query_read_oob_callback_data(int fd); PMOD_EXPORT void *query_write_oob_callback_data(int fd);
ca6bd02012-05-01Bill Welliver PMOD_EXPORT void *query_fs_event_callback_data(int fd);
e817332004-04-05Martin Stjernholm #endif
5267b71995-08-09Fredrik Hübinette (Hubbe) 
41061b2007-05-26Henrik Grubbström (Grubba) PMOD_EXPORT void backend_wake_up_backend(struct Backend_struct *be);
02428c2007-06-26Henrik Grubbström (Grubba) PMOD_EXPORT void backend_lower_timeout(struct Backend_struct *me, struct timeval *tv);
41061b2007-05-26Henrik Grubbström (Grubba) PMOD_EXPORT struct object *get_backend_obj (struct Backend_struct *b); PMOD_EXPORT struct callback *backend_debug_add_backend_callback( struct Backend_struct *be, callback_func call, void *arg, callback_func free_func);
424d9c1999-05-02Fredrik Hübinette (Hubbe) #define add_backend_callback(X,Y,Z) \ dmalloc_touch(struct callback *,debug_add_backend_callback((X),(Y),(Z)))
41061b2007-05-26Henrik Grubbström (Grubba) #define backend_add_backend_callback(B,X,Y,Z) \ dmalloc_touch(struct callback *,\ backend_debug_add_backend_callback((B),(X),(Y),(Z)))
5267b71995-08-09Fredrik Hübinette (Hubbe) #endif