2011-03-15 19:14:43 +00:00
|
|
|
/*
|
2013-11-18 20:25:34 -05:00
|
|
|
* Copyright (c) 2008-2013, Dave Benson. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are
|
|
|
|
* met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
2011-03-15 19:14:43 +00:00
|
|
|
*
|
2013-11-18 20:25:34 -05:00
|
|
|
* * Redistributions in binary form must reproduce the above
|
|
|
|
* copyright notice, this list of conditions and the following disclaimer
|
|
|
|
* in the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
2011-03-15 19:14:43 +00:00
|
|
|
*
|
2013-11-18 20:25:34 -05:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2011-03-15 19:14:43 +00:00
|
|
|
*/
|
|
|
|
|
2009-01-28 15:16:11 +00:00
|
|
|
/* NOTE: this may not work very well on windows, where i'm
|
|
|
|
not sure that "SOCKETs" are allocated nicely like
|
|
|
|
file-descriptors are */
|
2009-01-29 02:57:17 +00:00
|
|
|
/* TODO:
|
|
|
|
* * epoll() implementation
|
|
|
|
* * kqueue() implementation
|
|
|
|
* * windows port (yeah, right, volunteers are DEFINITELY needed for this one...)
|
|
|
|
*/
|
2009-01-22 15:27:27 +00:00
|
|
|
#include <assert.h>
|
2009-04-21 14:07:21 +00:00
|
|
|
#if HAVE_ALLOCA_H
|
|
|
|
# include <alloca.h>
|
|
|
|
#elif HAVE_MALLOC_H
|
|
|
|
# include <malloc.h>
|
|
|
|
#endif
|
2009-01-22 15:27:27 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
2009-04-21 14:07:21 +00:00
|
|
|
#if HAVE_SYS_POLL_H
|
|
|
|
# include <sys/poll.h>
|
|
|
|
# define USE_POLL 1
|
|
|
|
#elif HAVE_SYS_SELECT_H
|
|
|
|
# include <sys/select.h>
|
|
|
|
# define USE_POLL 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* windows annoyances: use select, use a full-fledges map for fds */
|
|
|
|
#ifdef WIN32
|
|
|
|
# include <winsock.h>
|
|
|
|
# define USE_POLL 0
|
|
|
|
# define HAVE_SMALL_FDS 0
|
|
|
|
#endif
|
2009-01-22 15:27:27 +00:00
|
|
|
#include <limits.h>
|
|
|
|
#include <errno.h>
|
2009-01-28 15:16:11 +00:00
|
|
|
#include <signal.h>
|
2009-01-21 08:57:24 +00:00
|
|
|
#include "protobuf-c-dispatch.h"
|
2009-01-21 16:35:53 +00:00
|
|
|
#include "gskrbtreemacros.h"
|
2009-01-25 13:51:30 +00:00
|
|
|
#include "gsklistmacros.h"
|
2009-01-21 08:57:24 +00:00
|
|
|
|
2009-01-28 15:16:11 +00:00
|
|
|
#define DEBUG_DISPATCH_INTERNALS 0
|
|
|
|
#define DEBUG_DISPATCH 0
|
|
|
|
|
2009-04-21 14:07:21 +00:00
|
|
|
#ifndef HAVE_SMALL_FDS
|
|
|
|
# define HAVE_SMALL_FDS 1
|
|
|
|
#endif
|
|
|
|
|
2009-01-22 15:27:27 +00:00
|
|
|
#define protobuf_c_assert(condition) assert(condition)
|
|
|
|
|
2009-01-21 08:57:24 +00:00
|
|
|
#define ALLOC_WITH_ALLOCATOR(allocator, size) ((allocator)->alloc ((allocator)->allocator_data, (size)))
|
|
|
|
#define FREE_WITH_ALLOCATOR(allocator, ptr) ((allocator)->free ((allocator)->allocator_data, (ptr)))
|
2009-01-22 15:27:27 +00:00
|
|
|
|
|
|
|
/* macros that assume you have a ProtobufCAllocator* named
|
|
|
|
allocator in scope */
|
|
|
|
#define ALLOC(size) ALLOC_WITH_ALLOCATOR((allocator), size)
|
|
|
|
#define FREE(ptr) FREE_WITH_ALLOCATOR((allocator), ptr)
|
2009-01-21 08:57:24 +00:00
|
|
|
|
|
|
|
typedef struct _Callback Callback;
|
|
|
|
struct _Callback
|
|
|
|
{
|
2009-01-21 16:35:53 +00:00
|
|
|
ProtobufCDispatchCallback func;
|
2009-01-21 08:57:24 +00:00
|
|
|
void *data;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct _FDMap FDMap;
|
|
|
|
struct _FDMap
|
|
|
|
{
|
|
|
|
int notify_desired_index; /* -1 if not an known fd */
|
|
|
|
int change_index; /* -1 if no prior change */
|
|
|
|
int closed_since_notify_started;
|
|
|
|
};
|
|
|
|
|
2009-04-21 14:07:21 +00:00
|
|
|
#if !HAVE_SMALL_FDS
|
|
|
|
typedef struct _FDMapNode FDMapNode;
|
|
|
|
struct _FDMapNode
|
|
|
|
{
|
|
|
|
ProtobufC_FD fd;
|
|
|
|
FDMapNode *left, *right, *parent;
|
|
|
|
protobuf_c_boolean is_red;
|
|
|
|
FDMap map;
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2009-01-21 08:57:24 +00:00
|
|
|
typedef struct _RealDispatch RealDispatch;
|
|
|
|
struct _RealDispatch
|
|
|
|
{
|
2009-01-21 16:35:53 +00:00
|
|
|
ProtobufCDispatch base;
|
2009-01-21 08:57:24 +00:00
|
|
|
Callback *callbacks; /* parallels notifies_desired */
|
|
|
|
size_t notifies_desired_alloced;
|
|
|
|
size_t changes_alloced;
|
2009-04-21 14:07:21 +00:00
|
|
|
#if HAVE_SMALL_FDS
|
2009-01-21 08:57:24 +00:00
|
|
|
FDMap *fd_map; /* map indexed by fd */
|
|
|
|
size_t fd_map_size; /* number of elements of fd_map */
|
2009-04-21 14:07:21 +00:00
|
|
|
#else
|
|
|
|
FDMapNode *fd_map_tree; /* map indexed by fd */
|
|
|
|
#endif
|
|
|
|
|
2011-03-21 21:28:39 +00:00
|
|
|
protobuf_c_boolean is_dispatching;
|
2009-01-21 16:35:53 +00:00
|
|
|
|
|
|
|
ProtobufCDispatchTimer *timer_tree;
|
2009-01-22 15:27:27 +00:00
|
|
|
ProtobufCAllocator *allocator;
|
|
|
|
ProtobufCDispatchTimer *recycled_timeouts;
|
|
|
|
|
|
|
|
ProtobufCDispatchIdle *first_idle, *last_idle;
|
|
|
|
ProtobufCDispatchIdle *recycled_idles;
|
2009-01-21 16:35:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct _ProtobufCDispatchTimer
|
|
|
|
{
|
2009-01-22 15:27:27 +00:00
|
|
|
RealDispatch *dispatch;
|
|
|
|
|
2009-01-21 16:35:53 +00:00
|
|
|
/* the actual timeout time */
|
|
|
|
unsigned long timeout_secs;
|
|
|
|
unsigned timeout_usecs;
|
|
|
|
|
|
|
|
/* red-black tree stuff */
|
|
|
|
ProtobufCDispatchTimer *left, *right, *parent;
|
|
|
|
protobuf_c_boolean is_red;
|
|
|
|
|
|
|
|
/* user callback */
|
|
|
|
ProtobufCDispatchTimerFunc func;
|
|
|
|
void *func_data;
|
2009-01-21 08:57:24 +00:00
|
|
|
};
|
|
|
|
|
2009-01-25 13:51:30 +00:00
|
|
|
struct _ProtobufCDispatchIdle
|
|
|
|
{
|
|
|
|
RealDispatch *dispatch;
|
|
|
|
|
|
|
|
ProtobufCDispatchIdle *prev, *next;
|
|
|
|
|
|
|
|
/* user callback */
|
|
|
|
ProtobufCDispatchIdleFunc func;
|
|
|
|
void *func_data;
|
|
|
|
};
|
2009-01-21 16:35:53 +00:00
|
|
|
/* Define the tree of timers, as per gskrbtreemacros.h */
|
|
|
|
#define TIMER_GET_IS_RED(n) ((n)->is_red)
|
|
|
|
#define TIMER_SET_IS_RED(n,v) ((n)->is_red = (v))
|
|
|
|
#define TIMERS_COMPARE(a,b, rv) \
|
|
|
|
if (a->timeout_secs < b->timeout_secs) rv = -1; \
|
|
|
|
else if (a->timeout_secs > b->timeout_secs) rv = 1; \
|
|
|
|
else if (a->timeout_usecs < b->timeout_usecs) rv = -1; \
|
|
|
|
else if (a->timeout_usecs > b->timeout_usecs) rv = 1; \
|
|
|
|
else if (a < b) rv = -1; \
|
|
|
|
else if (a > b) rv = 1; \
|
|
|
|
else rv = 0;
|
|
|
|
#define GET_TIMER_TREE(d) \
|
|
|
|
(d)->timer_tree, ProtobufCDispatchTimer *, \
|
|
|
|
TIMER_GET_IS_RED, TIMER_SET_IS_RED, \
|
|
|
|
parent, left, right, \
|
|
|
|
TIMERS_COMPARE
|
|
|
|
|
2009-04-21 14:07:21 +00:00
|
|
|
#if !HAVE_SMALL_FDS
|
|
|
|
#define FD_MAP_NODES_COMPARE(a,b, rv) \
|
|
|
|
if (a->fd < b->fd) rv = -1; \
|
|
|
|
else if (a->fd > b->fd) rv = 1; \
|
|
|
|
else rv = 0;
|
|
|
|
#define GET_FD_MAP_TREE(d) \
|
|
|
|
(d)->fd_map_tree, FDMapNode *, \
|
|
|
|
TIMER_GET_IS_RED, TIMER_SET_IS_RED, \
|
|
|
|
parent, left, right, \
|
|
|
|
FD_MAP_NODES_COMPARE
|
|
|
|
#define COMPARE_FD_TO_FD_MAP_NODE(a,b, rv) \
|
|
|
|
if (a < b->fd) rv = -1; \
|
|
|
|
else if (a > b->fd) rv = 1; \
|
|
|
|
else rv = 0;
|
|
|
|
#endif
|
|
|
|
|
2009-01-25 13:51:30 +00:00
|
|
|
/* declare the idle-handler list */
|
|
|
|
#define GET_IDLE_LIST(d) \
|
|
|
|
ProtobufCDispatchIdle *, d->first_idle, d->last_idle, prev, next
|
|
|
|
|
2009-01-21 08:57:24 +00:00
|
|
|
/* Create or destroy a Dispatch */
|
2009-01-22 15:27:27 +00:00
|
|
|
ProtobufCDispatch *protobuf_c_dispatch_new (ProtobufCAllocator *allocator)
|
2009-01-21 08:57:24 +00:00
|
|
|
{
|
|
|
|
RealDispatch *rv = ALLOC (sizeof (RealDispatch));
|
2009-01-28 15:16:11 +00:00
|
|
|
struct timeval tv;
|
2009-01-21 08:57:24 +00:00
|
|
|
rv->base.n_changes = 0;
|
|
|
|
rv->notifies_desired_alloced = 8;
|
|
|
|
rv->base.notifies_desired = ALLOC (sizeof (ProtobufC_FDNotify) * rv->notifies_desired_alloced);
|
2009-01-28 15:16:11 +00:00
|
|
|
rv->base.n_notifies_desired = 0;
|
2009-01-21 08:57:24 +00:00
|
|
|
rv->callbacks = ALLOC (sizeof (Callback) * rv->notifies_desired_alloced);
|
|
|
|
rv->changes_alloced = 8;
|
2011-11-02 02:25:28 +00:00
|
|
|
rv->base.changes = ALLOC (sizeof (ProtobufC_FDNotifyChange) * rv->changes_alloced);
|
2009-04-21 14:07:21 +00:00
|
|
|
#if HAVE_SMALL_FDS
|
2009-01-21 08:57:24 +00:00
|
|
|
rv->fd_map_size = 16;
|
|
|
|
rv->fd_map = ALLOC (sizeof (FDMap) * rv->fd_map_size);
|
2009-04-21 14:07:21 +00:00
|
|
|
memset (rv->fd_map, 255, sizeof (FDMap) * rv->fd_map_size);
|
|
|
|
#else
|
|
|
|
rv->fd_map_tree = NULL;
|
|
|
|
#endif
|
2009-01-28 05:29:56 +00:00
|
|
|
rv->allocator = allocator;
|
2009-01-28 15:16:11 +00:00
|
|
|
rv->timer_tree = NULL;
|
|
|
|
rv->first_idle = rv->last_idle = NULL;
|
2011-03-28 13:32:01 +00:00
|
|
|
rv->base.has_idle = 0;
|
2009-01-28 15:16:11 +00:00
|
|
|
rv->recycled_idles = NULL;
|
|
|
|
rv->recycled_timeouts = NULL;
|
2011-03-21 21:28:39 +00:00
|
|
|
rv->is_dispatching = 0;
|
2009-01-28 15:16:11 +00:00
|
|
|
|
|
|
|
/* need to handle SIGPIPE more gracefully than default */
|
|
|
|
signal (SIGPIPE, SIG_IGN);
|
|
|
|
|
|
|
|
gettimeofday (&tv, NULL);
|
|
|
|
rv->base.last_dispatch_secs = tv.tv_sec;
|
|
|
|
rv->base.last_dispatch_usecs = tv.tv_usec;
|
|
|
|
|
2009-01-21 08:57:24 +00:00
|
|
|
return &rv->base;
|
|
|
|
}
|
|
|
|
|
2009-04-21 14:07:21 +00:00
|
|
|
#if !HAVE_SMALL_FDS
|
|
|
|
void free_fd_tree_recursive (ProtobufCAllocator *allocator,
|
|
|
|
FDMapNode *node)
|
|
|
|
{
|
|
|
|
if (node)
|
|
|
|
{
|
|
|
|
free_fd_tree_recursive (allocator, node->left);
|
|
|
|
free_fd_tree_recursive (allocator, node->right);
|
|
|
|
FREE (node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* XXX: leaking timer_tree seemingly? */
|
2009-01-21 08:57:24 +00:00
|
|
|
void
|
2009-01-21 16:35:53 +00:00
|
|
|
protobuf_c_dispatch_free(ProtobufCDispatch *dispatch)
|
2009-01-21 08:57:24 +00:00
|
|
|
{
|
|
|
|
RealDispatch *d = (RealDispatch *) dispatch;
|
2009-01-22 15:27:27 +00:00
|
|
|
ProtobufCAllocator *allocator = d->allocator;
|
2009-01-28 15:16:11 +00:00
|
|
|
while (d->recycled_timeouts != NULL)
|
|
|
|
{
|
|
|
|
ProtobufCDispatchTimer *t = d->recycled_timeouts;
|
|
|
|
d->recycled_timeouts = t->right;
|
|
|
|
FREE (t);
|
|
|
|
}
|
|
|
|
while (d->recycled_idles != NULL)
|
|
|
|
{
|
|
|
|
ProtobufCDispatchIdle *i = d->recycled_idles;
|
|
|
|
d->recycled_idles = i->next;
|
|
|
|
FREE (i);
|
|
|
|
}
|
2009-01-21 08:57:24 +00:00
|
|
|
FREE (d->base.notifies_desired);
|
2009-01-28 15:16:11 +00:00
|
|
|
FREE (d->base.changes);
|
2009-01-21 08:57:24 +00:00
|
|
|
FREE (d->callbacks);
|
2009-04-21 14:07:21 +00:00
|
|
|
|
|
|
|
#if HAVE_SMALL_FDS
|
2009-01-21 08:57:24 +00:00
|
|
|
FREE (d->fd_map);
|
2009-04-21 14:07:21 +00:00
|
|
|
#else
|
|
|
|
free_fd_tree_recursive (allocator, d->fd_map_tree);
|
|
|
|
#endif
|
2009-01-21 08:57:24 +00:00
|
|
|
FREE (d);
|
|
|
|
}
|
|
|
|
|
2009-01-25 13:51:30 +00:00
|
|
|
ProtobufCAllocator *
|
|
|
|
protobuf_c_dispatch_peek_allocator (ProtobufCDispatch *dispatch)
|
|
|
|
{
|
|
|
|
RealDispatch *d = (RealDispatch *) dispatch;
|
|
|
|
return d->allocator;
|
|
|
|
}
|
|
|
|
|
2009-01-25 13:59:55 +00:00
|
|
|
/* TODO: perhaps thread-private dispatches make more sense? */
|
2009-01-28 15:16:11 +00:00
|
|
|
static ProtobufCDispatch *def = NULL;
|
2009-01-25 13:59:55 +00:00
|
|
|
ProtobufCDispatch *protobuf_c_dispatch_default (void)
|
|
|
|
{
|
|
|
|
if (def == NULL)
|
2009-01-28 05:29:56 +00:00
|
|
|
def = protobuf_c_dispatch_new (&protobuf_c_default_allocator);
|
2009-01-25 13:59:55 +00:00
|
|
|
return def;
|
|
|
|
}
|
|
|
|
|
2009-04-21 14:07:21 +00:00
|
|
|
#if HAVE_SMALL_FDS
|
2009-01-21 08:57:24 +00:00
|
|
|
static void
|
|
|
|
enlarge_fd_map (RealDispatch *d,
|
|
|
|
unsigned fd)
|
|
|
|
{
|
|
|
|
size_t new_size = d->fd_map_size * 2;
|
|
|
|
FDMap *new_map;
|
2009-01-22 15:27:27 +00:00
|
|
|
ProtobufCAllocator *allocator = d->allocator;
|
2009-01-21 08:57:24 +00:00
|
|
|
while (fd >= new_size)
|
|
|
|
new_size *= 2;
|
|
|
|
new_map = ALLOC (sizeof (FDMap) * new_size);
|
|
|
|
memcpy (new_map, d->fd_map, d->fd_map_size * sizeof (FDMap));
|
|
|
|
memset (new_map + d->fd_map_size,
|
|
|
|
255,
|
|
|
|
sizeof (FDMap) * (new_size - d->fd_map_size));
|
|
|
|
FREE (d->fd_map);
|
|
|
|
d->fd_map = new_map;
|
|
|
|
d->fd_map_size = new_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
ensure_fd_map_big_enough (RealDispatch *d,
|
|
|
|
unsigned fd)
|
|
|
|
{
|
|
|
|
if (fd >= d->fd_map_size)
|
|
|
|
enlarge_fd_map (d, fd);
|
|
|
|
}
|
2009-04-21 14:07:21 +00:00
|
|
|
#endif
|
2009-01-21 08:57:24 +00:00
|
|
|
|
|
|
|
static unsigned
|
|
|
|
allocate_notifies_desired_index (RealDispatch *d)
|
|
|
|
{
|
|
|
|
unsigned rv = d->base.n_notifies_desired++;
|
2009-01-22 15:27:27 +00:00
|
|
|
ProtobufCAllocator *allocator = d->allocator;
|
2009-01-21 08:57:24 +00:00
|
|
|
if (rv == d->notifies_desired_alloced)
|
|
|
|
{
|
|
|
|
unsigned new_size = d->notifies_desired_alloced * 2;
|
|
|
|
ProtobufC_FDNotify *n = ALLOC (new_size * sizeof (ProtobufC_FDNotify));
|
2010-01-24 17:09:48 +00:00
|
|
|
Callback *c = ALLOC (new_size * sizeof (Callback));
|
2009-01-21 08:57:24 +00:00
|
|
|
memcpy (n, d->base.notifies_desired, d->notifies_desired_alloced * sizeof (ProtobufC_FDNotify));
|
|
|
|
FREE (d->base.notifies_desired);
|
2010-01-24 17:09:48 +00:00
|
|
|
memcpy (c, d->callbacks, d->notifies_desired_alloced * sizeof (Callback));
|
|
|
|
FREE (d->callbacks);
|
2009-01-21 08:57:24 +00:00
|
|
|
d->base.notifies_desired = n;
|
2010-01-24 17:09:48 +00:00
|
|
|
d->callbacks = c;
|
2009-01-21 08:57:24 +00:00
|
|
|
d->notifies_desired_alloced = new_size;
|
|
|
|
}
|
2009-01-28 15:16:11 +00:00
|
|
|
#if DEBUG_DISPATCH_INTERNALS
|
|
|
|
fprintf (stderr, "allocate_notifies_desired_index: returning %u\n", rv);
|
|
|
|
#endif
|
2009-01-21 08:57:24 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
static unsigned
|
|
|
|
allocate_change_index (RealDispatch *d)
|
|
|
|
{
|
|
|
|
unsigned rv = d->base.n_changes++;
|
|
|
|
if (rv == d->changes_alloced)
|
|
|
|
{
|
2009-01-22 15:27:27 +00:00
|
|
|
ProtobufCAllocator *allocator = d->allocator;
|
2009-01-21 08:57:24 +00:00
|
|
|
unsigned new_size = d->changes_alloced * 2;
|
2009-01-29 15:41:30 +00:00
|
|
|
ProtobufC_FDNotifyChange *n = ALLOC (new_size * sizeof (ProtobufC_FDNotifyChange));
|
|
|
|
memcpy (n, d->base.changes, d->changes_alloced * sizeof (ProtobufC_FDNotifyChange));
|
2009-01-21 08:57:24 +00:00
|
|
|
FREE (d->base.changes);
|
|
|
|
d->base.changes = n;
|
|
|
|
d->changes_alloced = new_size;
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
2009-04-21 14:07:21 +00:00
|
|
|
|
|
|
|
static inline FDMap *
|
|
|
|
get_fd_map (RealDispatch *d, ProtobufC_FD fd)
|
|
|
|
{
|
|
|
|
#if HAVE_SMALL_FDS
|
|
|
|
if ((unsigned)fd >= d->fd_map_size)
|
|
|
|
return NULL;
|
|
|
|
else
|
|
|
|
return d->fd_map + fd;
|
|
|
|
#else
|
|
|
|
FDMapNode *node;
|
|
|
|
GSK_RBTREE_LOOKUP_COMPARATOR (GET_FD_MAP_TREE (d), fd, COMPARE_FD_TO_FD_MAP_NODE, node);
|
|
|
|
return node ? &node->map : NULL;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
static inline FDMap *
|
|
|
|
force_fd_map (RealDispatch *d, ProtobufC_FD fd)
|
|
|
|
{
|
|
|
|
#if HAVE_SMALL_FDS
|
|
|
|
ensure_fd_map_big_enough (d, fd);
|
|
|
|
return d->fd_map + fd;
|
|
|
|
#else
|
|
|
|
{
|
|
|
|
FDMap *fm = get_fd_map (d, fd);
|
|
|
|
ProtobufCAllocator *allocator = d->allocator;
|
|
|
|
if (fm == NULL)
|
|
|
|
{
|
|
|
|
FDMapNode *node = ALLOC (sizeof (FDMapNode));
|
|
|
|
FDMapNode *conflict;
|
|
|
|
node->fd = fd;
|
|
|
|
memset (&node->map, 255, sizeof (FDMap));
|
|
|
|
GSK_RBTREE_INSERT (GET_FD_MAP_TREE (d), node, conflict);
|
|
|
|
assert (conflict == NULL);
|
|
|
|
fm = &node->map;
|
|
|
|
}
|
|
|
|
return fm;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-01-21 08:57:24 +00:00
|
|
|
static void
|
|
|
|
deallocate_change_index (RealDispatch *d,
|
2009-04-21 14:07:21 +00:00
|
|
|
FDMap *fm)
|
2009-01-21 08:57:24 +00:00
|
|
|
{
|
2009-04-21 14:07:21 +00:00
|
|
|
unsigned ch_ind = fm->change_index;
|
2009-01-22 15:27:27 +00:00
|
|
|
unsigned from = d->base.n_changes - 1;
|
2009-04-21 14:07:21 +00:00
|
|
|
ProtobufC_FD from_fd;
|
2010-05-26 01:25:38 +00:00
|
|
|
fm->change_index = -1;
|
2009-01-21 08:57:24 +00:00
|
|
|
if (ch_ind == from)
|
|
|
|
{
|
2009-01-22 15:27:27 +00:00
|
|
|
d->base.n_changes--;
|
2009-01-21 08:57:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
from_fd = d->base.changes[ch_ind].fd;
|
2009-04-21 14:07:21 +00:00
|
|
|
get_fd_map (d, from_fd)->change_index = ch_ind;
|
2009-01-22 15:27:27 +00:00
|
|
|
d->base.changes[ch_ind] = d->base.changes[from];
|
|
|
|
d->base.n_changes--;
|
2009-01-21 08:57:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
deallocate_notify_desired_index (RealDispatch *d,
|
2009-04-21 14:07:21 +00:00
|
|
|
ProtobufC_FD fd,
|
|
|
|
FDMap *fm)
|
2009-01-21 08:57:24 +00:00
|
|
|
{
|
2009-04-21 14:07:21 +00:00
|
|
|
unsigned nd_ind = fm->notify_desired_index;
|
2009-01-22 15:27:27 +00:00
|
|
|
unsigned from = d->base.n_notifies_desired - 1;
|
2009-04-21 14:07:21 +00:00
|
|
|
ProtobufC_FD from_fd;
|
|
|
|
(void) fd;
|
2009-01-28 15:16:11 +00:00
|
|
|
#if DEBUG_DISPATCH_INTERNALS
|
|
|
|
fprintf (stderr, "deallocate_notify_desired_index: fd=%d, nd_ind=%u\n",fd,nd_ind);
|
|
|
|
#endif
|
2009-04-21 14:07:21 +00:00
|
|
|
fm->notify_desired_index = -1;
|
2009-01-21 08:57:24 +00:00
|
|
|
if (nd_ind == from)
|
|
|
|
{
|
2009-01-22 15:27:27 +00:00
|
|
|
d->base.n_notifies_desired--;
|
2009-01-21 08:57:24 +00:00
|
|
|
return;
|
|
|
|
}
|
2009-01-28 15:16:11 +00:00
|
|
|
from_fd = d->base.notifies_desired[from].fd;
|
2009-04-21 14:07:21 +00:00
|
|
|
get_fd_map (d, from_fd)->notify_desired_index = nd_ind;
|
2009-01-22 15:27:27 +00:00
|
|
|
d->base.notifies_desired[nd_ind] = d->base.notifies_desired[from];
|
2010-02-05 00:06:43 +00:00
|
|
|
d->callbacks[nd_ind] = d->callbacks[from];
|
2009-01-22 15:27:27 +00:00
|
|
|
d->base.n_notifies_desired--;
|
2009-01-21 08:57:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Registering file-descriptors to watch. */
|
|
|
|
void
|
2009-01-21 16:35:53 +00:00
|
|
|
protobuf_c_dispatch_watch_fd (ProtobufCDispatch *dispatch,
|
2010-12-18 23:51:37 +00:00
|
|
|
ProtobufC_FD fd,
|
2009-01-21 08:57:24 +00:00
|
|
|
unsigned events,
|
2009-01-21 16:35:53 +00:00
|
|
|
ProtobufCDispatchCallback callback,
|
2009-01-21 08:57:24 +00:00
|
|
|
void *callback_data)
|
|
|
|
{
|
|
|
|
RealDispatch *d = (RealDispatch *) dispatch;
|
|
|
|
unsigned f = fd; /* avoid tiring compiler warnings: "comparison of signed versus unsigned" */
|
2009-01-22 15:27:27 +00:00
|
|
|
unsigned nd_ind, change_ind;
|
2009-01-29 15:41:30 +00:00
|
|
|
unsigned old_events;
|
2009-04-21 14:07:21 +00:00
|
|
|
FDMap *fm;
|
2009-01-28 15:16:11 +00:00
|
|
|
#if DEBUG_DISPATCH
|
|
|
|
fprintf (stderr, "dispatch: watch_fd: %d, %s%s\n",
|
|
|
|
fd,
|
|
|
|
(events&PROTOBUF_C_EVENT_READABLE)?"r":"",
|
|
|
|
(events&PROTOBUF_C_EVENT_WRITABLE)?"w":"");
|
|
|
|
#endif
|
2009-01-21 08:57:24 +00:00
|
|
|
if (callback == NULL)
|
|
|
|
assert (events == 0);
|
|
|
|
else
|
|
|
|
assert (events != 0);
|
2009-04-21 14:07:21 +00:00
|
|
|
fm = force_fd_map (d, f);
|
|
|
|
|
|
|
|
/* XXX: should we set fm->map.closed_since_notify_started=0 ??? */
|
|
|
|
|
|
|
|
if (fm->notify_desired_index == -1)
|
2009-01-21 08:57:24 +00:00
|
|
|
{
|
2009-01-28 15:16:11 +00:00
|
|
|
if (callback != NULL)
|
2009-04-21 14:07:21 +00:00
|
|
|
nd_ind = fm->notify_desired_index = allocate_notifies_desired_index (d);
|
2009-01-29 15:41:30 +00:00
|
|
|
old_events = 0;
|
2009-01-21 08:57:24 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-04-21 14:07:21 +00:00
|
|
|
old_events = dispatch->notifies_desired[fm->notify_desired_index].events;
|
2009-01-21 08:57:24 +00:00
|
|
|
if (callback == NULL)
|
2009-04-21 14:07:21 +00:00
|
|
|
deallocate_notify_desired_index (d, fd, fm);
|
2009-01-28 15:16:11 +00:00
|
|
|
else
|
2009-04-21 14:07:21 +00:00
|
|
|
nd_ind = fm->notify_desired_index;
|
2009-01-21 08:57:24 +00:00
|
|
|
}
|
|
|
|
if (callback == NULL)
|
|
|
|
{
|
2009-04-21 14:07:21 +00:00
|
|
|
if (fm->change_index == -1)
|
2009-01-29 15:41:30 +00:00
|
|
|
{
|
2009-04-21 14:07:21 +00:00
|
|
|
change_ind = fm->change_index = allocate_change_index (d);
|
2009-01-29 15:41:30 +00:00
|
|
|
dispatch->changes[change_ind].old_events = old_events;
|
|
|
|
}
|
|
|
|
else
|
2009-04-21 14:07:21 +00:00
|
|
|
change_ind = fm->change_index;
|
2009-01-21 08:57:24 +00:00
|
|
|
d->base.changes[change_ind].fd = f;
|
|
|
|
d->base.changes[change_ind].events = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
assert (callback != NULL && events != 0);
|
2009-04-21 14:07:21 +00:00
|
|
|
if (fm->change_index == -1)
|
2009-01-29 15:41:30 +00:00
|
|
|
{
|
2009-04-21 14:07:21 +00:00
|
|
|
change_ind = fm->change_index = allocate_change_index (d);
|
2009-01-29 15:41:30 +00:00
|
|
|
dispatch->changes[change_ind].old_events = old_events;
|
|
|
|
}
|
|
|
|
else
|
2009-04-21 14:07:21 +00:00
|
|
|
change_ind = fm->change_index;
|
2009-01-21 08:57:24 +00:00
|
|
|
|
2009-01-22 15:27:27 +00:00
|
|
|
d->base.changes[change_ind].fd = fd;
|
|
|
|
d->base.changes[change_ind].events = events;
|
2009-01-21 08:57:24 +00:00
|
|
|
d->base.notifies_desired[nd_ind].fd = fd;
|
|
|
|
d->base.notifies_desired[nd_ind].events = events;
|
|
|
|
d->callbacks[nd_ind].func = callback;
|
|
|
|
d->callbacks[nd_ind].data = callback_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-01-21 16:35:53 +00:00
|
|
|
protobuf_c_dispatch_close_fd (ProtobufCDispatch *dispatch,
|
2010-12-18 23:51:37 +00:00
|
|
|
ProtobufC_FD fd)
|
2009-01-21 08:57:24 +00:00
|
|
|
{
|
|
|
|
protobuf_c_dispatch_fd_closed (dispatch, fd);
|
|
|
|
close (fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-01-21 16:35:53 +00:00
|
|
|
protobuf_c_dispatch_fd_closed(ProtobufCDispatch *dispatch,
|
2009-04-21 14:07:21 +00:00
|
|
|
ProtobufC_FD fd)
|
2009-01-21 08:57:24 +00:00
|
|
|
{
|
|
|
|
RealDispatch *d = (RealDispatch *) dispatch;
|
2009-04-21 14:07:21 +00:00
|
|
|
FDMap *fm;
|
2009-01-28 15:16:11 +00:00
|
|
|
#if DEBUG_DISPATCH
|
|
|
|
fprintf (stderr, "dispatch: fd %d closed\n", fd);
|
|
|
|
#endif
|
2009-04-21 14:07:21 +00:00
|
|
|
fm = force_fd_map (d, fd);
|
|
|
|
fm->closed_since_notify_started = 1;
|
|
|
|
if (fm->change_index != -1)
|
|
|
|
deallocate_change_index (d, fm);
|
|
|
|
if (fm->notify_desired_index != -1)
|
|
|
|
deallocate_notify_desired_index (d, fd, fm);
|
2009-01-21 08:57:24 +00:00
|
|
|
}
|
|
|
|
|
2009-01-21 16:35:53 +00:00
|
|
|
static void
|
|
|
|
free_timer (ProtobufCDispatchTimer *timer)
|
|
|
|
{
|
|
|
|
RealDispatch *d = timer->dispatch;
|
|
|
|
timer->right = d->recycled_timeouts;
|
|
|
|
d->recycled_timeouts = timer;
|
|
|
|
}
|
|
|
|
|
2009-01-21 08:57:24 +00:00
|
|
|
void
|
2009-01-21 16:35:53 +00:00
|
|
|
protobuf_c_dispatch_dispatch (ProtobufCDispatch *dispatch,
|
2009-01-21 08:57:24 +00:00
|
|
|
size_t n_notifies,
|
|
|
|
ProtobufC_FDNotify *notifies)
|
|
|
|
{
|
|
|
|
RealDispatch *d = (RealDispatch *) dispatch;
|
|
|
|
unsigned fd_max;
|
|
|
|
unsigned i;
|
2009-01-22 15:27:27 +00:00
|
|
|
struct timeval tv;
|
2011-03-16 03:31:09 +00:00
|
|
|
|
|
|
|
/* Re-entrancy guard. If this is triggerred, then
|
|
|
|
you are calling protobuf_c_dispatch_dispatch (or _run)
|
|
|
|
from a callback function. That's not allowed. */
|
|
|
|
protobuf_c_assert (!d->is_dispatching);
|
|
|
|
d->is_dispatching = 1;
|
|
|
|
|
2011-12-09 03:18:09 +00:00
|
|
|
gettimeofday (&tv, NULL);
|
|
|
|
dispatch->last_dispatch_secs = tv.tv_sec;
|
|
|
|
dispatch->last_dispatch_usecs = tv.tv_usec;
|
|
|
|
|
2009-01-21 08:57:24 +00:00
|
|
|
fd_max = 0;
|
|
|
|
for (i = 0; i < n_notifies; i++)
|
|
|
|
if (fd_max < (unsigned) notifies[i].fd)
|
|
|
|
fd_max = notifies[i].fd;
|
|
|
|
ensure_fd_map_big_enough (d, fd_max);
|
|
|
|
for (i = 0; i < n_notifies; i++)
|
2011-11-02 02:25:28 +00:00
|
|
|
d->fd_map[notifies[i].fd].closed_since_notify_started = 0;
|
2009-01-21 08:57:24 +00:00
|
|
|
for (i = 0; i < n_notifies; i++)
|
|
|
|
{
|
|
|
|
unsigned fd = notifies[i].fd;
|
2011-11-02 02:25:28 +00:00
|
|
|
if (!d->fd_map[fd].closed_since_notify_started
|
|
|
|
&& d->fd_map[fd].notify_desired_index != -1)
|
2009-01-21 08:57:24 +00:00
|
|
|
{
|
2011-11-02 02:25:28 +00:00
|
|
|
unsigned nd_ind = d->fd_map[fd].notify_desired_index;
|
2009-01-21 08:57:24 +00:00
|
|
|
unsigned events = d->base.notifies_desired[nd_ind].events & notifies[i].events;
|
|
|
|
if (events != 0)
|
|
|
|
d->callbacks[nd_ind].func (fd, events, d->callbacks[nd_ind].data);
|
|
|
|
}
|
|
|
|
}
|
2009-01-21 16:35:53 +00:00
|
|
|
|
2009-01-29 15:41:30 +00:00
|
|
|
/* clear changes */
|
|
|
|
for (i = 0; i < dispatch->n_changes; i++)
|
|
|
|
d->fd_map[dispatch->changes[i].fd].change_index = -1;
|
|
|
|
dispatch->n_changes = 0;
|
|
|
|
|
2009-01-28 15:16:11 +00:00
|
|
|
/* handle idle functions */
|
|
|
|
while (d->first_idle != NULL)
|
|
|
|
{
|
|
|
|
ProtobufCDispatchIdle *idle = d->first_idle;
|
|
|
|
ProtobufCDispatchIdleFunc func = idle->func;
|
|
|
|
void *data = idle->func_data;
|
|
|
|
GSK_LIST_REMOVE_FIRST (GET_IDLE_LIST (d));
|
|
|
|
|
|
|
|
idle->func = NULL; /* set to NULL to render remove_idle a no-op */
|
|
|
|
func (dispatch, data);
|
|
|
|
|
|
|
|
idle->next = d->recycled_idles;
|
|
|
|
d->recycled_idles = idle;
|
|
|
|
}
|
2011-03-10 19:00:08 +00:00
|
|
|
dispatch->has_idle = 0;
|
2009-01-21 16:35:53 +00:00
|
|
|
|
|
|
|
/* handle timers */
|
|
|
|
while (d->timer_tree != NULL)
|
|
|
|
{
|
|
|
|
ProtobufCDispatchTimer *min_timer;
|
|
|
|
GSK_RBTREE_FIRST (GET_TIMER_TREE (d), min_timer);
|
2009-01-22 15:27:27 +00:00
|
|
|
if (min_timer->timeout_secs < (unsigned long) tv.tv_sec
|
|
|
|
|| (min_timer->timeout_secs == (unsigned long) tv.tv_sec
|
|
|
|
&& min_timer->timeout_usecs <= (unsigned) tv.tv_usec))
|
2009-01-21 16:35:53 +00:00
|
|
|
{
|
|
|
|
ProtobufCDispatchTimerFunc func = min_timer->func;
|
|
|
|
void *func_data = min_timer->func_data;
|
|
|
|
GSK_RBTREE_REMOVE (GET_TIMER_TREE (d), min_timer);
|
|
|
|
/* Set to NULL as a way to tell protobuf_c_dispatch_remove_timer()
|
|
|
|
that we are in the middle of notifying */
|
|
|
|
min_timer->func = NULL;
|
|
|
|
min_timer->func_data = NULL;
|
|
|
|
func (&d->base, func_data);
|
|
|
|
free_timer (min_timer);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
d->base.has_timeout = 1;
|
|
|
|
d->base.timeout_secs = min_timer->timeout_secs;
|
|
|
|
d->base.timeout_usecs = min_timer->timeout_usecs;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (d->timer_tree == NULL)
|
|
|
|
d->base.has_timeout = 0;
|
2011-03-16 03:31:09 +00:00
|
|
|
|
|
|
|
/* Finish reentrance guard. */
|
|
|
|
d->is_dispatching = 0;
|
2009-01-21 08:57:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-01-21 16:35:53 +00:00
|
|
|
protobuf_c_dispatch_clear_changes (ProtobufCDispatch *dispatch)
|
2009-01-21 08:57:24 +00:00
|
|
|
{
|
|
|
|
RealDispatch *d = (RealDispatch *) dispatch;
|
|
|
|
unsigned i;
|
|
|
|
for (i = 0; i < dispatch->n_changes; i++)
|
|
|
|
{
|
2009-04-21 14:07:21 +00:00
|
|
|
FDMap *fm = get_fd_map (d, dispatch->changes[i].fd);
|
|
|
|
assert (fm->change_index == (int) i);
|
|
|
|
fm->change_index = -1;
|
2009-01-21 08:57:24 +00:00
|
|
|
}
|
|
|
|
dispatch->n_changes = 0;
|
|
|
|
}
|
|
|
|
|
2009-01-22 15:27:27 +00:00
|
|
|
static inline unsigned
|
|
|
|
events_to_pollfd_events (unsigned ev)
|
|
|
|
{
|
|
|
|
return ((ev & PROTOBUF_C_EVENT_READABLE) ? POLLIN : 0)
|
|
|
|
| ((ev & PROTOBUF_C_EVENT_WRITABLE) ? POLLOUT : 0)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
static inline unsigned
|
|
|
|
pollfd_events_to_events (unsigned ev)
|
|
|
|
{
|
2011-11-02 02:25:28 +00:00
|
|
|
return ((ev & (POLLIN|POLLHUP)) ? PROTOBUF_C_EVENT_READABLE : 0)
|
2009-01-22 15:27:27 +00:00
|
|
|
| ((ev & POLLOUT) ? PROTOBUF_C_EVENT_WRITABLE : 0)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2009-01-21 08:57:24 +00:00
|
|
|
void
|
2009-01-21 16:35:53 +00:00
|
|
|
protobuf_c_dispatch_run (ProtobufCDispatch *dispatch)
|
2009-01-21 08:57:24 +00:00
|
|
|
{
|
|
|
|
struct pollfd *fds;
|
2009-01-22 15:27:27 +00:00
|
|
|
void *to_free = NULL, *to_free2 = NULL;
|
2009-01-21 08:57:24 +00:00
|
|
|
size_t n_events;
|
2009-01-22 15:27:27 +00:00
|
|
|
RealDispatch *d = (RealDispatch *) dispatch;
|
|
|
|
ProtobufCAllocator *allocator = d->allocator;
|
|
|
|
unsigned i;
|
|
|
|
int timeout;
|
|
|
|
ProtobufC_FDNotify *events;
|
2009-01-21 08:57:24 +00:00
|
|
|
if (dispatch->n_notifies_desired < 128)
|
|
|
|
fds = alloca (sizeof (struct pollfd) * dispatch->n_notifies_desired);
|
|
|
|
else
|
|
|
|
to_free = fds = ALLOC (sizeof (struct pollfd) * dispatch->n_notifies_desired);
|
|
|
|
for (i = 0; i < dispatch->n_notifies_desired; i++)
|
|
|
|
{
|
|
|
|
fds[i].fd = dispatch->notifies_desired[i].fd;
|
|
|
|
fds[i].events = events_to_pollfd_events (dispatch->notifies_desired[i].events);
|
|
|
|
fds[i].revents = 0;
|
|
|
|
}
|
|
|
|
|
2009-01-22 15:27:27 +00:00
|
|
|
/* compute timeout */
|
2011-03-10 19:00:08 +00:00
|
|
|
if (dispatch->has_idle)
|
|
|
|
timeout = 0;
|
|
|
|
else if (!dispatch->has_timeout)
|
2009-01-22 15:27:27 +00:00
|
|
|
timeout = -1;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday (&tv, NULL);
|
2011-03-10 19:00:08 +00:00
|
|
|
if (dispatch->timeout_secs < (unsigned long) tv.tv_sec
|
|
|
|
|| (dispatch->timeout_secs == (unsigned long) tv.tv_sec
|
|
|
|
&& dispatch->timeout_usecs <= (unsigned) tv.tv_usec))
|
2009-01-22 15:27:27 +00:00
|
|
|
timeout = 0;
|
|
|
|
else
|
|
|
|
{
|
2011-03-10 19:00:08 +00:00
|
|
|
int du = dispatch->timeout_usecs - tv.tv_usec;
|
|
|
|
int ds = dispatch->timeout_secs - tv.tv_sec;
|
2009-01-22 15:27:27 +00:00
|
|
|
if (du < 0)
|
|
|
|
{
|
|
|
|
du += 1000000;
|
|
|
|
ds -= 1;
|
|
|
|
}
|
|
|
|
if (ds > INT_MAX / 1000)
|
|
|
|
timeout = INT_MAX / 1000 * 1000;
|
|
|
|
else
|
|
|
|
/* Round up, so that we ensure that something can run
|
|
|
|
if they just wait the full duration */
|
|
|
|
timeout = ds * 1000 + (du + 999) / 1000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-21 08:57:24 +00:00
|
|
|
if (poll (fds, dispatch->n_notifies_desired, timeout) < 0)
|
|
|
|
{
|
|
|
|
if (errno == EINTR)
|
2009-01-22 15:27:27 +00:00
|
|
|
return; /* probably a signal interrupted the poll-- let the user have control */
|
2009-01-21 08:57:24 +00:00
|
|
|
|
|
|
|
/* i don't really know what would plausibly cause this */
|
|
|
|
fprintf (stderr, "error polling: %s\n", strerror (errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
n_events = 0;
|
|
|
|
for (i = 0; i < dispatch->n_notifies_desired; i++)
|
|
|
|
if (fds[i].revents)
|
|
|
|
n_events++;
|
|
|
|
if (n_events < 128)
|
2009-01-22 15:27:27 +00:00
|
|
|
events = alloca (sizeof (ProtobufC_FDNotify) * n_events);
|
2009-01-21 08:57:24 +00:00
|
|
|
else
|
2009-01-22 15:27:27 +00:00
|
|
|
to_free2 = events = ALLOC (sizeof (ProtobufC_FDNotify) * n_events);
|
2009-01-21 08:57:24 +00:00
|
|
|
n_events = 0;
|
|
|
|
for (i = 0; i < dispatch->n_notifies_desired; i++)
|
|
|
|
if (fds[i].revents)
|
|
|
|
{
|
|
|
|
events[n_events].fd = fds[i].fd;
|
|
|
|
events[n_events].events = pollfd_events_to_events (fds[i].revents);
|
|
|
|
|
|
|
|
/* note that we may actually wind up with fewer events
|
|
|
|
now that we actually call pollfd_events_to_events() */
|
|
|
|
if (events[n_events].events != 0)
|
|
|
|
n_events++;
|
|
|
|
}
|
|
|
|
protobuf_c_dispatch_dispatch (dispatch, n_events, events);
|
|
|
|
if (to_free)
|
|
|
|
FREE (to_free);
|
|
|
|
if (to_free2)
|
|
|
|
FREE (to_free2);
|
|
|
|
}
|
2009-01-21 16:35:53 +00:00
|
|
|
|
|
|
|
ProtobufCDispatchTimer *
|
|
|
|
protobuf_c_dispatch_add_timer(ProtobufCDispatch *dispatch,
|
|
|
|
unsigned timeout_secs,
|
|
|
|
unsigned timeout_usecs,
|
|
|
|
ProtobufCDispatchTimerFunc func,
|
|
|
|
void *func_data)
|
|
|
|
{
|
|
|
|
RealDispatch *d = (RealDispatch *) dispatch;
|
2009-01-22 15:27:27 +00:00
|
|
|
ProtobufCDispatchTimer *rv;
|
2009-01-28 15:16:11 +00:00
|
|
|
ProtobufCDispatchTimer *at;
|
2009-01-22 15:27:27 +00:00
|
|
|
ProtobufCDispatchTimer *conflict;
|
2009-01-21 16:35:53 +00:00
|
|
|
protobuf_c_assert (func != NULL);
|
|
|
|
if (d->recycled_timeouts != NULL)
|
|
|
|
{
|
|
|
|
rv = d->recycled_timeouts;
|
|
|
|
d->recycled_timeouts = rv->right;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rv = d->allocator->alloc (d->allocator, sizeof (ProtobufCDispatchTimer));
|
|
|
|
}
|
|
|
|
rv->timeout_secs = timeout_secs;
|
|
|
|
rv->timeout_usecs = timeout_usecs;
|
|
|
|
rv->func = func;
|
|
|
|
rv->func_data = func_data;
|
|
|
|
rv->dispatch = d;
|
|
|
|
GSK_RBTREE_INSERT (GET_TIMER_TREE (d), rv, conflict);
|
2009-01-28 15:16:11 +00:00
|
|
|
|
|
|
|
/* is this the first element in the tree */
|
|
|
|
for (at = rv; at != NULL; at = at->parent)
|
|
|
|
if (at->parent && at->parent->right == at)
|
|
|
|
break;
|
|
|
|
if (at == NULL) /* yes, so set the public members */
|
|
|
|
{
|
|
|
|
dispatch->has_timeout = 1;
|
|
|
|
dispatch->timeout_secs = rv->timeout_secs;
|
|
|
|
dispatch->timeout_usecs = rv->timeout_usecs;
|
|
|
|
}
|
2009-01-21 16:35:53 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2009-01-22 15:27:27 +00:00
|
|
|
ProtobufCDispatchTimer *
|
|
|
|
protobuf_c_dispatch_add_timer_millis
|
|
|
|
(ProtobufCDispatch *dispatch,
|
|
|
|
unsigned millis,
|
|
|
|
ProtobufCDispatchTimerFunc func,
|
|
|
|
void *func_data)
|
|
|
|
{
|
|
|
|
unsigned tsec = dispatch->last_dispatch_secs;
|
|
|
|
unsigned tusec = dispatch->last_dispatch_usecs;
|
|
|
|
tusec += 1000 * (millis % 1000);
|
|
|
|
tsec += millis / 1000;
|
|
|
|
if (tusec >= 1000*1000)
|
|
|
|
{
|
|
|
|
tusec -= 1000*1000;
|
|
|
|
tsec += 1;
|
|
|
|
}
|
|
|
|
return protobuf_c_dispatch_add_timer (dispatch, tsec, tusec, func, func_data);
|
|
|
|
}
|
|
|
|
|
2009-01-21 16:35:53 +00:00
|
|
|
void protobuf_c_dispatch_remove_timer (ProtobufCDispatchTimer *timer)
|
|
|
|
{
|
|
|
|
protobuf_c_boolean may_be_first;
|
|
|
|
RealDispatch *d = timer->dispatch;
|
|
|
|
|
|
|
|
/* ignore mid-notify removal */
|
|
|
|
if (timer->func == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
may_be_first = d->base.timeout_usecs == timer->timeout_usecs
|
|
|
|
&& d->base.timeout_secs == timer->timeout_secs;
|
|
|
|
|
|
|
|
GSK_RBTREE_REMOVE (GET_TIMER_TREE (d), timer);
|
|
|
|
|
|
|
|
if (may_be_first)
|
|
|
|
{
|
|
|
|
if (d->timer_tree == NULL)
|
|
|
|
d->base.has_timeout = 0;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ProtobufCDispatchTimer *min;
|
|
|
|
GSK_RBTREE_FIRST (GET_TIMER_TREE (d), min);
|
2009-01-22 15:27:27 +00:00
|
|
|
d->base.timeout_secs = min->timeout_secs;
|
|
|
|
d->base.timeout_usecs = min->timeout_usecs;
|
2009-01-21 16:35:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-01-25 13:51:30 +00:00
|
|
|
ProtobufCDispatchIdle *
|
|
|
|
protobuf_c_dispatch_add_idle (ProtobufCDispatch *dispatch,
|
|
|
|
ProtobufCDispatchIdleFunc func,
|
|
|
|
void *func_data)
|
|
|
|
{
|
|
|
|
RealDispatch *d = (RealDispatch *) dispatch;
|
|
|
|
ProtobufCDispatchIdle *rv;
|
|
|
|
if (d->recycled_idles != NULL)
|
|
|
|
{
|
|
|
|
rv = d->recycled_idles;
|
|
|
|
d->recycled_idles = rv->next;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ProtobufCAllocator *allocator = d->allocator;
|
|
|
|
rv = ALLOC (sizeof (ProtobufCDispatchIdle));
|
|
|
|
}
|
|
|
|
GSK_LIST_APPEND (GET_IDLE_LIST (d), rv);
|
|
|
|
rv->func = func;
|
|
|
|
rv->func_data = func_data;
|
|
|
|
rv->dispatch = d;
|
2011-03-10 19:00:08 +00:00
|
|
|
dispatch->has_idle = 1;
|
2009-01-25 13:51:30 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
protobuf_c_dispatch_remove_idle (ProtobufCDispatchIdle *idle)
|
|
|
|
{
|
2009-01-28 15:16:11 +00:00
|
|
|
if (idle->func != NULL)
|
|
|
|
{
|
|
|
|
RealDispatch *d = idle->dispatch;
|
|
|
|
GSK_LIST_REMOVE (GET_IDLE_LIST (d), idle);
|
|
|
|
idle->next = d->recycled_idles;
|
|
|
|
d->recycled_idles = idle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void protobuf_c_dispatch_destroy_default (void)
|
|
|
|
{
|
|
|
|
if (def)
|
|
|
|
{
|
2009-01-29 15:41:30 +00:00
|
|
|
ProtobufCDispatch *to_kill = def;
|
2009-01-28 15:16:11 +00:00
|
|
|
def = NULL;
|
2009-01-29 15:41:30 +00:00
|
|
|
protobuf_c_dispatch_free (to_kill);
|
2009-01-28 15:16:11 +00:00
|
|
|
}
|
2009-01-25 13:51:30 +00:00
|
|
|
}
|