2012-06-22 04:05:17 +00:00
|
|
|
/*
|
|
|
|
Serval Distributed Numbering Architecture (DNA)
|
|
|
|
Copyright (C) 2012 Paul Gardner-Stephen
|
|
|
|
|
|
|
|
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
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "serval.h"
|
|
|
|
#include <poll.h>
|
|
|
|
|
|
|
|
#define MAX_WATCHED_FDS 128
|
|
|
|
struct pollfd fds[MAX_WATCHED_FDS];
|
|
|
|
int fdcount=0;
|
2012-07-02 03:49:54 +00:00
|
|
|
struct sched_ent *fd_callbacks[MAX_WATCHED_FDS];
|
|
|
|
struct sched_ent *next_alarm=NULL;
|
2012-07-02 06:34:00 +00:00
|
|
|
struct profile_total poll_stats={NULL,0,"Idle (in poll)",0,0,0};
|
2012-06-25 05:05:55 +00:00
|
|
|
|
2012-07-02 03:49:54 +00:00
|
|
|
void list_alarms(){
|
2012-06-22 06:22:00 +00:00
|
|
|
long long now=overlay_gettime_ms();
|
2012-07-02 03:49:54 +00:00
|
|
|
struct sched_ent *alarm = next_alarm;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
INFO("Alarms;");
|
|
|
|
while(alarm){
|
2012-07-02 05:50:30 +00:00
|
|
|
INFOF("%s in %lldms",
|
|
|
|
(alarm->stats?alarm->stats->name:"Unnamed"),
|
|
|
|
alarm->alarm - now);
|
2012-07-02 03:49:54 +00:00
|
|
|
alarm = alarm->_next;
|
2012-06-22 06:22:00 +00:00
|
|
|
}
|
2012-07-02 03:49:54 +00:00
|
|
|
INFO("File handles;");
|
|
|
|
for (i=0;i<fdcount;i++)
|
2012-07-02 05:50:30 +00:00
|
|
|
INFOF("%s watching #%d",
|
|
|
|
(fd_callbacks[i]->stats?fd_callbacks[i]->stats->name:"Unnamed"),
|
|
|
|
fds[i].fd);
|
2012-06-22 06:22:00 +00:00
|
|
|
}
|
|
|
|
|
2012-07-02 03:49:54 +00:00
|
|
|
// add an alarm to the list of scheduled function calls.
|
|
|
|
// simply populate .alarm with the absolute time, and .function with the method to call.
|
|
|
|
// on calling .poll.revents will be zero.
|
|
|
|
int schedule(struct sched_ent *alarm){
|
|
|
|
struct sched_ent *node = next_alarm, *last = NULL;
|
|
|
|
while(node!=NULL){
|
|
|
|
if (node->alarm > alarm->alarm)
|
2012-06-27 07:24:42 +00:00
|
|
|
break;
|
2012-07-02 03:49:54 +00:00
|
|
|
last = node;
|
|
|
|
node = node->_next;
|
2012-06-25 05:05:55 +00:00
|
|
|
}
|
2012-07-02 03:49:54 +00:00
|
|
|
if (last == NULL){
|
|
|
|
next_alarm = alarm;
|
|
|
|
}else{
|
|
|
|
last->_next=alarm;
|
|
|
|
}
|
|
|
|
alarm->_prev = last;
|
|
|
|
if(node!=NULL)
|
|
|
|
node->_prev = alarm;
|
|
|
|
alarm->_next = node;
|
|
|
|
|
2012-06-22 04:05:17 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-07-02 03:49:54 +00:00
|
|
|
// remove a function from the schedule before it has fired
|
|
|
|
// safe to unschedule twice...
|
|
|
|
int unschedule(struct sched_ent *alarm){
|
|
|
|
struct sched_ent *prev = alarm->_prev;
|
|
|
|
struct sched_ent *next = alarm->_next;
|
2012-07-02 05:50:30 +00:00
|
|
|
|
|
|
|
if (prev)
|
2012-07-02 03:49:54 +00:00
|
|
|
prev->_next = next;
|
|
|
|
else if(next_alarm==alarm)
|
|
|
|
next_alarm = next;
|
2012-07-02 05:50:30 +00:00
|
|
|
|
|
|
|
if (next)
|
2012-07-02 03:49:54 +00:00
|
|
|
next->_prev = prev;
|
2012-07-02 05:50:30 +00:00
|
|
|
|
2012-07-02 03:49:54 +00:00
|
|
|
alarm->_prev = NULL;
|
|
|
|
alarm->_next = NULL;
|
2012-06-22 04:05:17 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-07-02 03:49:54 +00:00
|
|
|
// start watching a file handle, call this function again if you wish to change the event mask
|
|
|
|
int watch(struct sched_ent *alarm){
|
|
|
|
if (alarm->_poll_index>=0 && fd_callbacks[alarm->_poll_index]==alarm){
|
|
|
|
// updating event flags
|
2012-07-02 05:50:30 +00:00
|
|
|
INFOF("Updating watch %s, #%d for %d", (alarm->stats?alarm->stats->name:"Unnamed"), alarm->poll.fd, alarm->poll.events);
|
2012-07-02 03:49:54 +00:00
|
|
|
}else{
|
2012-07-02 05:50:30 +00:00
|
|
|
INFOF("Adding watch %s, #%d for %d", (alarm->stats?alarm->stats->name:"Unnamed"), alarm->poll.fd, alarm->poll.events);
|
2012-07-02 03:49:54 +00:00
|
|
|
if (fdcount>=MAX_WATCHED_FDS)
|
|
|
|
return WHY("Too many file handles to watch");
|
|
|
|
fd_callbacks[fdcount]=alarm;
|
|
|
|
alarm->_poll_index=fdcount;
|
|
|
|
fdcount++;
|
|
|
|
}
|
|
|
|
fds[alarm->_poll_index]=alarm->poll;
|
|
|
|
return 0;
|
|
|
|
}
|
2012-06-22 04:05:17 +00:00
|
|
|
|
2012-07-02 03:49:54 +00:00
|
|
|
// stop watching a file handle
|
|
|
|
int unwatch(struct sched_ent *alarm){
|
|
|
|
int index = alarm->_poll_index;
|
|
|
|
if (index <0 || fds[index].fd!=alarm->poll.fd)
|
|
|
|
return WHY("Attempted to unwatch a handle that is not being watched");
|
|
|
|
|
|
|
|
fdcount--;
|
|
|
|
if (index!=fdcount){
|
|
|
|
// squash fds
|
|
|
|
fds[index] = fds[fdcount];
|
|
|
|
fd_callbacks[index] = fd_callbacks[fdcount];
|
|
|
|
fd_callbacks[index]->_poll_index=index;
|
2012-06-22 04:05:17 +00:00
|
|
|
}
|
2012-07-02 03:49:54 +00:00
|
|
|
fds[fdcount].fd=-1;
|
|
|
|
fd_callbacks[fdcount]=NULL;
|
|
|
|
alarm->_poll_index=-1;
|
2012-07-02 05:50:30 +00:00
|
|
|
INFOF("%s stopped watching #%d for %d", (alarm->stats?alarm->stats->name:"Unnamed"), alarm->poll.fd, alarm->poll.events);
|
2012-07-02 03:49:54 +00:00
|
|
|
return 0;
|
2012-06-22 04:05:17 +00:00
|
|
|
}
|
|
|
|
|
2012-07-02 03:49:54 +00:00
|
|
|
void call_alarm(struct sched_ent *alarm, int revents){
|
|
|
|
struct call_stats call_stats;
|
2012-07-02 06:34:00 +00:00
|
|
|
struct profile_total *stats = alarm->stats;
|
2012-07-02 05:50:30 +00:00
|
|
|
|
|
|
|
if (stats)
|
|
|
|
fd_func_enter(&call_stats);
|
2012-07-02 03:49:54 +00:00
|
|
|
|
|
|
|
alarm->poll.revents = revents;
|
|
|
|
alarm->function(alarm);
|
|
|
|
|
2012-07-02 05:50:30 +00:00
|
|
|
if (stats)
|
|
|
|
fd_func_exit(&call_stats, stats);
|
2012-06-25 05:05:55 +00:00
|
|
|
}
|
|
|
|
|
2012-06-22 04:05:17 +00:00
|
|
|
int fd_checkalarms()
|
|
|
|
{
|
|
|
|
long long now=overlay_gettime_ms();
|
2012-07-02 03:49:54 +00:00
|
|
|
if (next_alarm!=NULL&&next_alarm->alarm <=now){
|
|
|
|
struct sched_ent *alarm = next_alarm;
|
|
|
|
unschedule(alarm);
|
|
|
|
call_alarm(alarm, 0);
|
|
|
|
now=overlay_gettime_ms();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (next_alarm)
|
|
|
|
return next_alarm->alarm - now;
|
|
|
|
return 15000;
|
2012-06-22 04:05:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int fd_poll()
|
|
|
|
{
|
2012-07-02 03:49:54 +00:00
|
|
|
int i, r;
|
|
|
|
|
2012-06-22 04:05:17 +00:00
|
|
|
/* See if any alarms have expired before we do anything.
|
|
|
|
This also returns the time to the next alarm that is due. */
|
|
|
|
int ms=fd_checkalarms();
|
|
|
|
/* Make sure we don't have any silly timeouts that will make us wait for ever. */
|
2012-07-02 03:49:54 +00:00
|
|
|
if (ms<0) ms=0;
|
2012-06-25 05:05:55 +00:00
|
|
|
|
|
|
|
/* Wait for action or timeout */
|
2012-07-02 03:49:54 +00:00
|
|
|
{
|
|
|
|
struct call_stats call_stats;
|
|
|
|
fd_func_enter(&call_stats);
|
|
|
|
r=poll(fds, fdcount, ms);
|
|
|
|
fd_func_exit(&call_stats, &poll_stats);
|
|
|
|
}
|
2012-06-22 04:05:17 +00:00
|
|
|
|
|
|
|
/* If file descriptors are ready, then call the appropriate functions */
|
|
|
|
if (r>0) {
|
|
|
|
for(i=0;i<fdcount;i++)
|
|
|
|
if (fds[i].revents) {
|
2012-06-27 07:24:42 +00:00
|
|
|
/* Make socket non-blocking */
|
|
|
|
SET_NONBLOCKING(fds[i].fd);
|
2012-07-02 03:49:54 +00:00
|
|
|
call_alarm(fd_callbacks[i], fds[i].revents);
|
2012-06-27 07:24:42 +00:00
|
|
|
|
|
|
|
/* Make socket blocking again */
|
|
|
|
SET_BLOCKING(fds[i].fd);
|
2012-06-22 04:05:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|