… | |
… | |
11 | #include <EXTERN.h> |
11 | #include <EXTERN.h> |
12 | #include <perl.h> |
12 | #include <perl.h> |
13 | #include <XSUB.h> |
13 | #include <XSUB.h> |
14 | |
14 | |
15 | #include <EventAPI.h> |
15 | #include <EventAPI.h> |
|
|
16 | #include <CoroAPI.h> |
16 | |
17 | |
|
|
18 | #include "util.h" |
|
|
19 | #include "keyword.h" |
|
|
20 | #include "dynbuf.h" |
17 | #include "callback.h" |
21 | #include "callback.h" |
|
|
22 | |
|
|
23 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
18 | |
24 | |
19 | // optimisations/workaround for functions requiring my_perl in scope (anti-bloat) |
25 | // optimisations/workaround for functions requiring my_perl in scope (anti-bloat) |
20 | #undef localtime |
26 | #undef localtime |
21 | #undef srand48 |
27 | #undef srand48 |
22 | #undef drand48 |
28 | #undef drand48 |
… | |
… | |
28 | #undef strerror |
34 | #undef strerror |
29 | |
35 | |
30 | // same here, massive symbol spamming |
36 | // same here, massive symbol spamming |
31 | #undef do_open |
37 | #undef do_open |
32 | #undef do_close |
38 | #undef do_close |
|
|
39 | #undef ref |
|
|
40 | #undef seed |
33 | |
41 | |
34 | // perl bug #40256: perl does overwrite those with reentrant versions |
42 | // perl bug #40256: perl does overwrite those with reentrant versions |
35 | // but does not initialise their state structures. |
43 | // but does not initialise their state structures. |
36 | #undef random |
44 | #undef random |
37 | #undef crypt |
45 | #undef crypt |
38 | |
46 | |
|
|
47 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
|
48 | |
|
|
49 | // some macros to simplify perl in-calls |
|
|
50 | |
|
|
51 | #define CHECK_ERROR \ |
|
|
52 | if (SvTRUE (ERRSV)) \ |
|
|
53 | LOG (llevError, "runtime error in %s: %s\n", __func__, SvPVutf8_nolen (ERRSV)); |
|
|
54 | |
|
|
55 | #define CALL_BEGIN(args) dSP; ENTER; SAVETMPS; PUSHMARK (SP); EXTEND (SP, args) |
|
|
56 | #define CALL_ARG_SV(sv) PUSHs (sv_2mortal (sv)) // separate because no refcount inc |
|
|
57 | #define CALL_ARG(expr) PUSHs (sv_2mortal (to_sv (expr))) |
|
|
58 | #define CALL_CALL(name, flags) PUTBACK; int count = call_pv (name, (flags) | G_EVAL); SPAGAIN; |
|
|
59 | #define CALL_END CHECK_ERROR; FREETMPS; LEAVE |
|
|
60 | |
|
|
61 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
|
62 | |
39 | void cfperl_init (); |
63 | void cfperl_init (); |
40 | void cfperl_main (); |
64 | void cfperl_main (); |
|
|
65 | void cfperl_emergency_save (); |
|
|
66 | |
|
|
67 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
|
68 | |
|
|
69 | // virtual server time, excluding time jumps and lag |
|
|
70 | extern double runtime; |
41 | |
71 | |
42 | enum event_klass |
72 | enum event_klass |
43 | { |
73 | { |
44 | KLASS_NONE, |
74 | KLASS_NONE, |
45 | KLASS_GLOBAL, |
75 | KLASS_GLOBAL, |
… | |
… | |
89 | #define RESULT_INT(idx) RESULT(idx, INT) |
119 | #define RESULT_INT(idx) RESULT(idx, INT) |
90 | |
120 | |
91 | double cfperl_result_DOUBLE (int idx); |
121 | double cfperl_result_DOUBLE (int idx); |
92 | int cfperl_result_INT (int idx); |
122 | int cfperl_result_INT (int idx); |
93 | |
123 | |
|
|
124 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
|
125 | |
|
|
126 | INTERFACE_CLASS (attachable) |
94 | struct attachable |
127 | struct attachable |
95 | { |
128 | { |
|
|
129 | static MGVTBL vtbl; |
|
|
130 | |
96 | static unordered_vector<attachable *> mortals; |
131 | static unordered_vector<attachable *> mortals; |
97 | static void check_mortals (); |
132 | MTH static void check_mortals (); |
98 | |
133 | |
99 | enum { |
134 | enum { |
100 | F_DESTROYED = 0x01, |
135 | F_DESTROYED = 0x01, |
101 | F_BORROWED = 0x02, |
136 | F_DEBUG_TRACE = 0x02, |
102 | }; |
137 | }; |
103 | |
138 | |
104 | // object is delete'd after the refcount reaches 0 |
139 | // object is delete'd after the refcount reaches 0 |
105 | mutable int refcnt; |
140 | mutable int ACC (RW, refcnt); |
106 | int flags; |
141 | int ACC (RW, flags); |
107 | |
142 | |
108 | void refcnt_inc () const { ++refcnt; } |
143 | MTH void refcnt_inc () const { ++refcnt; } |
109 | void refcnt_dec () const { --refcnt; } |
144 | MTH void refcnt_dec () const { --refcnt; } |
110 | |
145 | |
|
|
146 | MTH int refcnt_cnt () const; |
111 | // check wether the object has died and destroy |
147 | // check wether the object has died and destroy |
112 | void refcnt_chk () { if (refcnt <= 0) do_check (); } |
148 | MTH void refcnt_chk () { if (refcnt <= 0) do_check (); } |
113 | |
149 | |
114 | // destroy the object unless it was already destroyed |
150 | // destroy the object unless it was already destroyed |
115 | // this politely asks everybody interested the reduce |
151 | // this politely asks everybody interested the reduce |
116 | // the refcount to 0 as soon as possible. |
152 | // the refcount to 0 as soon as possible. |
117 | void destroy (); |
153 | MTH void destroy (); |
118 | |
154 | |
119 | // return wether an object was destroyed already |
155 | // return wether an object was destroyed already |
120 | bool destroyed () const { return flags & F_DESTROYED; } |
156 | MTH bool destroyed () const { return flags & F_DESTROYED; } |
121 | |
157 | |
122 | virtual void gather_callbacks (AV *&callbacks, event_type event) const; |
158 | virtual void gather_callbacks (AV *&callbacks, event_type event) const; |
123 | |
159 | |
124 | #if 0 |
160 | #if 0 |
125 | private: |
161 | private: |
… | |
… | |
129 | |
165 | |
130 | HV *self; // CF+ perl self |
166 | HV *self; // CF+ perl self |
131 | AV *cb; // CF+ callbacks |
167 | AV *cb; // CF+ callbacks |
132 | shstr attach; // generic extension attachment information |
168 | shstr attach; // generic extension attachment information |
133 | |
169 | |
|
|
170 | void sever_self (); // sever this object from its self, if it has one. |
134 | void optimise (); // possibly save some memory by destroying unneeded data |
171 | void optimise (); // possibly save some memory by destroying unneeded data |
135 | |
172 | |
136 | attachable () |
173 | attachable () |
137 | : flags (0), refcnt (0), self (0), cb (0), attach (0) |
174 | : flags (0), refcnt (0), self (0), cb (0), attach (0) |
138 | { |
175 | { |
139 | } |
176 | } |
… | |
… | |
146 | virtual ~attachable (); |
183 | virtual ~attachable (); |
147 | |
184 | |
148 | attachable &operator =(const attachable &src); |
185 | attachable &operator =(const attachable &src); |
149 | |
186 | |
150 | bool invoke (event_type event, ...); |
187 | bool invoke (event_type event, ...); |
151 | void instantiate (); |
188 | MTH void instantiate (); |
152 | void reattach (); |
189 | void reattach (); |
153 | |
190 | |
154 | protected: |
191 | protected: |
155 | // do the real refcount checking work |
192 | // do the real refcount checking work |
156 | void do_check (); |
193 | void do_check (); |
… | |
… | |
164 | { |
201 | { |
165 | void gather_callbacks (AV *&callbacks, event_type event) const; |
202 | void gather_callbacks (AV *&callbacks, event_type event) const; |
166 | }; |
203 | }; |
167 | |
204 | |
168 | extern struct global gbl_ev; |
205 | extern struct global gbl_ev; |
|
|
206 | |
|
|
207 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
169 | |
208 | |
170 | struct object_freezer : dynbuf |
209 | struct object_freezer : dynbuf |
171 | { |
210 | { |
172 | AV *av; |
211 | AV *av; |
173 | |
212 | |
… | |
… | |
213 | } |
252 | } |
214 | |
253 | |
215 | *p = '\n'; |
254 | *p = '\n'; |
216 | } |
255 | } |
217 | |
256 | |
218 | void put (keyword k, const char *v) |
257 | void put (keyword k, const char *v = 0) |
219 | { |
258 | { |
220 | put_string (k, v); |
259 | put_string (k, v); |
221 | } |
260 | } |
222 | |
261 | |
223 | void put (keyword k, const shstr &v) |
262 | void put (keyword k, const shstr &v) |
… | |
… | |
288 | void put (keyword k, const refptr<T> &v) |
327 | void put (keyword k, const refptr<T> &v) |
289 | { |
328 | { |
290 | put (k, (T *)v); |
329 | put (k, (T *)v); |
291 | } |
330 | } |
292 | |
331 | |
293 | bool save (const char *filename); |
332 | bool save (const char *path); |
294 | char *as_string (); // like strdup |
333 | char *as_string (); // like strdup |
295 | |
334 | |
296 | operator bool () { return !!av; } |
335 | operator bool () { return !!av; } |
297 | }; |
336 | }; |
298 | |
337 | |
… | |
… | |
302 | |
341 | |
303 | struct object_thawer |
342 | struct object_thawer |
304 | { |
343 | { |
305 | SV *text; // text part |
344 | SV *text; // text part |
306 | AV *av; // perl part |
345 | AV *av; // perl part |
|
|
346 | int linenum; |
307 | char *line; // current beginning of line |
347 | char *line; // current beginning of line |
308 | char *last_value; |
348 | char *last_keyword, *last_value; |
|
|
349 | const char *name; |
309 | |
350 | |
310 | operator bool () { return !!text; } |
351 | operator bool () { return !!text; } |
311 | |
352 | |
312 | object_thawer (const char *filename = 0); |
353 | object_thawer (const char *path = 0); |
313 | object_thawer (const char *data, AV *perlav); |
354 | object_thawer (const char *data, AV *perlav); |
314 | ~object_thawer (); |
355 | ~object_thawer (); |
315 | |
356 | |
316 | void get (attachable *obj, int oid); |
357 | void get (attachable *obj, int oid); |
317 | |
358 | |
318 | keyword get_kv (); // also parse value for later use |
359 | keyword get_kv (); // also parse value for later use |
|
|
360 | void skip_kv (keyword kw); |
319 | |
361 | |
320 | const char *get_str () { return last_value; } // may be 0 |
362 | const char *get_str () { return last_value; } // may be 0 |
321 | |
363 | |
322 | void get (shstr &sh) const; |
364 | void get (shstr &sh) const; |
323 | void get_ornull (shstr &sh) const { sh = last_value; } |
365 | void get_ornull (shstr &sh) const { sh = last_value; } |
… | |
… | |
325 | |
367 | |
326 | sint32 get_sint32 () const; |
368 | sint32 get_sint32 () const; |
327 | sint64 get_sint64 () const; |
369 | sint64 get_sint64 () const; |
328 | double get_double () const; |
370 | double get_double () const; |
329 | |
371 | |
330 | void get (float &v) { v = get_double (); } |
372 | void get (float &v) { v = get_double (); } |
331 | void get (double &v) { v = get_double (); } |
373 | void get (double &v) { v = get_double (); } |
332 | |
374 | |
|
|
375 | void get (bool &i) { i = get_sint32 (); } |
333 | void get (sint8 &i) { i = get_sint32 (); } |
376 | void get (sint8 &i) { i = get_sint32 (); } |
334 | void get (uint8 &i) { i = get_sint32 (); } |
377 | void get (uint8 &i) { i = get_sint32 (); } |
335 | void get (sint16 &i) { i = get_sint32 (); } |
378 | void get (sint16 &i) { i = get_sint32 (); } |
336 | void get (uint16 &i) { i = get_sint32 (); } |
379 | void get (uint16 &i) { i = get_sint32 (); } |
337 | void get (sint32 &i) { i = get_sint32 (); } |
380 | void get (sint32 &i) { i = get_sint32 (); } |
338 | |
381 | |
339 | void get (uint32 &i) { i = get_sint64 (); } |
382 | void get (uint32 &i) { i = get_sint64 (); } |
340 | void get (sint64 &i) { i = get_sint64 (); } |
383 | void get (sint64 &i) { i = get_sint64 (); } |
341 | }; |
|
|
342 | |
384 | |
|
|
385 | bool parse_error (keyword kw, const char *type = 0, const char *name = 0, bool skip = true); |
|
|
386 | }; |
|
|
387 | |
|
|
388 | //TODO: remove |
343 | char *fgets (char *s, int n, object_thawer &thawer); |
389 | char *fgets (char *s, int n, object_thawer &thawer); |
|
|
390 | |
|
|
391 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
|
392 | |
|
|
393 | struct coroapi { |
|
|
394 | static struct CoroAPI *GCoroAPI; |
|
|
395 | |
|
|
396 | static int nready () { return CORO_NREADY; } |
|
|
397 | static int cede () { return CORO_CEDE ; } |
|
|
398 | |
|
|
399 | static int cede_counter; |
|
|
400 | |
|
|
401 | static void cede_every (int count) |
|
|
402 | { |
|
|
403 | if (++cede_counter >= count) |
|
|
404 | { |
|
|
405 | cede_counter = 0; |
|
|
406 | |
|
|
407 | if (coroapi::nready ()) |
|
|
408 | coroapi::cede (); |
|
|
409 | } |
|
|
410 | } |
|
|
411 | }; |
344 | |
412 | |
345 | struct watcher_base |
413 | struct watcher_base |
346 | { |
414 | { |
347 | static struct EventAPI *GEventAPI; |
415 | static struct EventAPI *GEventAPI; |
348 | }; |
416 | }; |