Ignore files generated in the dependency checks.
[libpri-bristuff.git] / prisched.c
blob92c05535cf2ed7c78ab719ff0ee106e320fd70b1
1 /*
2 * libpri: An implementation of Primary Rate ISDN
4 * Written by Mark Spencer <markster@linux-support.net>
6 * Copyright (C) 2003-2006 Junghanns.NET GmbH
7 * Klaus-Peter Junghanns <kpj@junghanns.net>
8 * Copyright (C) 2001-2005, Digium, Inc.
9 * All Rights Reserved.
13 * See http://www.asterisk.org for more information about
14 * the Asterisk project. Please do not directly contact
15 * any of the maintainers of this project for assistance;
16 * the project provides a web site, mailing lists and IRC
17 * channels for your use.
19 * This program is free software, distributed under the terms of
20 * the GNU General Public License Version 2 as published by the
21 * Free Software Foundation. See the LICENSE file included with
22 * this program for more details.
25 #include "libpri.h"
26 #include "pri_internal.h"
27 #include <stdio.h>
30 static int maxsched = 0;
32 /* Scheduler routines */
33 int pri_schedule_event(struct pri *pri, int ms, void (*function)(void *data), void *data)
35 int x;
36 struct timeval tv;
37 for (x=1;x<MAX_SCHED;x++)
38 if ((!pri->pri_sched[x].callback2) && (!pri->pri_sched[x].callback))
39 break;
40 if (x == MAX_SCHED) {
41 pri_error(pri, "No more room in scheduler\n");
42 return -1;
44 if (x > maxsched)
45 maxsched = x;
46 gettimeofday(&tv, NULL);
47 tv.tv_sec += ms / 1000;
48 tv.tv_usec += (ms % 1000) * 1000;
49 if (tv.tv_usec > 1000000) {
50 tv.tv_usec -= 1000000;
51 tv.tv_sec += 1;
53 pri->pri_sched[x].when = tv;
54 pri->pri_sched[x].callback = function;
55 pri->pri_sched[x].callback2 = NULL;
56 pri->pri_sched[x].data = data;
57 pri->pri_sched[x].hasdata2 = 0;
58 pri->pri_sched[x].data2 = 0;
59 return x;
62 int pri_schedule_event2(struct pri *pri, int ms, void (*function)(void *data, int data2), void *data, int data2)
64 int x;
65 struct timeval tv;
66 for (x=1;x<MAX_SCHED;x++)
67 if ((!pri->pri_sched[x].callback2) && (!pri->pri_sched[x].callback))
68 break;
69 if (x == MAX_SCHED) {
70 pri_error(pri, "No more room in scheduler\n");
71 return -1;
73 if (x > maxsched)
74 maxsched = x;
75 gettimeofday(&tv, NULL);
76 tv.tv_sec += ms / 1000;
77 tv.tv_usec += (ms % 1000) * 1000;
78 if (tv.tv_usec > 1000000) {
79 tv.tv_usec -= 1000000;
80 tv.tv_sec += 1;
82 pri->pri_sched[x].when = tv;
83 pri->pri_sched[x].callback = NULL;
84 pri->pri_sched[x].callback2 = function;
85 pri->pri_sched[x].data = data;
86 pri->pri_sched[x].hasdata2 = 1;
87 pri->pri_sched[x].data2 = data2;
88 return x;
91 struct timeval *pri_schedule_next(struct pri *pri)
93 struct timeval *closest = NULL;
94 int x;
95 /* Check subchannels */
96 if (pri->subchannel)
97 closest = pri_schedule_next(pri->subchannel);
98 for (x=1;x<MAX_SCHED;x++) {
99 if ((pri->pri_sched[x].callback || pri->pri_sched[x].callback2) &&
100 (!closest || (closest->tv_sec > pri->pri_sched[x].when.tv_sec) ||
101 ((closest->tv_sec == pri->pri_sched[x].when.tv_sec) &&
102 (closest->tv_usec > pri->pri_sched[x].when.tv_usec))))
103 closest = &pri->pri_sched[x].when;
105 return closest;
108 static pri_event *__pri_schedule_run(struct pri *pri, struct timeval *tv)
110 int x;
111 void (*callback)(void *);
112 void (*callback2)(void *, int);
113 void *data;
114 int data2;
115 pri_event *e;
117 if (pri->subchannel) {
118 if ((e = __pri_schedule_run(pri->subchannel, tv))) {
119 return e;
122 for (x=1;x<MAX_SCHED;x++) {
123 if ((pri->pri_sched[x].callback || pri->pri_sched[x].callback2) &&
124 ((pri->pri_sched[x].when.tv_sec < tv->tv_sec) ||
125 ((pri->pri_sched[x].when.tv_sec == tv->tv_sec) &&
126 (pri->pri_sched[x].when.tv_usec <= tv->tv_usec)))) {
127 pri->schedev = 0;
128 callback = pri->pri_sched[x].callback;
129 callback2 = pri->pri_sched[x].callback2;
130 data = pri->pri_sched[x].data;
131 data2 = pri->pri_sched[x].data2;
132 pri->pri_sched[x].callback = NULL;
133 pri->pri_sched[x].callback2 = NULL;
134 pri->pri_sched[x].data = NULL;
135 pri->pri_sched[x].data2 = 0;
136 if (pri->pri_sched[x].hasdata2 == 1) {
137 pri->pri_sched[x].hasdata2 = 0;
138 callback2(data, data2);
139 } else {
140 callback(data);
142 if (pri->schedev)
143 return &pri->ev;
146 return NULL;
149 pri_event *pri_schedule_run(struct pri *pri)
151 struct timeval tv;
152 gettimeofday(&tv, NULL);
153 return __pri_schedule_run(pri, &tv);
157 void pri_schedule_del(struct pri *pri,int id)
159 if ((id >= MAX_SCHED) || (id < 0))
160 pri_error(pri, "Asked to delete sched id %d???\n", id);
161 pri->pri_sched[id].callback = NULL;
162 pri->pri_sched[id].callback2 = NULL;