1 /* $NetBSD: thread.c,v 1.6 2014/12/10 04:38:00 christos Exp $ */ 2 3 /* 4 * Copyright (C) 2004, 2005, 2007, 2013 Internet Systems Consortium, Inc. ("ISC") 5 * Copyright (C) 2000, 2001, 2003 Internet Software Consortium. 6 * 7 * Permission to use, copy, modify, and/or distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20 /* Id: thread.c,v 1.17 2007/06/19 23:47:18 tbox Exp */ 21 22 /*! \file */ 23 24 #include <config.h> 25 26 #if defined(HAVE_SCHED_H) 27 #include <sched.h> 28 #endif 29 30 #include <isc/thread.h> 31 #include <isc/util.h> 32 33 #ifndef THREAD_MINSTACKSIZE 34 #define THREAD_MINSTACKSIZE (1024U * 1024) 35 #endif 36 37 isc_result_t 38 isc_thread_create(isc_threadfunc_t func, isc_threadarg_t arg, 39 isc_thread_t *thread) 40 { 41 pthread_attr_t attr; 42 size_t stacksize; 43 int ret; 44 45 pthread_attr_init(&attr); 46 47 #if defined(HAVE_PTHREAD_ATTR_GETSTACKSIZE) && \ 48 defined(HAVE_PTHREAD_ATTR_SETSTACKSIZE) 49 ret = pthread_attr_getstacksize(&attr, &stacksize); 50 if (ret != 0) 51 return (ISC_R_UNEXPECTED); 52 53 if (stacksize < THREAD_MINSTACKSIZE) { 54 ret = pthread_attr_setstacksize(&attr, THREAD_MINSTACKSIZE); 55 if (ret != 0) 56 return (ISC_R_UNEXPECTED); 57 } 58 #endif 59 60 #if defined(PTHREAD_SCOPE_SYSTEM) && defined(NEED_PTHREAD_SCOPE_SYSTEM) 61 ret = pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); 62 if (ret != 0) 63 return (ISC_R_UNEXPECTED); 64 #endif 65 66 ret = pthread_create(thread, &attr, func, arg); 67 if (ret != 0) 68 return (ISC_R_UNEXPECTED); 69 70 pthread_attr_destroy(&attr); 71 72 return (ISC_R_SUCCESS); 73 } 74 75 void 76 isc_thread_setconcurrency(unsigned int level) { 77 #if defined(CALL_PTHREAD_SETCONCURRENCY) 78 (void)pthread_setconcurrency(level); 79 #else 80 UNUSED(level); 81 #endif 82 } 83 84 void 85 isc_thread_yield(void) { 86 #if defined(HAVE_SCHED_YIELD) 87 sched_yield(); 88 #elif defined( HAVE_PTHREAD_YIELD) 89 pthread_yield(); 90 #elif defined( HAVE_PTHREAD_YIELD_NP) 91 pthread_yield_np(); 92 #endif 93 } 94