Home | History | Annotate | Line # | Download | only in libpthread
t_fpu.c revision 1.1.6.1
      1  1.1.6.1  yamt /* $NetBSD: t_fpu.c,v 1.1.6.1 2014/05/22 11:42:22 yamt Exp $ */
      2      1.1  jmmv 
      3      1.1  jmmv /*
      4      1.1  jmmv  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      5      1.1  jmmv  * All rights reserved.
      6      1.1  jmmv  *
      7      1.1  jmmv  * Redistribution and use in source and binary forms, with or without
      8      1.1  jmmv  * modification, are permitted provided that the following conditions
      9      1.1  jmmv  * are met:
     10      1.1  jmmv  * 1. Redistributions of source code must retain the above copyright
     11      1.1  jmmv  *    notice, this list of conditions and the following disclaimer.
     12      1.1  jmmv  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.1  jmmv  *    notice, this list of conditions and the following disclaimer in the
     14      1.1  jmmv  *    documentation and/or other materials provided with the distribution.
     15      1.1  jmmv  *
     16      1.1  jmmv  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17      1.1  jmmv  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18      1.1  jmmv  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19      1.1  jmmv  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20      1.1  jmmv  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21      1.1  jmmv  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22      1.1  jmmv  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23      1.1  jmmv  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24      1.1  jmmv  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25      1.1  jmmv  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26      1.1  jmmv  * POSSIBILITY OF SUCH DAMAGE.
     27      1.1  jmmv  */
     28      1.1  jmmv 
     29      1.1  jmmv #include <sys/cdefs.h>
     30      1.1  jmmv __COPYRIGHT("@(#) Copyright (c) 2008\
     31      1.1  jmmv  The NetBSD Foundation, inc. All rights reserved.");
     32  1.1.6.1  yamt __RCSID("$NetBSD: t_fpu.c,v 1.1.6.1 2014/05/22 11:42:22 yamt Exp $");
     33      1.1  jmmv 
     34      1.1  jmmv /*
     35      1.1  jmmv  * This is adapted from part of csw/cstest of the MPD implementation by
     36      1.1  jmmv  * the University of Arizona CS department (http://www.cs.arizona.edu/sr/)
     37      1.1  jmmv  * which is in the public domain:
     38      1.1  jmmv  *
     39      1.1  jmmv  * "The MPD system is in the public domain and you may use and distribute it
     40      1.1  jmmv  *  as you wish.  We ask that you retain credits referencing the University
     41      1.1  jmmv  *  of Arizona and that you identify any changes you make.
     42      1.1  jmmv  *
     43      1.1  jmmv  *  We can't provide a warranty with MPD; it's up to you to determine its
     44      1.1  jmmv  *  suitability and reliability for your needs.  We would like to hear of
     45      1.1  jmmv  *  any problems you encounter but we cannot promise a timely correction."
     46      1.1  jmmv  *
     47      1.1  jmmv  * It was changed to use pthread_create() and sched_yield() instead of
     48      1.1  jmmv  * the internal MPD context switching primitives by Ignatios Souvatzis
     49      1.1  jmmv  * <is (at) netbsd.org>.
     50      1.1  jmmv  */
     51      1.1  jmmv 
     52      1.1  jmmv #include <math.h>
     53      1.1  jmmv #include <pthread.h>
     54      1.1  jmmv #include <sched.h>
     55      1.1  jmmv #include <stdio.h>
     56      1.1  jmmv #include <stdlib.h>
     57      1.1  jmmv #include <unistd.h>
     58      1.1  jmmv 
     59      1.1  jmmv #include <atf-c.h>
     60      1.1  jmmv 
     61      1.1  jmmv #include "h_common.h"
     62      1.1  jmmv 
     63      1.1  jmmv #define N_RECURSE 10
     64      1.1  jmmv 
     65      1.1  jmmv static void recurse(void);
     66      1.1  jmmv 
     67      1.1  jmmv int recursion_depth = 0;
     68      1.1  jmmv pthread_mutex_t recursion_depth_lock;
     69      1.1  jmmv 
     70      1.1  jmmv static void *
     71      1.1  jmmv stir(void *p)
     72      1.1  jmmv {
     73      1.1  jmmv 	double *q = (double *)p;
     74      1.1  jmmv 	double x = *q++;
     75      1.1  jmmv 	double y = *q++;
     76      1.1  jmmv 	double z = *q++;
     77      1.1  jmmv 
     78      1.1  jmmv 	for (;;) {
     79      1.1  jmmv 		x = sin ((y = cos (x + y + .4)) - (z = cos (x + z + .6)));
     80      1.1  jmmv 		PTHREAD_REQUIRE(sched_yield());
     81      1.1  jmmv 	}
     82      1.1  jmmv }
     83      1.1  jmmv 
     84      1.1  jmmv static double
     85      1.1  jmmv mul3(double x, double y, double z)
     86      1.1  jmmv {
     87      1.1  jmmv 	PTHREAD_REQUIRE(sched_yield());
     88      1.1  jmmv 
     89      1.1  jmmv 	return x * y * z;
     90      1.1  jmmv }
     91      1.1  jmmv 
     92      1.1  jmmv static void *
     93      1.1  jmmv bar(void *p)
     94      1.1  jmmv {
     95      1.1  jmmv 	double d;
     96      1.1  jmmv 	int rc;
     97      1.1  jmmv 
     98      1.1  jmmv 	d = mul3(mul3(2., 3., 5.), mul3(7., 11., 13.), mul3(17., 19., 23.));
     99      1.1  jmmv 	ATF_REQUIRE_EQ(d, 223092870.);
    100      1.1  jmmv 
    101      1.1  jmmv 	PTHREAD_REQUIRE(pthread_mutex_lock(&recursion_depth_lock));
    102      1.1  jmmv 	rc = recursion_depth++;
    103      1.1  jmmv 	PTHREAD_REQUIRE(pthread_mutex_unlock(&recursion_depth_lock));
    104      1.1  jmmv 
    105      1.1  jmmv 	if (rc < N_RECURSE)
    106      1.1  jmmv 		recurse();
    107      1.1  jmmv 	else
    108      1.1  jmmv 		atf_tc_pass();
    109      1.1  jmmv 
    110      1.1  jmmv 	/* NOTREACHED */
    111      1.1  jmmv 	return NULL;
    112      1.1  jmmv }
    113      1.1  jmmv 
    114      1.1  jmmv static void
    115      1.1  jmmv recurse(void) {
    116      1.1  jmmv 	pthread_t s2;
    117      1.1  jmmv 	pthread_create(&s2, 0, bar, 0);
    118      1.1  jmmv 	sleep(20); /* XXX must be long enough for our slowest machine */
    119      1.1  jmmv }
    120      1.1  jmmv 
    121      1.1  jmmv ATF_TC(fpu);
    122      1.1  jmmv ATF_TC_HEAD(fpu, tc)
    123      1.1  jmmv {
    124      1.1  jmmv 	atf_tc_set_md_var(tc, "descr",
    125      1.1  jmmv 		"Checks that thread context switches will leave the "
    126      1.1  jmmv 		"floating point computations unharmed");
    127      1.1  jmmv }
    128      1.1  jmmv ATF_TC_BODY(fpu, tc)
    129      1.1  jmmv {
    130      1.1  jmmv 	double stirseed[] = { 1.7, 3.2, 2.4 };
    131      1.1  jmmv 	pthread_t s5;
    132      1.1  jmmv 
    133      1.1  jmmv 	printf("Testing threaded floating point computations...\n");
    134      1.1  jmmv 
    135      1.1  jmmv 	PTHREAD_REQUIRE(pthread_mutex_init(&recursion_depth_lock, 0));
    136      1.1  jmmv 
    137      1.1  jmmv 	pthread_create(&s5, 0, stir, stirseed);
    138      1.1  jmmv 	recurse();
    139      1.1  jmmv 
    140  1.1.6.1  yamt 	atf_tc_fail("exiting from main");
    141      1.1  jmmv }
    142      1.1  jmmv 
    143      1.1  jmmv ATF_TP_ADD_TCS(tp)
    144      1.1  jmmv {
    145      1.1  jmmv 	ATF_TP_ADD_TC(tp, fpu);
    146      1.1  jmmv 
    147      1.1  jmmv 	return atf_no_error();
    148      1.1  jmmv }
    149