"Fossies" - the Fresh Open Source Software Archive

Member "bind-9.17.5/lib/isc/pthreads/thread.c" (4 Sep 2020, 4666 Bytes) of package /linux/misc/dns/bind9/9.17.5/bind-9.17.5.tar.xz:


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 /*! \file */
   13 
   14 #if defined(HAVE_SCHED_H)
   15 #include <sched.h>
   16 #endif /* if defined(HAVE_SCHED_H) */
   17 
   18 #if defined(HAVE_CPUSET_H)
   19 #include <sys/cpuset.h>
   20 #include <sys/param.h>
   21 #endif /* if defined(HAVE_CPUSET_H) */
   22 
   23 #if defined(HAVE_SYS_PROCSET_H)
   24 #include <sys/processor.h>
   25 #include <sys/procset.h>
   26 #include <sys/types.h>
   27 #endif /* if defined(HAVE_SYS_PROCSET_H) */
   28 
   29 #include <isc/strerr.h>
   30 #include <isc/thread.h>
   31 #include <isc/util.h>
   32 
   33 #ifndef THREAD_MINSTACKSIZE
   34 #define THREAD_MINSTACKSIZE (1024U * 1024)
   35 #endif /* ifndef THREAD_MINSTACKSIZE */
   36 
   37 #define _FATAL(r, f)                                                          \
   38     {                                                                     \
   39         char strbuf[ISC_STRERRORSIZE];                                \
   40         strerror_r(r, strbuf, sizeof(strbuf));                        \
   41         isc_error_fatal(__FILE__, __LINE__, f " failed: %s", strbuf); \
   42     }
   43 
   44 void
   45 isc_thread_create(isc_threadfunc_t func, isc_threadarg_t arg,
   46           isc_thread_t *thread) {
   47     pthread_attr_t attr;
   48 #if defined(HAVE_PTHREAD_ATTR_GETSTACKSIZE) && \
   49     defined(HAVE_PTHREAD_ATTR_SETSTACKSIZE)
   50     size_t stacksize;
   51 #endif /* if defined(HAVE_PTHREAD_ATTR_GETSTACKSIZE) && \
   52     * defined(HAVE_PTHREAD_ATTR_SETSTACKSIZE) */
   53     int ret;
   54 
   55     pthread_attr_init(&attr);
   56 
   57 #if defined(HAVE_PTHREAD_ATTR_GETSTACKSIZE) && \
   58     defined(HAVE_PTHREAD_ATTR_SETSTACKSIZE)
   59     ret = pthread_attr_getstacksize(&attr, &stacksize);
   60     if (ret != 0) {
   61         _FATAL(ret, "pthread_attr_getstacksize()");
   62     }
   63 
   64     if (stacksize < THREAD_MINSTACKSIZE) {
   65         ret = pthread_attr_setstacksize(&attr, THREAD_MINSTACKSIZE);
   66         if (ret != 0) {
   67             _FATAL(ret, "pthread_attr_setstacksize()");
   68         }
   69     }
   70 #endif /* if defined(HAVE_PTHREAD_ATTR_GETSTACKSIZE) && \
   71     * defined(HAVE_PTHREAD_ATTR_SETSTACKSIZE) */
   72 
   73     ret = pthread_create(thread, &attr, func, arg);
   74     if (ret != 0) {
   75         _FATAL(ret, "pthread_create()");
   76     }
   77 
   78     pthread_attr_destroy(&attr);
   79 
   80     return;
   81 }
   82 
   83 void
   84 isc_thread_join(isc_thread_t thread, isc_threadresult_t *result) {
   85     int ret = pthread_join(thread, result);
   86     if (ret != 0) {
   87         _FATAL(ret, "pthread_join()");
   88     }
   89 }
   90 
   91 #ifdef __NetBSD__
   92 #define pthread_setconcurrency(a) (void)a /* nothing */
   93 #endif                    /* ifdef __NetBSD__ */
   94 
   95 void
   96 isc_thread_setconcurrency(unsigned int level) {
   97     (void)pthread_setconcurrency(level);
   98 }
   99 
  100 void
  101 isc_thread_setname(isc_thread_t thread, const char *name) {
  102 #if defined(HAVE_PTHREAD_SETNAME_NP) && !defined(__APPLE__)
  103     /*
  104      * macOS has pthread_setname_np but only works on the
  105      * current thread so it's not used here
  106      */
  107 #if defined(__NetBSD__)
  108     (void)pthread_setname_np(thread, name, NULL);
  109 #else  /* if defined(__NetBSD__) */
  110     (void)pthread_setname_np(thread, name);
  111 #endif /* if defined(__NetBSD__) */
  112 #elif defined(HAVE_PTHREAD_SET_NAME_NP)
  113     (void)pthread_set_name_np(thread, name);
  114 #else  /* if defined(HAVE_PTHREAD_SETNAME_NP) && !defined(__APPLE__) */
  115     UNUSED(thread);
  116     UNUSED(name);
  117 #endif /* if defined(HAVE_PTHREAD_SETNAME_NP) && !defined(__APPLE__) */
  118 }
  119 
  120 void
  121 isc_thread_yield(void) {
  122 #if defined(HAVE_SCHED_YIELD)
  123     sched_yield();
  124 #elif defined(HAVE_PTHREAD_YIELD)
  125     pthread_yield();
  126 #elif defined(HAVE_PTHREAD_YIELD_NP)
  127     pthread_yield_np();
  128 #endif /* if defined(HAVE_SCHED_YIELD) */
  129 }
  130 
  131 isc_result_t
  132 isc_thread_setaffinity(int cpu) {
  133 #if defined(HAVE_CPUSET_SETAFFINITY)
  134     cpuset_t cpuset;
  135     CPU_ZERO(&cpuset);
  136     CPU_SET(cpu, &cpuset);
  137     if (cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1,
  138                    sizeof(cpuset), &cpuset) != 0)
  139     {
  140         return (ISC_R_FAILURE);
  141     }
  142 #elif defined(HAVE_PTHREAD_SETAFFINITY_NP)
  143 #if defined(__NetBSD__)
  144     cpuset_t *cset;
  145     cset = cpuset_create();
  146     if (cset == NULL) {
  147         return (ISC_R_FAILURE);
  148     }
  149     cpuset_set(cpu, cset);
  150     if (pthread_setaffinity_np(pthread_self(), cpuset_size(cset), cset) !=
  151         0) {
  152         cpuset_destroy(cset);
  153         return (ISC_R_FAILURE);
  154     }
  155     cpuset_destroy(cset);
  156 #else  /* linux? */
  157     cpu_set_t set;
  158     CPU_ZERO(&set);
  159     CPU_SET(cpu, &set);
  160     if (pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &set) !=
  161         0) {
  162         return (ISC_R_FAILURE);
  163     }
  164 #endif /* __NetBSD__ */
  165 #elif defined(HAVE_PROCESSOR_BIND)
  166     if (processor_bind(P_LWPID, P_MYID, cpu, NULL) != 0) {
  167         return (ISC_R_FAILURE);
  168     }
  169 #else  /* if defined(HAVE_CPUSET_SETAFFINITY) */
  170     UNUSED(cpu);
  171 #endif /* if defined(HAVE_CPUSET_SETAFFINITY) */
  172     return (ISC_R_SUCCESS);
  173 }