[PATCH 03/11] Add an abstraction layer for threading in tests
Mike Hommey
mh+jemalloc at glandium.org
Wed Apr 18 09:29:42 PDT 2012
From: Mike Hommey <mh at glandium.org>
---
test/allocated.c | 29 +++++++++--------------------
test/jemalloc_test.h.in | 20 ++++++++++++++++++++
test/thread_arena.c | 18 +++++-------------
test/thread_tcache_enabled.c | 29 +++++++++--------------------
4 files changed, 43 insertions(+), 53 deletions(-)
diff --git a/test/allocated.c b/test/allocated.c
index 81cd4ca..3f889f4 100644
--- a/test/allocated.c
+++ b/test/allocated.c
@@ -2,7 +2,6 @@
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
-#include <pthread.h>
#include <assert.h>
#include <errno.h>
#include <string.h>
@@ -11,7 +10,7 @@
#include "jemalloc_test.h"
void *
-thread_start(void *arg)
+je_thread_start(void *arg)
{
int err;
void *p;
@@ -106,31 +105,21 @@ int
main(void)
{
int ret = 0;
- pthread_t thread;
+ je_thread_t thread;
malloc_printf("Test begin\n");
- thread_start(NULL);
+ je_thread_start(NULL);
- if (pthread_create(&thread, NULL, thread_start, NULL)
- != 0) {
- malloc_printf("%s(): Error in pthread_create()\n", __func__);
- ret = 1;
- goto label_return;
- }
- pthread_join(thread, (void *)&ret);
+ je_thread_create(&thread, je_thread_start, NULL);
+ je_thread_join(thread, (void *)&ret);
- thread_start(NULL);
+ je_thread_start(NULL);
- if (pthread_create(&thread, NULL, thread_start, NULL)
- != 0) {
- malloc_printf("%s(): Error in pthread_create()\n", __func__);
- ret = 1;
- goto label_return;
- }
- pthread_join(thread, (void *)&ret);
+ je_thread_create(&thread, je_thread_start, NULL);
+ je_thread_join(thread, (void *)&ret);
- thread_start(NULL);
+ je_thread_start(NULL);
label_return:
malloc_printf("Test end\n");
diff --git a/test/jemalloc_test.h.in b/test/jemalloc_test.h.in
index 58fa08e..cb1a89a 100644
--- a/test/jemalloc_test.h.in
+++ b/test/jemalloc_test.h.in
@@ -5,3 +5,23 @@
*/
#include "jemalloc/jemalloc at install_suffix@.h"
#include "jemalloc/internal/jemalloc_internal.h"
+
+/* Abstraction layer for threading in tests */
+#include <pthread.h>
+
+typedef pthread_t je_thread_t;
+
+void
+je_thread_create(je_thread_t *thread, void *(*proc)(void *), void *arg)
+{
+ if (pthread_create(thread, NULL, proc, arg) != 0) {
+ malloc_printf("Error in pthread_create()\n");
+ exit(1);
+ }
+}
+
+void
+je_thread_join(je_thread_t thread, void **ret)
+{
+ pthread_join(thread, ret);
+}
diff --git a/test/thread_arena.c b/test/thread_arena.c
index e443b71..9835428 100644
--- a/test/thread_arena.c
+++ b/test/thread_arena.c
@@ -1,6 +1,5 @@
#include <stdio.h>
#include <stdlib.h>
-#include <pthread.h>
#include <string.h>
#include <assert.h>
@@ -10,7 +9,7 @@
#define NTHREADS 10
void *
-thread_start(void *arg)
+je_thread_start(void *arg)
{
unsigned main_arena_ind = *(unsigned *)arg;
void *p;
@@ -52,7 +51,7 @@ main(void)
unsigned arena_ind;
size_t size;
int err;
- pthread_t threads[NTHREADS];
+ je_thread_t threads[NTHREADS];
unsigned i;
malloc_printf("Test begin\n");
@@ -72,18 +71,11 @@ main(void)
goto label_return;
}
- for (i = 0; i < NTHREADS; i++) {
- if (pthread_create(&threads[i], NULL, thread_start,
- (void *)&arena_ind) != 0) {
- malloc_printf("%s(): Error in pthread_create()\n",
- __func__);
- ret = 1;
- goto label_return;
- }
- }
+ for (i = 0; i < NTHREADS; i++)
+ je_thread_create(&threads[i], je_thread_start, (void *)&arena_ind);
for (i = 0; i < NTHREADS; i++)
- pthread_join(threads[i], (void *)&ret);
+ je_thread_join(threads[i], (void *)&ret);
label_return:
malloc_printf("Test end\n");
diff --git a/test/thread_tcache_enabled.c b/test/thread_tcache_enabled.c
index 59b76a2..e1cba43 100644
--- a/test/thread_tcache_enabled.c
+++ b/test/thread_tcache_enabled.c
@@ -1,7 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
-#include <pthread.h>
#include <assert.h>
#include <errno.h>
@@ -9,7 +8,7 @@
#include "jemalloc_test.h"
void *
-thread_start(void *arg)
+je_thread_start(void *arg)
{
int err;
size_t sz;
@@ -77,31 +76,21 @@ int
main(void)
{
int ret = 0;
- pthread_t thread;
+ je_thread_t thread;
malloc_printf("Test begin\n");
- thread_start(NULL);
+ je_thread_start(NULL);
- if (pthread_create(&thread, NULL, thread_start, NULL)
- != 0) {
- malloc_printf("%s(): Error in pthread_create()\n", __func__);
- ret = 1;
- goto label_return;
- }
- pthread_join(thread, (void *)&ret);
+ je_thread_create(&thread, je_thread_start, NULL);
+ je_thread_join(thread, (void *)&ret);
- thread_start(NULL);
+ je_thread_start(NULL);
- if (pthread_create(&thread, NULL, thread_start, NULL)
- != 0) {
- malloc_printf("%s(): Error in pthread_create()\n", __func__);
- ret = 1;
- goto label_return;
- }
- pthread_join(thread, (void *)&ret);
+ je_thread_create(&thread, je_thread_start, NULL);
+ je_thread_join(thread, (void *)&ret);
- thread_start(NULL);
+ je_thread_start(NULL);
label_return:
malloc_printf("Test end\n");
--
1.7.10
More information about the jemalloc-discuss
mailing list