"Fossies" - the Fresh Open Source Software Archive

Member "bind-9.11.23/lib/isc/pthreads/thread.c" (7 Sep 2020, 2337 Bytes) of package /linux/misc/dns/bind9/9.11.23/bind-9.11.23.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "thread.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
    3  *
    4  * This Source Code Form is subject to the terms of the Mozilla Public
    5  * License, v. 2.0. If a copy of the MPL was not distributed with this
    6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
    7  *
    8  * See the COPYRIGHT file distributed with this work for additional
    9  * information regarding copyright ownership.
   10  */
   11 
   12 
   13 /*! \file */
   14 
   15 #include <config.h>
   16 
   17 #if defined(HAVE_SCHED_H)
   18 #include <sched.h>
   19 #endif
   20 
   21 #include <isc/thread.h>
   22 #include <isc/util.h>
   23 
   24 #ifndef THREAD_MINSTACKSIZE
   25 #define THREAD_MINSTACKSIZE     (1024U * 1024)
   26 #endif
   27 
   28 isc_result_t
   29 isc_thread_create(isc_threadfunc_t func, isc_threadarg_t arg,
   30           isc_thread_t *thread)
   31 {
   32     pthread_attr_t attr;
   33 #if defined(HAVE_PTHREAD_ATTR_GETSTACKSIZE) && \
   34     defined(HAVE_PTHREAD_ATTR_SETSTACKSIZE)
   35     size_t stacksize;
   36 #endif
   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
   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 
   78 void
   79 isc_thread_setname(isc_thread_t thread, const char *name) {
   80 #if defined(HAVE_PTHREAD_SETNAME_NP) && defined(_GNU_SOURCE)
   81     /*
   82      * macOS has pthread_setname_np but only works on the
   83      * current thread so it's not used here
   84     */
   85     (void)pthread_setname_np(thread, name);
   86 #elif defined(HAVE_PTHREAD_SET_NAME_NP)
   87     (void)pthread_set_name_np(thread, name);
   88 #else
   89     UNUSED(thread);
   90     UNUSED(name);
   91 #endif
   92 }
   93 
   94 void
   95 isc_thread_yield(void) {
   96 #if defined(HAVE_SCHED_YIELD)
   97     sched_yield();
   98 #elif defined( HAVE_PTHREAD_YIELD)
   99     pthread_yield();
  100 #elif defined( HAVE_PTHREAD_YIELD_NP)
  101     pthread_yield_np();
  102 #endif
  103 }