1 |
/* |
2 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
* |
4 |
* Copyright (c) 2000-2016 ircd-hybrid development team |
5 |
* |
6 |
* This program is free software; you can redistribute it and/or modify |
7 |
* it under the terms of the GNU General Public License as published by |
8 |
* the Free Software Foundation; either version 2 of the License, or |
9 |
* (at your option) any later version. |
10 |
* |
11 |
* This program is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with this program; if not, write to the Free Software |
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
19 |
* USA |
20 |
*/ |
21 |
|
22 |
/*! \file event.c |
23 |
* \brief Timer based event execution |
24 |
* \version $Id$ |
25 |
*/ |
26 |
|
27 |
#include "stdinc.h" |
28 |
#include "list.h" |
29 |
#include "ircd.h" |
30 |
#include "event.h" |
31 |
#include "rng_mt.h" |
32 |
|
33 |
|
34 |
static dlink_list event_list; |
35 |
|
36 |
const dlink_list * |
37 |
event_get_list(void) |
38 |
{ |
39 |
return &event_list; |
40 |
} |
41 |
|
42 |
void |
43 |
event_add(struct event *ev, void *data) |
44 |
{ |
45 |
dlink_node *node; |
46 |
|
47 |
event_delete(ev); |
48 |
|
49 |
ev->data = data; |
50 |
ev->next = CurrentTime + ev->when; |
51 |
ev->active = 1; |
52 |
|
53 |
DLINK_FOREACH(node, event_list.head) |
54 |
{ |
55 |
struct event *e = node->data; |
56 |
|
57 |
if (e->next > ev->next) |
58 |
{ |
59 |
dlinkAddBefore(node, ev, &ev->node, &event_list); |
60 |
return; |
61 |
} |
62 |
} |
63 |
|
64 |
dlinkAddTail(ev, &ev->node, &event_list); |
65 |
} |
66 |
|
67 |
void |
68 |
event_addish(struct event *ev, void *data) |
69 |
{ |
70 |
if (ev->when >= 3) |
71 |
{ |
72 |
const uintmax_t two_third = (2 * ev->when) / 3; |
73 |
|
74 |
ev->when = two_third + ((genrand_int32() % 1000) * two_third) / 1000; |
75 |
} |
76 |
|
77 |
event_add(ev, data); |
78 |
} |
79 |
|
80 |
void |
81 |
event_delete(struct event *ev) |
82 |
{ |
83 |
if (!ev->active) |
84 |
return; |
85 |
|
86 |
dlinkDelete(&ev->node, &event_list); |
87 |
ev->active = 0; |
88 |
} |
89 |
|
90 |
void |
91 |
event_run(void) |
92 |
{ |
93 |
static uintmax_t last = 0; |
94 |
|
95 |
if (last == CurrentTime) |
96 |
return; |
97 |
last = CurrentTime; |
98 |
|
99 |
unsigned int len = dlink_list_length(&event_list); |
100 |
while (len-- && dlink_list_length(&event_list)) |
101 |
{ |
102 |
struct event *ev = event_list.head->data; |
103 |
|
104 |
if (ev->next > CurrentTime) |
105 |
break; |
106 |
|
107 |
event_delete(ev); |
108 |
|
109 |
ev->handler(ev->data); |
110 |
|
111 |
if (!ev->oneshot) |
112 |
event_add(ev, ev->data); |
113 |
} |
114 |
} |
115 |
|
116 |
/* |
117 |
* void event_set_back_events(uintmax_t by) |
118 |
* Input: Time to set back events by. |
119 |
* Output: None. |
120 |
* Side-effects: Sets back all events by "by" seconds. |
121 |
*/ |
122 |
void |
123 |
event_set_back_events(uintmax_t by) |
124 |
{ |
125 |
dlink_node *node; |
126 |
|
127 |
DLINK_FOREACH(node, event_list.head) |
128 |
{ |
129 |
struct event *ev = node->data; |
130 |
ev->next -= by; |
131 |
} |
132 |
} |