Add an abstraction layer for threading in tests

This commit is contained in:
Mike Hommey 2012-04-18 18:29:42 +02:00 committed by Jason Evans
parent 188da7c3f5
commit e38e45743f
4 changed files with 45 additions and 55 deletions

View File

@ -2,7 +2,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#include <pthread.h>
#include <assert.h> #include <assert.h>
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
@ -11,7 +10,7 @@
#include "jemalloc_test.h" #include "jemalloc_test.h"
void * void *
thread_start(void *arg) je_thread_start(void *arg)
{ {
int err; int err;
void *p; void *p;
@ -106,33 +105,22 @@ int
main(void) main(void)
{ {
int ret = 0; int ret = 0;
pthread_t thread; je_thread_t thread;
malloc_printf("Test begin\n"); malloc_printf("Test begin\n");
thread_start(NULL); je_thread_start(NULL);
if (pthread_create(&thread, NULL, thread_start, NULL) je_thread_create(&thread, je_thread_start, NULL);
!= 0) { je_thread_join(thread, (void *)&ret);
malloc_printf("%s(): Error in pthread_create()\n", __func__);
ret = 1;
goto label_return;
}
pthread_join(thread, (void *)&ret);
thread_start(NULL); je_thread_start(NULL);
if (pthread_create(&thread, NULL, thread_start, NULL) je_thread_create(&thread, je_thread_start, NULL);
!= 0) { je_thread_join(thread, (void *)&ret);
malloc_printf("%s(): Error in pthread_create()\n", __func__);
ret = 1;
goto label_return;
}
pthread_join(thread, (void *)&ret);
thread_start(NULL); je_thread_start(NULL);
label_return:
malloc_printf("Test end\n"); malloc_printf("Test end\n");
return (ret); return (ret);
} }

View File

@ -5,3 +5,25 @@
*/ */
#include "jemalloc/jemalloc@install_suffix@.h" #include "jemalloc/jemalloc@install_suffix@.h"
#include "jemalloc/internal/jemalloc_internal.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);
}

View File

@ -1,6 +1,5 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <pthread.h>
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
@ -10,7 +9,7 @@
#define NTHREADS 10 #define NTHREADS 10
void * void *
thread_start(void *arg) je_thread_start(void *arg)
{ {
unsigned main_arena_ind = *(unsigned *)arg; unsigned main_arena_ind = *(unsigned *)arg;
void *p; void *p;
@ -52,7 +51,7 @@ main(void)
unsigned arena_ind; unsigned arena_ind;
size_t size; size_t size;
int err; int err;
pthread_t threads[NTHREADS]; je_thread_t threads[NTHREADS];
unsigned i; unsigned i;
malloc_printf("Test begin\n"); malloc_printf("Test begin\n");
@ -72,18 +71,11 @@ main(void)
goto label_return; goto label_return;
} }
for (i = 0; i < NTHREADS; i++) { for (i = 0; i < NTHREADS; i++)
if (pthread_create(&threads[i], NULL, thread_start, je_thread_create(&threads[i], je_thread_start, (void *)&arena_ind);
(void *)&arena_ind) != 0) {
malloc_printf("%s(): Error in pthread_create()\n",
__func__);
ret = 1;
goto label_return;
}
}
for (i = 0; i < NTHREADS; i++) for (i = 0; i < NTHREADS; i++)
pthread_join(threads[i], (void *)&ret); je_thread_join(threads[i], (void *)&ret);
label_return: label_return:
malloc_printf("Test end\n"); malloc_printf("Test end\n");

View File

@ -1,7 +1,6 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h> #include <stdbool.h>
#include <pthread.h>
#include <assert.h> #include <assert.h>
#include <errno.h> #include <errno.h>
@ -9,7 +8,7 @@
#include "jemalloc_test.h" #include "jemalloc_test.h"
void * void *
thread_start(void *arg) je_thread_start(void *arg)
{ {
int err; int err;
size_t sz; size_t sz;
@ -77,33 +76,22 @@ int
main(void) main(void)
{ {
int ret = 0; int ret = 0;
pthread_t thread; je_thread_t thread;
malloc_printf("Test begin\n"); malloc_printf("Test begin\n");
thread_start(NULL); je_thread_start(NULL);
if (pthread_create(&thread, NULL, thread_start, NULL) je_thread_create(&thread, je_thread_start, NULL);
!= 0) { je_thread_join(thread, (void *)&ret);
malloc_printf("%s(): Error in pthread_create()\n", __func__);
ret = 1;
goto label_return;
}
pthread_join(thread, (void *)&ret);
thread_start(NULL); je_thread_start(NULL);
if (pthread_create(&thread, NULL, thread_start, NULL) je_thread_create(&thread, je_thread_start, NULL);
!= 0) { je_thread_join(thread, (void *)&ret);
malloc_printf("%s(): Error in pthread_create()\n", __func__);
ret = 1;
goto label_return;
}
pthread_join(thread, (void *)&ret);
thread_start(NULL); je_thread_start(NULL);
label_return:
malloc_printf("Test end\n"); malloc_printf("Test end\n");
return (ret); return (ret);
} }