176913Sjasone/*
276913Sjasone * Copyright (C) 2001 Jason Evans <jasone@freebsd.org>.
376913Sjasone * All rights reserved.
476913Sjasone *
576913Sjasone * Redistribution and use in source and binary forms, with or without
676913Sjasone * modification, are permitted provided that the following conditions
776913Sjasone * are met:
876913Sjasone * 1. Redistributions of source code must retain the above copyright
976913Sjasone *    notice(s), this list of conditions and the following disclaimer as
1076913Sjasone *    the first lines of this file unmodified other than the possible
1176913Sjasone *    addition of one or more copyright notices.
1276913Sjasone * 2. Redistributions in binary form must reproduce the above copyright
1376913Sjasone *    notice(s), this list of conditions and the following disclaimer in
1476913Sjasone *    the documentation and/or other materials provided with the
1576913Sjasone *    distribution.
1676913Sjasone *
1776913Sjasone * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
1876913Sjasone * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1976913Sjasone * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2076913Sjasone * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
2176913Sjasone * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2276913Sjasone * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2376913Sjasone * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
2476913Sjasone * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
2576913Sjasone * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
2676913Sjasone * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
2776913Sjasone * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2876913Sjasone *
2976913Sjasone * $FreeBSD$
3076913Sjasone *
3176913Sjasone * Test for leaked joined threads.
3276913Sjasone */
3376913Sjasone
3476913Sjasone#include <stdio.h>
3576913Sjasone#include <stdlib.h>
3676913Sjasone#include <unistd.h>
3776913Sjasone
3876913Sjasone#include <errno.h>
3976913Sjasone#include <string.h>
4076913Sjasone#include <pthread.h>
4176913Sjasone
4276913Sjasone#define	NITERATIONS	16384
4376913Sjasone#define	MAXGROWTH	16384
4476913Sjasone
4576913Sjasonevoid *
4676913Sjasonethread_entry(void *a_arg)
4776913Sjasone{
4876913Sjasone	return NULL;
4976913Sjasone}
5076913Sjasone
5176913Sjasoneint
5276913Sjasonemain(void)
5376913Sjasone{
5476913Sjasone	pthread_t	thread;
5576913Sjasone	int		i, error;
5676913Sjasone	char		*brk, *nbrk;
5776913Sjasone	unsigned	growth;
5876913Sjasone
5976913Sjasone	fprintf(stderr, "Test begin\n");
6076913Sjasone
6176913Sjasone	/* Get an initial brk value. */
6276913Sjasone	brk = sbrk(0);
6376913Sjasone
6476913Sjasone	/* Create threads and join them, one at a time. */
6576913Sjasone	for (i = 0; i < NITERATIONS; i++) {
6676913Sjasone		if ((error = pthread_create(&thread, NULL, thread_entry, NULL))
6776913Sjasone		    != 0) {
6876913Sjasone			fprintf(stderr, "Error in pthread_create(): %s\n",
6976913Sjasone			    strerror(error));
7076913Sjasone			exit(1);
7176913Sjasone		}
7276913Sjasone		if ((error = pthread_join(thread, NULL)) != 0) {
7376913Sjasone			fprintf(stderr, "Error in pthread_join(): %s\n",
7476913Sjasone			    strerror(error));
7576913Sjasone			exit(1);
7676913Sjasone		}
7776913Sjasone	}
7876913Sjasone
7976913Sjasone	/* Get a final brk value. */
8076913Sjasone	nbrk = sbrk(0);
8176913Sjasone
8276913Sjasone	/*
8376913Sjasone	 * Check that the amount of heap space allocated is below an acceptable
8476913Sjasone	 * threshold.  We could just compare brk and nbrk, but the test could
8576913Sjasone	 * conceivably break if the internals of the threads library changes.
8676913Sjasone	 */
8776913Sjasone	if (nbrk > brk) {
8876913Sjasone		/* Heap grows up. */
8976913Sjasone		growth = nbrk - brk;
9076913Sjasone	} else if (nbrk <= brk) {
9176913Sjasone		/* Heap grows down, or no growth. */
9276913Sjasone		growth = brk - nbrk;
9376913Sjasone	}
9476913Sjasone
9576913Sjasone	if (growth > MAXGROWTH) {
9676913Sjasone		fprintf(stderr, "Heap growth exceeded maximum (%u > %u)\n",
9776913Sjasone		    growth, MAXGROWTH);
9876913Sjasone	}
9976913Sjasone#if (0)
10076913Sjasone	else {
10176913Sjasone		fprintf(stderr, "Heap growth acceptable (%u <= %u)\n",
10276913Sjasone		    growth, MAXGROWTH);
10376913Sjasone	}
10476913Sjasone#endif
10576913Sjasone
10676913Sjasone	fprintf(stderr, "Test end\n");
10776913Sjasone	return 0;
10876913Sjasone}
109