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