timeout.c

Go to the documentation of this file.
00001 /* Routines for time-delayed actions.
00002  *
00003  * (C) 2003-2008 Anope Team
00004  * Contact us at info@anope.org
00005  *
00006  * Please read COPYING and README for further details.
00007  *
00008  * Based on the original code of Epona by Lara.
00009  * Based on the original code of Services by Andy Church. 
00010  * 
00011  * $Id: timeout.c 1345 2008-01-13 12:54:14Z geniusdex $ 
00012  *
00013  */
00014 
00015 #include "services.h"
00016 #include "pseudo.h"
00017 
00018 static Timeout *timeouts = NULL;
00019 
00020 /*************************************************************************/
00021 
00022 #ifdef DEBUG_COMMANDS
00023 
00024 /* Send the timeout list to the given user. */
00025 
00026 int send_timeout_list(User * u)
00027 {
00028     Timeout *to, *last;
00029 
00030     notice(s_OperServ, u->nick, "Now: %ld", (long int) time(NULL));
00031     for (to = timeouts, last = NULL; to; last = to, to = to->next) {
00032         notice(s_OperServ, u->nick, "0x%p: %ld: 0x%p (0x%p)",
00033                (void *) to, (long int) to->timeout, (void *) to->code,
00034                (void *) to->data);
00035         if (to->prev != last)
00036             notice(s_OperServ, u->nick,
00037                    "    to->prev incorrect!  expected=0x%p seen=0x%p",
00038                    (void *) last, (void *) to->prev);
00039     }
00040     return MOD_CONT;
00041 }
00042 
00043 #endif                          /* DEBUG_COMMANDS */
00044 
00045 /*************************************************************************/
00046 
00047 /* Check the timeout list for any pending actions. */
00048 
00049 void check_timeouts(void)
00050 {
00051     Timeout *to, *to2;
00052     time_t t = time(NULL);
00053 
00054     if (debug >= 2)
00055         alog("debug: Checking timeouts at %ld", (long int) t);
00056 
00057     to = timeouts;
00058     while (to) {
00059         if (t < to->timeout) {
00060             to = to->next;
00061             continue;
00062         }
00063         if (debug >= 4) {
00064             alog("debug: Running timeout 0x%p (code=0x%p repeat=%d)",
00065                  (void *) to, (void *) to->code, to->repeat);
00066         }
00067         to->code(to);
00068         if (to->repeat) {
00069             to = to->next;
00070             continue;
00071         }
00072         to2 = to->next;
00073         if (to->next)
00074             to->next->prev = to->prev;
00075         if (to->prev)
00076             to->prev->next = to->next;
00077         else
00078             timeouts = to->next;
00079         free(to);
00080         to = to2;
00081     }
00082     if (debug >= 2)
00083         alog("debug: Finished timeout list");
00084 }
00085 
00086 /*************************************************************************/
00087 
00088 /* Add a timeout to the list to be triggered in `delay' seconds.  If
00089  * `repeat' is nonzero, do not delete the timeout after it is triggered.
00090  * This must maintain the property that timeouts added from within a
00091  * timeout routine do not get checked during that run of the timeout list.
00092  */
00093 
00094 Timeout *add_timeout(int delay, void (*code) (Timeout *), int repeat)
00095 {
00096     Timeout *t = scalloc(sizeof(Timeout), 1);
00097     t->settime = time(NULL);
00098     t->timeout = t->settime + delay;
00099     t->code = code;
00100     t->repeat = repeat;
00101     t->next = timeouts;
00102     t->prev = NULL;
00103     if (timeouts)
00104         timeouts->prev = t;
00105     timeouts = t;
00106     return t;
00107 }
00108 
00109 /*************************************************************************/
00110 
00111 /* Remove a timeout from the list (if it's there). */
00112 
00113 void del_timeout(Timeout * t)
00114 {
00115     Timeout *ptr;
00116 
00117     for (ptr = timeouts; ptr; ptr = ptr->next) {
00118         if (ptr == t)
00119             break;
00120     }
00121     if (!ptr)
00122         return;
00123     if (t->prev)
00124         t->prev->next = t->next;
00125     else
00126         timeouts = t->next;
00127     if (t->next)
00128         t->next->prev = t->prev;
00129     free(t);
00130 }
00131 
00132 /*************************************************************************/

Generated on Sun Oct 5 09:06:58 2008 for Anope by  doxygen 1.5.7.1