1/*
2 * Copyright (C) 2004, 2005, 2007, 2013  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#if defined(HAVE_SCHED_H)
25#include <sched.h>
26#endif
27
28#include <isc/thread.h>
29#include <isc/util.h>
30
31#ifndef THREAD_MINSTACKSIZE
32#define THREAD_MINSTACKSIZE		(1024U * 1024)
33#endif
34
35isc_result_t
36isc_thread_create(isc_threadfunc_t func, isc_threadarg_t arg,
37		  isc_thread_t *thread)
38{
39	pthread_attr_t attr;
40	size_t stacksize;
41	int ret;
42
43	pthread_attr_init(&attr);
44
45#if defined(HAVE_PTHREAD_ATTR_GETSTACKSIZE) && \
46    defined(HAVE_PTHREAD_ATTR_SETSTACKSIZE)
47	ret = pthread_attr_getstacksize(&attr, &stacksize);
48	if (ret != 0)
49		return (ISC_R_UNEXPECTED);
50
51	if (stacksize < THREAD_MINSTACKSIZE) {
52		ret = pthread_attr_setstacksize(&attr, THREAD_MINSTACKSIZE);
53		if (ret != 0)
54			return (ISC_R_UNEXPECTED);
55	}
56#endif
57
58#if defined(PTHREAD_SCOPE_SYSTEM) && defined(NEED_PTHREAD_SCOPE_SYSTEM)
59	ret = pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
60	if (ret != 0)
61		return (ISC_R_UNEXPECTED);
62#endif
63
64	ret = pthread_create(thread, &attr, func, arg);
65	if (ret != 0)
66		return (ISC_R_UNEXPECTED);
67
68	pthread_attr_destroy(&attr);
69
70	return (ISC_R_SUCCESS);
71}
72
73void
74isc_thread_setconcurrency(unsigned int level) {
75#if defined(CALL_PTHREAD_SETCONCURRENCY)
76	(void)pthread_setconcurrency(level);
77#else
78	UNUSED(level);
79#endif
80}
81
82void
83isc_thread_yield(void) {
84#if defined(HAVE_SCHED_YIELD)
85	sched_yield();
86#elif defined( HAVE_PTHREAD_YIELD)
87	pthread_yield();
88#elif defined( HAVE_PTHREAD_YIELD_NP)
89	pthread_yield_np();
90#endif
91}
92