summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: bdbde94)
raw | patch | inline | side by side (parent: bdbde94)
author | Sebastian Harl <sh@tokkee.org> | |
Fri, 3 Feb 2012 13:57:05 +0000 (14:57 +0100) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Fri, 3 Feb 2012 14:04:30 +0000 (15:04 +0100) |
This function is a wrapper around pthread_create() which copies the plugin
context to the new thread. Else, that information would be lost.
context to the new thread. Else, that information would be lost.
src/plugin.c | patch | blob | history | |
src/plugin.h | patch | blob | history |
diff --git a/src/plugin.c b/src/plugin.c
index 7d66bd87670953cdfacbacd75ecfaeaa3cd4cc41..5387f9c4b8a0c6e8c6c6922dda3604400a8486d1 100644 (file)
--- a/src/plugin.c
+++ b/src/plugin.c
return (old);
} /* void plugin_set_ctx */
+typedef struct {
+ plugin_ctx_t ctx;
+ void *(*start_routine) (void *);
+ void *arg;
+} plugin_thread_t;
+
+static void *plugin_thread_start (void *arg)
+{
+ plugin_thread_t *plugin_thread = arg;
+
+ void *(*start_routine) (void *) = plugin_thread->start_routine;
+ void *plugin_arg = plugin_thread->arg;
+
+ plugin_set_ctx (plugin_thread->ctx);
+
+ free (plugin_thread);
+
+ return start_routine (plugin_arg);
+} /* void *plugin_thread_start */
+
+int plugin_thread_create (pthread_t *thread, const pthread_attr_t *attr,
+ void *(*start_routine) (void *), void *arg)
+{
+ plugin_thread_t *plugin_thread;
+
+ plugin_thread = malloc (sizeof (*plugin_thread));
+ if (plugin_thread == NULL)
+ return -1;
+
+ plugin_thread->ctx = plugin_get_ctx ();
+ plugin_thread->start_routine = start_routine;
+ plugin_thread->arg = arg;
+
+ return pthread_create (thread, attr,
+ plugin_thread_start, plugin_thread);
+} /* int plugin_thread_create */
+
/* vim: set sw=8 ts=8 noet fdm=marker : */
diff --git a/src/plugin.h b/src/plugin.h
index a2f7f098113f84a1453c0a37077e2e0bc224c37b..5036a63b56b1b783a7bc7a6cbc728a1cd0544c48 100644 (file)
--- a/src/plugin.h
+++ b/src/plugin.h
plugin_ctx_t plugin_get_ctx (void);
plugin_ctx_t plugin_set_ctx (plugin_ctx_t ctx);
+/*
+ * Context-aware thread management.
+ */
+
+int plugin_thread_create (pthread_t *thread, const pthread_attr_t *attr,
+ void *(*start_routine) (void *), void *arg);
+
#endif /* PLUGIN_H */