ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/evthread.C
(Generate patch)

Comparing deliantra/server/server/evthread.C (file contents):
Revision 1.3 by root, Thu Apr 3 09:31:33 2008 UTC vs.
Revision 1.4 by root, Sun Apr 6 16:13:23 2008 UTC

26thread evthread; 26thread evthread;
27static ev_loop *loop; 27static ev_loop *loop;
28 28
29#define TICK_CYCLES 4 29#define TICK_CYCLES 4
30 30
31// the tick watcher is activated every TICK_CYCLES
32ev_async tick_watcher;
33EV_ATOMIC_T coroapi::cede_pending;
34
31// the ticker runs TICK_CYCLES times as fast as the main server tick 35// the ticker runs TICK_CYCLES times as fast as the main server tick
32static ev_periodic cede_watcher; 36static ev_periodic cede_watcher;
33// the tick watcher is activated every TICK_CYCLES
34ev_async tick_watcher;
35 37
36EV_ATOMIC_T coroapi::cede_pending; 38/////////////////////////////////////////////////////////////////////////////
37 39
38// it is a bit silly to have to use a mutex, but contention 40// it is a bit silly to have to use a mutex, but contention
39// is basically non-existant, favouring good mutex implementations. 41// is basically non-existant, favouring good mutex implementations.
40static SMUTEX (evthread_mutex); 42static SMUTEX (evthread_mutex);
41static NV next_tick; 43static NV next_tick;
59 61
60 SMUTEX_LOCK (evthread_mutex); 62 SMUTEX_LOCK (evthread_mutex);
61 next_tick = w->at + TICK / TICK_CYCLES * (TICK_CYCLES - 1); 63 next_tick = w->at + TICK / TICK_CYCLES * (TICK_CYCLES - 1);
62 SMUTEX_UNLOCK (evthread_mutex); 64 SMUTEX_UNLOCK (evthread_mutex);
63 65
64 ev_async_send (EV_DEFAULT, &tick_watcher); 66 ev_async_send (EV_DEFAULT_UC, &tick_watcher);
65 } 67 }
66 68
67 coroapi::cede_pending = 1; 69 coroapi::cede_pending = 1;
68} 70}
69 71
71tick_cb (EV_P_ ev_async *w, int revents) 73tick_cb (EV_P_ ev_async *w, int revents)
72{ 74{
73 cfperl_tick (); 75 cfperl_tick ();
74} 76}
75 77
78/////////////////////////////////////////////////////////////////////////////
79
80static ev_async async_watcher;
81static ev_io aio1_watcher;
82static ev_idle aio2_watcher;
83static EV_ATOMIC_T aio_pending;
84
85static void
86aio2_cb (EV_P_ ev_idle *w, int revents)
87{
88 CALL_BEGIN (0);
89 CALL_CALL ("IO::AIO::poll_cb", G_SCALAR);
90
91 if (count > 0 && TOPi > 0)
92 ev_idle_start (EV_A, w);
93 else
94 ev_idle_stop (EV_A, w);
95
96 CALL_END;
97}
98
99static void
100async_cb (EV_P_ ev_async *w, int revents)
101{
102 aio2_cb (EV_A, &aio2_watcher, 0);
103}
104
105static void
106aio1_cb (EV_P_ ev_io *w, int revents)
107{
108 char dummy;
109
110 if (read (w->fd, &dummy, 1) > 0)
111 {
112 ev_async_send (EV_DEFAULT_UC, &async_watcher);
113 coroapi::cede_pending = 1;
114 }
115}
116
117/////////////////////////////////////////////////////////////////////////////
118
76static void * 119static void *
77evthread_proc (void *arg) 120evthread_proc (void *arg)
78{ 121{
122 ev_loop (loop, 0);
123
124 return 0;
125}
126
127void evthread_start (int aiofd)
128{
129 I_EV_API ("evthread");
130
131 // main loop
132 ev_async_init (&tick_watcher, tick_cb);
133 ev_set_priority (&tick_watcher, 1);
134 ev_async_start (EV_DEFAULT_UC, &tick_watcher);
135
136 ev_async_init (&async_watcher, async_cb);
137 ev_set_priority (&async_watcher, 1);
138 ev_async_start (EV_DEFAULT_UC, &async_watcher);
139
140 ev_idle_init (&aio2_watcher, aio2_cb);
141
142 // secondary loop
79 loop = ev_loop_new (0); 143 loop = ev_loop_new (0);
80 144
81 ev_periodic_init (&cede_watcher, cede_cb, 0, TICK / TICK_CYCLES, 0); 145 ev_periodic_init (&cede_watcher, cede_cb, 0, TICK / TICK_CYCLES, 0);
82 ev_periodic_start (loop, &cede_watcher); 146 ev_periodic_start (loop, &cede_watcher);
83 147
84 ev_loop (loop, 0); 148 ev_io_init (&aio1_watcher, aio1_cb, aiofd, EV_READ);
85 149 ev_io_start (loop, &aio1_watcher);
86 return 0;
87}
88
89void evthread_start ()
90{
91 I_EV_API ("evthread");
92
93 ev_async_init (&tick_watcher, tick_cb);
94 ev_set_priority (&tick_watcher, 1);
95 ev_async_start (EV_DEFAULT, &tick_watcher);
96 150
97 evthread.start (evthread_proc); 151 evthread.start (evthread_proc);
98} 152}
99 153
100 154

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines