thread.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004, 2005, 2007, 2013  Internet Systems Consortium, Inc. ("ISC")
00003  * Copyright (C) 2000, 2001, 2003  Internet Software Consortium.
00004  *
00005  * Permission to use, copy, modify, and/or distribute this software for any
00006  * purpose with or without fee is hereby granted, provided that the above
00007  * copyright notice and this permission notice appear in all copies.
00008  *
00009  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
00010  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00011  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
00012  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00013  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00014  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00015  * PERFORMANCE OF THIS SOFTWARE.
00016  */
00017 
00018 /* $Id: thread.c,v 1.17 2007/06/19 23:47:18 tbox Exp $ */
00019 
00020 /*! \file */
00021 
00022 #include <config.h>
00023 
00024 #if defined(HAVE_SCHED_H)
00025 #include <sched.h>
00026 #endif
00027 
00028 #include <isc/thread.h>
00029 #include <isc/util.h>
00030 
00031 #ifndef THREAD_MINSTACKSIZE
00032 #define THREAD_MINSTACKSIZE             (1024U * 1024)
00033 #endif
00034 
00035 isc_result_t
00036 isc_thread_create(isc_threadfunc_t func, isc_threadarg_t arg,
00037                   isc_thread_t *thread)
00038 {
00039         pthread_attr_t attr;
00040         size_t stacksize;
00041         int ret;
00042 
00043         pthread_attr_init(&attr);
00044 
00045 #if defined(HAVE_PTHREAD_ATTR_GETSTACKSIZE) && \
00046     defined(HAVE_PTHREAD_ATTR_SETSTACKSIZE)
00047         ret = pthread_attr_getstacksize(&attr, &stacksize);
00048         if (ret != 0)
00049                 return (ISC_R_UNEXPECTED);
00050 
00051         if (stacksize < THREAD_MINSTACKSIZE) {
00052                 ret = pthread_attr_setstacksize(&attr, THREAD_MINSTACKSIZE);
00053                 if (ret != 0)
00054                         return (ISC_R_UNEXPECTED);
00055         }
00056 #endif
00057 
00058 #if defined(PTHREAD_SCOPE_SYSTEM) && defined(NEED_PTHREAD_SCOPE_SYSTEM)
00059         ret = pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
00060         if (ret != 0)
00061                 return (ISC_R_UNEXPECTED);
00062 #endif
00063 
00064         ret = pthread_create(thread, &attr, func, arg);
00065         if (ret != 0)
00066                 return (ISC_R_UNEXPECTED);
00067 
00068         pthread_attr_destroy(&attr);
00069 
00070         return (ISC_R_SUCCESS);
00071 }
00072 
00073 void
00074 isc_thread_setconcurrency(unsigned int level) {
00075 #if defined(CALL_PTHREAD_SETCONCURRENCY)
00076         (void)pthread_setconcurrency(level);
00077 #else
00078         UNUSED(level);
00079 #endif
00080 }
00081 
00082 void
00083 isc_thread_yield(void) {
00084 #if defined(HAVE_SCHED_YIELD)
00085         sched_yield();
00086 #elif defined( HAVE_PTHREAD_YIELD)
00087         pthread_yield();
00088 #elif defined( HAVE_PTHREAD_YIELD_NP)
00089         pthread_yield_np();
00090 #endif
00091 }

Generated on Tue Apr 28 17:41:05 2015 by Doxygen 1.5.4 for BIND9 Internals 9.11.0pre-alpha