1 /* IOTimer.c - IOMultiplexer v2
2 * Copyright (C) 2014 Philipp Kreil (pk910)
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #define _IOHandler_internals
18 #include "IOInternal.h"
19 #include "IOHandler.h"
26 static void _rearrange_timer(struct _IOTimerDescriptor *timer);
27 static void _autoreload_timer(struct _IOTimerDescriptor *timer);
29 struct _IOTimerDescriptor *iotimer_sorted_descriptors;
31 /* public functions */
33 struct IOTimerDescriptor *iotimer_create(struct timeval *timeout) {
34 struct IOTimerDescriptor *descriptor = calloc(1, sizeof(*descriptor));
36 iolog_trigger(IOLOG_ERROR, "could not allocate memory for IOTimerDescriptor in %s:%d", __FILE__, __LINE__);
39 struct _IOTimerDescriptor *timer = _create_timer(timeout);
44 timer->parent = descriptor;
45 timer->flags |= IOTIMERFLAG_PARENT_PUBLIC;
46 descriptor->iotimer = timer;
51 void iotimer_start(struct IOTimerDescriptor *descriptor) {
52 struct _IOTimerDescriptor *timer = descriptor->iotimer;
54 iolog_trigger(IOLOG_WARNING, "called iotimer_set_autoreload for destroyed IOTimerDescriptor in %s:%d", __FILE__, __LINE__);
57 timer->flags |= IOTIMERFLAG_ACTIVE;
58 if(!(timer->flags & IOTIMERFLAG_IN_LIST))
59 _trigger_timer(timer);
62 void iotimer_set_autoreload(struct IOTimerDescriptor *descriptor, struct timeval *autoreload) {
63 struct _IOTimerDescriptor *timer = descriptor->iotimer;
65 iolog_trigger(IOLOG_WARNING, "called iotimer_set_autoreload for destroyed IOTimerDescriptor in %s:%d", __FILE__, __LINE__);
69 timer->flags |= IOTIMERFLAG_PERIODIC;
70 timer->autoreload = *autoreload;
72 if(!(timer->flags & IOTIMERFLAG_IN_LIST)) {
74 gettimeofday(&now, NULL);
76 _autoreload_timer(timer);
79 timer->flags &= ~IOTIMERFLAG_PERIODIC;
83 void iotimer_set_callback(struct IOTimerDescriptor *descriptor, iotimer_callback *callback) {
84 descriptor->callback = callback;
87 void iotimer_destroy(struct IOTimerDescriptor *descriptor) {
88 struct _IOTimerDescriptor *timer = descriptor->iotimer;
90 iolog_trigger(IOLOG_WARNING, "called iotimer_destroy for destroyed IOTimerDescriptor in %s:%d", __FILE__, __LINE__);
93 descriptor->iotimer = NULL;
94 _destroy_timer(timer);
99 /* internal functions */
100 void _init_timers() {
104 struct _IOTimerDescriptor *_create_timer(struct timeval *timeout) {
105 struct _IOTimerDescriptor *timer = calloc(1, sizeof(*timer));
107 iolog_trigger(IOLOG_ERROR, "could not allocate memory for _IOTimerDescriptor in %s:%d", __FILE__, __LINE__);
111 timer->timeout = *timeout;
112 _rearrange_timer(timer);
117 static void _rearrange_timer(struct _IOTimerDescriptor *timer) {
118 if((timer->flags & IOTIMERFLAG_IN_LIST)) {
119 if(timer->prev == NULL)
120 iotimer_sorted_descriptors = timer->next;
122 timer->prev->next = timer->next;
123 if(timer->next != NULL)
124 timer->next->prev = timer->prev;
126 struct _IOTimerDescriptor *ctimer;
127 for(ctimer = iotimer_sorted_descriptors; ctimer; ctimer = ctimer->next) {
128 if(timeval_is_bigger(ctimer->timeout, timer->timeout)) {
129 timer->next = ctimer;
130 timer->prev = ctimer->prev;
132 ctimer->prev->next = timer;
134 iotimer_sorted_descriptors = timer;
135 ctimer->prev = timer;
138 else if(ctimer->next == NULL) {
139 ctimer->next = timer;
140 timer->prev = ctimer;
145 iotimer_sorted_descriptors = timer;
146 timer->flags |= IOTIMERFLAG_IN_LIST;
149 void _destroy_timer(struct _IOTimerDescriptor *timer) {
150 if((timer->flags & IOTIMERFLAG_IN_LIST)) {
151 if(timer->prev == NULL)
152 iotimer_sorted_descriptors = timer->next;
154 timer->prev->next = timer->next;
155 if(timer->next != NULL)
156 timer->next->prev = timer->prev;
161 static void _autoreload_timer(struct _IOTimerDescriptor *timer) {
162 timer->timeout.tv_usec += timer->autoreload.tv_usec;
163 timer->timeout.tv_sec += timer->autoreload.tv_sec;
164 if(timer->timeout.tv_usec > 1000000) {
165 timer->timeout.tv_sec += (timer->timeout.tv_usec / 1000000);
166 timer->timeout.tv_usec %= 1000000;
168 _rearrange_timer(timer);
171 void _trigger_timer() {
173 _trigger_timer_start:
174 gettimeofday(&now, NULL);
176 struct _IOTimerDescriptor *timer = iotimer_sorted_descriptors;
177 if(!timer || timeval_is_bigger(timer->timeout, now)) {
180 iotimer_sorted_descriptors = timer->next;
181 if(timer->next != NULL)
182 timer->next->prev = timer->prev;
183 timer->flags &= ~IOTIMERFLAG_IN_LIST;
185 if((timer->flags & IOTIMERFLAG_PERIODIC))
186 _autoreload_timer(timer);
188 if(timer->flags & IOTIMERFLAG_PARENT_PUBLIC) {
189 struct IOTimerDescriptor *descriptor = timer->parent;
190 if(descriptor->callback)
191 descriptor->callback(descriptor);
192 if(!(timer->flags & IOTIMERFLAG_PERIODIC))
193 iotimer_destroy(descriptor);
195 if(!(timer->flags & IOTIMERFLAG_PERIODIC))
196 _destroy_timer(timer);
199 goto _trigger_timer_start;