2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
2011-12-31 03:39:20 +01:00
|
|
|
gbx_watch.c
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-08-03 17:38:01 +02:00
|
|
|
(c) 2000-2013 Benoît Minisini <gambas@users.sourceforge.net>
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-12-31 03:39:20 +01:00
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
any later version.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-12-31 03:39:20 +01:00
|
|
|
This program 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 General Public License for more details.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-12-31 03:39:20 +01:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
MA 02110-1301, USA.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#define __GBX_WATCH_C
|
|
|
|
|
|
|
|
#include "gb_common.h"
|
|
|
|
#include "gb_error.h"
|
|
|
|
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "gb_array.h"
|
|
|
|
#include "gbx_exec.h"
|
|
|
|
#include "gbx_event.h"
|
|
|
|
#include "gbx_date.h"
|
|
|
|
#include "gbx_c_timer.h"
|
|
|
|
#include "gbx_watch.h"
|
|
|
|
|
2010-08-25 11:18:33 +02:00
|
|
|
//#define DEBUG_TIMER 1
|
|
|
|
//#define DEBUG_WATCH 1
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
static fd_set read_fd;
|
|
|
|
static fd_set write_fd;
|
|
|
|
|
|
|
|
static WATCH_CALLBACK *watch_callback = NULL;
|
2010-08-28 17:00:39 +02:00
|
|
|
//static short *watch_index = NULL;
|
2010-08-25 13:23:19 +02:00
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
static int max_fd = 0;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
static WATCH_TIMER *_timers = NULL;
|
|
|
|
|
|
|
|
static int _do_not_really_delete_callback = 0;
|
|
|
|
static bool _must_delete_callback = FALSE;
|
|
|
|
|
2015-01-02 03:22:22 +01:00
|
|
|
static struct timeval _last_time = { 0 };
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
#ifdef DEBUG_TIMER
|
|
|
|
double _debug_time;
|
|
|
|
static double time_to_double(const struct timeval *t)
|
|
|
|
{
|
|
|
|
return (double)t->tv_sec + t->tv_usec / 1E6 - _debug_time;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void time_add(struct timeval *t1, const struct timeval *t2)
|
|
|
|
{
|
|
|
|
t1->tv_sec += t2->tv_sec;
|
|
|
|
t1->tv_usec += t2->tv_usec;
|
|
|
|
if (t1->tv_usec > 1000000)
|
|
|
|
{
|
|
|
|
t1->tv_usec -= 1000000;
|
|
|
|
t1->tv_sec++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void time_sub(struct timeval *t1, const struct timeval *t2)
|
|
|
|
{
|
|
|
|
t1->tv_sec -= t2->tv_sec;
|
|
|
|
t1->tv_usec -= t2->tv_usec;
|
|
|
|
if (t1->tv_usec < 0)
|
|
|
|
{
|
|
|
|
t1->tv_usec += 1000000;
|
|
|
|
t1->tv_sec--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define time_comp(t1, t2, op) ((t1)->tv_sec op (t2)->tv_sec || ((t1)->tv_sec == (t2)->tv_sec && (t1)->tv_usec op (t2)->tv_usec))
|
|
|
|
#define time_lower_than(t1, t2) time_comp(t1, t2, <)
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
static void time_from_ms(struct timeval *t, int ms)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
t->tv_sec = ms / 1000;
|
|
|
|
t->tv_usec = (ms % 1000) * 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct timeval *time_now(void)
|
|
|
|
{
|
|
|
|
static struct timeval current;
|
|
|
|
|
2009-10-12 12:38:35 +02:00
|
|
|
gettimeofday(¤t, NULL);
|
|
|
|
if (current.tv_usec < 0 || current.tv_usec >= 1000000)
|
2015-01-02 03:22:22 +01:00
|
|
|
fprintf(stderr, "gbx3: warning: gettimeofday: tv_usec = %ld!\n", current.tv_usec);
|
2009-10-12 12:38:35 +02:00
|
|
|
return ¤t;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void add_timer(GB_TIMER *timer, const struct timeval *timeout)
|
|
|
|
{
|
2009-10-12 12:38:35 +02:00
|
|
|
int i;
|
|
|
|
WATCH_TIMER *wt;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_count(_timers); i++)
|
|
|
|
{
|
|
|
|
//if (_timers[i].timeout > timeout)
|
|
|
|
if (time_lower_than(timeout, &_timers[i].timeout))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
wt = ARRAY_insert(&_timers, i);
|
|
|
|
wt->timer = timer;
|
|
|
|
wt->timeout = *timeout;
|
|
|
|
|
|
|
|
#ifdef DEBUG_TIMER
|
|
|
|
fprintf(stderr, "add_timer: %p at %i: %g\n", timer, i, time_to_double(timeout));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2010-08-27 23:09:34 +02:00
|
|
|
static int find_timer(GB_TIMER *timer)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-10-12 12:38:35 +02:00
|
|
|
int i;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_count(_timers); i++)
|
|
|
|
{
|
|
|
|
if (_timers[i].timer == timer)
|
2010-08-27 23:09:34 +02:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void remove_timer(GB_TIMER *timer)
|
|
|
|
{
|
|
|
|
int i = find_timer(timer);
|
|
|
|
|
|
|
|
if (i >= 0)
|
|
|
|
{
|
|
|
|
ARRAY_remove(&_timers, i)
|
|
|
|
#ifdef DEBUG_TIMER
|
|
|
|
fprintf(stderr, "remove_timer: %p at %i\n", timer, i);
|
|
|
|
#endif
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-27 23:09:34 +02:00
|
|
|
double WATCH_get_timeout(GB_TIMER *timer)
|
|
|
|
{
|
|
|
|
int i = find_timer(timer);
|
|
|
|
|
|
|
|
if (i < 0)
|
|
|
|
return 0.0;
|
|
|
|
else
|
|
|
|
return DATE_to_double(&_timers[i].timeout, TRUE);
|
|
|
|
}
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
static void raise_timers()
|
|
|
|
{
|
|
|
|
struct timeval timeout;
|
|
|
|
struct timeval delay;
|
|
|
|
struct timeval *now;
|
|
|
|
GB_TIMER *timer;
|
|
|
|
|
|
|
|
now = time_now();
|
|
|
|
|
|
|
|
while (ARRAY_count(_timers))
|
|
|
|
{
|
|
|
|
timeout = _timers[0].timeout;
|
|
|
|
if (time_lower_than(now, &timeout))
|
|
|
|
return;
|
|
|
|
|
|
|
|
timer = _timers[0].timer;
|
|
|
|
|
|
|
|
#ifdef DEBUG_TIMER
|
|
|
|
fprintf(stderr, "raise_timers: %p has been triggered! now = %.7f timeout = %.7f\n", timer, time_to_double(now), time_to_double(&timeout));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
time_from_ms(&delay, timer->delay);
|
|
|
|
time_add(&timeout, &delay);
|
|
|
|
|
|
|
|
if (time_lower_than(&timeout, now))
|
|
|
|
{
|
|
|
|
timeout = *now;
|
|
|
|
time_add(&timeout, &delay);
|
|
|
|
}
|
|
|
|
|
|
|
|
remove_timer(timer);
|
|
|
|
add_timer(timer, &timeout);
|
|
|
|
|
|
|
|
CTIMER_raise(timer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool get_timeout(const struct timeval *wait, struct timeval *tv)
|
|
|
|
{
|
|
|
|
struct timeval timeout;
|
|
|
|
struct timeval *now;
|
|
|
|
|
|
|
|
if (ARRAY_count(_timers) == 0 && !wait)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
now = time_now();
|
|
|
|
|
|
|
|
#ifdef DEBUG_TIMER
|
|
|
|
fprintf(stderr, "get_timeout: now = %.7g timeout = %.7g\n", time_to_double(now), time_to_double(&_timers[0].timeout));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (ARRAY_count(_timers) == 0)
|
|
|
|
timeout = *wait;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
timeout = _timers[0].timeout;
|
|
|
|
if (wait && time_lower_than(wait, &timeout))
|
|
|
|
timeout = *wait;
|
|
|
|
}
|
|
|
|
|
|
|
|
time_sub(&timeout, now);
|
|
|
|
|
|
|
|
// HZ = 100 on Linux. If a timer must be triggered in less than 10 ms, then
|
|
|
|
// we do a busy wait instead of calling the system.
|
|
|
|
|
|
|
|
if (timeout.tv_sec == 0 && timeout.tv_usec < (1000000 / 100))
|
|
|
|
{
|
2010-08-25 11:18:33 +02:00
|
|
|
#ifdef DEBUG_TIMER
|
|
|
|
fprintf(stderr, "busy loop\n");
|
|
|
|
#endif
|
2007-12-30 17:41:49 +01:00
|
|
|
// busy loop!
|
|
|
|
time_add(&timeout, now);
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
now = time_now();
|
|
|
|
if (!time_lower_than(now, &timeout))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
timeout.tv_sec = 0;
|
|
|
|
timeout.tv_usec = 0;
|
|
|
|
}
|
|
|
|
else if (timeout.tv_sec < 0)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_TIMER
|
|
|
|
fprintf(stderr, "timeout < 0: %ld %ld\n", timeout.tv_sec, timeout.tv_usec);
|
|
|
|
#endif
|
|
|
|
timeout.tv_sec = 0;
|
|
|
|
timeout.tv_usec = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
*tv = timeout;
|
|
|
|
|
|
|
|
#ifdef DEBUG_TIMER
|
|
|
|
fprintf(stderr, "timeout: %.7f %ld %ld\n", timeout.tv_sec + timeout.tv_usec / 1E6, tv->tv_sec, tv->tv_usec);
|
|
|
|
#endif
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void WATCH_init(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-10-12 12:38:35 +02:00
|
|
|
FD_ZERO(&read_fd);
|
|
|
|
FD_ZERO(&write_fd);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-10-12 12:38:35 +02:00
|
|
|
ARRAY_create(&watch_callback);
|
2010-08-28 17:00:39 +02:00
|
|
|
//ARRAY_create(&watch_index);
|
2009-10-12 12:38:35 +02:00
|
|
|
ARRAY_create(&_timers);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
#ifdef DEBUG_TIMER
|
|
|
|
DATE_timer(&_debug_time, FALSE);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void WATCH_exit(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-10-12 12:38:35 +02:00
|
|
|
ARRAY_delete(&watch_callback);
|
2010-08-28 17:00:39 +02:00
|
|
|
//ARRAY_delete(&watch_index);
|
2009-10-12 12:38:35 +02:00
|
|
|
ARRAY_delete(&_timers);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-07 01:31:48 +02:00
|
|
|
static void watch_fd(int fd, int flag, bool watch)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-10-12 12:38:35 +02:00
|
|
|
#if DEBUG_WATCH
|
2014-12-01 03:50:18 +01:00
|
|
|
fprintf(stderr, "watch_fd: %d for %s: %s\n", fd, flag == GB_WATCH_READ ? "read" : "write", watch ? "set" : "clear");
|
2009-10-12 12:38:35 +02:00
|
|
|
#endif
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2014-04-07 00:09:08 +02:00
|
|
|
if (flag == GB_WATCH_READ)
|
2009-06-07 01:31:48 +02:00
|
|
|
{
|
|
|
|
if (watch)
|
|
|
|
FD_SET(fd, &read_fd);
|
|
|
|
else
|
|
|
|
FD_CLR(fd, &read_fd);
|
|
|
|
}
|
2014-04-07 00:09:08 +02:00
|
|
|
else if (flag == GB_WATCH_WRITE)
|
2009-06-07 01:31:48 +02:00
|
|
|
{
|
|
|
|
if (watch)
|
|
|
|
FD_SET(fd, &write_fd);
|
|
|
|
else
|
|
|
|
FD_CLR(fd, &write_fd);
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
static int watch_find_callback(int fd)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-08-28 17:00:39 +02:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_count(watch_callback); i++)
|
|
|
|
{
|
|
|
|
if (watch_callback[i].fd == fd)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
static int find_max_fd(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-10-12 12:38:35 +02:00
|
|
|
int i;
|
|
|
|
int max = -1;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-10-12 12:38:35 +02:00
|
|
|
for (i = 0; i < ARRAY_count(watch_callback); i++)
|
|
|
|
{
|
|
|
|
if (watch_callback[i].fd > max)
|
|
|
|
max = watch_callback[i].fd;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-10-12 12:38:35 +02:00
|
|
|
return max;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2010-08-28 17:00:39 +02:00
|
|
|
/*static void ensure_watch_index(int fd)
|
2010-08-25 13:23:19 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int count = ARRAY_count(watch_index);
|
|
|
|
|
|
|
|
//fprintf(stderr, "ensure_watch_index: %d (%d)\n", fd, count);
|
|
|
|
|
|
|
|
if (fd < count)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ARRAY_add_many(&watch_index, fd - count + 1);
|
|
|
|
for (i = count; i <= fd; i++)
|
|
|
|
watch_index[i] = -1;
|
2010-08-28 17:00:39 +02:00
|
|
|
}*/
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
static WATCH_CALLBACK *watch_create_callback(int fd)
|
|
|
|
{
|
2009-10-12 12:38:35 +02:00
|
|
|
int pos;
|
|
|
|
WATCH_CALLBACK *wcb;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-10-12 12:38:35 +02:00
|
|
|
#if DEBUG_WATCH
|
|
|
|
fprintf(stderr, "watch_create_callback: %d\n", fd);
|
|
|
|
#endif
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-10-12 12:38:35 +02:00
|
|
|
pos = watch_find_callback(fd);
|
|
|
|
if (pos < 0)
|
|
|
|
{
|
2010-08-25 13:23:19 +02:00
|
|
|
pos = ARRAY_count(watch_callback);
|
2009-10-12 12:38:35 +02:00
|
|
|
wcb = ARRAY_add_void(&watch_callback);
|
|
|
|
wcb->fd = fd;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
wcb = &watch_callback[pos];
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
if (fd > max_fd)
|
|
|
|
max_fd = fd;
|
|
|
|
|
2010-08-25 11:18:33 +02:00
|
|
|
#if DEBUG_WATCH
|
|
|
|
fprintf(stderr, "watch_create_callback: %d -> %d read = %p (%p) write = %p (%p)\n", fd, pos, wcb->callback_read, (void *)wcb->param_read, wcb->callback_write, (void *)wcb->param_write);
|
|
|
|
#endif
|
|
|
|
|
2010-08-28 17:00:39 +02:00
|
|
|
//ensure_watch_index(fd);
|
|
|
|
//watch_index[fd] = pos;
|
2010-08-25 13:23:19 +02:00
|
|
|
|
2009-10-12 12:38:35 +02:00
|
|
|
return wcb;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void watch_delete_callback(int fd)
|
|
|
|
{
|
2010-08-25 11:18:33 +02:00
|
|
|
WATCH_CALLBACK *wcb;
|
2007-12-30 17:41:49 +01:00
|
|
|
int pos;
|
|
|
|
|
2010-08-25 11:18:33 +02:00
|
|
|
#if DEBUG_WATCH
|
|
|
|
fprintf(stderr, "watch_delete_callback: %d (%d)\n", fd, _do_not_really_delete_callback);
|
|
|
|
#endif
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
pos = watch_find_callback(fd);
|
|
|
|
if (pos < 0)
|
|
|
|
return;
|
2014-12-01 03:50:18 +01:00
|
|
|
|
2010-08-28 17:00:39 +02:00
|
|
|
//watch_index[fd] = -1;
|
2014-12-01 03:50:18 +01:00
|
|
|
|
2010-08-25 11:18:33 +02:00
|
|
|
wcb = &watch_callback[pos];
|
|
|
|
wcb->fd = -1;
|
2014-04-07 00:09:08 +02:00
|
|
|
watch_fd(fd, GB_WATCH_READ, FALSE);
|
|
|
|
watch_fd(fd, GB_WATCH_WRITE, FALSE);
|
2007-12-30 17:41:49 +01:00
|
|
|
max_fd = find_max_fd();
|
|
|
|
|
|
|
|
if (_do_not_really_delete_callback)
|
|
|
|
{
|
2014-12-01 03:50:18 +01:00
|
|
|
#if DEBUG_WATCH
|
|
|
|
fprintf(stderr, "--> do not really delete\n");
|
|
|
|
#endif
|
2007-12-30 17:41:49 +01:00
|
|
|
_must_delete_callback = TRUE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ARRAY_remove(&watch_callback, pos);
|
2014-12-01 03:50:18 +01:00
|
|
|
|
|
|
|
#if DEBUG_WATCH
|
|
|
|
fprintf(stderr, "--> deleted\n");
|
|
|
|
#endif
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void WATCH_watch(int fd, int type, void *callback, intptr_t param)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-10-12 12:38:35 +02:00
|
|
|
WATCH_CALLBACK *wcb;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
if (fd < 0 || fd > FD_SETSIZE)
|
|
|
|
{
|
2014-04-07 00:09:08 +02:00
|
|
|
if (type != GB_WATCH_NONE)
|
2012-08-29 02:43:48 +02:00
|
|
|
ERROR_warning("trying to watch fd #%d", fd);
|
2007-12-30 17:41:49 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-07 00:09:08 +02:00
|
|
|
if (type == GB_WATCH_NONE)
|
2009-10-12 12:38:35 +02:00
|
|
|
watch_delete_callback(fd);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wcb = watch_create_callback(fd);
|
2014-04-07 00:09:08 +02:00
|
|
|
if (type == GB_WATCH_READ)
|
2009-06-07 01:31:48 +02:00
|
|
|
{
|
|
|
|
wcb->callback_read = callback;
|
|
|
|
wcb->param_read = param;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wcb->callback_write = callback;
|
|
|
|
wcb->param_write = param;
|
|
|
|
}
|
2010-08-25 11:18:33 +02:00
|
|
|
|
|
|
|
#if DEBUG_WATCH
|
|
|
|
fprintf(stderr, "add watch: %d -> %d read = %p (%p) write = %p (%p)\n", fd, watch_find_callback(fd), wcb->callback_read, (void *)wcb->param_read, wcb->callback_write, (void *)wcb->param_write);
|
|
|
|
#endif
|
|
|
|
|
2009-06-07 01:31:48 +02:00
|
|
|
if (!wcb->callback_read && !wcb->callback_write)
|
|
|
|
watch_delete_callback(fd);
|
|
|
|
else
|
|
|
|
{
|
2014-04-07 00:09:08 +02:00
|
|
|
watch_fd(fd, GB_WATCH_READ, wcb->callback_read != NULL);
|
|
|
|
watch_fd(fd, GB_WATCH_WRITE, wcb->callback_write != NULL);
|
2009-06-07 01:31:48 +02:00
|
|
|
}
|
2009-10-12 12:38:35 +02:00
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void raise_callback(fd_set *rfd, fd_set *wfd)
|
|
|
|
{
|
2009-10-12 12:38:35 +02:00
|
|
|
int i;
|
|
|
|
WATCH_CALLBACK wcb;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
_do_not_really_delete_callback++;
|
|
|
|
|
2009-10-12 12:38:35 +02:00
|
|
|
#if DEBUG_WATCH
|
2010-08-25 11:18:33 +02:00
|
|
|
fprintf(stderr, "raise_callback: max_fd = %d\n", max_fd);
|
2009-10-12 12:38:35 +02:00
|
|
|
#endif
|
|
|
|
for (i = 0; i < ARRAY_count(watch_callback); i++)
|
|
|
|
{
|
|
|
|
// We copy the callback structure, because the watch_callback array can change during the
|
|
|
|
// execution of the callbacks.
|
|
|
|
|
2010-08-25 13:35:34 +02:00
|
|
|
if (watch_callback[i].fd < 0)
|
|
|
|
continue;
|
|
|
|
|
2009-10-12 12:38:35 +02:00
|
|
|
wcb = watch_callback[i];
|
2010-08-25 11:18:33 +02:00
|
|
|
|
|
|
|
#if DEBUG_WATCH
|
|
|
|
fprintf(stderr, "raise_callback: [%d] fd = %d read = %p (%p) write = %p (%p)\n", i, wcb.fd, wcb.callback_read, (void *)wcb.param_read, wcb.callback_write, (void *)wcb.param_write);
|
|
|
|
#endif
|
|
|
|
|
2009-10-12 12:38:35 +02:00
|
|
|
if (FD_ISSET(wcb.fd, rfd))
|
|
|
|
{
|
2014-01-14 13:06:39 +01:00
|
|
|
FD_CLR(wcb.fd, rfd);
|
2009-10-12 12:38:35 +02:00
|
|
|
if (wcb.callback_read)
|
2010-08-25 11:18:33 +02:00
|
|
|
{
|
|
|
|
#ifdef DEBUG_WATCH
|
|
|
|
fprintf(stderr, "call read callback on fd %d\n", wcb.fd);
|
|
|
|
#endif
|
2014-04-07 00:09:08 +02:00
|
|
|
(*(wcb.callback_read))(wcb.fd, GB_WATCH_READ, wcb.param_read);
|
2010-08-25 11:18:33 +02:00
|
|
|
}
|
2009-10-12 12:38:35 +02:00
|
|
|
}
|
|
|
|
|
2016-04-27 02:05:54 +02:00
|
|
|
if (watch_callback[i].fd < 0)
|
|
|
|
continue;
|
|
|
|
|
2009-10-12 12:38:35 +02:00
|
|
|
if (FD_ISSET(wcb.fd, wfd))
|
|
|
|
{
|
2014-01-14 13:06:39 +01:00
|
|
|
FD_CLR(wcb.fd, wfd);
|
2009-10-12 12:38:35 +02:00
|
|
|
if (wcb.callback_write)
|
2010-08-25 11:18:33 +02:00
|
|
|
{
|
|
|
|
#ifdef DEBUG_WATCH
|
|
|
|
fprintf(stderr, "call write callback on fd %d\n", wcb.fd);
|
|
|
|
#endif
|
2014-04-07 00:09:08 +02:00
|
|
|
(*(wcb.callback_write))(wcb.fd, GB_WATCH_WRITE, wcb.param_write);
|
2010-08-25 11:18:33 +02:00
|
|
|
}
|
2009-10-12 12:38:35 +02:00
|
|
|
}
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
_do_not_really_delete_callback--;
|
|
|
|
|
|
|
|
if (!_do_not_really_delete_callback && _must_delete_callback)
|
|
|
|
{
|
2014-12-01 03:50:18 +01:00
|
|
|
#if DEBUG_WATCH
|
|
|
|
fprintf(stderr, "do must delete callback\n");
|
|
|
|
#endif
|
2007-12-30 17:41:49 +01:00
|
|
|
i = 0;
|
|
|
|
while (i < ARRAY_count(watch_callback))
|
|
|
|
{
|
|
|
|
if (watch_callback[i].fd < 0)
|
|
|
|
{
|
|
|
|
ARRAY_remove(&watch_callback, i);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
_must_delete_callback = FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int do_select(fd_set *rfd, fd_set *wfd, struct timeval *timeout)
|
|
|
|
{
|
2009-10-12 12:38:35 +02:00
|
|
|
int fd;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-10-12 12:38:35 +02:00
|
|
|
for (fd = max_fd; fd >= 0; fd--)
|
|
|
|
{
|
|
|
|
if (FD_ISSET(fd, &read_fd) || FD_ISSET(fd, &write_fd))
|
|
|
|
break;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-10-12 12:38:35 +02:00
|
|
|
if (fd < 0 && !timeout)
|
|
|
|
return 0;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-10-12 12:38:35 +02:00
|
|
|
max_fd = fd;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-10-12 12:38:35 +02:00
|
|
|
*rfd = read_fd;
|
|
|
|
*wfd = write_fd;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-10-12 12:38:35 +02:00
|
|
|
return select(max_fd + 1, rfd, wfd, NULL, timeout);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool do_loop(struct timeval *wait)
|
|
|
|
{
|
2009-10-12 12:38:35 +02:00
|
|
|
int ret;
|
|
|
|
struct timeval tv;
|
|
|
|
fd_set rfd, wfd;
|
|
|
|
bool something_done = FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-07-19 14:33:57 +02:00
|
|
|
if (EVENT_check_post())
|
|
|
|
something_done = TRUE;
|
|
|
|
|
2010-08-25 11:18:33 +02:00
|
|
|
#ifdef DEBUG_TIMER
|
|
|
|
fprintf(stderr, "\ndo_loop: now = %.7g: select (%d)\n", time_to_double(time_now()), something_done);
|
|
|
|
#endif
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
if (get_timeout(wait, &tv))
|
|
|
|
ret = do_select(&rfd, &wfd, NULL);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = do_select(&rfd, &wfd, &tv);
|
2015-01-02 04:44:30 +01:00
|
|
|
something_done = ARRAY_count(_timers) > 0;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2011-10-09 04:05:07 +02:00
|
|
|
if (ret < 0 && errno != EINTR)
|
|
|
|
THROW_SYSTEM(errno, NULL);
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
#ifdef DEBUG_TIMER
|
2010-08-25 11:18:33 +02:00
|
|
|
fprintf(stderr, "do_loop: now = %.7g: timers (%d)\n", time_to_double(time_now()), something_done);
|
2007-12-30 17:41:49 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
raise_timers();
|
|
|
|
|
2010-08-25 11:18:33 +02:00
|
|
|
#ifdef DEBUG_TIMER
|
|
|
|
fprintf(stderr, "do_loop: now = %.7g: callbacks\n", time_to_double(time_now()));
|
|
|
|
#endif
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
if (ret > 0)
|
|
|
|
{
|
|
|
|
raise_callback(&rfd, &wfd);
|
|
|
|
something_done = TRUE;
|
|
|
|
}
|
|
|
|
else if (ret < 0)
|
|
|
|
{
|
|
|
|
something_done = TRUE;
|
|
|
|
}
|
|
|
|
|
2010-07-19 14:33:57 +02:00
|
|
|
//if (EVENT_check_post())
|
|
|
|
// something_done = TRUE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
return something_done;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
bool WATCH_one_loop(int wait)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
struct timeval timeout;
|
|
|
|
|
|
|
|
if (wait == 0)
|
|
|
|
return do_loop(NULL);
|
|
|
|
else
|
|
|
|
{
|
2009-10-12 12:38:35 +02:00
|
|
|
time_from_ms(&timeout, wait);
|
2007-12-30 17:41:49 +01:00
|
|
|
return do_loop(&timeout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void WATCH_loop(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-10-12 12:38:35 +02:00
|
|
|
while (do_loop(NULL));
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void WATCH_wait(int wait)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
struct timeval *now;
|
|
|
|
struct timeval timeout;
|
|
|
|
|
2009-10-12 12:38:35 +02:00
|
|
|
if (wait == 0)
|
|
|
|
{
|
|
|
|
timeout.tv_sec = 0;
|
|
|
|
timeout.tv_usec = 0;
|
|
|
|
do_loop(&timeout);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
now = time_now();
|
|
|
|
time_from_ms(&timeout, wait);
|
|
|
|
time_add(&timeout, now);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
2009-10-12 12:38:35 +02:00
|
|
|
now = time_now();
|
2007-12-30 17:41:49 +01:00
|
|
|
if (time_lower_than(&timeout, now))
|
|
|
|
break;
|
|
|
|
do_loop(&timeout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-31 00:49:21 +02:00
|
|
|
int WATCH_process(int fd_end, int fd_output, int timeout)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-10-12 12:38:35 +02:00
|
|
|
fd_set rfd;
|
|
|
|
int ret, fd_max;
|
2012-08-31 00:49:21 +02:00
|
|
|
struct timeval tv;
|
2009-10-12 12:38:35 +02:00
|
|
|
|
|
|
|
fd_max = fd_end > fd_output ? fd_end : fd_output;
|
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
FD_ZERO(&rfd);
|
|
|
|
FD_SET(fd_end, &rfd);
|
|
|
|
if (fd_output >= 0)
|
|
|
|
FD_SET(fd_output, &rfd);
|
|
|
|
|
2012-08-31 00:49:21 +02:00
|
|
|
if (timeout > 0)
|
|
|
|
{
|
|
|
|
tv.tv_sec = timeout / 1000;
|
|
|
|
tv.tv_usec = (timeout % 1000) * 1000;
|
|
|
|
ret = select(fd_max + 1, &rfd, NULL, NULL, &tv);
|
|
|
|
if (ret == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = select(fd_max + 1, &rfd, NULL, NULL, NULL);
|
|
|
|
}
|
2009-10-12 12:38:35 +02:00
|
|
|
|
|
|
|
if (ret > 0)
|
|
|
|
break;
|
|
|
|
if (errno != EINTR)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-08-31 00:49:21 +02:00
|
|
|
ret = timeout > 0 ? WP_TIMEOUT : WP_NOTHING;
|
2012-08-31 00:13:25 +02:00
|
|
|
|
|
|
|
if (FD_ISSET(fd_end, &rfd)) ret += WP_END;
|
|
|
|
if (fd_output >= 0 && FD_ISSET(fd_output, &rfd)) ret += WP_OUTPUT;
|
|
|
|
return ret;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void WATCH_timer(void *t, int on)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
GB_TIMER *timer = (GB_TIMER *)t;
|
|
|
|
struct timeval timeout;
|
|
|
|
|
|
|
|
if (on)
|
|
|
|
{
|
|
|
|
time_from_ms(&timeout, timer->delay);
|
|
|
|
time_add(&timeout, time_now());
|
|
|
|
add_timer(timer, &timeout);
|
2008-01-17 22:39:26 +01:00
|
|
|
timer->id = (intptr_t)timer;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
remove_timer(timer);
|
|
|
|
timer->id = 0;
|
|
|
|
}
|
|
|
|
}
|
2010-08-25 11:18:33 +02:00
|
|
|
|
2015-01-02 04:44:30 +01:00
|
|
|
void WATCH_little_sleep(void)
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
struct timeval *now;
|
|
|
|
|
|
|
|
now = time_now();
|
|
|
|
tv = *now;
|
|
|
|
time_sub(&tv, &_last_time);
|
|
|
|
if (tv.tv_sec == 0 && tv.tv_usec < 10000)
|
|
|
|
usleep(10000 - tv.tv_usec);
|
|
|
|
_last_time = *now;
|
|
|
|
}
|
|
|
|
|