00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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 }