unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* elisp test-and-set?
@ 2018-05-25 21:00 Stephen Leake
  2018-05-26  0:38 ` Noam Postavsky
  2018-05-26  7:08 ` Eli Zaretskii
  0 siblings, 2 replies; 5+ messages in thread
From: Stephen Leake @ 2018-05-25 21:00 UTC (permalink / raw)
  To: emacs-devel

In ada-mode, I'm using a parser running in an external process to
compute faces and indentation.

Since faces are computed by font-lock, which runs on a timer in the
background, it can try to run when indent is running in the foreground.

So in the code that deals with the external process, I need mutual
exclusion; prevent font-lock from starting a new parse if a parse is
already running.

The code I currently have for that is:

  (if (wisi-process--parser-busy parser)
      (error "%s parse abandoned; parser busy" wisi--parse-action)

    (setf (wisi-process--parser-busy parser) t)
    ... more parsing stuff
    )

There is a tiny window of time when this can fail; if font-lock and
foreground both execute the "if" condition 'simultaneously' (ie, before
the other sets busy t), both will go on to parse.

In other contexts I'd use a "test and set" instruction for this; read
the flag and set it true in one uninterruptible action. I don't see
anything like that in elisp, and searching in the elisp manual for
"mutual" and "exclusion" is no help.

I also don't see "font-lock-inhibit", which could be useful here, but would
probably have the same race condition.

Is there a way to do this right?

-- 
-- Stephe



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: elisp test-and-set?
  2018-05-25 21:00 elisp test-and-set? Stephen Leake
@ 2018-05-26  0:38 ` Noam Postavsky
  2018-05-26  7:08 ` Eli Zaretskii
  1 sibling, 0 replies; 5+ messages in thread
From: Noam Postavsky @ 2018-05-26  0:38 UTC (permalink / raw)
  To: Stephen Leake; +Cc: emacs-devel

On 25 May 2018 at 17:00, Stephen Leake <stephen_leake@stephe-leake.org> wrote:

> Since faces are computed by font-lock, which runs on a timer in the
> background, it can try to run when indent is running in the foreground.

Only if the foreground indent function calls `accept-process-output',
`sit-for', or something like that.

> In other contexts I'd use a "test and set" instruction for this; read
> the flag and set it true in one uninterruptible action. I don't see
> anything like that in elisp, and searching in the elisp manual for
> "mutual" and "exclusion" is no help.

There is `(elisp) Mutexes' (as of Emacs 26), but even with threads
enabled, only a single Lisp thread runs at one time, so there's not
really a need for it at the moment.



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: elisp test-and-set?
  2018-05-25 21:00 elisp test-and-set? Stephen Leake
  2018-05-26  0:38 ` Noam Postavsky
@ 2018-05-26  7:08 ` Eli Zaretskii
  2018-05-26  7:53   ` Stephen Leake
  2018-05-26 21:14   ` Stefan Monnier
  1 sibling, 2 replies; 5+ messages in thread
From: Eli Zaretskii @ 2018-05-26  7:08 UTC (permalink / raw)
  To: Stephen Leake; +Cc: emacs-devel

> From: Stephen Leake <stephen_leake@stephe-leake.org>
> Date: Fri, 25 May 2018 16:00:04 -0500
> 
> In ada-mode, I'm using a parser running in an external process to
> compute faces and indentation.
> 
> Since faces are computed by font-lock, which runs on a timer in the
> background, it can try to run when indent is running in the foreground.

Do you have evidence of this?  Emacs is single-threaded, so only one
Lisp function can run at any given time.  I'm guessing you saw
something that led you to above conclusion, but I think the conclusion
is at least inaccurate if not incorrect.  So please tell more detail
about what you see, and probably about the implementation of that
parser.  E.g., are you running the parser from font-lock, or are you
running it from some other hook?



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: elisp test-and-set?
  2018-05-26  7:08 ` Eli Zaretskii
@ 2018-05-26  7:53   ` Stephen Leake
  2018-05-26 21:14   ` Stefan Monnier
  1 sibling, 0 replies; 5+ messages in thread
From: Stephen Leake @ 2018-05-26  7:53 UTC (permalink / raw)
  To: emacs-devel

Eli Zaretskii <eliz@gnu.org> writes:

>> From: Stephen Leake <stephen_leake@stephe-leake.org>
>> Date: Fri, 25 May 2018 16:00:04 -0500
>>
>> In ada-mode, I'm using a parser running in an external process to
>> compute faces and indentation.
>>
>> Since faces are computed by font-lock, which runs on a timer in the
>> background, it can try to run when indent is running in the foreground.
>
> Do you have evidence of this?

I thought I did, but it turned out there was a different problem.

> Emacs is single-threaded, so only one Lisp function can run at any
> given time.

Ok, I'll add a comment to this effect in the code.

Thanks.

--
-- Stephe



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: elisp test-and-set?
  2018-05-26  7:08 ` Eli Zaretskii
  2018-05-26  7:53   ` Stephen Leake
@ 2018-05-26 21:14   ` Stefan Monnier
  1 sibling, 0 replies; 5+ messages in thread
From: Stefan Monnier @ 2018-05-26 21:14 UTC (permalink / raw)
  To: emacs-devel

> Do you have evidence of this?  Emacs is single-threaded, so only one
> Lisp function can run at any given time.  I'm guessing you saw

And even with the new concurrency support, while it's not
quite single-threaded any more, its threading is cooperative.


        Stefan




^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2018-05-26 21:14 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-05-25 21:00 elisp test-and-set? Stephen Leake
2018-05-26  0:38 ` Noam Postavsky
2018-05-26  7:08 ` Eli Zaretskii
2018-05-26  7:53   ` Stephen Leake
2018-05-26 21:14   ` Stefan Monnier

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).