comparison spandsp-0.0.3/spandsp-0.0.3/src/schedule.c @ 5:f762bf195c4b

import spandsp-0.0.3
author Peter Meerwald <pmeerw@cosy.sbg.ac.at>
date Fri, 25 Jun 2010 16:00:21 +0200
parents
children
comparison
equal deleted inserted replaced
4:26cd8f1ef0b1 5:f762bf195c4b
1 /*
2 * SpanDSP - a series of DSP components for telephony
3 *
4 * schedule.c
5 *
6 * Written by Steve Underwood <steveu@coppice.org>
7 *
8 * Copyright (C) 2004 Steve Underwood
9 *
10 * All rights reserved.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2, as
14 * published by the Free Software Foundation.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 *
25 * $Id: schedule.c,v 1.12 2006/10/24 13:45:26 steveu Exp $
26 */
27
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31
32 #include <stdio.h>
33 #include <inttypes.h>
34 #include <stdlib.h>
35 #include <memory.h>
36
37 #include "spandsp/telephony.h"
38 #include "spandsp/schedule.h"
39
40 int span_schedule_event(span_sched_state_t *s, int ms, span_sched_callback_func_t function, void *user_data)
41 {
42 int i;
43
44 for (i = 0; i < s->max_to_date; i++)
45 {
46 if (s->sched[i].callback == NULL)
47 break;
48 /*endif*/
49 }
50 /*endfor*/
51 if (i >= s->allocated)
52 {
53 s->allocated += 5;
54 s->sched = (span_sched_t *) realloc(s->sched, sizeof(span_sched_t)*s->allocated);
55 }
56 /*endif*/
57 if (i >= s->max_to_date)
58 s->max_to_date = i + 1;
59 /*endif*/
60 s->sched[i].when = s->ticker + ms*SAMPLE_RATE/1000;
61 s->sched[i].callback = function;
62 s->sched[i].user_data = user_data;
63 return i;
64 }
65 /*- End of function --------------------------------------------------------*/
66
67 uint64_t span_schedule_next(span_sched_state_t *s)
68 {
69 int i;
70 uint64_t earliest;
71
72 earliest = ~((uint64_t) 0);
73 for (i = 0; i < s->max_to_date; i++)
74 {
75 if (s->sched[i].callback && earliest > s->sched[i].when)
76 earliest = s->sched[i].when;
77 /*endif*/
78 }
79 /*endfor*/
80 return earliest;
81 }
82 /*- End of function --------------------------------------------------------*/
83
84 uint64_t span_schedule_time(span_sched_state_t *s)
85 {
86 return s->ticker;
87 }
88 /*- End of function --------------------------------------------------------*/
89
90 void span_schedule_update(span_sched_state_t *s, int samples)
91 {
92 int i;
93 span_sched_callback_func_t callback;
94 void *user_data;
95
96 s->ticker += samples;
97 for (i = 0; i < s->max_to_date; i++)
98 {
99 if (s->sched[i].callback && s->sched[i].when <= s->ticker)
100 {
101 callback = s->sched[i].callback;
102 user_data = s->sched[i].user_data;
103 s->sched[i].callback = NULL;
104 s->sched[i].user_data = NULL;
105 callback(s, user_data);
106 }
107 /*endif*/
108 }
109 /*endfor*/
110 }
111 /*- End of function --------------------------------------------------------*/
112
113 void span_schedule_del(span_sched_state_t *s, int i)
114 {
115 if (i >= s->max_to_date
116 ||
117 i < 0
118 ||
119 s->sched[i].callback == NULL)
120 {
121 fprintf(stderr, "Asked to delete scheduled ID %d???\n", i);
122 return;
123 }
124 /*endif*/
125 s->sched[i].callback = NULL;
126 }
127 /*- End of function --------------------------------------------------------*/
128
129 span_sched_state_t *span_schedule_init(span_sched_state_t *s)
130 {
131 memset(s, 0, sizeof(*s));
132 return s;
133 }
134 /*- End of function --------------------------------------------------------*/
135
136 int span_schedule_release(span_sched_state_t *s)
137 {
138 if (s->sched)
139 {
140 free(s->sched);
141 s->sched = NULL;
142 }
143 return 0;
144 }
145 /*- End of function --------------------------------------------------------*/
146 /*- End of file ------------------------------------------------------------*/

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.