… | |
… | |
24 | #include "cfperl.h" |
24 | #include "cfperl.h" |
25 | |
25 | |
26 | thread evthread; |
26 | thread evthread; |
27 | static ev_loop *loop; |
27 | static ev_loop *loop; |
28 | |
28 | |
29 | #define TICK_CYCLES 5 |
29 | #define TICK_CYCLES 4 |
|
|
30 | |
|
|
31 | // the tick watcher is activated every TICK_CYCLES |
|
|
32 | ev_async tick_watcher; |
|
|
33 | EV_ATOMIC_T coroapi::cede_pending; |
30 | |
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 |
32 | static ev_periodic cede_watcher; |
36 | static ev_periodic cede_watcher; |
33 | // the tick watcher is activated every TICK_CYCLES |
|
|
34 | ev_async tick_watcher; |
|
|
35 | |
37 | |
36 | EV_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. |
40 | static SMUTEX (evthread_mutex); |
42 | static SMUTEX (evthread_mutex); |
41 | static NV next_tick; |
43 | static 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 | |
… | |
… | |
71 | tick_cb (EV_P_ ev_async *w, int revents) |
73 | tick_cb (EV_P_ ev_async *w, int revents) |
72 | { |
74 | { |
73 | cfperl_tick (); |
75 | cfperl_tick (); |
74 | } |
76 | } |
75 | |
77 | |
|
|
78 | ///////////////////////////////////////////////////////////////////////////// |
|
|
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 | IV reqs; |
|
|
89 | |
|
|
90 | CALL_BEGIN (0); |
|
|
91 | CALL_CALL ("IO::AIO::poll_cb", G_SCALAR); |
|
|
92 | |
|
|
93 | reqs = POPi; |
|
|
94 | if (count > 0 && reqs < 0) |
|
|
95 | ev_idle_start (EV_A, w); |
|
|
96 | else |
|
|
97 | ev_idle_stop (EV_A, w); |
|
|
98 | |
|
|
99 | CALL_END; |
|
|
100 | } |
|
|
101 | |
|
|
102 | static void |
|
|
103 | async_cb (EV_P_ ev_async *w, int revents) |
|
|
104 | { |
|
|
105 | aio2_cb (EV_A, &aio2_watcher, 0); |
|
|
106 | } |
|
|
107 | |
|
|
108 | static void |
|
|
109 | aio1_cb (EV_P_ ev_io *w, int revents) |
|
|
110 | { |
|
|
111 | char dummy; |
|
|
112 | |
|
|
113 | if (read (w->fd, &dummy, 1) > 0) |
|
|
114 | { |
|
|
115 | ev_async_send (EV_DEFAULT_UC, &async_watcher); |
|
|
116 | coroapi::cede_pending = 1; |
|
|
117 | } |
|
|
118 | } |
|
|
119 | |
|
|
120 | ///////////////////////////////////////////////////////////////////////////// |
|
|
121 | |
76 | static void * |
122 | static void * |
77 | evthread_proc (void *arg) |
123 | evthread_proc (void *arg) |
78 | { |
124 | { |
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); |
125 | ev_loop (loop, 0); |
89 | |
126 | |
90 | return 0; |
127 | return 0; |
91 | } |
128 | } |
92 | |
129 | |
93 | void evthread_start () |
130 | void evthread_start (int aiofd) |
94 | { |
131 | { |
95 | I_EV_API ("evthread"); |
132 | I_EV_API ("evthread"); |
|
|
133 | |
|
|
134 | // main loop |
|
|
135 | ev_async_init (&tick_watcher, tick_cb); |
|
|
136 | ev_set_priority (&tick_watcher, 1); |
|
|
137 | ev_async_start (EV_DEFAULT_UC, &tick_watcher); |
|
|
138 | |
|
|
139 | ev_async_init (&async_watcher, async_cb); |
|
|
140 | ev_set_priority (&async_watcher, 1); |
|
|
141 | ev_async_start (EV_DEFAULT_UC, &async_watcher); |
|
|
142 | |
|
|
143 | ev_idle_init (&aio2_watcher, aio2_cb); |
|
|
144 | |
|
|
145 | // secondary loop |
|
|
146 | loop = ev_loop_new (0); |
|
|
147 | |
|
|
148 | ev_periodic_init (&cede_watcher, cede_cb, 0, TICK / TICK_CYCLES, 0); |
|
|
149 | ev_periodic_start (loop, &cede_watcher); |
|
|
150 | |
|
|
151 | ev_io_init (&aio1_watcher, aio1_cb, aiofd, EV_READ); |
|
|
152 | ev_io_start (loop, &aio1_watcher); |
|
|
153 | |
96 | evthread.start (evthread_proc); |
154 | evthread.start (evthread_proc); |
97 | } |
155 | } |
98 | |
156 | |
99 | |
157 | |
100 | |
158 | |