unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* [PATCH] Support threads in modules
@ 2017-04-22 15:24 Philipp Stephani
  2017-04-22 19:09 ` Eli Zaretskii
  0 siblings, 1 reply; 20+ messages in thread
From: Philipp Stephani @ 2017-04-22 15:24 UTC (permalink / raw)
  To: emacs-devel; +Cc: Philipp Stephani

Rather than checking for the main thread, store the owning thread in
the module structures and check for it.

* emacs-module.c (check_thread): New function.
(MODULE_FUNCTION_BEGIN, module_get_environment)
(module_non_local_exit_check, module_non_local_exit_clear)
(module_non_local_exit_get, module_non_local_exit_signal)
(module_non_local_exit_throw, module_is_not_nil, module_eq): Use it.
(initialize_environment): Initialize thread.
---
 src/emacs-module.c | 40 +++++++++++++++++++++++++---------------
 1 file changed, 25 insertions(+), 15 deletions(-)

diff --git a/src/emacs-module.c b/src/emacs-module.c
index 1b445dcc3b..22a0ed44f7 100644
--- a/src/emacs-module.c
+++ b/src/emacs-module.c
@@ -29,6 +29,7 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #include "dynlib.h"
 #include "coding.h"
 #include "syssignal.h"
+#include "thread.h"
 
 #include <intprops.h>
 #include <verify.h>
@@ -86,6 +87,9 @@ struct emacs_env_private
      storage is always available for them, even in an out-of-memory
      situation.  */
   Lisp_Object non_local_exit_symbol, non_local_exit_data;
+
+  /* Thread that owns this environment object. */
+  struct thread_state *thread;
 };
 
 /* The private parts of an `emacs_runtime' object contain the initial
@@ -106,7 +110,7 @@ static Lisp_Object module_format_fun_env (const struct module_fun_env *);
 static Lisp_Object value_to_lisp (emacs_value);
 static emacs_value lisp_to_value (Lisp_Object);
 static enum emacs_funcall_exit module_non_local_exit_check (emacs_env *);
-static void check_main_thread (void);
+static void check_thread (emacs_env *);
 static void finalize_environment (struct emacs_env_private *);
 static void initialize_environment (emacs_env *, struct emacs_env_private *priv);
 static void module_handle_signal (emacs_env *, Lisp_Object);
@@ -206,7 +210,7 @@ struct module_fun_env
 
    1. The first argument should always be a pointer to emacs_env.
 
-   2. Each function should first call check_main_thread.  Note that
+   2. Each function should first call check_thread.  Note that
       this function is a no-op unless Emacs was built with
       --enable-checking.
 
@@ -238,7 +242,7 @@ struct module_fun_env
    should be a sentinel value.  */
 
 #define MODULE_FUNCTION_BEGIN(error_retval)                             \
-  check_main_thread ();                                                 \
+  check_thread (env);                                                   \
   if (module_non_local_exit_check (env) != emacs_funcall_exit_return)   \
     return error_retval;                                                \
   MODULE_HANDLE_NONLOCAL_EXIT (error_retval)
@@ -256,8 +260,9 @@ CHECK_USER_PTR (Lisp_Object obj)
 static emacs_env *
 module_get_environment (struct emacs_runtime *ert)
 {
-  check_main_thread ();
-  return &ert->private_members->pub;
+  emacs_env *env = &ert->private_members->pub;
+  check_thread (env);
+  return env;
 }
 
 /* To make global refs (GC-protected global values) keep a hash that
@@ -318,21 +323,21 @@ module_free_global_ref (emacs_env *env, emacs_value ref)
 static enum emacs_funcall_exit
 module_non_local_exit_check (emacs_env *env)
 {
-  check_main_thread ();
+  check_thread (env);
   return env->private_members->pending_non_local_exit;
 }
 
 static void
 module_non_local_exit_clear (emacs_env *env)
 {
-  check_main_thread ();
+  check_thread (env);
   env->private_members->pending_non_local_exit = emacs_funcall_exit_return;
 }
 
 static enum emacs_funcall_exit
 module_non_local_exit_get (emacs_env *env, emacs_value *sym, emacs_value *data)
 {
-  check_main_thread ();
+  check_thread (env);
   struct emacs_env_private *p = env->private_members;
   if (p->pending_non_local_exit != emacs_funcall_exit_return)
     {
@@ -347,7 +352,7 @@ module_non_local_exit_get (emacs_env *env, emacs_value *sym, emacs_value *data)
 static void
 module_non_local_exit_signal (emacs_env *env, emacs_value sym, emacs_value data)
 {
-  check_main_thread ();
+  check_thread (env);
   if (module_non_local_exit_check (env) == emacs_funcall_exit_return)
     module_non_local_exit_signal_1 (env, value_to_lisp (sym),
 				    value_to_lisp (data));
@@ -356,7 +361,7 @@ module_non_local_exit_signal (emacs_env *env, emacs_value sym, emacs_value data)
 static void
 module_non_local_exit_throw (emacs_env *env, emacs_value tag, emacs_value value)
 {
-  check_main_thread ();
+  check_thread (env);
   if (module_non_local_exit_check (env) == emacs_funcall_exit_return)
     module_non_local_exit_throw_1 (env, value_to_lisp (tag),
 				   value_to_lisp (value));
@@ -448,7 +453,7 @@ module_type_of (emacs_env *env, emacs_value value)
 static bool
 module_is_not_nil (emacs_env *env, emacs_value value)
 {
-  check_main_thread ();
+  check_thread (env);
   if (module_non_local_exit_check (env) != emacs_funcall_exit_return)
     return false;
   return ! NILP (value_to_lisp (value));
@@ -457,7 +462,7 @@ module_is_not_nil (emacs_env *env, emacs_value value)
 static bool
 module_eq (emacs_env *env, emacs_value a, emacs_value b)
 {
-  check_main_thread ();
+  check_thread (env);
   if (module_non_local_exit_check (env) != emacs_funcall_exit_return)
     return false;
   return EQ (value_to_lisp (a), value_to_lisp (b));
@@ -743,12 +748,16 @@ usage: (module-call ENVOBJ &rest ARGLIST)   */)
 /* Helper functions.  */
 
 static void
-check_main_thread (void)
+check_thread (emacs_env *env)
 {
+  struct thread_state *thread = env->private_members->thread;
+  eassert (thread != NULL);
+  eassert (current_thread != NULL);
+  eassert (current_thread == thread);
 #ifdef HAVE_PTHREAD
-  eassert (pthread_equal (pthread_self (), main_thread_id));
+  eassert (pthread_equal (pthread_self (), current_thread->thread_id));
 #elif defined WINDOWSNT
-  eassert (GetCurrentThreadId () == dwMainThreadId);
+  eassert (GetCurrentThreadId () == current_thread->thread_id);
 #endif
 }
 
@@ -902,6 +911,7 @@ static void
 initialize_environment (emacs_env *env, struct emacs_env_private *priv)
 {
   priv->pending_non_local_exit = emacs_funcall_exit_return;
+  priv->thread = current_thread;
   env->size = sizeof *env;
   env->private_members = priv;
   env->make_global_ref = module_make_global_ref;
-- 
2.12.2




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

end of thread, other threads:[~2017-06-12 15:04 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-04-22 15:24 [PATCH] Support threads in modules Philipp Stephani
2017-04-22 19:09 ` Eli Zaretskii
2017-04-22 19:21   ` Philipp Stephani
2017-04-22 19:49     ` Eli Zaretskii
2017-04-22 20:05       ` Philipp Stephani
2017-04-22 20:14         ` Eli Zaretskii
2017-04-23  6:14           ` John Wiegley
2017-04-23 12:40             ` Stefan Monnier
2017-04-26  5:23               ` Eli Zaretskii
2017-04-23 15:54             ` Philipp Stephani
2017-04-23 15:52           ` Philipp Stephani
2017-04-26  5:31             ` Eli Zaretskii
2017-06-10 11:38               ` Philipp Stephani
2017-06-10 12:38                 ` Eli Zaretskii
2017-06-10 19:58                   ` Philipp Stephani
2017-06-11 13:25                     ` Philipp Stephani
2017-06-11 14:54                       ` Eli Zaretskii
2017-06-11 17:12                         ` Philipp Stephani
2017-06-11 15:01                     ` Eli Zaretskii
2017-06-12 15:04                       ` Philipp Stephani

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).