This is the mail archive of the guile@cygnus.com mailing list for the guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: SCM_DEFER/ALLOW_INTS


Aleksey Demakov <avd@gcom.ru> writes:

> Hello,
> 
> I'm currently writing a guile extension and I wonder how
> to use these SCM_DEFER_INTS and ACM_ALLOW_INTS properly.
> What do they actually do and what kind of code should have
> interrupts deferred?
> 

Basically, to prevent other threads and async events from mucking
about with your objects. For example, if you have a global list, you
should use a DEFER/ALLOW pair around operations that modify it. The
most typical case is with creating objects, where you should do:

allocated_foo_stuff = scm_must_malloc(sizeof(foo_stuff), "foo allocation");
SCM_NEWCELL(foo)
SCM_DEFER_INTS;
SCM_SETCAR(foo, tc16_foo);
SCM_SETCDR(foo, allocated_foo_stuff);
SCM_ALLOW_INTS;
return foo;

Note that, if you change the gc to not screw with freecells it traces
(this was one of the changes I made with the `improved' gc), you can
remove most of the DEFER/ALLOW pairs around this kind of operation,
provided that you always set the cdr first (so that the gc only sees a
free cell and ignores it, not an incomplete foo cell that it tries to
mark and free). 


-- 
Greg