1 |
/* |
2 |
* This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 |
* |
4 |
* Copyright (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team |
5 |
* Copyright (©) 2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
6 |
* |
7 |
* Deliantra is free software: you can redistribute it and/or modify it under |
8 |
* the terms of the Affero GNU General Public License as published by the |
9 |
* Free Software Foundation, either version 3 of the License, or (at your |
10 |
* option) any later version. |
11 |
* |
12 |
* This program is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
* GNU General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the Affero GNU General Public License |
18 |
* and the GNU General Public License along with this program. If not, see |
19 |
* <http://www.gnu.org/licenses/>. |
20 |
* |
21 |
* The authors can be reached via e-mail to <support@deliantra.net> |
22 |
*/ |
23 |
|
24 |
#include "global.h" |
25 |
#include "evthread.h" |
26 |
#include "cfperl.h" |
27 |
|
28 |
thread evthread; |
29 |
static ev_loop *loop; |
30 |
|
31 |
///////////////////////////////////////////////////////////////////////////// |
32 |
|
33 |
ev_async tick_watcher; |
34 |
EV_ATOMIC_T coroapi::cede_pending; |
35 |
|
36 |
static ev_periodic cede_watcher; |
37 |
|
38 |
// it is a bit silly to have to use a mutex, but contention |
39 |
// is basically non-existant, favouring good mutex implementations. |
40 |
static SMUTEX (evthread_mutex); |
41 |
static NV next_tick; |
42 |
|
43 |
NV get_next_tick () |
44 |
{ |
45 |
SMUTEX_LOCK (evthread_mutex); |
46 |
NV retval = next_tick; |
47 |
SMUTEX_UNLOCK (evthread_mutex); |
48 |
return retval; |
49 |
} |
50 |
|
51 |
static void |
52 |
cede_cb (EV_P_ ev_periodic *w, int revents) |
53 |
{ |
54 |
SMUTEX_LOCK (evthread_mutex); |
55 |
next_tick = w->at; |
56 |
SMUTEX_UNLOCK (evthread_mutex); |
57 |
|
58 |
coroapi::cede_pending = 1; |
59 |
|
60 |
ev_async_send (EV_DEFAULT_UC, &tick_watcher); |
61 |
} |
62 |
|
63 |
static void |
64 |
tick_cb (EV_P_ ev_async *w, int revents) |
65 |
{ |
66 |
cfperl_tick (); |
67 |
} |
68 |
|
69 |
///////////////////////////////////////////////////////////////////////////// |
70 |
|
71 |
static SMUTEX(cb_mutex); |
72 |
static SCOND(cb_done); |
73 |
|
74 |
static ev_async async_watcher; |
75 |
static ev_io aio_watcher; |
76 |
|
77 |
static void |
78 |
async_cb (EV_P_ ev_async *w, int revents) |
79 |
{ |
80 |
CALL_BEGIN (0); |
81 |
CALL_CALL ("IO::AIO::poll_cb", G_SCALAR); |
82 |
IV reqs = POPi; |
83 |
CALL_END; |
84 |
|
85 |
SMUTEX_LOCK (cb_mutex); |
86 |
SCOND_SIGNAL (cb_done); |
87 |
SMUTEX_UNLOCK (cb_mutex); |
88 |
} |
89 |
|
90 |
static void |
91 |
aio_cb (EV_P_ ev_io *w, int revents) |
92 |
{ |
93 |
ev_async_send (EV_DEFAULT_UC, &async_watcher); |
94 |
|
95 |
coroapi::cede_pending = 1; // to decrease latency |
96 |
|
97 |
SCOND_WAIT (cb_done, cb_mutex); |
98 |
} |
99 |
|
100 |
///////////////////////////////////////////////////////////////////////////// |
101 |
|
102 |
static void * |
103 |
evthread_proc (void *arg) |
104 |
{ |
105 |
SMUTEX_LOCK (cb_mutex); |
106 |
|
107 |
ev_run (loop, 0); |
108 |
|
109 |
return 0; |
110 |
} |
111 |
|
112 |
void evthread_start (int aiofd) |
113 |
{ |
114 |
I_EV_API ("evthread"); |
115 |
|
116 |
// main loop |
117 |
ev_async_init (&tick_watcher, tick_cb); |
118 |
ev_set_priority (&tick_watcher, 1); |
119 |
ev_async_start (EV_DEFAULT_UC, &tick_watcher); |
120 |
|
121 |
ev_async_init (&async_watcher, async_cb); |
122 |
ev_set_priority (&async_watcher, 1); |
123 |
ev_async_start (EV_DEFAULT_UC, &async_watcher); |
124 |
|
125 |
// secondary loop, we use select/poll as epoll has very |
126 |
// bad timing characteristics and we only watch a single low fd. |
127 |
loop = ev_loop_new (EVBACKEND_SELECT); |
128 |
|
129 |
ev_periodic_init (&cede_watcher, cede_cb, 0, TICK, 0); |
130 |
ev_periodic_start (loop, &cede_watcher); |
131 |
|
132 |
ev_io_init (&aio_watcher, aio_cb, aiofd, EV_READ); |
133 |
ev_io_start (loop, &aio_watcher); |
134 |
|
135 |
evthread.start (evthread_proc); |
136 |
} |
137 |
|
138 |
|
139 |
|