aboutsummaryrefslogtreecommitdiff
path: root/dep/src/jmalloc/mutex.c
diff options
context:
space:
mode:
authorraczman <none@none>2010-04-20 10:58:41 +0200
committerraczman <none@none>2010-04-20 10:58:41 +0200
commit1532a2f9a2b227e0011a81959403a3b0b05153ba (patch)
tree293e9bea38c5caee2fcef1c8cc1fcf38b505ece9 /dep/src/jmalloc/mutex.c
parentb34e2bdb1c988450d9737d7565f9d357edff4dc7 (diff)
Use jemalloc as memory allocator on linux.
In comparison to standard glibc allocator, jemalloc fargments adress space less, and scales linearly in multithreaded environment. Author: Jason Evans, mad props to him. --HG-- branch : trunk
Diffstat (limited to 'dep/src/jmalloc/mutex.c')
-rw-r--r--dep/src/jmalloc/mutex.c70
1 files changed, 70 insertions, 0 deletions
diff --git a/dep/src/jmalloc/mutex.c b/dep/src/jmalloc/mutex.c
new file mode 100644
index 00000000000..3b6081a4c4f
--- /dev/null
+++ b/dep/src/jmalloc/mutex.c
@@ -0,0 +1,70 @@
+#define JEMALLOC_MUTEX_C_
+#include "jemalloc/internal/jemalloc_internal.h"
+
+/******************************************************************************/
+/* Data. */
+
+#ifdef JEMALLOC_LAZY_LOCK
+bool isthreaded = false;
+#endif
+
+#ifdef JEMALLOC_LAZY_LOCK
+static void pthread_create_once(void);
+#endif
+
+/******************************************************************************/
+/*
+ * We intercept pthread_create() calls in order to toggle isthreaded if the
+ * process goes multi-threaded.
+ */
+
+#ifdef JEMALLOC_LAZY_LOCK
+static int (*pthread_create_fptr)(pthread_t *__restrict, const pthread_attr_t *,
+ void *(*)(void *), void *__restrict);
+
+static void
+pthread_create_once(void)
+{
+
+ pthread_create_fptr = dlsym(RTLD_NEXT, "pthread_create");
+ if (pthread_create_fptr == NULL) {
+ malloc_write("<jemalloc>: Error in dlsym(RTLD_NEXT, "
+ "\"pthread_create\")\n");
+ abort();
+ }
+
+ isthreaded = true;
+}
+
+JEMALLOC_ATTR(visibility("default"))
+int
+pthread_create(pthread_t *__restrict thread,
+ const pthread_attr_t *__restrict attr, void *(*start_routine)(void *),
+ void *__restrict arg)
+{
+ static pthread_once_t once_control = PTHREAD_ONCE_INIT;
+
+ pthread_once(&once_control, pthread_create_once);
+
+ return (pthread_create_fptr(thread, attr, start_routine, arg));
+}
+#endif
+
+/******************************************************************************/
+
+bool
+malloc_mutex_init(malloc_mutex_t *mutex)
+{
+ pthread_mutexattr_t attr;
+
+ if (pthread_mutexattr_init(&attr) != 0)
+ return (true);
+ pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ADAPTIVE_NP);
+ if (pthread_mutex_init(mutex, &attr) != 0) {
+ pthread_mutexattr_destroy(&attr);
+ return (true);
+ }
+ pthread_mutexattr_destroy(&attr);
+
+ return (false);
+}