diff --git a/src/iperf.h b/src/iperf.h index 7d14a3453..b541d2c8c 100644 --- a/src/iperf.h +++ b/src/iperf.h @@ -193,6 +193,7 @@ struct iperf_stream struct iperf_test* test; pthread_t thr; + int thread_created; int done; /* configurable members */ diff --git a/src/iperf_client_api.c b/src/iperf_client_api.c index 67b05cc69..8807541ff 100644 --- a/src/iperf_client_api.c +++ b/src/iperf_client_api.c @@ -715,6 +715,7 @@ iperf_run_client(struct iperf_test * test) i_errno = IEPTHREADCREATE; goto cleanup_and_fail; } + sp->thread_created = 1; if (test->debug_level >= DEBUG_LEVEL_INFO) { iperf_printf(test, "Thread FD %d created\n", sp->socket); } @@ -753,22 +754,25 @@ iperf_run_client(struct iperf_test * test) if (sp->sender) { int rc; sp->done = 1; - rc = pthread_cancel(sp->thr); - if (rc != 0 && rc != ESRCH) { - i_errno = IEPTHREADCANCEL; - errno = rc; - iperf_err(test, "sender cancel in pthread_cancel - %s", iperf_strerror(i_errno)); - goto cleanup_and_fail; - } - rc = pthread_join(sp->thr, NULL); - if (rc != 0 && rc != ESRCH) { - i_errno = IEPTHREADJOIN; - errno = rc; - iperf_err(test, "sender cancel in pthread_join - %s", iperf_strerror(i_errno)); - goto cleanup_and_fail; - } - if (test->debug_level >= DEBUG_LEVEL_INFO) { - iperf_printf(test, "Thread FD %d stopped\n", sp->socket); + if (sp->thread_created == 1) { + rc = pthread_cancel(sp->thr); + if (rc != 0 && rc != ESRCH) { + i_errno = IEPTHREADCANCEL; + errno = rc; + iperf_err(test, "sender cancel in pthread_cancel - %s", iperf_strerror(i_errno)); + goto cleanup_and_fail; + } + rc = pthread_join(sp->thr, NULL); + if (rc != 0 && rc != ESRCH) { + i_errno = IEPTHREADJOIN; + errno = rc; + iperf_err(test, "sender cancel in pthread_join - %s", iperf_strerror(i_errno)); + goto cleanup_and_fail; + } + if (test->debug_level >= DEBUG_LEVEL_INFO) { + iperf_printf(test, "Thread FD %d stopped\n", sp->socket); + } + sp->thread_created = 0; } } } @@ -791,22 +795,25 @@ iperf_run_client(struct iperf_test * test) if (!sp->sender) { int rc; sp->done = 1; - rc = pthread_cancel(sp->thr); - if (rc != 0 && rc != ESRCH) { - i_errno = IEPTHREADCANCEL; - errno = rc; - iperf_err(test, "receiver cancel in pthread_cancel - %s", iperf_strerror(i_errno)); - goto cleanup_and_fail; - } - rc = pthread_join(sp->thr, NULL); - if (rc != 0 && rc != ESRCH) { - i_errno = IEPTHREADJOIN; - errno = rc; - iperf_err(test, "receiver cancel in pthread_join - %s", iperf_strerror(i_errno)); - goto cleanup_and_fail; - } - if (test->debug_level >= DEBUG_LEVEL_INFO) { - iperf_printf(test, "Thread FD %d stopped\n", sp->socket); + if (sp->thread_created == 1) { + rc = pthread_cancel(sp->thr); + if (rc != 0 && rc != ESRCH) { + i_errno = IEPTHREADCANCEL; + errno = rc; + iperf_err(test, "receiver cancel in pthread_cancel - %s", iperf_strerror(i_errno)); + goto cleanup_and_fail; + } + rc = pthread_join(sp->thr, NULL); + if (rc != 0 && rc != ESRCH) { + i_errno = IEPTHREADJOIN; + errno = rc; + iperf_err(test, "receiver cancel in pthread_join - %s", iperf_strerror(i_errno)); + goto cleanup_and_fail; + } + if (test->debug_level >= DEBUG_LEVEL_INFO) { + iperf_printf(test, "Thread FD %d stopped\n", sp->socket); + } + sp->thread_created = 0; } } } @@ -835,20 +842,23 @@ iperf_run_client(struct iperf_test * test) } sp->done = 1; int rc; - rc = pthread_cancel(sp->thr); - if (rc != 0 && rc != ESRCH) { - i_errno = IEPTHREADCANCEL; - errno = rc; - iperf_err(test, "cleanup_and_fail in pthread_cancel - %s", iperf_strerror(i_errno)); - } - rc = pthread_join(sp->thr, NULL); - if (rc != 0 && rc != ESRCH) { - i_errno = IEPTHREADJOIN; - errno = rc; - iperf_err(test, "cleanup_and_fail in pthread_join - %s", iperf_strerror(i_errno)); - } - if (test->debug_level >= DEBUG_LEVEL_INFO) { - iperf_printf(test, "Thread FD %d stopped\n", sp->socket); + if (sp->thread_created == 1) { + rc = pthread_cancel(sp->thr); + if (rc != 0 && rc != ESRCH) { + i_errno = IEPTHREADCANCEL; + errno = rc; + iperf_err(test, "cleanup_and_fail in pthread_cancel - %s", iperf_strerror(i_errno)); + } + rc = pthread_join(sp->thr, NULL); + if (rc != 0 && rc != ESRCH) { + i_errno = IEPTHREADJOIN; + errno = rc; + iperf_err(test, "cleanup_and_fail in pthread_join - %s", iperf_strerror(i_errno)); + } + if (test->debug_level >= DEBUG_LEVEL_INFO) { + iperf_printf(test, "Thread FD %d stopped\n", sp->socket); + } + sp->thread_created = 0; } } if (test->debug_level >= DEBUG_LEVEL_INFO) { diff --git a/src/iperf_server_api.c b/src/iperf_server_api.c index 9727cdddb..aa601c3ec 100644 --- a/src/iperf_server_api.c +++ b/src/iperf_server_api.c @@ -448,20 +448,23 @@ cleanup_server(struct iperf_test *test) SLIST_FOREACH(sp, &test->streams, streams) { int rc; sp->done = 1; - rc = pthread_cancel(sp->thr); - if (rc != 0 && rc != ESRCH) { - i_errno = IEPTHREADCANCEL; - errno = rc; - iperf_err(test, "cleanup_server in pthread_cancel - %s", iperf_strerror(i_errno)); - } - rc = pthread_join(sp->thr, NULL); - if (rc != 0 && rc != ESRCH) { - i_errno = IEPTHREADJOIN; - errno = rc; - iperf_err(test, "cleanup_server in pthread_join - %s", iperf_strerror(i_errno)); - } - if (test->debug_level >= DEBUG_LEVEL_INFO) { - iperf_printf(test, "Thread FD %d stopped\n", sp->socket); + if (sp->thread_created == 1) { + rc = pthread_cancel(sp->thr); + if (rc != 0 && rc != ESRCH) { + i_errno = IEPTHREADCANCEL; + errno = rc; + iperf_err(test, "cleanup_server in pthread_cancel - %s", iperf_strerror(i_errno)); + } + rc = pthread_join(sp->thr, NULL); + if (rc != 0 && rc != ESRCH) { + i_errno = IEPTHREADJOIN; + errno = rc; + iperf_err(test, "cleanup_server in pthread_join - %s", iperf_strerror(i_errno)); + } + if (test->debug_level >= DEBUG_LEVEL_INFO) { + iperf_printf(test, "Thread FD %d stopped\n", sp->socket); + } + sp->thread_created = 0; } } i_errno = i_errno_save; @@ -909,6 +912,7 @@ iperf_run_server(struct iperf_test *test) cleanup_server(test); return -1; } + sp->thread_created = 1; if (test->debug_level >= DEBUG_LEVEL_INFO) { iperf_printf(test, "Thread FD %d created\n", sp->socket); }