comparison spandsp-0.0.6pre17/src/schedule.c @ 4:26cd8f1ef0b1

import spandsp-0.0.6pre17
author Peter Meerwald <pmeerw@cosy.sbg.ac.at>
date Fri, 25 Jun 2010 15:50:58 +0200
parents
children
comparison
equal deleted inserted replaced
3:c6c5a16ce2f2 4:26cd8f1ef0b1
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 Lesser General Public License version 2.1,
14 * as 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 Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License 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.22 2009/02/10 13:06:46 steveu Exp $
26 */
27
28 #if defined(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/logging.h"
39 #include "spandsp/schedule.h"
40
41 #include "spandsp/private/logging.h"
42 #include "spandsp/private/schedule.h"
43
44 SPAN_DECLARE(int) span_schedule_event(span_sched_state_t *s, int us, span_sched_callback_func_t function, void *user_data)
45 {
46 int i;
47
48 for (i = 0; i < s->max_to_date; i++)
49 {
50 if (s->sched[i].callback == NULL)
51 break;
52 /*endif*/
53 }
54 /*endfor*/
55 if (i >= s->allocated)
56 {
57 s->allocated += 5;
58 s->sched = (span_sched_t *) realloc(s->sched, sizeof(span_sched_t)*s->allocated);
59 }
60 /*endif*/
61 if (i >= s->max_to_date)
62 s->max_to_date = i + 1;
63 /*endif*/
64 s->sched[i].when = s->ticker + us;
65 s->sched[i].callback = function;
66 s->sched[i].user_data = user_data;
67 return i;
68 }
69 /*- End of function --------------------------------------------------------*/
70
71 SPAN_DECLARE(uint64_t) span_schedule_next(span_sched_state_t *s)
72 {
73 int i;
74 uint64_t earliest;
75
76 earliest = ~((uint64_t) 0);
77 for (i = 0; i < s->max_to_date; i++)
78 {
79 if (s->sched[i].callback && earliest > s->sched[i].when)
80 earliest = s->sched[i].when;
81 /*endif*/
82 }
83 /*endfor*/
84 return earliest;
85 }
86 /*- End of function --------------------------------------------------------*/
87
88 SPAN_DECLARE(uint64_t) span_schedule_time(span_sched_state_t *s)
89 {
90 return s->ticker;
91 }
92 /*- End of function --------------------------------------------------------*/
93
94 SPAN_DECLARE(void) span_schedule_update(span_sched_state_t *s, int us)
95 {
96 int i;
97 span_sched_callback_func_t callback;
98 void *user_data;
99
100 s->ticker += us;
101 for (i = 0; i < s->max_to_date; i++)
102 {
103 if (s->sched[i].callback && s->sched[i].when <= s->ticker)
104 {
105 callback = s->sched[i].callback;
106 user_data = s->sched[i].user_data;
107 s->sched[i].callback = NULL;
108 s->sched[i].user_data = NULL;
109 callback(s, user_data);
110 }
111 /*endif*/
112 }
113 /*endfor*/
114 }
115 /*- End of function --------------------------------------------------------*/
116
117 SPAN_DECLARE(void) span_schedule_del(span_sched_state_t *s, int i)
118 {
119 if (i >= s->max_to_date
120 ||
121 i < 0
122 ||
123 s->sched[i].callback == NULL)
124 {
125 span_log(&s->logging, SPAN_LOG_WARNING, "Requested to delete invalid scheduled ID %d ?\n", i);
126 return;
127 }
128 /*endif*/
129 s->sched[i].callback = NULL;
130 }
131 /*- End of function --------------------------------------------------------*/
132
133 SPAN_DECLARE(span_sched_state_t *) span_schedule_init(span_sched_state_t *s)
134 {
135 memset(s, 0, sizeof(*s));
136 span_log_init(&s->logging, SPAN_LOG_NONE, NULL);
137 span_log_set_protocol(&s->logging, "SCHEDULE");
138 return s;
139 }
140 /*- End of function --------------------------------------------------------*/
141
142 SPAN_DECLARE(int) span_schedule_release(span_sched_state_t *s)
143 {
144 if (s->sched)
145 {
146 free(s->sched);
147 s->sched = NULL;
148 }
149 return 0;
150 }
151 /*- End of function --------------------------------------------------------*/
152
153 SPAN_DECLARE(int) span_schedule_free(span_sched_state_t *s)
154 {
155 span_schedule_release(s);
156 if (s)
157 free(s);
158 return 0;
159 }
160 /*- End of function --------------------------------------------------------*/
161 /*- End of file ------------------------------------------------------------*/

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