From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Ihor Radchenko Newsgroups: gmane.emacs.devel Subject: Re: Concurrency via isolated process/thread Date: Sat, 08 Jul 2023 11:55:34 +0000 Message-ID: <87a5w6aa89.fsf@localhost> References: <871qhnr4ty.fsf@localhost> <87sfa28ura.fsf@localhost> <87cz16o8vz.fsf@yahoo.com> <87jzve8r4m.fsf@localhost> <871qhmo5nv.fsf@yahoo.com> <87bkgq8p5t.fsf@localhost> <831qhmjwk0.fsf@gnu.org> <875y6y8nlr.fsf@localhost> <87h6qhnalc.fsf@yahoo.com> <87ilax71wo.fsf@localhost> <831qhli14t.fsf@gnu.org> <87wmzdxewc.fsf@localhost> <83r0plgjeo.fsf@gnu.org> <87o7kpxapo.fsf@localhost> <83mt09gcaf.fsf@gnu.org> <87wmzbc3af.fsf@localhost> <83cz13g811.fsf@gnu.org> <87lefrbvjw.fsf@localhost> <83h6qfecxt.fsf@gnu.org> <875y6vbiej.fsf@localhost> <834jmeew2u.fsf@gnu.org> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="8652"; mail-complaints-to="usenet@ciao.gmane.io" Cc: luangruo@yahoo.com, emacs-devel@gnu.org To: Eli Zaretskii Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Sat Jul 08 13:56:24 2023 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1qI6Y3-000236-NH for ged-emacs-devel@m.gmane-mx.org; Sat, 08 Jul 2023 13:56:23 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qI6XK-0004P5-F3; Sat, 08 Jul 2023 07:55:38 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qI6XJ-0004Oh-08 for emacs-devel@gnu.org; Sat, 08 Jul 2023 07:55:37 -0400 Original-Received: from mout02.posteo.de ([185.67.36.66]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qI6XG-0001KJ-KS for emacs-devel@gnu.org; Sat, 08 Jul 2023 07:55:36 -0400 Original-Received: from submission (posteo.de [185.67.36.169]) by mout02.posteo.de (Postfix) with ESMTPS id 04604240103 for ; Sat, 8 Jul 2023 13:55:31 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.net; s=2017; t=1688817332; bh=L6BBhfe1Blb4+F5azYL/6IBBXXbLL8zw+PIaYwqU/cE=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version:From; b=gwUz6PP0csnIFSwhjsO3+1/9X2XfIZatBOTjgmNIazvpp1/Yif1np+4JpbZsCkZ3m vVI+HZ2BLhlhASt0HoEn6ZaOKjXGEiZdAwLSrKmqyHgpQbEmfcKgK+D6xJtgaL0ax8 yDvq0HZYL8OtwiMwbvrcGbhOvvrQ+j1LpKB9PQL/V40/oSTj2oGFmbp9waZibK6Dxf zNE2rrjkv8C6SnYtOITxMNXYh7ToQ8Y3B/UCWDX2AcWjijRa/zKuLLnZCOkdp09Esh Vl/vKVgmVZVxv1mEmsBLjZIfJeApsFoMoEd2NaNxCMFJlfHMS9md7H+PLmCzeMdoPn AQ33m/I150eww== Original-Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4QypcM1h7Gz6txS; Sat, 8 Jul 2023 13:55:30 +0200 (CEST) In-Reply-To: <834jmeew2u.fsf@gnu.org> Received-SPF: pass client-ip=185.67.36.66; envelope-from=yantar92@posteo.net; helo=mout02.posteo.de X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:307611 Archived-At: Eli Zaretskii writes: >> Does it mean that we can safely call, for example, Fcons asynchronously? > > Not necessarily, because Fcons is not just about allocating memory. > > I think we once again have a misunderstanding here, because when you > say "memory allocation" you mean something very different than I do, > which is a call to malloc to get more memory from the system. It > sounds like you think that Fcons _is_ memory allocation? But if so, > this terminology is so confusing that it is not useful in a detailed > technical discussion such as this one. We use the term "consing" to > refer to creation of Lisp objects, which includes memory allocation, > but also other stuff. > In particular, consing modifies memory blocks (already available to > Emacs, so no "memory allocation" per se) used to keep track of live > and dead Lisp objects, and those modifications cannot be concurrently > done by more than one thread, at least in some cases. Thanks for the clarification. I heard this term, but was unsure what exactly it refers to. >> > So your solution to each such problem is to lock variables? If so, >> > you will end up locking a lot of them, and how is this different from >> > using the global lock we do today with Lisp threads? >> >> The idea is to prevent simultaneous write, which will only lock for a >> small fraction of time. > > If one thread writes to a data structure, reading from it could also > need to block, or else the reader will risk getting inconsistent data. > So this is not just about simultaneous writing, it's much more > general. Sure. Of course, locking should be on write. May you elaborate what you mean by inconsistent data? >> And I still fail to see where base-buffer is _changed_. Is base buffer >> ever supposed to be changed? > > Another thread might change it while this thread examines it. I was able to identify a single place in C code where buffer's base buffer is being set: in make-indirect-buffer, when the buffer is just created. So, it is safe to assume that buffer->base_buffer remain constant for any given live buffer. Unless I miss something. >> No, I am saying that the current logic of updating the undo-list will not work >> when multiple async threads are involved. It will no longer be safe to >> assume that we can safely update undo-list right before/after switching >> current_buffer. >> >> So, I asked if an alternative approach could be used instead. > > Undo records changes in text properties and markers, and those are > different in the indirect buffers from the base buffers. Does this > explain why we cannot simply point to the base buffer? Are you sure? Text properties are certainly shared between indirect buffers. bset_undo_list (old_buf->base_buffer, BVAR (old_buf, undo_list)); INLINE void bset_undo_list (struct buffer *b, Lisp_Object val) { b->undo_list_ = val; } The markers that are not shared are pt_marker, begv_marker, and zv_marker. But those could probably be made attached to a given thread. >> >> /* Look down buffer's list of local Lisp variables >> >> to find and update any that forward into C variables. */ >> > >> > The C code accesses some buffer-local variables via Vfoo_bar C >> > variables. Those need to be updated when the current buffer changes. >> >> Now, when you explained this, it is also a big problem. Such C variables >> are a global state that needs to be kept up to date. Async will break >> the existing logic of these updates. > > Exactly. I now looked a bit further, and what you are talking about are the variables defined via DEFVAR_PER_BUFFER. These global variables have the following type: /* Forwarding pointer to a Lisp_Object variable. This is allowed only in the value cell of a symbol, and it means that the symbol's value really lives in the specified variable. */ struct Lisp_Objfwd { enum Lisp_Fwd_Type type; /* = Lisp_Fwd_Obj */ Lisp_Object *objvar; }; The code in set_buffer calls Fsymbol_value->find_symbol_value->swap_in_symval_forwarding for every symbol with C variable equivalent. These calls update internal pointer to Lisp object corresponding to variable value in current_buffer. If my understanding is correct, it should be safe to convert them into thread-local variables and update them within current thread when current_buffer (already thread-local) is altered. >> > Oh, yes, they will: see fetch_buffer_markers, called by >> > set_buffer_internal_2. >> >> Do you mean that in the existing cooperative Elisp threads, if one >> thread moves the point and yields to other thread, the other thread will >> be left with point in the same position (arbitrary, from the point of >> view of this other thread)? > > That's one problem, yes. There are others. Emacs Lisp uses point, > both explicitly and implicitly, all over the board. It is unthinkable > that a thread will find point not in a place where it last moved it. It is exactly what happens with current cooperative threads, AFAIK. Will it make sense to convert PT, ZV, and BEGV into thread-local variables? >> Is it buffer's marker list? I thought that you are referring to >> BUF_MARKERS, not to PT, BEGV, and ZV. > > Buffer's marker list are referenced in subroutines of > record_buffer_markers. Do you mean record_buffer_markers->set_marker_both->attach_marker-> if (m->buffer != b) { unchain_marker (m); m->buffer = b; m->next = BUF_MARKERS (b); BUF_MARKERS (b) = m; } But will this `if' ever trigger for PT, BEGV, and ZV? Also, it looks reasonable to block BUF_MARKERS when we need to change BUF_MARKERS. -- Ihor Radchenko // yantar92, Org mode contributor, Learn more about Org mode at . Support Org development at , or support my work at