1 | /* |
1 | /* |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 | * |
3 | * |
4 | * Copyright (©) 2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
4 | * Copyright (©) 2008,2007,2008,2009 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * |
5 | * |
6 | * Deliantra is free software: you can redistribute it and/or modify |
6 | * Deliantra is free software: you can redistribute it and/or modify it under |
7 | * it under the terms of the GNU General Public License as published by |
7 | * the terms of the Affero GNU General Public License as published by the |
8 | * the Free Software Foundation, either version 3 of the License, or |
8 | * Free Software Foundation, either version 3 of the License, or (at your |
9 | * (at your option) any later version. |
9 | * option) any later version. |
10 | * |
10 | * |
11 | * This program is distributed in the hope that it will be useful, |
11 | * This program is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * GNU General Public License for more details. |
14 | * GNU General Public License for more details. |
15 | * |
15 | * |
16 | * You should have received a copy of the GNU General Public License |
16 | * You should have received a copy of the Affero GNU General Public License |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | * and the GNU General Public License along with this program. If not, see |
|
|
18 | * <http://www.gnu.org/licenses/>. |
18 | * |
19 | * |
19 | * The authors can be reached via e-mail to <support@deliantra.net> |
20 | * The authors can be reached via e-mail to <support@deliantra.net> |
20 | */ |
21 | */ |
21 | |
22 | |
22 | #include "global.h" |
23 | #include "global.h" |
… | |
… | |
24 | #include "cfperl.h" |
25 | #include "cfperl.h" |
25 | |
26 | |
26 | thread evthread; |
27 | thread evthread; |
27 | static ev_loop *loop; |
28 | static ev_loop *loop; |
28 | |
29 | |
29 | #define TICK_CYCLES 5 |
30 | #define TICK_CYCLES 4 |
|
|
31 | |
|
|
32 | // the tick watcher is activated every TICK_CYCLES |
|
|
33 | ev_async tick_watcher; |
|
|
34 | EV_ATOMIC_T coroapi::cede_pending; |
30 | |
35 | |
31 | // the ticker runs TICK_CYCLES times as fast as the main server tick |
36 | // the ticker runs TICK_CYCLES times as fast as the main server tick |
32 | static ev_periodic cede_watcher; |
37 | static ev_periodic cede_watcher; |
33 | // the tick watcher is activated every TICK_CYCLES |
|
|
34 | ev_async tick_watcher; |
|
|
35 | |
38 | |
36 | EV_ATOMIC_T coroapi::cede_pending; |
39 | ///////////////////////////////////////////////////////////////////////////// |
37 | |
40 | |
38 | // it is a bit silly to have to use a mutex, but contention |
41 | // it is a bit silly to have to use a mutex, but contention |
39 | // is basically non-existant, favouring good mutex implementations. |
42 | // is basically non-existant, favouring good mutex implementations. |
40 | static SMUTEX (evthread_mutex); |
43 | static SMUTEX (evthread_mutex); |
41 | static NV next_tick; |
44 | static NV next_tick; |
… | |
… | |
59 | |
62 | |
60 | SMUTEX_LOCK (evthread_mutex); |
63 | SMUTEX_LOCK (evthread_mutex); |
61 | next_tick = w->at + TICK / TICK_CYCLES * (TICK_CYCLES - 1); |
64 | next_tick = w->at + TICK / TICK_CYCLES * (TICK_CYCLES - 1); |
62 | SMUTEX_UNLOCK (evthread_mutex); |
65 | SMUTEX_UNLOCK (evthread_mutex); |
63 | |
66 | |
64 | ev_async_send (EV_DEFAULT, &tick_watcher); |
67 | ev_async_send (EV_DEFAULT_UC, &tick_watcher); |
65 | } |
68 | } |
66 | |
69 | |
67 | coroapi::cede_pending = 1; |
70 | coroapi::cede_pending = 1; |
68 | } |
71 | } |
69 | |
72 | |
… | |
… | |
71 | tick_cb (EV_P_ ev_async *w, int revents) |
74 | tick_cb (EV_P_ ev_async *w, int revents) |
72 | { |
75 | { |
73 | cfperl_tick (); |
76 | cfperl_tick (); |
74 | } |
77 | } |
75 | |
78 | |
|
|
79 | ///////////////////////////////////////////////////////////////////////////// |
|
|
80 | |
|
|
81 | static ev_async async_watcher; |
|
|
82 | static ev_io aio1_watcher; |
|
|
83 | static ev_idle aio2_watcher; |
|
|
84 | static EV_ATOMIC_T aio_pending; |
|
|
85 | |
|
|
86 | static void |
|
|
87 | aio2_cb (EV_P_ ev_idle *w, int revents) |
|
|
88 | { |
|
|
89 | IV reqs; |
|
|
90 | |
|
|
91 | CALL_BEGIN (0); |
|
|
92 | CALL_CALL ("IO::AIO::poll_cb", G_SCALAR); |
|
|
93 | |
|
|
94 | reqs = POPi; |
|
|
95 | if (count > 0 && reqs < 0) |
|
|
96 | ev_idle_start (EV_A, w); |
|
|
97 | else |
|
|
98 | ev_idle_stop (EV_A, w); |
|
|
99 | |
|
|
100 | CALL_END; |
|
|
101 | } |
|
|
102 | |
|
|
103 | static void |
|
|
104 | async_cb (EV_P_ ev_async *w, int revents) |
|
|
105 | { |
|
|
106 | aio2_cb (EV_A, &aio2_watcher, 0); |
|
|
107 | } |
|
|
108 | |
|
|
109 | static void |
|
|
110 | aio1_cb (EV_P_ ev_io *w, int revents) |
|
|
111 | { |
|
|
112 | char dummy[9]; |
|
|
113 | |
|
|
114 | if (read (w->fd, dummy, sizeof (dummy)) > 0) |
|
|
115 | { |
|
|
116 | ev_async_send (EV_DEFAULT_UC, &async_watcher); |
|
|
117 | coroapi::cede_pending = 1; |
|
|
118 | } |
|
|
119 | } |
|
|
120 | |
|
|
121 | ///////////////////////////////////////////////////////////////////////////// |
|
|
122 | |
76 | static void * |
123 | static void * |
77 | evthread_proc (void *arg) |
124 | evthread_proc (void *arg) |
78 | { |
125 | { |
79 | loop = ev_loop_new (0); |
|
|
80 | |
|
|
81 | ev_async_init (&tick_watcher, tick_cb); |
|
|
82 | ev_set_priority (&tick_watcher, 1); |
|
|
83 | ev_async_start (EV_DEFAULT, &tick_watcher); |
|
|
84 | |
|
|
85 | ev_periodic_init (&cede_watcher, cede_cb, 0, TICK / TICK_CYCLES, 0); |
|
|
86 | ev_periodic_start (loop, &cede_watcher); |
|
|
87 | |
|
|
88 | ev_loop (loop, 0); |
126 | ev_loop (loop, 0); |
89 | |
127 | |
90 | return 0; |
128 | return 0; |
91 | } |
129 | } |
92 | |
130 | |
93 | void evthread_start () |
131 | void evthread_start (int aiofd) |
94 | { |
132 | { |
95 | I_EV_API ("evthread"); |
133 | I_EV_API ("evthread"); |
|
|
134 | |
|
|
135 | // main loop |
|
|
136 | ev_async_init (&tick_watcher, tick_cb); |
|
|
137 | ev_set_priority (&tick_watcher, 1); |
|
|
138 | ev_async_start (EV_DEFAULT_UC, &tick_watcher); |
|
|
139 | |
|
|
140 | ev_async_init (&async_watcher, async_cb); |
|
|
141 | ev_set_priority (&async_watcher, 1); |
|
|
142 | ev_async_start (EV_DEFAULT_UC, &async_watcher); |
|
|
143 | |
|
|
144 | ev_idle_init (&aio2_watcher, aio2_cb); |
|
|
145 | |
|
|
146 | // secondary loop |
|
|
147 | loop = ev_loop_new (0); |
|
|
148 | |
|
|
149 | ev_periodic_init (&cede_watcher, cede_cb, 0, TICK / TICK_CYCLES, 0); |
|
|
150 | ev_periodic_start (loop, &cede_watcher); |
|
|
151 | |
|
|
152 | ev_io_init (&aio1_watcher, aio1_cb, aiofd, EV_READ); |
|
|
153 | ev_io_start (loop, &aio1_watcher); |
|
|
154 | |
96 | evthread.start (evthread_proc); |
155 | evthread.start (evthread_proc); |
97 | } |
156 | } |
98 | |
157 | |
99 | |
158 | |
100 | |
159 | |