[DGD] am I in atomic?

Erwin Harte harte at is-here.com
Thu Dec 21 22:00:28 CET 2006


Shentino wrote:
> Is there a way for a piece of code to determine if it's under an atomic 
> context?
> 
> I thought of doing a dummy write with a catch, but that's causing
> reentry in my errord when it logs.

When code is running inside an atomic context, execution cost (ticks) is 
  higher than when it runs 'normally.'

To see what I mean, examine this piece of code:

   # include <status.h>

   int count_em()
   {
       return status()[ST_TICKS] - status()[ST_TICKS];
   }

   atomic int ticks()
   {
       return count_em();
   }

   int ticks2()
   {
       return count_em();
   }

Now compare the results of the two functions:

   > compile atom.c
   $23 = </usr/nino/atom>
   > code $23->ticks()
   $24 = 340
   > code $23->ticks2()
   $25 = 170

So what you could do is figure out what the result of this function is 
in -your- code (it'll vary depending on what has or has not been 
precompiled, or what code may exist in a second level auto object, etc) 
and then a 'if (count_em() > tuned_cost) { ... } else { ... }' could 
tell you if you're running inside atomic context.

Recompute your tuned_cost when your game boots up and when you recompile 
the object in which you placed it.

Hope that helps, and good luck. :)

Erwin.



More information about the DGD mailing list