ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/cfperl.h
(Generate patch)

Comparing deliantra/server/include/cfperl.h (file contents):
Revision 1.41 by root, Mon Dec 25 11:25:49 2006 UTC vs.
Revision 1.51 by root, Tue Jan 23 03:56:45 2007 UTC

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
39void cfperl_init (); 63void cfperl_init ();
40void cfperl_main (); 64void cfperl_main ();
65void cfperl_emergency_save ();
66
67//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
68
69// virtual server time, excluding time jumps and lag
70extern double runtime;
41 71
42enum event_klass 72enum 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
91double cfperl_result_DOUBLE (int idx); 121double cfperl_result_DOUBLE (int idx);
92int cfperl_result_INT (int idx); 122int cfperl_result_INT (int idx);
93 123
124//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
125
126INTERFACE_CLASS (attachable)
94struct attachable 127struct 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,
102 }; 136 };
103 137
104 // object is delete'd after the refcount reaches 0 138 // object is delete'd after the refcount reaches 0
105 mutable int refcnt; 139 mutable int ACC (RW, refcnt);
106 int flags; 140 int ACC (RW, flags);
107 141
108 void refcnt_inc () const { ++refcnt; } 142 MTH void refcnt_inc () const { ++refcnt; }
109 void refcnt_dec () const { --refcnt; } 143 MTH void refcnt_dec () const { --refcnt; }
110 144
145 MTH int refcnt_cnt () const;
111 // check wether the object has died and destroy 146 // check wether the object has died and destroy
112 void refcnt_chk () { if (refcnt <= 0) do_check (); } 147 MTH void refcnt_chk () { if (refcnt <= 0) do_check (); }
113 148
114 // destroy the object unless it was already destroyed 149 // destroy the object unless it was already destroyed
115 // this politely asks everybody interested the reduce 150 // this politely asks everybody interested the reduce
116 // the refcount to 0 as soon as possible. 151 // the refcount to 0 as soon as possible.
117 void destroy (); 152 MTH void destroy ();
118 153
119 // return wether an object was destroyed already 154 // return wether an object was destroyed already
120 bool destroyed () const { return flags & F_DESTROYED; } 155 MTH bool destroyed () const { return flags & F_DESTROYED; }
121 156
122 virtual void gather_callbacks (AV *&callbacks, event_type event) const; 157 virtual void gather_callbacks (AV *&callbacks, event_type event) const;
123 158
124#if 0 159#if 0
125private: 160private:
146 virtual ~attachable (); 181 virtual ~attachable ();
147 182
148 attachable &operator =(const attachable &src); 183 attachable &operator =(const attachable &src);
149 184
150 bool invoke (event_type event, ...); 185 bool invoke (event_type event, ...);
151 void instantiate (); 186 MTH void instantiate ();
152 void reattach (); 187 void reattach ();
153 188
154protected: 189protected:
155 // do the real refcount checking work 190 // do the real refcount checking work
156 void do_check (); 191 void do_check ();
164{ 199{
165 void gather_callbacks (AV *&callbacks, event_type event) const; 200 void gather_callbacks (AV *&callbacks, event_type event) const;
166}; 201};
167 202
168extern struct global gbl_ev; 203extern struct global gbl_ev;
204
205//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
169 206
170struct object_freezer : dynbuf 207struct object_freezer : dynbuf
171{ 208{
172 AV *av; 209 AV *av;
173 210
213 } 250 }
214 251
215 *p = '\n'; 252 *p = '\n';
216 } 253 }
217 254
218 void put (keyword k, const char *v) 255 void put (keyword k, const char *v = 0)
219 { 256 {
220 put_string (k, v); 257 put_string (k, v);
221 } 258 }
222 259
223 void put (keyword k, const shstr &v) 260 void put (keyword k, const shstr &v)
288 void put (keyword k, const refptr<T> &v) 325 void put (keyword k, const refptr<T> &v)
289 { 326 {
290 put (k, (T *)v); 327 put (k, (T *)v);
291 } 328 }
292 329
293 bool save (const char *filename); 330 bool save (const char *path);
294 char *as_string (); // like strdup 331 char *as_string (); // like strdup
295 332
296 operator bool () { return !!av; } 333 operator bool () { return !!av; }
297}; 334};
298 335
303struct object_thawer 340struct object_thawer
304{ 341{
305 SV *text; // text part 342 SV *text; // text part
306 AV *av; // perl part 343 AV *av; // perl part
307 char *line; // current beginning of line 344 char *line; // current beginning of line
308 char *last_value; 345 char *last_keyword, *last_value;
346 const char *name;
309 347
310 operator bool () { return !!text; } 348 operator bool () { return !!text; }
311 349
312 object_thawer (const char *filename = 0); 350 object_thawer (const char *path = 0);
313 object_thawer (const char *data, AV *perlav); 351 object_thawer (const char *data, AV *perlav);
314 ~object_thawer (); 352 ~object_thawer ();
315 353
316 void get (attachable *obj, int oid); 354 void get (attachable *obj, int oid);
317 355
318 keyword get_kv (); // also parse value for later use 356 keyword get_kv (); // also parse value for later use
357 void skip_kv (keyword kw);
319 358
320 const char *get_str () { return last_value; } // may be 0 359 const char *get_str () { return last_value; } // may be 0
321 360
322 void get (shstr &sh) const; 361 void get (shstr &sh) const;
323 void get_ornull (shstr &sh) const { sh = last_value; } 362 void get_ornull (shstr &sh) const { sh = last_value; }
325 364
326 sint32 get_sint32 () const; 365 sint32 get_sint32 () const;
327 sint64 get_sint64 () const; 366 sint64 get_sint64 () const;
328 double get_double () const; 367 double get_double () const;
329 368
330 void get (float &v) { v = get_double (); } 369 void get (float &v) { v = get_double (); }
331 void get (double &v) { v = get_double (); } 370 void get (double &v) { v = get_double (); }
332 371
372 void get (bool &i) { i = get_sint32 (); }
333 void get (sint8 &i) { i = get_sint32 (); } 373 void get (sint8 &i) { i = get_sint32 (); }
334 void get (uint8 &i) { i = get_sint32 (); } 374 void get (uint8 &i) { i = get_sint32 (); }
335 void get (sint16 &i) { i = get_sint32 (); } 375 void get (sint16 &i) { i = get_sint32 (); }
336 void get (uint16 &i) { i = get_sint32 (); } 376 void get (uint16 &i) { i = get_sint32 (); }
337 void get (sint32 &i) { i = get_sint32 (); } 377 void get (sint32 &i) { i = get_sint32 (); }
340 void get (sint64 &i) { i = get_sint64 (); } 380 void get (sint64 &i) { i = get_sint64 (); }
341}; 381};
342 382
343char *fgets (char *s, int n, object_thawer &thawer); 383char *fgets (char *s, int n, object_thawer &thawer);
344 384
385//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
386
387struct coroapi {
388 static struct CoroAPI *GCoroAPI;
389
390 static int nready () { return CORO_NREADY; }
391 static int cede () { return CORO_CEDE ; }
392
393 static int cede_counter;
394
395 static void cede_every (int count)
396 {
397 if (++cede_counter >= count)
398 {
399 cede_counter = 0;
400
401 if (coroapi::nready ())
402 coroapi::cede ();
403 }
404 }
405};
406
345struct watcher_base 407struct watcher_base
346{ 408{
347 static struct EventAPI *GEventAPI; 409 static struct EventAPI *GEventAPI;
348}; 410};
349 411

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines