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.
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.
26 #include "pri_internal.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
)
37 for (x
=1;x
<MAX_SCHED
;x
++)
38 if ((!pri
->pri_sched
[x
].callback2
) && (!pri
->pri_sched
[x
].callback
))
41 pri_error(pri
, "No more room in scheduler\n");
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;
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;
62 int pri_schedule_event2(struct pri
*pri
, int ms
, void (*function
)(void *data
, int data2
), void *data
, int data2
)
66 for (x
=1;x
<MAX_SCHED
;x
++)
67 if ((!pri
->pri_sched
[x
].callback2
) && (!pri
->pri_sched
[x
].callback
))
70 pri_error(pri
, "No more room in scheduler\n");
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;
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
;
91 struct timeval
*pri_schedule_next(struct pri
*pri
)
93 struct timeval
*closest
= NULL
;
95 /* Check subchannels */
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
;
108 static pri_event
*__pri_schedule_run(struct pri
*pri
, struct timeval
*tv
)
111 void (*callback
)(void *);
112 void (*callback2
)(void *, int);
117 if (pri
->subchannel
) {
118 if ((e
= __pri_schedule_run(pri
->subchannel
, tv
))) {
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
)))) {
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
);
149 pri_event
*pri_schedule_run(struct pri
*pri
)
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
;