From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Eli Zaretskii Newsgroups: gmane.emacs.devel Subject: Re: Releasing the thread global_lock from the module API Date: Sat, 02 Mar 2024 08:43:21 +0200 Message-ID: <8634t9qgl2.fsf@gnu.org> References: <86cysdrja3.fsf@gnu.org> <86a5nhrdv0.fsf@gnu.org> <868r31rbxn.fsf@gnu.org> <865xy5r8e3.fsf@gnu.org> <864jdpr5zy.fsf@gnu.org> Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="29769"; mail-complaints-to="usenet@ciao.gmane.io" Cc: emacs-devel@gnu.org To: Spencer Baugh Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Sat Mar 02 07:44:08 2024 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 1rgJ6N-0007Z9-RL for ged-emacs-devel@m.gmane-mx.org; Sat, 02 Mar 2024 07:44:07 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rgJ5q-0006NL-0t; Sat, 02 Mar 2024 01:43:34 -0500 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 1rgJ5i-0006L8-Pl for emacs-devel@gnu.org; Sat, 02 Mar 2024 01:43:28 -0500 Original-Received: from fencepost.gnu.org ([2001:470:142:3::e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rgJ5h-0001Ly-Uz; Sat, 02 Mar 2024 01:43:25 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=References:Subject:In-Reply-To:To:From:Date: mime-version; bh=9aJHFRxAveMI62V2DCWNtBaRQ/Yp5msubin1wxb1qAI=; b=BHcRcaTZZvSP /pV+8CCg8Mktk5eX24o1mZgz9qHjjUzGPwsRNQpb9YIHBH8TEte4ipu73OckxY7zCbwcNDHeeIb17 arTaev+35AOPVHfAH7n9WIoTiOqDq18Q7Tak5dci6bAYPHVWXHsn8w41v+DZo/ZUZ12twll+FIEB8 5025Pbw3iIEQHLWfeN4cSKyzUGAkb5oYg8hlootcp5lmNHrJhgMoaM0Tb2QF6T1I4aSBtjUWgkwxF 9oi45EyVVatIOpps0E6pUs11yGt/jXNy67oLLAIriZwhWNQeBAWbPvOZo28nWomP51cOrc35i4IFC yM2ATntPm6QF2R2b0j8/ow==; In-Reply-To: (message from Spencer Baugh on Fri, 01 Mar 2024 16:56:55 -0500) 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:316691 Archived-At: > From: Spencer Baugh > Cc: emacs-devel@gnu.org > Date: Fri, 01 Mar 2024 16:56:55 -0500 > > Eli Zaretskii writes: > > >> Then I would release the lock and call into my library, which does some > >> useful work which takes a while. > > > > How is this different from starting your own native thread, then > > releasing the lock? > > They are completely different things. I think there's a misunderstanding here, because I cannot see how they could be "completely different things." See below. > > If a C library provides some blocking function which does some > > complicated form of IO, a module providing bindings for that C library > > can release global_lock before calling that function, and then > > re-acquire the lock after the function returns. Then Lisp threads > > calling this module function will not block the main Emacs thread. > > So, if I am running the following program in a Lisp thread: > > (while (do-emacs-things) > (let ((input (do-emacs-things))) > (let ((result (call-into-native-module input))) > (do-emacs-things result)))) > > and call-into-native-module unlocks the global lock around the calls > into my library, then during the part of call-into-native-module which > calls into my library, the main Emacs thread will not be blocked and can > run in parallel with call-into-native-module. I'm saying that your call-into-native-module method could do the following: . start a native thread running the call into your library . release the global lock by calling thread-yield or sleep-for or any other API which yields to other Lisp threads . wait for the native thread to finish . return when it succeeds to acquire the global lock following the completion of the native thread The _only_ difference between the above and what you described is that portions of call-into-native-module are run in a separate native thread, and the Lisp thread which runs the above snippet keeps yielding until the native thread finishes its job. How is this "completely different"? > > You didn't answer my question about doing this from a native thread. > > I hope my answer above clarifies it. I think it demonstrates some significant misunderstanding, which I how I have now clarified.