t_preempt.c revision 272343
1272343Sngie/* $NetBSD: t_preempt.c,v 1.2 2010/11/03 16:10:22 christos Exp $ */ 2272343Sngie 3272343Sngie/* 4272343Sngie * Copyright (c) 2008 The NetBSD Foundation, Inc. 5272343Sngie * All rights reserved. 6272343Sngie * 7272343Sngie * Redistribution and use in source and binary forms, with or without 8272343Sngie * modification, are permitted provided that the following conditions 9272343Sngie * are met: 10272343Sngie * 1. Redistributions of source code must retain the above copyright 11272343Sngie * notice, this list of conditions and the following disclaimer. 12272343Sngie * 2. Redistributions in binary form must reproduce the above copyright 13272343Sngie * notice, this list of conditions and the following disclaimer in the 14272343Sngie * documentation and/or other materials provided with the distribution. 15272343Sngie * 16272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19272343Sngie * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26272343Sngie * POSSIBILITY OF SUCH DAMAGE. 27272343Sngie */ 28272343Sngie 29272343Sngie#include <sys/cdefs.h> 30272343Sngie__COPYRIGHT("@(#) Copyright (c) 2008\ 31272343Sngie The NetBSD Foundation, inc. All rights reserved."); 32272343Sngie__RCSID("$NetBSD: t_preempt.c,v 1.2 2010/11/03 16:10:22 christos Exp $"); 33272343Sngie 34272343Sngie#include <errno.h> 35272343Sngie#include <fcntl.h> 36272343Sngie#include <pthread.h> 37272343Sngie#include <stdio.h> 38272343Sngie#include <stdlib.h> 39272343Sngie#include <string.h> 40272343Sngie#include <unistd.h> 41272343Sngie 42272343Sngie#include <atf-c.h> 43272343Sngie 44272343Sngie#include "h_common.h" 45272343Sngie 46272343Sngiepthread_mutex_t mutex; 47272343Sngiepthread_cond_t cond; 48272343Sngieint started; 49272343Sngie 50272343Sngie#define HUGE_BUFFER 1<<20 51272343Sngie#define NTHREADS 1 52272343Sngie 53272343Sngiestatic void * 54272343Sngiethreadfunc(void *arg) 55272343Sngie{ 56272343Sngie printf("2: Second thread.\n"); 57272343Sngie 58272343Sngie printf("2: Locking mutex\n"); 59272343Sngie PTHREAD_REQUIRE(pthread_mutex_lock(&mutex)); 60272343Sngie printf("2: Got mutex.\n"); 61272343Sngie started++; 62272343Sngie 63272343Sngie PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex)); 64272343Sngie PTHREAD_REQUIRE(pthread_cond_signal(&cond)); 65272343Sngie sleep(1); 66272343Sngie 67272343Sngie return NULL; 68272343Sngie} 69272343Sngie 70272343SngieATF_TC(preempt1); 71272343SngieATF_TC_HEAD(preempt1, tc) 72272343Sngie{ 73272343Sngie atf_tc_set_md_var(tc, "descr", 74272343Sngie "Checks kernel preemption during a large uiomove"); 75272343Sngie} 76272343SngieATF_TC_BODY(preempt1, tc) 77272343Sngie{ 78272343Sngie int i; 79272343Sngie ssize_t rv; 80272343Sngie pthread_t new; 81272343Sngie void *joinval; 82272343Sngie 83272343Sngie char *mem; 84272343Sngie int fd; 85272343Sngie 86272343Sngie mem = malloc(HUGE_BUFFER); 87272343Sngie ATF_REQUIRE_MSG(mem != NULL, "%s", strerror(errno)); 88272343Sngie 89272343Sngie fd = open("/dev/urandom", O_RDONLY, 0); 90272343Sngie ATF_REQUIRE_MSG(fd != -1, "%s", strerror(errno)); 91272343Sngie 92272343Sngie printf("1: preempt test\n"); 93272343Sngie 94272343Sngie PTHREAD_REQUIRE(pthread_cond_init(&cond, NULL)); 95272343Sngie PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL)); 96272343Sngie 97272343Sngie PTHREAD_REQUIRE(pthread_mutex_lock(&mutex)); 98272343Sngie 99272343Sngie started = 0; 100272343Sngie 101272343Sngie for (i = 0; i < NTHREADS; i++) { 102272343Sngie PTHREAD_REQUIRE(pthread_create(&new, NULL, threadfunc, NULL)); 103272343Sngie } 104272343Sngie 105272343Sngie while (started < NTHREADS) { 106272343Sngie PTHREAD_REQUIRE(pthread_cond_wait(&cond, &mutex)); 107272343Sngie } 108272343Sngie 109272343Sngie printf("1: Thread has started.\n"); 110272343Sngie 111272343Sngie PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex)); 112272343Sngie printf("1: After releasing the mutex.\n"); 113272343Sngie 114272343Sngie rv = read(fd, mem, HUGE_BUFFER); 115272343Sngie close(fd); 116272343Sngie ATF_REQUIRE_EQ(rv, HUGE_BUFFER); 117272343Sngie 118272343Sngie PTHREAD_REQUIRE(pthread_join(new, &joinval)); 119272343Sngie 120272343Sngie printf("1: Thread joined.\n"); 121272343Sngie} 122272343Sngie 123272343SngieATF_TP_ADD_TCS(tp) 124272343Sngie{ 125272343Sngie ATF_TP_ADD_TC(tp, preempt1); 126272343Sngie 127272343Sngie return atf_no_error(); 128272343Sngie} 129