From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Michael Albinus Newsgroups: gmane.emacs.devel Subject: Re: Return value of finished threads Date: Sat, 21 Jul 2018 11:39:17 +0200 Message-ID: <87wotphqre.fsf@gmx.de> References: <87h8kuq7kf.fsf@gmx.de> <87h8kt3hcj.fsf@tromey.com> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: blaine.gmane.org 1532165896 22664 195.159.176.226 (21 Jul 2018 09:38:16 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sat, 21 Jul 2018 09:38:16 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux) Cc: emacs-devel@gnu.org To: Tom Tromey Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Sat Jul 21 11:38:12 2018 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1fgoL5-0005mo-FD for ged-emacs-devel@m.gmane.org; Sat, 21 Jul 2018 11:38:11 +0200 Original-Received: from localhost ([::1]:51286 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fgoNA-0002xG-Ol for ged-emacs-devel@m.gmane.org; Sat, 21 Jul 2018 05:40:20 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:60204) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fgoMT-0002wz-IC for emacs-devel@gnu.org; Sat, 21 Jul 2018 05:39:38 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fgoMQ-0002GD-Ea for emacs-devel@gnu.org; Sat, 21 Jul 2018 05:39:37 -0400 Original-Received: from mout.gmx.net ([212.227.15.15]:46505) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fgoMQ-0002G1-38 for emacs-devel@gnu.org; Sat, 21 Jul 2018 05:39:34 -0400 Original-Received: from detlef.gmx.de ([178.20.95.32]) by mail.gmx.com (mrgmx002 [212.227.17.190]) with ESMTPSA (Nemesis) id 0MTCDO-1fYS5Q285W-00S9SP; Sat, 21 Jul 2018 11:39:18 +0200 In-Reply-To: <87h8kt3hcj.fsf@tromey.com> (Tom Tromey's message of "Fri, 20 Jul 2018 12:14:04 -0600") X-Provags-ID: V03:K1:WONS5qgYOj35vqSUnFtZY3aYiFEp6SWAEsWkZ6+mgjYKvfN6e9Y MmiKn7sox5J/wIm52ZVNTFBguY4bhDWV78Zi0ddQ5aGS3eiR4K8IJKqmh9LGswbUvTs/jbS ln8hfWOTzToFCcvhjp15cYkIOTFIZ+eZQZmzLod3WR2YhlhASTsM+emKHIEF5zRYWjXEvQj UVPX9at/9CNWZ2FxPpw1w== X-UI-Out-Filterresults: notjunk:1;V01:K0:YDnFk+XscVk=:PqAw7LWYkijXVV2PjEyQgf FXACgLjNNj0eNHBTZ8JMzr9GV/dERkMcYY50tfPKjDpfXF2D67pwbbKjkvX07TxGizs5bNTyy 9eiqzNsMRSlRUDKWYUkRIiw02f2Vf0j+dRLWT5p1oLIOIIJR5a7qavPcNooGDkw7TajtX1TiH qp1HxZ0772p016X1hHAi4svthOHHP/zY5oDL6oBkqWyYsbCVU/HwtzQdT2VXbpRZqL+Cj1x5P uW+U7JXrSb6JsIKWjtmy62y5wyfRxKqykrKhUwXn3zPsSdvMILEEX7MVNdZ46cTAEZdtiKzwh Z5ZAkWlJCgWtJwby07H+onjJ2w5tCi9K3bR56JwfBLdmVQnR/YF+fzXu+DVYTrXBAKsRuDFkn va3/GQFQc9Xun9qQ4AMzAX6Fu9OziRiFo26naclWJwBAnDxFLY4eK+LYeXoPabvf533c1vUJu QoUSzcq5FZTcNvUdF7wv5PyPDp+Y+f89HRQ9FqvZ/fCtevmSTsxESr6NDfHklUUPkxmKQRn0A aRpsTi/wezfLu86x8GkNKKP0nq3zvlEYJhMlyQXLQA0bDmb+Y3fbehP4NBgOe2nkKFOdE+LU/ WvI5U0uFmrVHu0LgGmC1M9ss7MdUcB62mW2JoeycFjAY0/8ZmyRj2Ceb5B+VUFguwCf4qEwbX OvBiSacOlrklstIx9uF483aNAnkhJ8/qonnl58uO+NLOPTPh28mJ26gMPIGXoInpS4X32KSWs wJpW2d2xCUx9y0/a5yYgzpiKFgBRq9GSMnmNugOR67FcYbNTU3DTbsirpL8= X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 212.227.15.15 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.21 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.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.org gmane.emacs.devel:227605 Archived-At: --=-=-= Content-Type: text/plain Tom Tromey writes: Hi Tom, > Michael> So I'm wondering if we could extend thread support by an > Michael> indication of the thread result, when finished. One idea > Michael> would be that `thread-join' returns with that value. > > This doesn't seem all that hard to implement. Yep. The appended patch does it for me. Maybe you could check whether I'm missing something? > Also if the thread exited due to an exception of some kind, this could > be propagated by thread-join as well. Yeah, this would be a bonus. I've failed to implement it, so I let it to somebody else. I don't need it first-hand. > Michael> The documentation recommends the use of global variables, > Michael> which would be inconvenient for many threads to supervise. > > Or you can use a queue of some kind and have each worker push its > result there. Yes, but you must indicate the worker threads where to push the result. Imagine find-file would support threads for wildcard file names, and somebody calls find-file this way twice, both running concurrently. Every worker thread must know, whether to push the result into the global variable associated with the first call or the second one. Still overhead to implement; I'll prefer that thread-join returns the result. > Tom Best regards, Michael. --=-=-= Content-Type: text/plain Content-Disposition: attachment diff --git a/src/thread.c b/src/thread.c index 754d286e9f..e2d16df7cb 100644 --- a/src/thread.c +++ b/src/thread.c @@ -681,7 +681,7 @@ invoke_thread_function (void) { ptrdiff_t count = SPECPDL_INDEX (); - Ffuncall (1, ¤t_thread->function); + current_thread->result = Ffuncall (1, ¤t_thread->function); return unbind_to (count, Qnil); } @@ -789,6 +789,7 @@ If NAME is given, it must be a string; it names the new thread. */) new_thread->m_last_thing_searched = Qnil; /* copy from parent? */ new_thread->m_saved_last_thing_searched = Qnil; new_thread->m_current_buffer = current_thread->m_current_buffer; + new_thread->result = Qnil; new_thread->error_symbol = Qnil; new_thread->error_data = Qnil; new_thread->event_object = Qnil; @@ -933,9 +934,9 @@ thread_join_callback (void *arg) DEFUN ("thread-join", Fthread_join, Sthread_join, 1, 1, 0, doc: /* Wait for THREAD to exit. -This blocks the current thread until THREAD exits or until -the current thread is signaled. -It is an error for a thread to try to join itself. */) +This blocks the current thread until THREAD exits or until the current +thread is signaled. It returns the result of the THREAD function. It +is an error for a thread to try to join itself. */) (Lisp_Object thread) { struct thread_state *tstate; @@ -949,7 +950,7 @@ It is an error for a thread to try to join itself. */) if (thread_alive_p (tstate)) flush_stack_call_func (thread_join_callback, tstate); - return Qnil; + return tstate->result; } DEFUN ("all-threads", Fall_threads, Sall_threads, 0, 0, 0, @@ -1017,6 +1018,7 @@ init_main_thread (void) main_thread.m_saved_last_thing_searched = Qnil; main_thread.name = Qnil; main_thread.function = Qnil; + main_thread.result = Qnil; main_thread.error_symbol = Qnil; main_thread.error_data = Qnil; main_thread.event_object = Qnil; @@ -1090,8 +1092,7 @@ syms_of_threads (void) DEFSYM (Qmutexp, "mutexp"); DEFSYM (Qcondition_variable_p, "condition-variable-p"); - DEFVAR_LISP ("main-thread", - Vmain_thread, + DEFVAR_LISP ("main-thread", Vmain_thread, doc: /* The main thread of Emacs. */); #ifdef THREADS_ENABLED XSETTHREAD (Vmain_thread, &main_thread); diff --git a/src/thread.h b/src/thread.h index c10e5ecb75..922eea6217 100644 --- a/src/thread.h +++ b/src/thread.h @@ -52,6 +52,9 @@ struct thread_state /* The thread's function. */ Lisp_Object function; + /* The thread's result, if function has finished. */ + Lisp_Object result; + /* If non-nil, this thread has been signaled. */ Lisp_Object error_symbol; Lisp_Object error_data; diff --git a/test/src/thread-tests.el b/test/src/thread-tests.el index a447fb3914..ddf9e987cc 100644 --- a/test/src/thread-tests.el +++ b/test/src/thread-tests.el @@ -100,9 +100,9 @@ threads-test-thread1 (progn (setq threads-test-global nil) (let ((thread (make-thread #'threads-test-thread1))) - (thread-join thread) - (and threads-test-global - (not (thread-alive-p thread))))))) + (and (= (thread-join thread) 23) + (= threads-test-global 23) + (not (thread-alive-p thread))))))) (ert-deftest threads-join-self () "Cannot `thread-join' the current thread." --=-=-=--