2001-05-13 00:57:53 +08:00
|
|
|
/*
|
|
|
|
* libpri: An implementation of Primary Rate ISDN
|
|
|
|
*
|
2005-06-22 06:47:39 +08:00
|
|
|
* Written by Mark Spencer <markster@digium.com>
|
2001-05-13 00:57:53 +08:00
|
|
|
*
|
2008-08-06 06:18:12 +08:00
|
|
|
* Copyright (C) 2001-2005, Digium, Inc.
|
2001-05-13 00:57:53 +08:00
|
|
|
* All Rights Reserved.
|
2008-08-06 06:18:12 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* See http://www.asterisk.org for more information about
|
|
|
|
* the Asterisk project. Please do not directly contact
|
|
|
|
* any of the maintainers of this project for assistance;
|
|
|
|
* the project provides a web site, mailing lists and IRC
|
|
|
|
* channels for your use.
|
2001-05-13 00:57:53 +08:00
|
|
|
*
|
2008-08-06 06:18:12 +08:00
|
|
|
* This program is free software, distributed under the terms of
|
|
|
|
* the GNU General Public License Version 2 as published by the
|
|
|
|
* Free Software Foundation. See the LICENSE file included with
|
|
|
|
* this program for more details.
|
2001-05-13 00:57:53 +08:00
|
|
|
*
|
2008-08-06 06:18:12 +08:00
|
|
|
* In addition, when this program is distributed with Asterisk in
|
|
|
|
* any form that would qualify as a 'combined work' or as a
|
|
|
|
* 'derivative work' (but not mere aggregation), you can redistribute
|
|
|
|
* and/or modify the combination under the terms of the license
|
|
|
|
* provided with that copy of Asterisk, instead of the license
|
|
|
|
* terms granted here.
|
2001-05-13 00:57:53 +08:00
|
|
|
*/
|
|
|
|
|
2005-06-22 06:47:39 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2001-05-13 00:57:53 +08:00
|
|
|
#include "libpri.h"
|
|
|
|
#include "pri_internal.h"
|
|
|
|
|
|
|
|
|
|
|
|
static int maxsched = 0;
|
|
|
|
|
|
|
|
/* Scheduler routines */
|
|
|
|
int pri_schedule_event(struct pri *pri, int ms, void (*function)(void *data), void *data)
|
|
|
|
{
|
|
|
|
int x;
|
|
|
|
struct timeval tv;
|
2008-05-08 03:51:44 +08:00
|
|
|
/* Scheduling runs on master channels only */
|
|
|
|
while (pri->master)
|
|
|
|
pri = pri->master;
|
2001-05-13 00:57:53 +08:00
|
|
|
for (x=1;x<MAX_SCHED;x++)
|
|
|
|
if (!pri->pri_sched[x].callback)
|
|
|
|
break;
|
|
|
|
if (x == MAX_SCHED) {
|
2005-05-23 23:06:33 +08:00
|
|
|
pri_error(pri, "No more room in scheduler\n");
|
2001-05-13 00:57:53 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (x > maxsched)
|
|
|
|
maxsched = x;
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
tv.tv_sec += ms / 1000;
|
|
|
|
tv.tv_usec += (ms % 1000) * 1000;
|
2001-11-26 10:54:32 +08:00
|
|
|
if (tv.tv_usec > 1000000) {
|
2001-05-13 00:57:53 +08:00
|
|
|
tv.tv_usec -= 1000000;
|
2001-11-26 10:54:32 +08:00
|
|
|
tv.tv_sec += 1;
|
|
|
|
}
|
2001-05-13 00:57:53 +08:00
|
|
|
pri->pri_sched[x].when = tv;
|
|
|
|
pri->pri_sched[x].callback = function;
|
|
|
|
pri->pri_sched[x].data = data;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct timeval *pri_schedule_next(struct pri *pri)
|
|
|
|
{
|
|
|
|
struct timeval *closest = NULL;
|
|
|
|
int x;
|
2004-06-05 14:50:55 +08:00
|
|
|
/* Check subchannels */
|
|
|
|
if (pri->subchannel)
|
|
|
|
closest = pri_schedule_next(pri->subchannel);
|
2001-05-13 00:57:53 +08:00
|
|
|
for (x=1;x<MAX_SCHED;x++) {
|
|
|
|
if (pri->pri_sched[x].callback &&
|
|
|
|
(!closest || (closest->tv_sec > pri->pri_sched[x].when.tv_sec) ||
|
|
|
|
((closest->tv_sec == pri->pri_sched[x].when.tv_sec) &&
|
|
|
|
(closest->tv_usec > pri->pri_sched[x].when.tv_usec))))
|
|
|
|
closest = &pri->pri_sched[x].when;
|
|
|
|
}
|
|
|
|
return closest;
|
|
|
|
}
|
|
|
|
|
2004-06-05 14:50:55 +08:00
|
|
|
static pri_event *__pri_schedule_run(struct pri *pri, struct timeval *tv)
|
2001-05-13 00:57:53 +08:00
|
|
|
{
|
|
|
|
int x;
|
|
|
|
void (*callback)(void *);
|
|
|
|
void *data;
|
2004-06-05 14:50:55 +08:00
|
|
|
pri_event *e;
|
|
|
|
if (pri->subchannel) {
|
|
|
|
if ((e = __pri_schedule_run(pri->subchannel, tv))) {
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
}
|
2001-05-13 00:57:53 +08:00
|
|
|
for (x=1;x<MAX_SCHED;x++) {
|
|
|
|
if (pri->pri_sched[x].callback &&
|
2004-06-05 14:50:55 +08:00
|
|
|
((pri->pri_sched[x].when.tv_sec < tv->tv_sec) ||
|
|
|
|
((pri->pri_sched[x].when.tv_sec == tv->tv_sec) &&
|
|
|
|
(pri->pri_sched[x].when.tv_usec <= tv->tv_usec)))) {
|
|
|
|
pri->schedev = 0;
|
2001-05-13 00:57:53 +08:00
|
|
|
callback = pri->pri_sched[x].callback;
|
|
|
|
data = pri->pri_sched[x].data;
|
|
|
|
pri->pri_sched[x].callback = NULL;
|
|
|
|
pri->pri_sched[x].data = NULL;
|
|
|
|
callback(data);
|
2001-11-26 10:54:32 +08:00
|
|
|
if (pri->schedev)
|
|
|
|
return &pri->ev;
|
2004-06-05 14:50:55 +08:00
|
|
|
}
|
2001-05-13 00:57:53 +08:00
|
|
|
}
|
2001-11-26 10:54:32 +08:00
|
|
|
return NULL;
|
2001-05-13 00:57:53 +08:00
|
|
|
}
|
|
|
|
|
2004-06-05 14:50:55 +08:00
|
|
|
pri_event *pri_schedule_run(struct pri *pri)
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
return __pri_schedule_run(pri, &tv);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-05-13 00:57:53 +08:00
|
|
|
void pri_schedule_del(struct pri *pri,int id)
|
|
|
|
{
|
2008-05-08 03:51:44 +08:00
|
|
|
while (pri->master)
|
|
|
|
pri = pri->master;
|
2001-05-13 00:57:53 +08:00
|
|
|
if ((id >= MAX_SCHED) || (id < 0))
|
2005-05-23 23:06:33 +08:00
|
|
|
pri_error(pri, "Asked to delete sched id %d???\n", id);
|
2001-05-13 00:57:53 +08:00
|
|
|
pri->pri_sched[id].callback = NULL;
|
|
|
|
}
|