summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWerner Koch <wk@gnupg.org>2016-11-11 20:26:49 +0100
committerWerner Koch <wk@gnupg.org>2016-11-11 20:28:09 +0100
commit25d463c67821901c8fd6736c815f11e85bbae66f (patch)
tree7034ee2e99db85225425fe2410654fd53f51dfb3
parentb1ccab5bf8e1206aae1307ad5d23890be4251c8f (diff)
downloadlibgpg-error-25d463c67821901c8fd6736c815f11e85bbae66f.tar.gz
Use the syscall clamp functions also for lock functions
* src/posix-lock.c (pre_lock_func, post_lock_func): New. (_gpgrt_lock_set_lock_clamp): New. (_gpgrt_lock_lock): Use clamp functions. * src/w32-lock.c (pre_lock_func, post_lock_func): New. (_gpgrt_lock_set_lock_clamp): New. (_gpgrt_lock_lock): Use clamp functions. * src/posix-lock.c (pre_syscall_func, post_syscall_func): New. (_gpgrt_thread_set_syscall_clamp): New. (_gpgrt_yield): Use clamp functions. * src/w32-lock.c (pre_syscall_func, post_syscall_func): New. (_gpgrt_thread_set_syscall_clamp): New. (_gpgrt_yield): Use clamp functions. * src/estream.c: Include lock.h and thread.h. (do_deinit): Call _gpgrt_lock_set_lock_clamp. (_gpgrt_set_syscall_clamp): Ditto. Signed-off-by: Werner Koch <wk@gnupg.org>
-rw-r--r--src/estream.c7
-rw-r--r--src/lock.h2
-rw-r--r--src/posix-lock.c22
-rw-r--r--src/posix-thread.c26
-rw-r--r--src/thread.h2
-rw-r--r--src/w32-lock.c24
-rw-r--r--src/w32-thread.c22
7 files changed, 104 insertions, 1 deletions
diff --git a/src/estream.c b/src/estream.c
index 95d7211..d0f0ba9 100644
--- a/src/estream.c
+++ b/src/estream.c
@@ -95,7 +95,8 @@
#include "gpgrt-int.h"
#include "estream-printf.h"
-
+#include "thread.h"
+#include "lock.h"
#ifndef O_BINARY
# define O_BINARY 0
@@ -564,6 +565,8 @@ do_deinit (void)
/* Reset the syscall clamp. */
pre_syscall_func = NULL;
post_syscall_func = NULL;
+ _gpgrt_thread_set_syscall_clamp (NULL, NULL);
+ _gpgrt_lock_set_lock_clamp (NULL, NULL);
}
@@ -598,6 +601,8 @@ _gpgrt_set_syscall_clamp (void (*pre)(void), void (*post)(void))
{
pre_syscall_func = pre;
post_syscall_func = post;
+ _gpgrt_thread_set_syscall_clamp (pre, post);
+ _gpgrt_lock_set_lock_clamp (pre, post);
}
diff --git a/src/lock.h b/src/lock.h
index a830b36..b7395db 100644
--- a/src/lock.h
+++ b/src/lock.h
@@ -20,5 +20,7 @@
#ifndef LOCK_H
#define LOCK_H
+void _gpgrt_lock_set_lock_clamp (void (*pre)(void), void (*post)(void));
+
#endif /*LOCK_H*/
diff --git a/src/posix-lock.c b/src/posix-lock.c
index 2e0ae92..d251d2f 100644
--- a/src/posix-lock.c
+++ b/src/posix-lock.c
@@ -44,6 +44,14 @@
#include "posix-lock-obj.h"
+/*
+ * Functions called before and after blocking syscalls.
+ * gpgrt_set_syscall_clamp is used to set them.
+ */
+static void (*pre_lock_func)(void);
+static void (*post_lock_func)(void);
+
+
#if USE_POSIX_THREADS
# if USE_POSIX_THREADS_WEAK
/* On ELF systems it is easy to use pthreads using weak
@@ -103,6 +111,16 @@ use_pthread_p (void)
#endif /*USE_POSIX_THREADS*/
+/* Helper to set the clamp functions. This is called as a helper from
+ * _gpgrt_set_syscall_clamp to keep the function pointers local. */
+void
+_gpgrt_lock_set_lock_clamp (void (*pre)(void), void (*post)(void))
+{
+ pre_lock_func = pre;
+ post_lock_func = post;
+}
+
+
static _gpgrt_lock_t *
get_lock_object (gpgrt_lock_t *lockhd)
@@ -171,9 +189,13 @@ _gpgrt_lock_lock (gpgrt_lock_t *lockhd)
#if USE_POSIX_THREADS
if (use_pthread_p())
{
+ if (pre_lock_func)
+ pre_lock_func ();
rc = pthread_mutex_lock (&lock->u.mtx);
if (rc)
rc = gpg_err_code_from_errno (rc);
+ if (post_lock_func)
+ post_lock_func ();
}
else
rc = 0; /* Threads are not used. */
diff --git a/src/posix-thread.c b/src/posix-thread.c
index 270dc91..00a43e2 100644
--- a/src/posix-thread.c
+++ b/src/posix-thread.c
@@ -43,18 +43,44 @@
#include "thread.h"
+/*
+ * Functions called before and after blocking syscalls.
+ * gpgrt_set_syscall_clamp is used to set them.
+ */
+static void (*pre_syscall_func)(void);
+static void (*post_syscall_func)(void);
+
+
+/* Helper to set the clamp functions. This is called as a helper from
+ * _gpgrt_set_syscall_clamp to keep the function pointers local. */
+void
+_gpgrt_thread_set_syscall_clamp (void (*pre)(void), void (*post)(void))
+{
+ pre_syscall_func = pre;
+ post_syscall_func = post;
+}
+
+
gpg_err_code_t
_gpgrt_yield (void)
{
#if USE_POSIX_THREADS
# ifdef _POSIX_PRIORITY_SCHEDULING
+ if (pre_syscall_func)
+ pre_syscall_func ();
sched_yield ();
+ if (post_syscall_func)
+ post_syscall_func ();
# else
return GPG_ERR_NOT_SUPPORTED;
# endif
#elif USE_SOLARIS_THREADS
+ if (pre_syscall_func)
+ pre_syscall_func ();
thr_yield ();
+ if (post_syscall_func)
+ post_syscall_func ();
#else
return GPG_ERR_NOT_SUPPORTED;
#endif
diff --git a/src/thread.h b/src/thread.h
index c650a99..f064cce 100644
--- a/src/thread.h
+++ b/src/thread.h
@@ -20,5 +20,7 @@
#ifndef THREAD_H
#define THREAD_H
+void _gpgrt_thread_set_syscall_clamp (void (*pre)(void), void (*post)(void));
+
#endif /*THREAD_H*/
diff --git a/src/w32-lock.c b/src/w32-lock.c
index d1decc9..51b13a1 100644
--- a/src/w32-lock.c
+++ b/src/w32-lock.c
@@ -37,6 +37,26 @@
#include "w32-lock-obj.h"
+
+/*
+ * Functions called before and after blocking syscalls.
+ * gpgrt_set_syscall_clamp is used to set them.
+ */
+static void (*pre_lock_func)(void);
+static void (*post_lock_func)(void);
+
+
+/* Helper to set the clamp functions. This is called as a helper from
+ * _gpgrt_set_syscall_clamp to keep the function pointers local. */
+void
+_gpgrt_lock_set_lock_clamp (void (*pre)(void), void (*post)(void))
+{
+ pre_lock_func = pre;
+ post_lock_func = post;
+}
+
+
+
static _gpgrt_lock_t *
get_lock_object (gpgrt_lock_t *lockhd)
{
@@ -101,7 +121,11 @@ _gpgrt_lock_lock (gpgrt_lock_t *lockhd)
}
}
+ if (pre_lock_func)
+ pre_lock_func ();
EnterCriticalSection (&lock->csec);
+ if (post_lock_func)
+ post_lock_func ();
return 0;
}
diff --git a/src/w32-thread.c b/src/w32-thread.c
index 6860075..aef421f 100644
--- a/src/w32-thread.c
+++ b/src/w32-thread.c
@@ -35,10 +35,32 @@
#include "thread.h"
+/*
+ * Functions called before and after blocking syscalls.
+ * gpgrt_set_syscall_clamp is used to set them.
+ */
+static void (*pre_syscall_func)(void);
+static void (*post_syscall_func)(void);
+
+
+/* Helper to set the clamp functions. This is called as a helper from
+ * _gpgrt_set_syscall_clamp to keep the function pointers local. */
+void
+_gpgrt_thread_set_syscall_clamp (void (*pre)(void), void (*post)(void))
+{
+ pre_syscall_func = pre;
+ post_syscall_func = post;
+}
+
+
gpg_err_code_t
_gpgrt_yield (void)
{
+ if (pre_syscall_func)
+ pre_syscall_func ();
Sleep (0);
+ if (post_syscall_func)
+ post_syscall_func ();
return 0;
}