0

Given code such as

(defun some-code ()
  (url-retrieve my-url #'my-callback)
  (setq *a-global-var* (some-func *a-global-var*))
  ...)

(defun my-callback (status &rest args)
  (setq *a-global-var* (another-func *a-global-var*)))

How can I ensure that the two updates of *a-global-var* happen atomically? In other words, how can I prevent the callback from changing *a-global-var* while some-func is running? I searched the Emacs documentation for a while, but I didn't find anything on the semantics of asynchronous operations.

11
  • What do asynchronously executed callbacks have to do with atomicity? That aside, what definition of it do you use here? I'd be seriously concerned if the mere act of setting a variable were to fail halfway...
    – wasamasa
    Commented Jun 21, 2017 at 16:56
  • 1
    "I didn't find anything on the semantics of asynchronous operations." - Emacs doesn't have asynchronous operations, hence no semantics.
    – npostavs
    Commented Jun 21, 2017 at 18:16
  • can you just wrap it in atomic-change-group? gnu.org/software/emacs/manual/html_node/elisp/… Commented Jun 21, 2017 at 22:20
  • @JohnKitchin My understanding of atomic-change-group is that it's about changes to buffers, not updates to variable values.
    – khinsen
    Commented Jun 22, 2017 at 17:58
  • 1
    The comment of npostavs is important. url-retrieve uses url-http which uses sentinels for the callback. Sentinels only run when emacs is waiting for user input. See: the section Sentinels of the elisp-documentation. So you do not have the problem of non-atomic writes to a variable.
    – Tobias
    Commented Jun 23, 2017 at 9:14

1 Answer 1

1

@npostavs made this comment on the question:

"I didn't find anything on the semantics of asynchronous operations." - Emacs doesn't have asynchronous operations, hence no semantics.

That comment is important. url-retrieve uses url-http which uses sentinels for the callback. Sentinels only run when emacs is waiting for user input. See: the section Sentinels of the elisp-documentation. So you do not have the problem of non-atomic writes to a variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.