… | |
… | |
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 | void cfperl_cleanup (int make_core); |
|
|
67 | |
|
|
68 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
|
69 | |
|
|
70 | // virtual server time, excluding time jumps and lag |
|
|
71 | extern double runtime; |
41 | |
72 | |
42 | enum event_klass |
73 | enum event_klass |
43 | { |
74 | { |
44 | KLASS_NONE, |
75 | KLASS_NONE, |
45 | KLASS_GLOBAL, |
76 | KLASS_GLOBAL, |
… | |
… | |
89 | #define RESULT_INT(idx) RESULT(idx, INT) |
120 | #define RESULT_INT(idx) RESULT(idx, INT) |
90 | |
121 | |
91 | double cfperl_result_DOUBLE (int idx); |
122 | double cfperl_result_DOUBLE (int idx); |
92 | int cfperl_result_INT (int idx); |
123 | int cfperl_result_INT (int idx); |
93 | |
124 | |
|
|
125 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
|
126 | |
|
|
127 | INTERFACE_CLASS (attachable) |
94 | struct attachable |
128 | struct attachable |
95 | { |
129 | { |
|
|
130 | static MGVTBL vtbl; |
|
|
131 | |
96 | static unordered_vector<attachable *> mortals; |
132 | static unordered_vector<attachable *> mortals; |
97 | static void check_mortals (); |
133 | MTH static void check_mortals (); |
98 | |
134 | |
99 | enum { |
135 | enum { |
100 | F_DESTROYED = 0x01, |
136 | F_DESTROYED = 0x01, |
101 | F_BORROWED = 0x02, |
137 | F_DEBUG_TRACE = 0x02, |
102 | }; |
138 | }; |
103 | |
139 | |
104 | // object is delete'd after the refcount reaches 0 |
140 | // object is delete'd after the refcount reaches 0 |
|
|
141 | int ACC (RW, flags); |
105 | mutable int refcnt; |
142 | mutable int ACC (RW, refcnt); |
106 | int flags; |
|
|
107 | |
143 | |
108 | void refcnt_inc () const { ++refcnt; } |
144 | MTH void refcnt_inc () const { ++refcnt; } |
109 | void refcnt_dec () const { --refcnt; } |
145 | MTH void refcnt_dec () const { --refcnt; } |
110 | |
146 | |
|
|
147 | MTH int refcnt_cnt () const; |
111 | // check wether the object has died and destroy |
148 | // check wether the object has died and destroy |
112 | void refcnt_chk () { if (refcnt <= 0) do_check (); } |
149 | MTH void refcnt_chk () { if (refcnt <= 0) do_check (); } |
113 | |
150 | |
114 | // destroy the object unless it was already destroyed |
151 | // destroy the object unless it was already destroyed |
115 | // this politely asks everybody interested the reduce |
152 | // this politely asks everybody interested the reduce |
116 | // the refcount to 0 as soon as possible. |
153 | // the refcount to 0 as soon as possible. |
117 | void destroy (); |
154 | MTH void destroy (); |
118 | |
155 | |
119 | // return wether an object was destroyed already |
156 | // return wether an object was destroyed already |
120 | bool destroyed () const { return flags & F_DESTROYED; } |
157 | MTH bool destroyed () const { return flags & F_DESTROYED; } |
121 | |
158 | |
122 | virtual void gather_callbacks (AV *&callbacks, event_type event) const; |
159 | virtual void gather_callbacks (AV *&callbacks, event_type event) const; |
123 | |
160 | |
124 | #if 0 |
161 | #if 0 |
125 | private: |
162 | private: |
… | |
… | |
129 | |
166 | |
130 | HV *self; // CF+ perl self |
167 | HV *self; // CF+ perl self |
131 | AV *cb; // CF+ callbacks |
168 | AV *cb; // CF+ callbacks |
132 | shstr attach; // generic extension attachment information |
169 | shstr attach; // generic extension attachment information |
133 | |
170 | |
|
|
171 | void sever_self (); // sever this object from its self, if it has one. |
134 | void optimise (); // possibly save some memory by destroying unneeded data |
172 | void optimise (); // possibly save some memory by destroying unneeded data |
135 | |
173 | |
136 | attachable () |
174 | attachable () |
137 | : flags (0), refcnt (0), self (0), cb (0), attach (0) |
175 | : flags (0), refcnt (0), self (0), cb (0), attach (0) |
138 | { |
176 | { |
139 | } |
177 | } |
… | |
… | |
146 | virtual ~attachable (); |
184 | virtual ~attachable (); |
147 | |
185 | |
148 | attachable &operator =(const attachable &src); |
186 | attachable &operator =(const attachable &src); |
149 | |
187 | |
150 | bool invoke (event_type event, ...); |
188 | bool invoke (event_type event, ...); |
151 | void instantiate (); |
189 | MTH void instantiate (); |
152 | void reattach (); |
190 | void reattach (); |
153 | |
191 | |
154 | protected: |
192 | protected: |
155 | // do the real refcount checking work |
193 | // do the real refcount checking work |
156 | void do_check (); |
194 | void do_check (); |
… | |
… | |
165 | void gather_callbacks (AV *&callbacks, event_type event) const; |
203 | void gather_callbacks (AV *&callbacks, event_type event) const; |
166 | }; |
204 | }; |
167 | |
205 | |
168 | extern struct global gbl_ev; |
206 | extern struct global gbl_ev; |
169 | |
207 | |
|
|
208 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
|
209 | |
170 | struct object_freezer : dynbuf |
210 | struct object_freezer : dynbuf_text |
171 | { |
211 | { |
172 | AV *av; |
212 | AV *av; |
173 | |
213 | |
174 | object_freezer (); |
214 | object_freezer (); |
175 | ~object_freezer (); |
215 | ~object_freezer (); |
… | |
… | |
213 | } |
253 | } |
214 | |
254 | |
215 | *p = '\n'; |
255 | *p = '\n'; |
216 | } |
256 | } |
217 | |
257 | |
218 | void put (keyword k, const char *v) |
258 | void put (keyword k, const char *v = 0) |
219 | { |
259 | { |
220 | put_string (k, v); |
260 | put_string (k, v); |
221 | } |
261 | } |
222 | |
262 | |
223 | void put (keyword k, const shstr &v) |
263 | void put (keyword k, const shstr &v) |
… | |
… | |
288 | void put (keyword k, const refptr<T> &v) |
328 | void put (keyword k, const refptr<T> &v) |
289 | { |
329 | { |
290 | put (k, (T *)v); |
330 | put (k, (T *)v); |
291 | } |
331 | } |
292 | |
332 | |
293 | bool save (const char *filename); |
333 | bool save (const char *path); |
294 | char *as_string (); // like strdup |
334 | char *as_string (); // like strdup |
295 | |
335 | |
296 | operator bool () { return !!av; } |
336 | operator bool () { return !!av; } |
297 | }; |
337 | }; |
298 | |
338 | |
299 | // compatibility support, should be removed when no longer needed |
339 | // compatibility support, should be removed when no longer needed |
300 | int fprintf (object_freezer &freezer, const char *format, ...); |
340 | void fprintf (object_freezer &freezer, const char *format, ...); |
301 | int fputs (const char *s, object_freezer &freezer); |
341 | void fputs (const char *s, object_freezer &freezer); |
302 | |
342 | |
303 | struct object_thawer |
343 | struct object_thawer |
304 | { |
344 | { |
305 | SV *text; // text part |
345 | SV *text; // text part |
306 | AV *av; // perl part |
346 | AV *av; // perl part |
|
|
347 | int linenum; |
307 | char *line; // current beginning of line |
348 | char *line; // current beginning of line |
|
|
349 | keyword kw; |
308 | char *last_value; |
350 | char *kw_str, *value; |
|
|
351 | const char *name; |
309 | |
352 | |
310 | operator bool () { return !!text; } |
353 | operator bool () { return !!text; } |
311 | |
354 | |
312 | object_thawer (const char *filename = 0); |
355 | object_thawer (const char *path = 0); |
313 | object_thawer (const char *data, AV *perlav); |
356 | object_thawer (const char *data, AV *perlav); |
314 | ~object_thawer (); |
357 | ~object_thawer (); |
315 | |
358 | |
316 | void get (attachable *obj, int oid); |
359 | void get (attachable *obj, int oid); |
317 | |
360 | |
318 | keyword get_kv (); // also parse value for later use |
361 | // parse next line |
|
|
362 | void next (); |
|
|
363 | // skip the current key-value (usually fetch next line, for |
|
|
364 | // multiline-fields, skips untilt he corresponding end-kw |
|
|
365 | void skip (); |
319 | |
366 | |
|
|
367 | //TODO: remove, deprecated |
|
|
368 | keyword get_kv () |
|
|
369 | { |
|
|
370 | next (); |
|
|
371 | return kw; |
|
|
372 | } |
|
|
373 | |
320 | const char *get_str () { return last_value; } // may be 0 |
374 | char *get_str () { return value; } // may be 0 |
321 | |
375 | |
322 | void get (shstr &sh) const; |
376 | void get (shstr &sh) const; |
323 | void get_ornull (shstr &sh) const { sh = last_value; } |
377 | void get_ornull (shstr &sh) const { sh = value; } |
324 | void get_ml (keyword kend, shstr &sh); |
378 | void get_ml (keyword kend, shstr &sh); |
325 | |
379 | |
326 | sint32 get_sint32 () const; |
380 | sint32 get_sint32 () const; |
327 | sint64 get_sint64 () const; |
381 | sint64 get_sint64 () const; |
328 | double get_double () const; |
382 | double get_double () const; |
329 | |
383 | |
330 | void get (float &v) { v = get_double (); } |
384 | void get (float &v) { v = get_double (); } |
331 | void get (double &v) { v = get_double (); } |
385 | void get (double &v) { v = get_double (); } |
332 | |
386 | |
|
|
387 | void get (bool &i) { i = get_sint32 (); } |
333 | void get (sint8 &i) { i = get_sint32 (); } |
388 | void get (sint8 &i) { i = get_sint32 (); } |
334 | void get (uint8 &i) { i = get_sint32 (); } |
389 | void get (uint8 &i) { i = get_sint32 (); } |
335 | void get (sint16 &i) { i = get_sint32 (); } |
390 | void get (sint16 &i) { i = get_sint32 (); } |
336 | void get (uint16 &i) { i = get_sint32 (); } |
391 | void get (uint16 &i) { i = get_sint32 (); } |
337 | void get (sint32 &i) { i = get_sint32 (); } |
392 | void get (sint32 &i) { i = get_sint32 (); } |
338 | |
393 | |
339 | void get (uint32 &i) { i = get_sint64 (); } |
394 | void get (uint32 &i) { i = get_sint64 (); } |
340 | void get (sint64 &i) { i = get_sint64 (); } |
395 | void get (sint64 &i) { i = get_sint64 (); } |
341 | }; |
|
|
342 | |
396 | |
|
|
397 | bool parse_error (const char *type = 0, const char *name = 0, bool skip = true); |
|
|
398 | }; |
|
|
399 | |
|
|
400 | //TODO: remove |
343 | char *fgets (char *s, int n, object_thawer &thawer); |
401 | char *fgets (char *s, int n, object_thawer &thawer); |
|
|
402 | |
|
|
403 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
|
404 | |
|
|
405 | struct coroapi { |
|
|
406 | static struct CoroAPI *GCoroAPI; |
|
|
407 | |
|
|
408 | static int nready () { return CORO_NREADY; } |
|
|
409 | static int cede () { return CORO_CEDE ; } |
|
|
410 | |
|
|
411 | static double (*time)(); |
|
|
412 | static double next_cede; |
|
|
413 | static int cede_counter; |
|
|
414 | |
|
|
415 | static void do_cede_every (); |
|
|
416 | static void do_cede_to_tick (); |
|
|
417 | static void do_cede_to_tick_every (); |
|
|
418 | |
|
|
419 | static void cede_every (int count) |
|
|
420 | { |
|
|
421 | if (++cede_counter >= count) |
|
|
422 | do_cede_every (); |
|
|
423 | } |
|
|
424 | |
|
|
425 | static void cede_to_tick () |
|
|
426 | { |
|
|
427 | if (time () >= next_cede) |
|
|
428 | do_cede_to_tick (); |
|
|
429 | } |
|
|
430 | |
|
|
431 | static void cede_to_tick_every (int count) |
|
|
432 | { |
|
|
433 | if (++cede_counter >= count) |
|
|
434 | cede_to_tick (); |
|
|
435 | } |
|
|
436 | |
|
|
437 | static void wait_for_tick (); |
|
|
438 | static void wait_for_tick_begin (); |
|
|
439 | }; |
344 | |
440 | |
345 | struct watcher_base |
441 | struct watcher_base |
346 | { |
442 | { |
347 | static struct EventAPI *GEventAPI; |
443 | static struct EventAPI *GEventAPI; |
348 | }; |
444 | }; |