diff options
author | Marco S Hyman <marc@cvs.openbsd.org> | 2002-11-12 03:17:17 +0000 |
---|---|---|
committer | Marco S Hyman <marc@cvs.openbsd.org> | 2002-11-12 03:17:17 +0000 |
commit | 83cce7161f704ad0197daa1410f28f6978c58aaf (patch) | |
tree | 4ac0513060deff43b2cdd6ecd7be55c180bd8ed1 /regress | |
parent | b2e6338136b1431de2fec79278472d7057a0af51 (diff) |
dueling mallocs -- test for proper serialization when multiple threads
try to malloc at the same time
Diffstat (limited to 'regress')
-rw-r--r-- | regress/lib/libc_r/Makefile | 4 | ||||
-rw-r--r-- | regress/lib/libc_r/malloc_duel/Makefile | 5 | ||||
-rw-r--r-- | regress/lib/libc_r/malloc_duel/malloc_duel.c | 70 | ||||
-rw-r--r-- | regress/lib/libpthread/Makefile | 4 | ||||
-rw-r--r-- | regress/lib/libpthread/malloc_duel/Makefile | 5 | ||||
-rw-r--r-- | regress/lib/libpthread/malloc_duel/malloc_duel.c | 70 |
6 files changed, 154 insertions, 4 deletions
diff --git a/regress/lib/libc_r/Makefile b/regress/lib/libc_r/Makefile index b5483d7be29..02d1500c284 100644 --- a/regress/lib/libc_r/Makefile +++ b/regress/lib/libc_r/Makefile @@ -1,6 +1,6 @@ -# $OpenBSD: Makefile,v 1.13 2002/10/21 18:47:44 marc Exp $ +# $OpenBSD: Makefile,v 1.14 2002/11/12 03:17:16 marc Exp $ -SUBDIR= cancel close cwd execve fork group netdb pcap poll \ +SUBDIR= cancel close cwd execve fork group malloc_duel netdb pcap poll \ preemption preemption_float pthread_cond_timedwait pthread_create \ pthread_join pthread_kill pthread_mutex pthread_specific readdir \ select setjmp signal sigdeliver siginfo signodefer sigsuspend \ diff --git a/regress/lib/libc_r/malloc_duel/Makefile b/regress/lib/libc_r/malloc_duel/Makefile new file mode 100644 index 00000000000..7626a3878e4 --- /dev/null +++ b/regress/lib/libc_r/malloc_duel/Makefile @@ -0,0 +1,5 @@ +# $OpenBSD: Makefile,v 1.1 2002/11/12 03:17:16 marc Exp $ + +PROG= malloc_duel + +.include <bsd.regress.mk> diff --git a/regress/lib/libc_r/malloc_duel/malloc_duel.c b/regress/lib/libc_r/malloc_duel/malloc_duel.c new file mode 100644 index 00000000000..c7733f63b45 --- /dev/null +++ b/regress/lib/libc_r/malloc_duel/malloc_duel.c @@ -0,0 +1,70 @@ +/* $OpenBSD: malloc_duel.c,v 1.1 2002/11/12 03:17:16 marc Exp $ */ +/* PUBLIC DOMAIN Nov 2002 <marc@snafu.org> */ + +/* + * Dueling malloc in different threads + */ + +#include <signal.h> +#include <stdlib.h> +#include <unistd.h> + +#include "test.h" + +volatile sig_atomic_t done; + +#define MALLOC_COUNT 1024 + +/* + * sigalrm handler. Initiate end-of-test + */ +static void +alarm_handler(int sig) +{ + done = 1; +} + +/* + * A function that does lots of mallocs, called by all threads. + */ +void +malloc_loop(void) +{ + int i; + int **a; + + a = calloc(MALLOC_COUNT, sizeof(int*)); + ASSERT(a != NULL); + while (!done) { + for (i = 0; i < MALLOC_COUNT; i++) { + a[i] = malloc(sizeof(int)); + ASSERT(a[i] != NULL); + } + for (i = 0; i < MALLOC_COUNT; i++) { + free(a[i]); + } + } +} + +/* + * A thread that does a lot of mallocs + */ +void * +thread(void *arg) +{ + malloc_loop(); + return NULL; +} + +int +main(int argc, char **argv) +{ + pthread_t child; + + CHECKr(pthread_create(&child, NULL, thread, NULL)); + ASSERT(signal(SIGALRM, alarm_handler) != SIG_ERR); + CHECKe(alarm(20)); + malloc_loop(); + CHECKr(pthread_join(child, NULL)); + SUCCEED; +} diff --git a/regress/lib/libpthread/Makefile b/regress/lib/libpthread/Makefile index b5483d7be29..02d1500c284 100644 --- a/regress/lib/libpthread/Makefile +++ b/regress/lib/libpthread/Makefile @@ -1,6 +1,6 @@ -# $OpenBSD: Makefile,v 1.13 2002/10/21 18:47:44 marc Exp $ +# $OpenBSD: Makefile,v 1.14 2002/11/12 03:17:16 marc Exp $ -SUBDIR= cancel close cwd execve fork group netdb pcap poll \ +SUBDIR= cancel close cwd execve fork group malloc_duel netdb pcap poll \ preemption preemption_float pthread_cond_timedwait pthread_create \ pthread_join pthread_kill pthread_mutex pthread_specific readdir \ select setjmp signal sigdeliver siginfo signodefer sigsuspend \ diff --git a/regress/lib/libpthread/malloc_duel/Makefile b/regress/lib/libpthread/malloc_duel/Makefile new file mode 100644 index 00000000000..7626a3878e4 --- /dev/null +++ b/regress/lib/libpthread/malloc_duel/Makefile @@ -0,0 +1,5 @@ +# $OpenBSD: Makefile,v 1.1 2002/11/12 03:17:16 marc Exp $ + +PROG= malloc_duel + +.include <bsd.regress.mk> diff --git a/regress/lib/libpthread/malloc_duel/malloc_duel.c b/regress/lib/libpthread/malloc_duel/malloc_duel.c new file mode 100644 index 00000000000..c7733f63b45 --- /dev/null +++ b/regress/lib/libpthread/malloc_duel/malloc_duel.c @@ -0,0 +1,70 @@ +/* $OpenBSD: malloc_duel.c,v 1.1 2002/11/12 03:17:16 marc Exp $ */ +/* PUBLIC DOMAIN Nov 2002 <marc@snafu.org> */ + +/* + * Dueling malloc in different threads + */ + +#include <signal.h> +#include <stdlib.h> +#include <unistd.h> + +#include "test.h" + +volatile sig_atomic_t done; + +#define MALLOC_COUNT 1024 + +/* + * sigalrm handler. Initiate end-of-test + */ +static void +alarm_handler(int sig) +{ + done = 1; +} + +/* + * A function that does lots of mallocs, called by all threads. + */ +void +malloc_loop(void) +{ + int i; + int **a; + + a = calloc(MALLOC_COUNT, sizeof(int*)); + ASSERT(a != NULL); + while (!done) { + for (i = 0; i < MALLOC_COUNT; i++) { + a[i] = malloc(sizeof(int)); + ASSERT(a[i] != NULL); + } + for (i = 0; i < MALLOC_COUNT; i++) { + free(a[i]); + } + } +} + +/* + * A thread that does a lot of mallocs + */ +void * +thread(void *arg) +{ + malloc_loop(); + return NULL; +} + +int +main(int argc, char **argv) +{ + pthread_t child; + + CHECKr(pthread_create(&child, NULL, thread, NULL)); + ASSERT(signal(SIGALRM, alarm_handler) != SIG_ERR); + CHECKe(alarm(20)); + malloc_loop(); + CHECKr(pthread_join(child, NULL)); + SUCCEED; +} |