ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/event.c
Revision: 7431
Committed: Tue Mar 8 18:20:10 2016 UTC (9 years, 5 months ago) by michael
Content type: text/x-csrc
File size: 2732 byte(s)
Log Message:
- Minor style cleanups & constification

File Contents

# User Rev Content
1 adx 30 /*
2 michael 4130 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 adx 30 *
4 michael 7006 * Copyright (c) 2000-2016 ircd-hybrid development team
5 adx 30 *
6 michael 4130 * 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 michael 4565 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
19 michael 4130 * USA
20 adx 30 */
21    
22 michael 4130 /*! \file event.c
23     * \brief Timer based event execution
24     * \version $Id$
25     */
26    
27 adx 30 #include "stdinc.h"
28 michael 1011 #include "list.h"
29 adx 30 #include "ircd.h"
30     #include "event.h"
31     #include "client.h"
32     #include "send.h"
33     #include "numeric.h"
34 michael 2680 #include "rng_mt.h"
35 adx 30
36 michael 6527
37 michael 6370 static dlink_list event_list;
38 michael 2680
39 michael 6306 const dlink_list *
40     event_get_list(void)
41     {
42 michael 6370 return &event_list;
43 michael 6306 }
44    
45 michael 4094 void
46     event_add(struct event *ev, void *data)
47     {
48     dlink_node *node;
49 adx 30
50 michael 4094 event_delete(ev);
51 michael 3599
52 michael 4094 ev->data = data;
53     ev->next = CurrentTime + ev->when;
54 michael 6642 ev->active = 1;
55 michael 3599
56 michael 6370 DLINK_FOREACH(node, event_list.head)
57 michael 4094 {
58     struct event *e = node->data;
59 michael 3599
60 michael 4094 if (e->next > ev->next)
61 adx 30 {
62 michael 6370 dlinkAddBefore(node, ev, &ev->node, &event_list);
63 adx 30 return;
64     }
65     }
66    
67 michael 6370 dlinkAddTail(ev, &ev->node, &event_list);
68 adx 30 }
69    
70     void
71 michael 4094 event_addish(struct event *ev, void *data)
72 adx 30 {
73 michael 4094 if (ev->when >= 3)
74 adx 30 {
75 michael 7330 const uintmax_t two_third = (2 * ev->when) / 3;
76 michael 4126
77 michael 4094 ev->when = two_third + ((genrand_int32() % 1000) * two_third) / 1000;
78 adx 30 }
79    
80 michael 4094 event_add(ev, data);
81 adx 30 }
82    
83     void
84 michael 4094 event_delete(struct event *ev)
85 adx 30 {
86 michael 6642 if (!ev->active)
87 michael 4094 return;
88    
89 michael 6370 dlinkDelete(&ev->node, &event_list);
90 michael 6642 ev->active = 0;
91 adx 30 }
92    
93 michael 4094 void
94     event_run(void)
95 adx 30 {
96 michael 7330 static uintmax_t last = 0;
97 adx 30
98 michael 4094 if (last == CurrentTime)
99     return;
100     last = CurrentTime;
101    
102 michael 7431 unsigned int len = dlink_list_length(&event_list);
103 michael 6370 while (len-- && dlink_list_length(&event_list))
104 michael 4094 {
105 michael 7431 struct event *ev = event_list.head->data;
106 michael 4094
107 michael 7431 if (ev->next > CurrentTime)
108 michael 4094 break;
109    
110 michael 7431 event_delete(ev);
111 michael 4094
112 michael 7431 ev->handler(ev->data);
113 michael 4094
114 michael 7431 if (!ev->oneshot)
115     event_add(ev, ev->data);
116 michael 4094 }
117 adx 30 }
118    
119     /*
120 michael 7330 * void event_set_back_events(uintmax_t by)
121 michael 4094 * Input: Time to set back events by.
122     * Output: None.
123     * Side-effects: Sets back all events by "by" seconds.
124 adx 30 */
125     void
126 michael 7330 event_set_back_events(uintmax_t by)
127 adx 30 {
128 michael 4094 dlink_node *node;
129    
130 michael 6370 DLINK_FOREACH(node, event_list.head)
131 michael 4094 {
132     struct event *ev = node->data;
133     ev->next -= by;
134     }
135 adx 30 }

Properties

Name Value
svn:eol-style native
svn:keywords Id Revision