[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[GNUnet-SVN] r16523 - gnunet-gtk/src/lib
From: |
gnunet |
Subject: |
[GNUnet-SVN] r16523 - gnunet-gtk/src/lib |
Date: |
Mon, 15 Aug 2011 14:30:49 +0200 |
Author: grothoff
Date: 2011-08-15 14:30:49 +0200 (Mon, 15 Aug 2011)
New Revision: 16523
Modified:
gnunet-gtk/src/lib/eventloop.c
Log:
some code cleanup
Modified: gnunet-gtk/src/lib/eventloop.c
===================================================================
--- gnunet-gtk/src/lib/eventloop.c 2011-08-15 12:23:08 UTC (rev 16522)
+++ gnunet-gtk/src/lib/eventloop.c 2011-08-15 12:30:49 UTC (rev 16523)
@@ -26,6 +26,14 @@
#include "gnunet_gtk.h"
+/**
+ * Initial size of our poll array cache.
+ *
+ * TODO: get some statistics, find the maximum number of fds ever
+ * polled during normal gnunet-gtk operation, and set this to that number.
+ */
+#define INITIAL_POLL_ARRAY_SIZE 30
+
/**
* Main context for our event loop.
*/
@@ -92,7 +100,6 @@
*/
gint max_priority;
-
#if WINDOWS
/**
* Array to hold pipe handles during a select() call
@@ -206,6 +213,21 @@
}
+/**
+ * Change the size of the cached poll array to the given value.
+ *
+ * @param ml main loop context with the cached poll array
+ * @param new_size desired size of the cached poll array
+ */
+static void
+resize_cached_poll_array (struct GNUNET_GTK_MainLoop *ml,
+ guint new_size)
+{
+ ml->cached_poll_array = g_new (GPollFD, new_size);
+ ml->cached_poll_array_size = new_size;
+}
+
+
#ifndef FD_COPY
#define FD_COPY(s, d) (memcpy ((d), (s), sizeof (fd_set)))
#endif
@@ -234,11 +256,9 @@
struct GNUNET_GTK_MainLoop *ml = cls;
int max_nfds;
gint poll_result;
- GPollFD *gfds;
gint delay;
guint i;
guint fd_counter = 0;
- guint allocated_nfds;
guint need_gfds;
fd_set aread;
fd_set awrite;
@@ -292,24 +312,17 @@
if (efds != NULL)
max_nfds = GNUNET_MAX (max_nfds, efds->nsds);
- allocated_nfds = ml->cached_poll_array_size;
- gfds = ml->cached_poll_array;
- if (allocated_nfds == 0)
- {
- /* TODO: get some statistics, find the maximum number of fds ever
- * polled during normal gnunet-gtk operation, and set this to that number.
- */
- ml->cached_poll_array = gfds = g_new (GPollFD, 30);
- ml->cached_poll_array_size = allocated_nfds = 30;
- }
+ ml->cached_poll_array_size = ml->cached_poll_array_size;
+ if (ml->cached_poll_array_size == 0)
+ resize_cached_poll_array (ml, INITIAL_POLL_ARRAY_SIZE);
while (1)
- {
- fd_counter = 0;
+ {
+ fd_counter = 0;
#if !WINDOWS
- gboolean need_realloc = FALSE;
- for (i = 0; !need_realloc && i < max_nfds; i += 1)
- {
+ gboolean need_realloc = FALSE;
+ for (i = 0; !need_realloc && i < max_nfds; i += 1)
+ {
int isset[3];
isset[0] = (rfds == NULL) ? 0 : FD_ISSET (i, &rfds->sds);
@@ -317,23 +330,22 @@
isset[2] = (efds == NULL) ? 0 : FD_ISSET (i, &efds->sds);
if (!isset[0] && !isset[1] && !isset[2])
continue;
- if (fd_counter >= allocated_nfds)
+ if (fd_counter >= ml->cached_poll_array_size)
{
need_realloc = TRUE;
break;
}
- gfds[fd_counter].fd = i;
- gfds[fd_counter].events = (isset[0] ? G_IO_IN | G_IO_HUP | G_IO_ERR : 0)
+ ml->cached_poll_array[fd_counter].fd = i;
+ ml->cached_poll_array[fd_counter].events = (isset[0] ? G_IO_IN |
G_IO_HUP | G_IO_ERR : 0)
| (isset[1] ? G_IO_OUT | G_IO_ERR : 0) | (isset[2] ? G_IO_ERR : 0);
fd_counter += 1;
}
if (need_realloc)
- {
- ml->cached_poll_array = gfds = g_renew (GPollFD, gfds,
ml->cached_poll_array_size * 2);
- ml->cached_poll_array_size = allocated_nfds = ml->cached_poll_array_size
* 2;
- fd_counter = 0;
- need_realloc = FALSE;
- }
+ {
+ resize_cached_poll_array (ml, ml->cached_poll_array_size * 2);
+ fd_counter = 0;
+ need_realloc = FALSE;
+ }
else
break;
#else
@@ -343,13 +355,13 @@
+ (rfds == NULL ? 0 : GNUNET_CONTAINER_slist_count (rfds->handles))
+ (wfds == NULL ? 0 : 1)
+ 1;
- if (need_nfds >= allocated_nfds)
+ if (need_nfds >= ml->cached_poll_array_size)
{
/* Since there are also gmainloop's own fds, just need_nfds won't be
* enough, so make it twice as long.
*/
- ml->cached_poll_array = gfds = g_renew (GPollFD, gfds, need_nfds * 2);
- ml->cached_poll_array_size = allocated_nfds = need_nfds * 2;
+ ml->cached_poll_array = ml->cached_poll_array = g_renew (GPollFD,
ml->cached_poll_array, need_nfds * 2);
+ ml->cached_poll_array_size = ml->cached_poll_array_size = need_nfds * 2;
}
if (ml->read_array_length < GNUNET_CONTAINER_slist_count (rfds->handles))
{
@@ -374,20 +386,20 @@
#if DEBUG_NETWORK
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding the pipe's 0x%x
overlapped event to the array as %d\n", fh->h, nhandles);
#endif
- gfds[fd_counter].fd = (intptr_t) fh->oOverlapRead->hEvent;
+ ml->cached_poll_array[fd_counter].fd = (intptr_t)
fh->oOverlapRead->hEvent;
/* On W32 .events makes no sense - g_poll will just OR its
* contents into .revents when the .fd event fires.
* So we'll use it in the way that suits us the best.
*/
- gfds[fd_counter].events = G_IO_IN;
+ ml->cached_poll_array[fd_counter].events = G_IO_IN;
fd_counter += 1;
ml->read_array[read_handles] = fh;
read_handles += 1;
}
else
{
- gfds[fd_counter].fd = (intptr_t) ml->hEventReadReady;
- gfds[fd_counter].events = G_IO_HUP;
+ ml->cached_poll_array[fd_counter].fd = (intptr_t)
ml->hEventReadReady;
+ ml->cached_poll_array[fd_counter].events = G_IO_HUP;
fd_counter += 1;
ml->read_array[read_handles] = fh;
read_handles += 1;
@@ -398,8 +410,8 @@
#if DEBUG_NETWORK
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding the read ready event
to the array as %d\n", nhandles);
#endif
- gfds[fd_counter].fd = (intptr_t) ml->hEventReadReady;
- gfds[fd_counter].events = G_IO_IN;
+ ml->cached_poll_array[fd_counter].fd = (intptr_t)
ml->hEventReadReady;
+ ml->cached_poll_array[fd_counter].events = G_IO_IN;
fd_counter += 1;
ml->read_array[read_handles] = fh;
read_handles += 1;
@@ -416,8 +428,8 @@
}
if (wfds != NULL && GNUNET_CONTAINER_slist_count (wfds->handles) > 0)
{
- gfds[fd_counter].fd = (intptr_t) ml->hEventPipeWrite;
- gfds[fd_counter].events = G_IO_OUT;
+ ml->cached_poll_array[fd_counter].fd = (intptr_t) ml->hEventPipeWrite;
+ ml->cached_poll_array[fd_counter].events = G_IO_OUT;
always_ready_write_fd = fd_counter;
fd_counter += 1;
}
@@ -449,8 +461,8 @@
#if DEBUG_NETWORK
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding the socket read event to
the array as %d\n", fd_counter);
#endif
- gfds[fd_counter].fd = (intptr_t) ml->hEventRead;
- gfds[fd_counter].events = G_IO_IN;
+ ml->cached_poll_array[fd_counter].fd = (intptr_t) ml->hEventRead;
+ ml->cached_poll_array[fd_counter].events = G_IO_IN;
for (i = 0; i < rfds->sds.fd_count; i++)
WSAEventSelect (rfds->sds.fd_array[i], ml->hEventRead, FD_ACCEPT |
FD_READ | FD_CLOSE);
fd_counter += 1;
@@ -462,8 +474,8 @@
#if DEBUG_NETWORK
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding the socket write event to
the array as %d\n", fd_counter);
#endif
- gfds[fd_counter].fd = (intptr_t) ml->hEventWrite;
- gfds[fd_counter].events = G_IO_OUT;
+ ml->cached_poll_array[fd_counter].fd = (intptr_t) ml->hEventWrite;
+ ml->cached_poll_array[fd_counter].events = G_IO_OUT;
for (i = 0; i < wfds->sds.fd_count; i++)
{
DWORD error;
@@ -487,8 +499,8 @@
#if DEBUG_NETWORK
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding the socket error event to
the array as %d\n", fd_counter);
#endif
- gfds[fd_counter].fd = (intptr_t) ml->hEventException;
- gfds[fd_counter].events = G_IO_ERR;
+ ml->cached_poll_array[fd_counter].fd = (intptr_t) ml->hEventException;
+ ml->cached_poll_array[fd_counter].events = G_IO_ERR;
for (i = 0; i < efds->sds.fd_count; i++)
WSAEventSelect (efds->sds.fd_array[i], ml->hEventException, FD_OOB |
FD_CLOSE);
fd_counter += 1;
@@ -500,12 +512,11 @@
socks = sock_read + sock_write + sock_err;
g_main_context_prepare (ml->gmc, &ml->max_priority);
- while (allocated_nfds < (need_gfds = g_main_context_query (ml->gmc,
- ml->max_priority, &delay, &gfds[fd_counter], allocated_nfds -
fd_counter)))
- {
- ml->cached_poll_array = gfds = g_renew (GPollFD, gfds, allocated_nfds -
fd_counter + need_gfds);
- ml->cached_poll_array_size = allocated_nfds = allocated_nfds - fd_counter
+ need_gfds;
- }
+ while (ml->cached_poll_array_size < (need_gfds = g_main_context_query
(ml->gmc,
+ ml->max_priority, &delay, &ml->cached_poll_array[fd_counter],
ml->cached_poll_array_size - fd_counter)))
+ resize_cached_poll_array (ml,
+ ml->cached_poll_array_size - fd_counter +
need_gfds);
+
ml->poll_array_active = fd_counter + need_gfds;
if (timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
@@ -528,7 +539,7 @@
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "We have %d of our FDs and %d of GMC
ones, going to wait %6dms\n", fd_counter, need_gfds, delay);
#endif
- poll_result = g_poll (gfds, fd_counter + need_gfds, delay);
+ poll_result = g_poll (ml->cached_poll_array, fd_counter + need_gfds, delay);
#if DEBUG_NETWORK
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "g_poll returned : %d\n", poll_result);
@@ -539,7 +550,7 @@
* from within a task (currently we're not in a task, but in a select()
call, remember)
* Startup reason is used to pass the scheduler sanity check.
*/
- if (TRUE == g_main_context_check (ml->gmc, ml->max_priority,
&gfds[fd_counter], need_gfds))
+ if (TRUE == g_main_context_check (ml->gmc, ml->max_priority,
&ml->cached_poll_array[fd_counter], need_gfds))
GNUNET_SCHEDULER_add_continuation (gnunet_gtk_dispatch_task, ml,
GNUNET_SCHEDULER_REASON_STARTUP);
@@ -553,12 +564,12 @@
for (i = 0; i < fd_counter; i++)
{
int set[3];
- if ((set[0] = FD_ISSET (gfds[i].fd, &aread)))
- FD_SET (gfds[i].fd, &rfds->sds);
- if ((set[1] = FD_ISSET (gfds[i].fd, &awrite)))
- FD_SET (gfds[i].fd, &wfds->sds);
- if ((set[2] = FD_ISSET (gfds[i].fd, &aexcept)))
- FD_SET (gfds[i].fd, &efds->sds);
+ if ((set[0] = FD_ISSET (ml->cached_poll_array[i].fd, &aread)))
+ FD_SET (ml->cached_poll_array[i].fd, &rfds->sds);
+ if ((set[1] = FD_ISSET (ml->cached_poll_array[i].fd, &awrite)))
+ FD_SET (ml->cached_poll_array[i].fd, &wfds->sds);
+ if ((set[2] = FD_ISSET (ml->cached_poll_array[i].fd, &aexcept)))
+ FD_SET (ml->cached_poll_array[i].fd, &efds->sds);
if (set[0] || set[1] || set[2])
result += 1;
}
@@ -575,7 +586,7 @@
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "select() returned %d\n", select_ret);
#endif
}
- if (always_ready_write_fd >= 0 && gfds[always_ready_write_fd].revents &
G_IO_OUT)
+ if (always_ready_write_fd >= 0 &&
ml->cached_poll_array[always_ready_write_fd].revents & G_IO_OUT)
{
GNUNET_CONTAINER_slist_append (ml->handles_write, wfds->handles);
result += GNUNET_CONTAINER_slist_count (ml->handles_write);
@@ -587,7 +598,7 @@
{
DWORD error;
BOOL bret;
- if (!(gfds[i].revents & (G_IO_IN | G_IO_HUP | G_IO_ERR)))
+ if (!(ml->cached_poll_array[i].revents & (G_IO_IN | G_IO_HUP | G_IO_ERR)))
continue;
SetLastError (0);
waitstatus = 0;
@@ -596,7 +607,7 @@
#if DEBUG_NETWORK
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peek at read pipe %d (0x%x) returned
%d (%d bytes available) GLE %u\n", i, ml->read_array[i]->h, bret, waitstatus,
error);
#endif
- if (bret == 0 || (gfds[i].revents & G_IO_ERR))
+ if (bret == 0 || (ml->cached_poll_array[i].revents & G_IO_ERR))
{
if (efds != NULL)
{
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [GNUnet-SVN] r16523 - gnunet-gtk/src/lib,
gnunet <=