2007-04-15 02:03:41 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the Sofia-SIP package
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005, 2006, 2007 Nokia Corporation.
|
|
|
|
*
|
|
|
|
* Contact: Pekka Pessi <pekka.pessi@nokia.com>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2.1 of
|
|
|
|
* the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
|
|
|
|
* 02110-1301 USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**@ingroup su_wait
|
|
|
|
* @CFILE su_epoll_port.c
|
|
|
|
*
|
|
|
|
* Port implementation using epoll(7)
|
|
|
|
*
|
|
|
|
* @author Pekka Pessi <Pekka.Pessi@nokia.com>
|
|
|
|
* @author Kai Vehmanen <kai.vehmanen@nokia.com>
|
|
|
|
*
|
|
|
|
* @date Created: Fri Jan 26 20:44:14 2007 ppessi
|
|
|
|
* @date Original: Tue Sep 14 15:51:04 1999 ppessi
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#define su_port_s su_epoll_port_s
|
|
|
|
|
|
|
|
#include "su_port.h"
|
|
|
|
|
|
|
|
#if HAVE_EPOLL
|
|
|
|
|
|
|
|
#include "sofia-sip/su.h"
|
|
|
|
#include "sofia-sip/su_alloc.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <sys/epoll.h>
|
|
|
|
|
|
|
|
#define POLL2EPOLL_NEEDED \
|
|
|
|
(POLLIN != EPOLLIN || POLLOUT != EPOLLOUT || POLLPRI != EPOLLPRI || \
|
|
|
|
POLLERR != EPOLLERR || POLLHUP != EPOLLHUP)
|
2008-12-16 18:05:22 +00:00
|
|
|
|
2007-04-15 02:03:41 +00:00
|
|
|
#define POLL2EPOLL(e) (e & (POLLIN|POLLOUT|POLLPRI|POLLERR|POLLHUP))
|
|
|
|
#define EPOLL2POLL(e) (e & (POLLIN|POLLOUT|POLLPRI|POLLERR|POLLHUP))
|
|
|
|
|
|
|
|
/** Port based on epoll(). */
|
|
|
|
|
|
|
|
struct su_epoll_port_s {
|
|
|
|
su_socket_port_t sup_base[1];
|
|
|
|
|
|
|
|
/** epoll fd */
|
|
|
|
int sup_epoll;
|
|
|
|
unsigned sup_multishot; /**< Multishot operation? */
|
|
|
|
|
2008-12-16 18:05:22 +00:00
|
|
|
unsigned sup_registers; /** Counter incremented by
|
|
|
|
su_port_register() or
|
2007-04-15 02:03:41 +00:00
|
|
|
su_port_unregister()
|
|
|
|
*/
|
|
|
|
int sup_n_registrations;
|
|
|
|
int sup_max_index; /**< Indexes are equal or smaller than this */
|
|
|
|
int sup_size_indices; /**< Size of allocated index table */
|
|
|
|
|
|
|
|
/** Structure containing registration data */
|
|
|
|
struct su_epoll_register {
|
|
|
|
struct su_epoll_register *ser_next; /* Next in free list */
|
2008-12-16 18:05:22 +00:00
|
|
|
su_wakeup_f ser_cb;
|
|
|
|
su_wakeup_arg_t*ser_arg;
|
|
|
|
su_root_t *ser_root;
|
2007-04-15 02:03:41 +00:00
|
|
|
int ser_id; /** registration identifier */
|
|
|
|
su_wait_t ser_wait[1];
|
|
|
|
} **sup_indices;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void su_epoll_port_decref(su_port_t *self,
|
|
|
|
int blocking,
|
|
|
|
char const *who);
|
|
|
|
static int su_epoll_port_register(su_port_t *self,
|
2008-12-16 18:05:22 +00:00
|
|
|
su_root_t *root,
|
|
|
|
su_wait_t *wait,
|
2007-04-15 02:03:41 +00:00
|
|
|
su_wakeup_f callback,
|
|
|
|
su_wakeup_arg_t *arg,
|
|
|
|
int priority);
|
|
|
|
static int su_epoll_port_unregister(su_port_t *port,
|
2008-12-16 18:05:22 +00:00
|
|
|
su_root_t *root,
|
|
|
|
su_wait_t *wait,
|
|
|
|
su_wakeup_f callback,
|
2007-04-15 02:03:41 +00:00
|
|
|
su_wakeup_arg_t *arg);
|
|
|
|
static int su_epoll_port_deregister(su_port_t *self, int i);
|
|
|
|
static int su_epoll_port_unregister_all(su_port_t *self, su_root_t *root);
|
2008-12-16 18:05:22 +00:00
|
|
|
static int su_epoll_port_eventmask(su_port_t *self,
|
2007-04-15 02:03:41 +00:00
|
|
|
int index,
|
|
|
|
int socket,
|
|
|
|
int events);
|
|
|
|
static int su_epoll_port_multishot(su_port_t *self, int multishot);
|
|
|
|
static int su_epoll_port_wait_events(su_port_t *self, su_duration_t tout);
|
|
|
|
static char const *su_epoll_port_name(su_port_t const *self);
|
|
|
|
|
|
|
|
su_port_vtable_t const su_epoll_port_vtable[1] =
|
|
|
|
{{
|
|
|
|
/* su_vtable_size: */ sizeof su_epoll_port_vtable,
|
|
|
|
su_pthread_port_lock,
|
|
|
|
su_pthread_port_unlock,
|
|
|
|
su_base_port_incref,
|
|
|
|
su_epoll_port_decref,
|
|
|
|
su_base_port_gsource,
|
2009-02-11 17:16:44 +00:00
|
|
|
su_base_port_send,
|
2007-04-15 02:03:41 +00:00
|
|
|
su_epoll_port_register,
|
|
|
|
su_epoll_port_unregister,
|
|
|
|
su_epoll_port_deregister,
|
|
|
|
su_epoll_port_unregister_all,
|
|
|
|
su_epoll_port_eventmask,
|
|
|
|
su_base_port_run,
|
|
|
|
su_base_port_break,
|
|
|
|
su_base_port_step,
|
Sync to current darcs tree:
Mon Sep 17 14:50:04 EDT 2007 Pekka.Pessi@nokia.com
* sofia-sip/sip_util.h: updated documentation
Mon Sep 17 14:50:18 EDT 2007 Pekka.Pessi@nokia.com
* sofia-sip/tport_tag.h: updated documentation
Mon Sep 17 14:50:28 EDT 2007 Pekka.Pessi@nokia.com
* soa_tag.c: updated documentation
Wed Sep 19 12:50:01 EDT 2007 Pekka.Pessi@nokia.com
* msg: updated documentation
Wed Sep 19 13:29:50 EDT 2007 Pekka.Pessi@nokia.com
* url: updated documentation
Wed Sep 19 13:32:14 EDT 2007 Pekka.Pessi@nokia.com
* nth: updated documentation
Wed Sep 19 13:32:27 EDT 2007 Pekka.Pessi@nokia.com
* nea: updated documentation
Wed Sep 19 13:33:36 EDT 2007 Pekka.Pessi@nokia.com
* http: updated documentation
Wed Sep 19 13:36:58 EDT 2007 Pekka.Pessi@nokia.com
* bnf: updated documentation
Wed Sep 19 13:38:58 EDT 2007 Pekka.Pessi@nokia.com
* nua: updated nua_stack_init_handle() prototype
Wed Sep 19 18:45:56 EDT 2007 Pekka.Pessi@nokia.com
* sip: added sip_name_addr_xtra(), sip_name_addr_dup()
Wed Sep 19 19:00:19 EDT 2007 Pekka.Pessi@nokia.com
* sip_basic.c: cleaned old crud
Thu Sep 20 13:34:04 EDT 2007 Pekka.Pessi@nokia.com
* iptsec: updated documentation
Thu Sep 20 13:36:22 EDT 2007 Pekka.Pessi@nokia.com
* tport: updated documentation
Thu Sep 20 13:36:56 EDT 2007 Pekka.Pessi@nokia.com
* su: updated documentation
Removed internal files from doxygen-generated documentation.
Thu Sep 20 13:38:29 EDT 2007 Pekka.Pessi@nokia.com
* soa: fixed documentation
Thu Sep 20 13:39:56 EDT 2007 Pekka.Pessi@nokia.com
* sdp: updated documentation
Thu Sep 20 13:40:16 EDT 2007 Pekka.Pessi@nokia.com
* ipt: updated documentation
Thu Sep 20 14:24:20 EDT 2007 Pekka.Pessi@nokia.com
* nta: updated documentation
Thu Sep 20 14:41:04 EDT 2007 Pekka.Pessi@nokia.com
* nua: updated documentation
Updated tag documentation.
Moved doxygen doc entries from sofia-sip/nua_tag.h to nua_tag.c.
Removed internal datatypes and files from the generated documents.
Wed Sep 19 13:34:20 EDT 2007 Pekka.Pessi@nokia.com
* docs: updated the generation of documentation. Updated links to header files.
Thu Sep 20 08:45:32 EDT 2007 Pekka.Pessi@nokia.com
* sip/Makefile.am: added tags to <sofia-sip/sip_extra.h>
Added check for extra tags in torture_sip.c.
Thu Sep 20 14:45:22 EDT 2007 Pekka.Pessi@nokia.com
* stun: updated documentation
Wed Jul 4 18:55:20 EDT 2007 Pekka.Pessi@nokia.com
* torture_heap.c: added tests for ##sort() and su_smoothsort()
Wed Jul 4 18:56:59 EDT 2007 Pekka.Pessi@nokia.com
* Makefile.am: added smoothsort.c
Fri Jul 13 12:38:44 EDT 2007 Pekka.Pessi@nokia.com
* sofia-sip/heap.h: heap_remove() now set()s index to 0 on removed item
Mon Jul 23 11:14:22 EDT 2007 Pekka.Pessi@nokia.com
* sofia-sip/heap.h: fixed bug in heap##remove()
If left kid was in heap but right was not, left kid was ignored.
Wed Jul 4 18:51:08 EDT 2007 Pekka.Pessi@nokia.com
* smoothsort.c: added
Wed Jul 4 18:51:34 EDT 2007 Pekka.Pessi@nokia.com
* heap.h: using su_smoothsort()
Fri Jul 6 10:20:27 EDT 2007 Pekka.Pessi@nokia.com
* smoothsort.c: added
Wed Sep 19 17:40:30 EDT 2007 Pekka.Pessi@nokia.com
* msg_parser.awk: generate two parser tables, default and extended
Wed Sep 19 18:39:45 EDT 2007 Pekka.Pessi@nokia.com
* msg_parser.awk: just generate list of extra headers
Allocate extended parser dynamically.
Wed Sep 19 18:59:59 EDT 2007 Pekka.Pessi@nokia.com
* sip: added Remote-Party-ID, P-Asserted-Identity, P-Preferred-Identity
Added functions sip_update_default_mclass() and sip_extend_mclass()
for handling the extended parser. Note that Reply-To and Alert-Info are only
available with the extended parser.
Wed Sep 19 19:05:44 EDT 2007 Pekka.Pessi@nokia.com
* RELEASE: updated
Thu Sep 20 13:38:59 EDT 2007 Pekka.Pessi@nokia.com
* sip: updated documentation
Thu Sep 20 14:17:28 EDT 2007 Pekka.Pessi@nokia.com
* docs/conformance.docs: updated
Mon Oct 1 10:11:14 EDT 2007 Pekka.Pessi@nokia.com
* tport_tag.c: re-enabled tptag_trusted
Thu Oct 4 09:21:07 EDT 2007 Pekka.Pessi@nokia.com
* su_osx_runloop.c: moved virtual function table after struct definition
Preparing for su_port_vtable_t refactoring.
Thu Oct 4 10:22:03 EDT 2007 Pekka.Pessi@nokia.com
* su_source.c: refactored initialization/deinitialization
Fri Oct 5 04:58:18 EDT 2007 Pekka Pessi <Pekka.Pessi@nokia.com>
* sip_extra.c: fixed prototypes with isize_t
Fri Oct 5 04:58:45 EDT 2007 Pekka Pessi <Pekka.Pessi@nokia.com>
* test_nta_api.c: removed warnings about signedness
Fri Oct 5 04:59:02 EDT 2007 Pekka Pessi <Pekka.Pessi@nokia.com>
* test_nua_params.c: removed warnings about constness
Fri Oct 5 07:20:26 EDT 2007 Pekka Pessi <first.lastname@nokia.com>
* su_port.h, su_root.c: cleaned argument checking
The su_root_*() and su_port_*() functions now check their arguments once
and do not assert() with NULL arguments. The sur_task->sut_port should
always be valid while su_root_t is alive.
Fri Oct 5 07:22:09 EDT 2007 Pekka Pessi <first.lastname@nokia.com>
* su: added su_root_obtain(), su_root_release() and su_root_has_thread()
When root is created with su_root_create() or cloned with su_clone_start(),
the resulting root is obtained by the calling or created thread,
respectively.
The root can be released with su_root_release() and another thread can
obtain it.
The function su_root_has_thread() can be used to check if a thread has
obtained or released the root.
Implementation upgraded the su_port_own_thread() method as su_port_thread().
Fri Oct 5 07:28:10 EDT 2007 Pekka Pessi <first.lastname@nokia.com>
* su_port.h: removed su_port_threadsafe() and su_port_yield() methods
su_port_wait_events() replaces su_port_yield().
Fri Oct 5 13:26:04 EDT 2007 Pekka Pessi <Pekka.Pessi@nokia.com>
* msg_parser.awk: not extending header structure unless needed.
Removed gawk-ish /* comments */.
Fri Oct 5 14:32:25 EDT 2007 Pekka Pessi <Pekka.Pessi@nokia.com>
* run_test_su: removed GNUisms
Fri Oct 5 14:32:47 EDT 2007 Pekka Pessi <Pekka.Pessi@nokia.com>
* Makefile.am: removed implicit check target test_urlmap
Fri Oct 5 14:22:32 EDT 2007 Pekka Pessi <first.lastname@nokia.com>
* torture_sresolv.c: use CLOCK_REALTIME if no CLOCK_PROCESS_CPUTIME_ID available
Casting timespec tv_sec to unsigned long.
Fri Oct * nua_s added handling nua_prack()
Thanks to Fabio Margarido for the patch.
Mon Oct 8 10:24:35 EDT 2007 Pekka.Pessi@nokia.com
* test_nua: added test for sf.net bug #1803686
Mon Oct 8 08:15:23 EDT 2007 Pekka.Pessi@nokia.com
* RELEASE: updated.
Mon Oct 8 09:30:36 EDT 2007 Pekka.Pessi@nokia.com
* nua_stack: added handling nua_prack()
Thanks to Fabio Margarido for the patch.
Mon Oct 8 10:24:35 EDT 2007 Pekka.Pessi@nokia.com
* test_nua: added test for sf.net bug #1803686
Mon Oct 8 10:26:31 EDT 2007 Pekka.Pessi@nokia.com
* nua: added test for nua_prack() (sf.net bug #1804248)
Avoid sending nua_i_state after nua_prack() if no SDP O/A is happening, too.
Mon Oct 8 10:32:04 EDT 2007 Mikhail Zabaluev <mikhail.zabaluev@nokia.com>
* su_source.c: don t leak the wait arrays
Mon Oct 8 10:37:11 EDT 2007 Pekka.Pessi@nokia.com
* RELEASE: updated
Wed Oct 10 11:55:21 EDT 2007 Pekka.Pessi@nokia.com
* sip_parser.c: silenced warning about extra const in sip_extend_mclass()
Wed Oct 10 11:57:08 EDT 2007 Pekka.Pessi@nokia.com
* nta_tag.c: updated tag documentation
Wed Oct 10 13:16:40 EDT 2007 Pekka.Pessi@nokia.com
* nua: fix logging crash if outbound used with application contact
Silenced warnings.
Wed Oct 10 13:30:45 EDT 2007 Pekka.Pessi@nokia.com
* msg_parser.awk: removed extra "const"
Wed Oct 10 13:31:45 EDT 2007 Pekka.Pessi@nokia.com
* Makefile.am's: fixed distclean of documentation
git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@5840 d0543943-73ff-0310-b7d9-9358b9ac24b2
2007-10-11 14:16:59 +00:00
|
|
|
su_pthread_port_thread,
|
2007-04-15 02:03:41 +00:00
|
|
|
su_base_port_add_prepoll,
|
|
|
|
su_base_port_remove_prepoll,
|
|
|
|
su_base_port_timers,
|
|
|
|
su_epoll_port_multishot,
|
|
|
|
su_epoll_port_wait_events,
|
|
|
|
su_base_port_getmsgs,
|
|
|
|
su_base_port_getmsgs_from,
|
|
|
|
su_epoll_port_name,
|
|
|
|
su_base_port_start_shared,
|
|
|
|
su_pthread_port_wait,
|
|
|
|
su_pthread_port_execute,
|
2009-02-11 17:16:44 +00:00
|
|
|
su_base_port_deferrable,
|
|
|
|
su_base_port_max_defer,
|
|
|
|
su_socket_port_wakeup,
|
|
|
|
su_base_port_is_running,
|
2007-04-15 02:03:41 +00:00
|
|
|
}};
|
|
|
|
|
|
|
|
static char const *su_epoll_port_name(su_port_t const *self)
|
|
|
|
{
|
|
|
|
return "epoll";
|
|
|
|
}
|
|
|
|
|
|
|
|
static void su_epoll_port_decref(su_port_t *self, int blocking, char const *who)
|
|
|
|
{
|
|
|
|
(void)su_base_port_decref(self, blocking, who);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void su_epoll_port_deinit(void *arg)
|
|
|
|
{
|
|
|
|
su_port_t *self = arg;
|
|
|
|
|
|
|
|
SU_DEBUG_9(("%s(%p) called\n", "su_epoll_port_deinit", (void* )self));
|
|
|
|
|
|
|
|
su_socket_port_deinit(self->sup_base);
|
|
|
|
|
|
|
|
close(self->sup_epoll), self->sup_epoll = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @internal
|
|
|
|
*
|
|
|
|
* Register a #su_wait_t object. The wait object, a callback function and
|
|
|
|
* an argument pointer is stored in the port object. The callback function
|
|
|
|
* will be called when the wait object is signaled.
|
|
|
|
*
|
|
|
|
* Please note if identical wait objects are inserted, only first one is
|
|
|
|
* ever signalled.
|
2008-12-16 18:05:22 +00:00
|
|
|
*
|
2007-04-15 02:03:41 +00:00
|
|
|
* @param self pointer to port
|
|
|
|
* @param root pointer to root object
|
|
|
|
* @param waits pointer to wait object
|
|
|
|
* @param callback callback function pointer
|
|
|
|
* @param arg argument given to callback function when it is invoked
|
2008-12-16 18:05:22 +00:00
|
|
|
* @param priority relative priority of the wait object
|
2007-04-15 02:03:41 +00:00
|
|
|
* (0 is normal, 1 important, 2 realtime)
|
2008-12-16 18:05:22 +00:00
|
|
|
*
|
2007-04-15 02:03:41 +00:00
|
|
|
* @return
|
2008-12-16 18:05:22 +00:00
|
|
|
* Positive index of the wait object,
|
2007-04-15 02:03:41 +00:00
|
|
|
* or -1 upon an error.
|
|
|
|
*/
|
|
|
|
int su_epoll_port_register(su_port_t *self,
|
2008-12-16 18:05:22 +00:00
|
|
|
su_root_t *root,
|
|
|
|
su_wait_t *wait,
|
2007-04-15 02:03:41 +00:00
|
|
|
su_wakeup_f callback,
|
|
|
|
su_wakeup_arg_t *arg,
|
|
|
|
int priority)
|
|
|
|
{
|
|
|
|
int i, j, n;
|
|
|
|
struct epoll_event ev;
|
|
|
|
struct su_epoll_register *ser;
|
|
|
|
struct su_epoll_register **indices = self->sup_indices;
|
|
|
|
|
|
|
|
assert(su_port_own_thread(self));
|
|
|
|
|
|
|
|
n = self->sup_size_indices;
|
|
|
|
|
|
|
|
if (n >= SU_WAIT_MAX)
|
|
|
|
return su_seterrno(ENOMEM);
|
|
|
|
|
|
|
|
ser = indices[0];
|
|
|
|
|
|
|
|
if (!ser) {
|
|
|
|
su_home_t *h = su_port_home(self);
|
|
|
|
|
|
|
|
i = self->sup_max_index, j = i == 0 ? 15 : i + 16;
|
2008-12-16 18:05:22 +00:00
|
|
|
|
2007-04-15 02:03:41 +00:00
|
|
|
if (j >= self->sup_size_indices) {
|
|
|
|
/* Reallocate index table */
|
|
|
|
n = n < 1024 ? 2 * n : n + 1024;
|
|
|
|
indices = su_realloc(h, indices, n * sizeof(indices[0]));
|
|
|
|
if (!indices)
|
|
|
|
return -1;
|
|
|
|
self->sup_indices = indices;
|
|
|
|
self->sup_size_indices = n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate registrations */
|
|
|
|
ser = su_zalloc(h, (j - i) * (sizeof *ser));
|
|
|
|
if (!ser)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
indices[0] = ser;
|
|
|
|
|
|
|
|
for (i++; i <= j; i++) {
|
|
|
|
ser->ser_id = i;
|
|
|
|
ser->ser_next = i < j ? ser + 1 : NULL;
|
|
|
|
indices[i] = ser++;
|
|
|
|
}
|
|
|
|
|
|
|
|
self->sup_max_index = j;
|
|
|
|
|
|
|
|
ser = indices[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
i = ser->ser_id;
|
|
|
|
|
|
|
|
ev.events = POLL2EPOLL(wait->events);
|
|
|
|
ev.data.u64 = 0;
|
|
|
|
ev.data.u32 = (uint32_t)i;
|
2008-12-16 18:05:22 +00:00
|
|
|
|
2007-04-15 02:03:41 +00:00
|
|
|
if (epoll_ctl(self->sup_epoll, EPOLL_CTL_ADD, wait->fd, &ev) == -1) {
|
|
|
|
SU_DEBUG_0(("EPOLL_CTL_ADD(%u, %u) failed: %s\n",
|
|
|
|
wait->fd, ev.events, strerror(errno)));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
indices[0] = ser->ser_next;
|
|
|
|
|
|
|
|
ser->ser_next = NULL;
|
|
|
|
*ser->ser_wait = *wait;
|
|
|
|
ser->ser_cb = callback;
|
|
|
|
ser->ser_arg = arg;
|
|
|
|
ser->ser_root = root;
|
|
|
|
|
|
|
|
self->sup_registers++;
|
|
|
|
self->sup_n_registrations++;
|
|
|
|
|
|
|
|
return i; /* return index */
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Deregister a su_wait_t object. */
|
|
|
|
static int su_epoll_port_deregister0(su_port_t *self, int i, int destroy_wait)
|
|
|
|
{
|
|
|
|
struct su_epoll_register **indices = self->sup_indices;
|
|
|
|
struct su_epoll_register *ser;
|
|
|
|
|
|
|
|
ser = self->sup_indices[i];
|
|
|
|
if (ser == NULL || ser->ser_cb == NULL) {
|
|
|
|
su_seterrno(ENOENT);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(ser->ser_id == i);
|
|
|
|
|
|
|
|
if (epoll_ctl(self->sup_epoll, EPOLL_CTL_DEL, ser->ser_wait->fd, NULL) == -1) {
|
2008-12-16 18:05:22 +00:00
|
|
|
SU_DEBUG_1(("su_port(%p): EPOLL_CTL_DEL(%u): %s\n", (void *)self,
|
2007-04-15 02:03:41 +00:00
|
|
|
ser->ser_wait->fd, su_strerror(su_errno())));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (destroy_wait)
|
|
|
|
su_wait_destroy(ser->ser_wait);
|
2008-12-16 18:05:22 +00:00
|
|
|
|
2007-04-15 02:03:41 +00:00
|
|
|
memset(ser, 0, sizeof *ser);
|
|
|
|
ser->ser_id = i;
|
|
|
|
ser->ser_next = indices[0], indices[0] = ser;
|
|
|
|
|
|
|
|
self->sup_n_registrations--;
|
|
|
|
self->sup_registers++;
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Unregister a su_wait_t object.
|
2008-12-16 18:05:22 +00:00
|
|
|
*
|
2007-04-15 02:03:41 +00:00
|
|
|
* The function su_epoll_port_unregister() unregisters a su_wait_t object. The
|
|
|
|
* wait object, a callback function and a argument are removed from the
|
|
|
|
* port object.
|
2008-12-16 18:05:22 +00:00
|
|
|
*
|
2007-04-15 02:03:41 +00:00
|
|
|
* @param self - pointer to port object
|
|
|
|
* @param root - pointer to root object
|
|
|
|
* @param wait - pointer to wait object
|
|
|
|
* @param callback - callback function pointer (may be NULL)
|
2008-12-16 18:05:22 +00:00
|
|
|
* @param arg - argument given to callback function when it is invoked
|
2007-04-15 02:03:41 +00:00
|
|
|
* (may be NULL)
|
|
|
|
*
|
2008-12-16 18:05:22 +00:00
|
|
|
* @deprecated Use su_epoll_port_deregister() instead.
|
2007-04-15 02:03:41 +00:00
|
|
|
*
|
|
|
|
* @return Nonzero index of the wait object, or -1 upon an error.
|
|
|
|
*/
|
|
|
|
int su_epoll_port_unregister(su_port_t *self,
|
2008-12-16 18:05:22 +00:00
|
|
|
su_root_t *root,
|
|
|
|
su_wait_t *wait,
|
2007-04-15 02:03:41 +00:00
|
|
|
su_wakeup_f callback, /* XXX - ignored */
|
|
|
|
su_wakeup_arg_t *arg)
|
|
|
|
{
|
|
|
|
int i, I;
|
|
|
|
|
|
|
|
struct su_epoll_register *ser;
|
|
|
|
|
|
|
|
assert(self);
|
|
|
|
assert(su_port_own_thread(self));
|
|
|
|
|
|
|
|
I = self->sup_max_index;
|
|
|
|
|
|
|
|
for (i = 1; i <= I; i++) {
|
|
|
|
ser = self->sup_indices[i];
|
|
|
|
|
|
|
|
if (ser->ser_cb &&
|
|
|
|
arg == ser->ser_arg &&
|
|
|
|
SU_WAIT_CMP(wait[0], ser->ser_wait[0]) == 0)
|
|
|
|
return su_epoll_port_deregister0(self, ser->ser_id, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
su_seterrno(ENOENT);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Deregister a su_wait_t object.
|
2008-12-16 18:05:22 +00:00
|
|
|
*
|
2007-04-15 02:03:41 +00:00
|
|
|
* Deregisters a registration by index. The wait object, a callback
|
|
|
|
* function and a argument are removed from the port object. The wait
|
|
|
|
* object is destroyed.
|
2008-12-16 18:05:22 +00:00
|
|
|
*
|
2007-04-15 02:03:41 +00:00
|
|
|
* @param self - pointer to port object
|
|
|
|
* @param i - registration index
|
2008-12-16 18:05:22 +00:00
|
|
|
*
|
2007-04-15 02:03:41 +00:00
|
|
|
* @return Index of the wait object, or -1 upon an error.
|
|
|
|
*/
|
|
|
|
int su_epoll_port_deregister(su_port_t *self, int i)
|
|
|
|
{
|
|
|
|
struct su_epoll_register *ser;
|
|
|
|
|
|
|
|
if (i <= 0 || i > self->sup_max_index)
|
|
|
|
return su_seterrno(EBADF);
|
|
|
|
|
|
|
|
ser = self->sup_indices[i];
|
|
|
|
if (!ser->ser_cb)
|
|
|
|
return su_seterrno(EBADF);
|
|
|
|
|
|
|
|
return su_epoll_port_deregister0(self, i, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** @internal
|
|
|
|
* Unregister all su_wait_t objects of given su_root_t instance.
|
|
|
|
*
|
|
|
|
* The function su_epoll_port_unregister_all() unregisters all su_wait_t
|
|
|
|
* objects associated with given root object.
|
2008-12-16 18:05:22 +00:00
|
|
|
*
|
2007-04-15 02:03:41 +00:00
|
|
|
* @param self - pointer to port object
|
|
|
|
* @param root - pointer to root object
|
2008-12-16 18:05:22 +00:00
|
|
|
*
|
2007-04-15 02:03:41 +00:00
|
|
|
* @return Number of wait objects removed.
|
|
|
|
*/
|
|
|
|
int su_epoll_port_unregister_all(su_port_t *self, su_root_t *root)
|
|
|
|
{
|
|
|
|
int i, I, n;
|
|
|
|
|
|
|
|
struct su_epoll_register *ser;
|
|
|
|
|
|
|
|
assert(self); assert(root);
|
|
|
|
assert(su_port_own_thread(self));
|
|
|
|
|
|
|
|
I = self->sup_max_index;
|
|
|
|
|
|
|
|
for (i = 1, n = 0; i <= I; i++) {
|
|
|
|
ser = self->sup_indices[i];
|
|
|
|
if (ser->ser_root != root)
|
|
|
|
continue;
|
|
|
|
su_epoll_port_deregister0(self, ser->ser_id, 0);
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**Set mask for a registered event. @internal
|
|
|
|
*
|
|
|
|
* The function su_epoll_port_eventmask() sets the mask describing events that can
|
|
|
|
* signal the registered callback.
|
|
|
|
*
|
|
|
|
* @param port pointer to port object
|
|
|
|
* @param index registration index
|
|
|
|
* @param socket socket
|
|
|
|
* @param events new event mask
|
|
|
|
*
|
|
|
|
* @retval 0 when successful,
|
|
|
|
* @retval -1 upon an error.
|
|
|
|
*/
|
|
|
|
int su_epoll_port_eventmask(su_port_t *self, int index, int socket, int events)
|
|
|
|
{
|
|
|
|
struct su_epoll_register *ser;
|
|
|
|
struct epoll_event ev;
|
|
|
|
|
|
|
|
if (index <= 0 || index > self->sup_max_index)
|
|
|
|
return su_seterrno(EBADF);
|
|
|
|
|
|
|
|
ser = self->sup_indices[index];
|
|
|
|
if (!ser->ser_cb)
|
|
|
|
return su_seterrno(EBADF);
|
|
|
|
|
|
|
|
ser->ser_wait->events = events;
|
|
|
|
|
|
|
|
ev.events = POLL2EPOLL(events);
|
|
|
|
ev.data.u64 = (uint64_t)0;
|
|
|
|
ev.data.u32 = (uint32_t)index;
|
|
|
|
|
|
|
|
if (epoll_ctl(self->sup_epoll, EPOLL_CTL_MOD, socket, &ev) == -1) {
|
2008-12-16 18:05:22 +00:00
|
|
|
SU_DEBUG_1(("su_port(%p): EPOLL_CTL_MOD(%u): %s\n", (void *)self,
|
2007-04-15 02:03:41 +00:00
|
|
|
socket, su_strerror(su_errno())));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @internal Enable multishot mode.
|
|
|
|
*
|
|
|
|
* Enables, disables or queries the multishot mode for the port. The
|
|
|
|
* multishot mode determines how the events are scheduled by port. If
|
|
|
|
* multishot mode is enabled, port serves all the sockets that have received
|
|
|
|
* network events. If it is disabled, only first socket event is served.
|
|
|
|
*
|
|
|
|
* @param self pointer to port object
|
|
|
|
* @param multishot multishot mode (0 => disables, 1 => enables, -1 => query)
|
2008-12-16 18:05:22 +00:00
|
|
|
*
|
2007-04-15 02:03:41 +00:00
|
|
|
* @retval 0 multishot mode is disabled
|
|
|
|
* @retval 1 multishot mode is enabled
|
|
|
|
* @retval -1 an error occurred
|
|
|
|
*/
|
|
|
|
static
|
|
|
|
int su_epoll_port_multishot(su_port_t *self, int multishot)
|
|
|
|
{
|
|
|
|
if (multishot < 0)
|
|
|
|
return self->sup_multishot;
|
|
|
|
else if (multishot == 0 || multishot == 1)
|
|
|
|
return self->sup_multishot = multishot;
|
2008-12-16 18:05:22 +00:00
|
|
|
else
|
2007-04-15 02:03:41 +00:00
|
|
|
return (errno = EINVAL), -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** @internal
|
|
|
|
* Wait (poll()) for wait objects in port.
|
|
|
|
*
|
|
|
|
* @param self pointer to port
|
|
|
|
* @param tout timeout in milliseconds
|
|
|
|
*
|
|
|
|
* @return number of events handled
|
|
|
|
*/
|
|
|
|
static
|
|
|
|
int su_epoll_port_wait_events(su_port_t *self, su_duration_t tout)
|
|
|
|
{
|
|
|
|
int j, n, events = 0, index;
|
|
|
|
unsigned version = self->sup_registers;
|
|
|
|
|
|
|
|
int const M = 4;
|
|
|
|
struct epoll_event ev[M];
|
2008-12-16 18:05:22 +00:00
|
|
|
|
2007-04-15 02:03:41 +00:00
|
|
|
n = epoll_wait(self->sup_epoll, ev, self->sup_multishot ? M : 1, tout);
|
|
|
|
|
|
|
|
assert(n <= M);
|
|
|
|
|
|
|
|
for (j = 0; j < n; j++) {
|
|
|
|
struct su_epoll_register *ser;
|
|
|
|
su_root_magic_t *magic;
|
|
|
|
|
|
|
|
index = (int)ev[j].data.u32;
|
|
|
|
if (!ev[j].events || index <= 0 || self->sup_max_index < index)
|
|
|
|
continue;
|
|
|
|
ser = self->sup_indices[index];
|
|
|
|
|
|
|
|
magic = ser->ser_root ? su_root_magic(ser->ser_root) : NULL;
|
|
|
|
ser->ser_wait->revents = ev[j].events;
|
|
|
|
ser->ser_cb(magic, ser->ser_wait, ser->ser_arg);
|
|
|
|
events++;
|
|
|
|
if (version != self->sup_registers)
|
|
|
|
/* Callback function used su_register()/su_deregister() */
|
|
|
|
return events;
|
2008-12-16 18:05:22 +00:00
|
|
|
}
|
2007-04-15 02:03:41 +00:00
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Create a port using epoll() or poll().
|
|
|
|
*/
|
|
|
|
su_port_t *su_epoll_port_create(void)
|
|
|
|
{
|
|
|
|
su_port_t *self;
|
|
|
|
int epoll = epoll_create(su_root_size_hint);
|
|
|
|
|
|
|
|
if (epoll == -1) {
|
|
|
|
/* Fallback to poll() */
|
2008-12-16 18:05:22 +00:00
|
|
|
SU_DEBUG_3(("%s(): epoll_create() => %u: %s\n",
|
2007-04-15 02:03:41 +00:00
|
|
|
"su_port_create", epoll, strerror(errno)));
|
|
|
|
return su_poll_port_create();
|
|
|
|
}
|
|
|
|
|
|
|
|
self = su_home_new(sizeof *self);
|
|
|
|
if (!self) {
|
|
|
|
close(epoll);
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
SU_DEBUG_9(("%s(%p): epoll_create() => %u: %s\n",
|
|
|
|
"su_port_create", (void *)self, self->sup_epoll, "OK"));
|
|
|
|
|
|
|
|
if (su_home_destructor(su_port_home(self), su_epoll_port_deinit) < 0 ||
|
2008-12-16 18:05:22 +00:00
|
|
|
!(self->sup_indices =
|
2007-04-15 02:03:41 +00:00
|
|
|
su_zalloc(su_port_home(self),
|
2008-12-16 18:05:22 +00:00
|
|
|
(sizeof self->sup_indices[0]) *
|
2007-04-15 02:03:41 +00:00
|
|
|
(self->sup_size_indices = 64)))) {
|
|
|
|
su_home_unref(su_port_home(self));
|
|
|
|
close(epoll);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
self->sup_epoll = epoll;
|
|
|
|
self->sup_multishot = SU_ENABLE_MULTISHOT_POLL;
|
|
|
|
|
|
|
|
if (su_socket_port_init(self->sup_base, su_epoll_port_vtable) < 0)
|
|
|
|
return su_home_unref(su_port_home(self)), NULL;
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
int su_epoll_clone_start(su_root_t *parent,
|
|
|
|
su_clone_r return_clone,
|
|
|
|
su_root_magic_t *magic,
|
|
|
|
su_root_init_f init,
|
|
|
|
su_root_deinit_f deinit)
|
|
|
|
{
|
2008-12-16 18:05:22 +00:00
|
|
|
return su_pthreaded_port_start(su_epoll_port_create,
|
2007-04-15 02:03:41 +00:00
|
|
|
parent, return_clone, magic, init, deinit);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
su_port_t *su_epoll_port_create(void)
|
|
|
|
{
|
|
|
|
return su_default_port_create();
|
|
|
|
}
|
|
|
|
|
|
|
|
int su_epoll_clone_start(su_root_t *parent,
|
|
|
|
su_clone_r return_clone,
|
|
|
|
su_root_magic_t *magic,
|
|
|
|
su_root_init_f init,
|
|
|
|
su_root_deinit_f deinit)
|
|
|
|
{
|
|
|
|
return su_default_clone_start(parent, return_clone, magic, init, deinit);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* HAVE_EPOLL */
|