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