ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/evthread.C
Revision: 1.5
Committed: Thu May 29 03:27:37 2008 UTC (16 years ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.4: +5 -1 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 /*
2     * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3     *
4     * Copyright (©) 2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5     *
6     * Deliantra 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 3 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, see <http://www.gnu.org/licenses/>.
18     *
19     * The authors can be reached via e-mail to <support@deliantra.net>
20     */
21    
22     #include "global.h"
23     #include "evthread.h"
24     #include "cfperl.h"
25    
26     thread evthread;
27     static ev_loop *loop;
28    
29 root 1.2 #define TICK_CYCLES 4
30 root 1.1
31 root 1.4 // the tick watcher is activated every TICK_CYCLES
32     ev_async tick_watcher;
33     EV_ATOMIC_T coroapi::cede_pending;
34    
35 root 1.1 // the ticker runs TICK_CYCLES times as fast as the main server tick
36     static ev_periodic cede_watcher;
37    
38 root 1.4 /////////////////////////////////////////////////////////////////////////////
39 root 1.1
40     // it is a bit silly to have to use a mutex, but contention
41     // is basically non-existant, favouring good mutex implementations.
42     static SMUTEX (evthread_mutex);
43     static NV next_tick;
44    
45     NV get_next_tick ()
46     {
47     SMUTEX_LOCK (evthread_mutex);
48     NV retval = next_tick;
49     SMUTEX_UNLOCK (evthread_mutex);
50     return retval;
51     }
52    
53     static void
54     cede_cb (EV_P_ ev_periodic *w, int revents)
55     {
56     static int cycle = 0;
57    
58     if (++cycle == TICK_CYCLES)
59     {
60     cycle = 0;
61    
62     SMUTEX_LOCK (evthread_mutex);
63     next_tick = w->at + TICK / TICK_CYCLES * (TICK_CYCLES - 1);
64     SMUTEX_UNLOCK (evthread_mutex);
65    
66 root 1.4 ev_async_send (EV_DEFAULT_UC, &tick_watcher);
67 root 1.1 }
68    
69     coroapi::cede_pending = 1;
70     }
71    
72     static void
73     tick_cb (EV_P_ ev_async *w, int revents)
74     {
75     cfperl_tick ();
76     }
77    
78 root 1.4 /////////////////////////////////////////////////////////////////////////////
79    
80     static ev_async async_watcher;
81     static ev_io aio1_watcher;
82     static ev_idle aio2_watcher;
83     static EV_ATOMIC_T aio_pending;
84    
85     static void
86     aio2_cb (EV_P_ ev_idle *w, int revents)
87     {
88     CALL_BEGIN (0);
89     CALL_CALL ("IO::AIO::poll_cb", G_SCALAR);
90    
91 root 1.5 fprintf (stderr, "pollcb %d\n", count ? TOPi : 0);//D
92     if (count > 0 && TOPi < 0)
93 root 1.4 ev_idle_start (EV_A, w);
94     else
95     ev_idle_stop (EV_A, w);
96    
97     CALL_END;
98     }
99    
100     static void
101     async_cb (EV_P_ ev_async *w, int revents)
102     {
103     aio2_cb (EV_A, &aio2_watcher, 0);
104     }
105    
106     static void
107     aio1_cb (EV_P_ ev_io *w, int revents)
108     {
109     char dummy;
110 root 1.5 ev_io_stop (EV_A, w);//D
111     return;
112 root 1.4
113     if (read (w->fd, &dummy, 1) > 0)
114     {
115 root 1.5 fprintf (stderr, "async\n");//D
116 root 1.4 ev_async_send (EV_DEFAULT_UC, &async_watcher);
117     coroapi::cede_pending = 1;
118     }
119     }
120    
121     /////////////////////////////////////////////////////////////////////////////
122    
123 root 1.1 static void *
124     evthread_proc (void *arg)
125     {
126     ev_loop (loop, 0);
127    
128     return 0;
129     }
130    
131 root 1.4 void evthread_start (int aiofd)
132 root 1.1 {
133     I_EV_API ("evthread");
134 root 1.3
135 root 1.4 // main loop
136 root 1.3 ev_async_init (&tick_watcher, tick_cb);
137     ev_set_priority (&tick_watcher, 1);
138 root 1.4 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 root 1.3
155 root 1.1 evthread.start (evthread_proc);
156     }
157    
158    
159