Anope IRC Services  Version 1.8
timeout.c
Go to the documentation of this file.
1 /* Routines for time-delayed actions.
2  *
3  * (C) 2003-2014 Anope Team
4  * Contact us at team@anope.org
5  *
6  * Please read COPYING and README for further details.
7  *
8  * Based on the original code of Epona by Lara.
9  * Based on the original code of Services by Andy Church.
10  *
11  *
12  */
13 
14 #include "services.h"
15 #include "pseudo.h"
16 
17 static Timeout *timeouts = NULL;
18 
19 /*************************************************************************/
20 
21 #ifdef DEBUG_COMMANDS
22 
23 /* Send the timeout list to the given user. */
24 
25 int send_timeout_list(User * u)
26 {
27  Timeout *to, *last;
28 
29  notice(s_OperServ, u->nick, "Now: %ld", (long int) time(NULL));
30  for (to = timeouts, last = NULL; to; last = to, to = to->next) {
31  notice(s_OperServ, u->nick, "0x%p: %ld: 0x%p (0x%p)",
32  (void *) to, (long int) to->timeout, (void *) to->code,
33  (void *) to->data);
34  if (to->prev != last)
36  " to->prev incorrect! expected=0x%p seen=0x%p",
37  (void *) last, (void *) to->prev);
38  }
39  return MOD_CONT;
40 }
41 
42 #endif /* DEBUG_COMMANDS */
43 
44 /*************************************************************************/
45 
46 /* Check the timeout list for any pending actions. */
47 
48 void check_timeouts(void)
49 {
50  Timeout *to, *to2;
51  time_t t = time(NULL);
52 
53  if (debug >= 2)
54  alog("debug: Checking timeouts at %ld", (long int) t);
55 
56  to = timeouts;
57  while (to) {
58  if (t < to->timeout) {
59  to = to->next;
60  continue;
61  }
62  if (debug >= 4) {
63  alog("debug: Running timeout 0x%p (code=0x%p repeat=%d)",
64  (void *) to, (void *) to->code, to->repeat);
65  }
66  to->code(to);
67  if (to->repeat) {
68  to = to->next;
69  continue;
70  }
71  to2 = to->next;
72  if (to->next)
73  to->next->prev = to->prev;
74  if (to->prev)
75  to->prev->next = to->next;
76  else
77  timeouts = to->next;
78  free(to);
79  to = to2;
80  }
81  if (debug >= 2)
82  alog("debug: Finished timeout list");
83 }
84 
85 /*************************************************************************/
86 
87 /* Add a timeout to the list to be triggered in `delay' seconds. If
88  * `repeat' is nonzero, do not delete the timeout after it is triggered.
89  * This must maintain the property that timeouts added from within a
90  * timeout routine do not get checked during that run of the timeout list.
91  */
92 
93 Timeout *add_timeout(int delay, void (*code) (Timeout *), int repeat)
94 {
95  Timeout *t = scalloc(sizeof(Timeout), 1);
96  t->settime = time(NULL);
97  t->timeout = t->settime + delay;
98  t->code = code;
99  t->repeat = repeat;
100  t->next = timeouts;
101  t->prev = NULL;
102  if (timeouts)
103  timeouts->prev = t;
104  timeouts = t;
105  return t;
106 }
107 
108 /*************************************************************************/
109 
110 /* Remove a timeout from the list (if it's there). */
111 
113 {
114  Timeout *ptr;
115 
116  for (ptr = timeouts; ptr; ptr = ptr->next) {
117  if (ptr == t)
118  break;
119  }
120  if (!ptr)
121  return;
122  if (t->prev)
123  t->prev->next = t->next;
124  else
125  timeouts = t->next;
126  if (t->next)
127  t->next->prev = t->prev;
128  free(t);
129 }
130 
131 /*************************************************************************/
char nick[NICKMAX]
Definition: services.h:875
Timeout * add_timeout(int delay, void(*code)(Timeout *), int repeat)
Definition: timeout.c:93
void(* code)(Timeout *)
Definition: timeout.h:26
E void notice(char *source, char *dest, const char *fmt,...)
Definition: send.c:274
void check_timeouts(void)
Definition: timeout.c:48
E void * scalloc(long elsize, long els)
Definition: memory.c:55
E char * s_OperServ
Definition: extern.h:289
Timeout * prev
Definition: timeout.h:23
time_t settime
Definition: timeout.h:24
E void alog(const char *fmt,...) FORMAT(printf
#define MOD_CONT
Definition: modules.h:54
int repeat
Definition: timeout.h:25
E int debug
Definition: extern.h:775
Timeout * next
Definition: timeout.h:23
time_t timeout
Definition: timeout.h:24
static Timeout * timeouts
Definition: timeout.c:17
void del_timeout(Timeout *t)
Definition: timeout.c:112