How do you ensure an operation is atomic? I read in the book (p. 147)
that all BIFs are atomic, but I'm note sure how user-defined
operations can be made atomic. As a first bash, I have tried to cram
what needs to be done into one operation on one line. Here's my
handle_msg for swap_handlers:
handle_msg({swap_handlers, OldHandler, NewHandler}, LoopData) ->
case lists:keysearch(OldHandler, 1, LoopData) of
false ->
{{error, instance}, LoopData};
{value, {OldHandler, Data}} ->
NewLoopData = [ { NewHandler,
NewHandler:init(OldHandler:terminate(Data)) } |
lists:keydelete(OldHandler, 1, LoopData) ],
{ok, NewLoopData}
end;
The "NewLoopData = " line adds the new handler and deletes the old
handler in one list-building operation. Is it atomic?
With thanks and best wishes
Ivan
p.s.: And wouldn't it be simpler just to add the new handler before
deleting the old one? ;)
Does it really matter in this case? I mean even if there is preemption
of the process the event_manager has to receive a message and the then
dispatch the message to the handler, but before it can receive a new
message it has to complete what ever it was doing, it has to go back
to the loop. Also consider that the messages are async. So the
'send_event' message will wait in the queue till the event_manager
handles it.
Am I right?
Cheers,
--
Ale.
Thanks. This sounds right for the current application.
I'd still like ot know if there's a general way of guaranteeing
atomicity, or if (like here( it has to be worked out for each
application.
Best
Ivan
On Apr 4, 7:10 pm, Ale <peralta.alejan...@gmail.com> wrote:
> 2010/4/4 Ivan Uemlianin <i...@llaisdy.com>: