From: Philipp Stephani <p.stephani2@gmail.com>
To: emacs-devel@gnu.org
Cc: Philipp Stephani <phst@google.com>
Subject: [PATCH] Support threads in modules
Date: Sat, 22 Apr 2017 17:24:44 +0200 [thread overview]
Message-ID: <20170422152444.48735-1-phst@google.com> (raw)
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
next reply other threads:[~2017-04-22 15:24 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-04-22 15:24 Philipp Stephani [this message]
2017-04-22 19:09 ` [PATCH] Support threads in modules 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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170422152444.48735-1-phst@google.com \
--to=p.stephani2@gmail.com \
--cc=emacs-devel@gnu.org \
--cc=phst@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.