]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Make isc_thread_create() assert internally on failure
authorOndřej Surý <ondrej@sury.org>
Thu, 18 Jul 2019 15:24:05 +0000 (17:24 +0200)
committerOndřej Surý <ondrej@sury.org>
Wed, 31 Jul 2019 09:56:58 +0000 (11:56 +0200)
Previously isc_thread_create() would return ISC_R_UNEXPECTED on a failure to
create new thread.  All such occurences were caught and wrapped into assert
function at higher level.  The function was simplified to assert directly in the
isc_thread_create() function and all caller level assertions were removed.

bin/tests/optional/rwlock_test.c
lib/dns/tests/name_test.c
lib/dns/tests/rbt_test.c
lib/isc/pthreads/include/isc/thread.h
lib/isc/pthreads/thread.c
lib/isc/task.c
lib/isc/tests/mem_test.c
lib/isc/timer.c
lib/isc/unix/socket.c
lib/isc/win32/include/isc/thread.h
lib/isc/win32/thread.c

index 96ae7b9d3f2928fc208b771a8ddb5cbf18a8f5c0..d7a87ac92a273e9d6602d8a80676386ea66a766c 100644 (file)
@@ -109,14 +109,11 @@ main(int argc, char *argv[]) {
                snprintf(name, sizeof(name), "%02u", i);
                dupname = strdup(name);
                RUNTIME_CHECK(dupname != NULL);
-               if (i != 0 && i % 3 == 0)
-                       RUNTIME_CHECK(isc_thread_create(run1, dupname,
-                                                       &workers[i]) ==
-                              ISC_R_SUCCESS);
-               else
-                       RUNTIME_CHECK(isc_thread_create(run2, dupname,
-                                                       &workers[i]) ==
-                              ISC_R_SUCCESS);
+               if (i != 0 && i % 3 == 0) {
+                       isc_thread_create(run1, dupname, &workers[i]);
+               } else {
+                       isc_thread_create(run2, dupname, &workers[i]);
+               }
        }
 
        for (i = 0; i < nworkers; i++)
index b3dcf81176f2e23be7069c967866524486b50ed7..4e51b9300fcf4b43c52c94830a8676bfce438eca 100644 (file)
@@ -743,8 +743,7 @@ benchmark_test(void **state) {
        nthreads = ISC_MIN(isc_os_ncpus(), 32);
        nthreads = ISC_MAX(nthreads, 1);
        for (i = 0; i < nthreads; i++) {
-               result = isc_thread_create(fromwire_thread, NULL, &threads[i]);
-               assert_int_equal(result, ISC_R_SUCCESS);
+               isc_thread_create(fromwire_thread, NULL, &threads[i]);
        }
 
        for (i = 0; i < nthreads; i++) {
index 478ef686733646ab288b62b50b6a4425c29e9954..34119f4dd038d834e051f6b1c5040595b1637977 100644 (file)
@@ -1295,8 +1295,7 @@ benchmark(void **state) {
        nthreads = ISC_MIN(isc_os_ncpus(), 32);
        nthreads = ISC_MAX(nthreads, 1);
        for (i = 0; i < nthreads; i++) {
-               result = isc_thread_create(find_thread, mytree, &threads[i]);
-               assert_int_equal(result, ISC_R_SUCCESS);
+               isc_thread_create(find_thread, mytree, &threads[i]);
        }
 
        for (i = 0; i < nthreads; i++) {
index 6d969500285685190c67627d7a50bc96de983126..a3a6106753fecc88d985b96a027b7a876721dd8f 100644 (file)
@@ -32,7 +32,7 @@ typedef void * isc_threadarg_t;
 typedef isc_threadresult_t (*isc_threadfunc_t)(isc_threadarg_t);
 typedef pthread_key_t isc_thread_key_t;
 
-isc_result_t
+void
 isc_thread_create(isc_threadfunc_t, isc_threadarg_t, isc_thread_t *);
 
 void
index 9c01e7db2af3b40ed5fba566959b8e847618ef07..6e4ebc4c0a05d2db9d0ff63ea7fc3ffcdc49e264 100644 (file)
 #define THREAD_MINSTACKSIZE            (1024U * 1024)
 #endif
 
-isc_result_t
+#define _FATAL(r, f)                                                   \
+       {                                                               \
+               char strbuf[ISC_STRERRORSIZE];                          \
+               strerror_r(r, strbuf, sizeof(strbuf));                  \
+               isc_error_fatal(__FILE__, __LINE__,                     \
+                               f # " failed: %s", \
+                               strbuf);                                \
+       }
+
+void
 isc_thread_create(isc_threadfunc_t func, isc_threadarg_t arg,
                  isc_thread_t *thread)
 {
@@ -50,23 +59,26 @@ isc_thread_create(isc_threadfunc_t func, isc_threadarg_t arg,
 #if defined(HAVE_PTHREAD_ATTR_GETSTACKSIZE) && \
     defined(HAVE_PTHREAD_ATTR_SETSTACKSIZE)
        ret = pthread_attr_getstacksize(&attr, &stacksize);
-       if (ret != 0)
-               return (ISC_R_UNEXPECTED);
+       if (ret != 0) {
+               _FATAL(ret, "pthread_attr_getstacksize");
+       }
 
        if (stacksize < THREAD_MINSTACKSIZE) {
                ret = pthread_attr_setstacksize(&attr, THREAD_MINSTACKSIZE);
-               if (ret != 0)
-                       return (ISC_R_UNEXPECTED);
+               if (ret != 0) {
+                       _FATAL(ret, pthread_attr_setstacksize);
+               }
        }
 #endif
 
        ret = pthread_create(thread, &attr, func, arg);
-       if (ret != 0)
-               return (ISC_R_UNEXPECTED);
+       if (ret != 0) {
+               _FATAL(ret,"pthread_create");
+       }
 
        pthread_attr_destroy(&attr);
 
-       return (ISC_R_SUCCESS);
+       return;
 }
 
 #ifdef __NetBSD__
index 651b0934aa2f78d6af99374acf0e87dd4e08e093..6199dc64fcd72beb61d259bdfcb87aa90a9fb104 100644 (file)
@@ -1384,9 +1384,8 @@ isc_taskmgr_create(isc_mem_t *mctx, unsigned int workers,
 
                manager->queues[i].manager = manager;
                manager->queues[i].threadid = i;
-               RUNTIME_CHECK(isc_thread_create(run, &manager->queues[i],
-                                               &manager->queues[i].thread)
-                             == ISC_R_SUCCESS);
+               isc_thread_create(run, &manager->queues[i],
+                                 &manager->queues[i].thread);
                char name[21];
                snprintf(name, sizeof(name), "isc-worker%04u", i);
                isc_thread_setname(manager->queues[i].thread, name);
index 293ba3fcd3aad2f946175cb85e5f24da4ce9d3fb..735d4116f0ba84a555a76738d591724ed1d2b91e 100644 (file)
@@ -463,8 +463,7 @@ isc_mem_benchmark(void **state) {
        assert_int_equal(result, ISC_R_SUCCESS);
 
        for (int i = 0; i < nthreads; i++) {
-               result = isc_thread_create(mem_thread, &size, &threads[i]);
-               assert_int_equal(result, ISC_R_SUCCESS);
+               isc_thread_create(mem_thread, &size, &threads[i]);
                size = size / 2;
        }
        for (int i = 0; i < nthreads; i++) {
@@ -527,8 +526,7 @@ isc_mempool_benchmark(void **state) {
        assert_int_equal(result, ISC_R_SUCCESS);
 
        for (int i = 0; i < nthreads; i++) {
-               result = isc_thread_create(mempool_thread, mp, &threads[i]);
-               assert_int_equal(result, ISC_R_SUCCESS);
+               isc_thread_create(mempool_thread, mp, &threads[i]);
                size = size / 2;
        }
        for (int i = 0; i < nthreads; i++) {
index 1c3ef3d0e371085bc1d5db651d4d7ca26219323c..276c2f5566f2732e3c33a0632e0955e88f4d5ea7 100644 (file)
@@ -697,17 +697,7 @@ isc_timermgr_create(isc_mem_t *mctx, isc_timermgr_t **managerp) {
        isc_mutex_init(&manager->lock);
        isc_mem_attach(mctx, &manager->mctx);
        isc_condition_init(&manager->wakeup);
-       if (isc_thread_create(run, manager, &manager->thread) !=
-           ISC_R_SUCCESS) {
-               isc_mem_detach(&manager->mctx);
-               (void)isc_condition_destroy(&manager->wakeup);
-               isc_mutex_destroy(&manager->lock);
-               isc_heap_destroy(&manager->heap);
-               isc_mem_put(mctx, manager, sizeof(*manager));
-               UNEXPECTED_ERROR(__FILE__, __LINE__, "%s",
-                                "isc_thread_create() failed");
-               return (ISC_R_UNEXPECTED);
-       }
+       isc_thread_create(run, manager, &manager->thread);
        isc_thread_setname(manager->thread, "isc-timer");
 
        *managerp = (isc_timermgr_t *)manager;
index b303134efc187397b8ecf5dda517760fe3933885..f9ed0cc9860f1714ab4fb7d8d72649f19321e2a1 100644 (file)
@@ -3810,10 +3810,9 @@ isc_socketmgr_create2(isc_mem_t *mctx, isc_socketmgr_t **managerp,
                manager->threads[i].manager = manager;
                manager->threads[i].threadid = i;
                setup_thread(&manager->threads[i]);
-               RUNTIME_CHECK(isc_thread_create(netthread,
-                                               &manager->threads[i],
-                                               &manager->threads[i].thread)
-                             == ISC_R_SUCCESS);
+               isc_thread_create(netthread,
+                                 &manager->threads[i],
+                                 &manager->threads[i].thread);
                char tname[1024];
                sprintf(tname, "isc-socket-%d", i);
                isc_thread_setname(manager->threads[i].thread, tname);
index 25b83af07382b065b9ec3725c05e7597c462b4ca..5642b974fb836e21a692b7e13959e68e0a47e5c3 100644 (file)
@@ -67,7 +67,7 @@ typedef DWORD isc_thread_key_t;
 
 ISC_LANG_BEGINDECLS
 
-isc_result_t
+void
 isc_thread_create(isc_threadfunc_t, isc_threadarg_t, isc_thread_t *);
 
 isc_result_t
index 16ce5c5c6f6437e5b68e106cae5eda3222558192..9d421b214897e2da87832c32414fedd9fb827a44 100644 (file)
 
 #include <process.h>
 
+#include <isc/strerr.h>
 #include <isc/thread.h>
 #include <isc/util.h>
 
-isc_result_t
+void
 isc_thread_create(isc_threadfunc_t start, isc_threadarg_t arg,
                  isc_thread_t *threadp)
 {
@@ -23,13 +24,16 @@ isc_thread_create(isc_threadfunc_t start, isc_threadarg_t arg,
 
        thread = (isc_thread_t)_beginthreadex(NULL, 0, start, arg, 0, &id);
        if (thread == NULL) {
-               /* XXX */
-               return (ISC_R_UNEXPECTED);
+               char strbuf[ISC_STRERRORSIZE];
+               /* FIXME */
+               strerror_r(GetLastError(), strbuf, sizeof(strbuf));
+               isc_error_fatal(__FILE__, __LINE__, "_beginthreadex failed: %s",
+                               strbuf);
        }
 
        *threadp = thread;
 
-       return (ISC_R_SUCCESS);
+       return;
 }
 
 isc_result_t