-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeout.c
126 lines (105 loc) · 2.91 KB
/
timeout.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* Routines for time-delayed actions.
*
* Services is copyright (c) 1996-1999 Andrew Church.
* E-mail: <achurch@dragonfire.net>
* Services is copyright (c) 1999-2000 Andrew Kempe.
* E-mail: <theshadow@shadowfire.org>
* This program is free but copyrighted software; see the file COPYING for
* details.
*/
#include "services.h"
#include "timeout.h"
static Timeout *timeouts = NULL;
/*************************************************************************/
#ifdef DEBUG_COMMANDS
/* Send the timeout list to the given user. */
void send_timeout_list(User *u)
{
Timeout *to, *last;
notice(s_OperServ, u->nick, "Now: %ld", time(NULL));
for (to = timeouts, last = NULL; to; last = to, to = to->next) {
privmsg(s_OperServ, u->nick, "%p: %ld: %p (%p)",
to, to->timeout, to->code, to->data);
if (to->prev != last)
privmsg(s_OperServ, u->nick,
" to->prev incorrect! expected=%p seen=%p",
last, to->prev);
}
}
#endif /* DEBUG_COMMANDS */
/*************************************************************************/
/* Check the timeout list for any pending actions. */
void check_timeouts(void)
{
Timeout *to, *to2;
time_t t = time(NULL);
if (debug >= 2)
log("debug: Checking timeouts at %ld", t);
to = timeouts;
while (to) {
if (t < to->timeout) {
to = to->next;
continue;
}
if (debug >= 4) {
log("debug: Running timeout %p (code=%p repeat=%d)",
to, to->code, to->repeat);
}
to->code(to);
if (to->repeat) {
to = to->next;
continue;
}
to2 = to->next;
if (to->next)
to->next->prev = to->prev;
if (to->prev)
to->prev->next = to->next;
else
timeouts = to->next;
free(to);
to = to2;
}
if (debug >= 2)
log("debug: Finished timeout list");
}
/*************************************************************************/
/* Add a timeout to the list to be triggered in `delay' seconds. If
* `repeat' is nonzero, do not delete the timeout after it is triggered.
* This must maintain the property that timeouts added from within a
* timeout routine do not get checked during that run of the timeout list.
*/
Timeout *add_timeout(int delay, void (*code)(Timeout *), int repeat)
{
Timeout *t = smalloc(sizeof(Timeout));
t->settime = time(NULL);
t->timeout = t->settime + delay;
t->code = code;
t->repeat = repeat;
t->next = timeouts;
t->prev = NULL;
if (timeouts)
timeouts->prev = t;
timeouts = t;
return t;
}
/*************************************************************************/
/* Remove a timeout from the list (if it's there). */
void del_timeout(Timeout *t)
{
Timeout *ptr;
for (ptr = timeouts; ptr; ptr = ptr->next) {
if (ptr == t)
break;
}
if (!ptr)
return;
if (t->prev)
t->prev->next = t->next;
else
timeouts = t->next;
if (t->next)
t->next->prev = t->prev;
free(t);
}
/*************************************************************************/