From patchwork Mon Aug 23 19:50:35 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 44754 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 1BC8D3858018 for ; Mon, 23 Aug 2021 19:58:18 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1BC8D3858018 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1629748698; bh=DTIpRo64P5x8Tia2PWusZ9jsE2lCoEda9DprPb8DM+M=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=oQ4jBM46f+jsCPIBn9y3zKdBvcoor8xIMwTEhZLa8yOBaEQ3niVM+vqiXgMzxgzof k9AtqB2rysjjkhIPfos3E2zLrOJiFju+G1OnsPfRAdsthiKEdG9SAqN/vtwpvWn1Qb UbWylO/5hhVnidO3zqXfDNT/ou8LL2xrs9zhBUvg= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qv1-xf32.google.com (mail-qv1-xf32.google.com [IPv6:2607:f8b0:4864:20::f32]) by sourceware.org (Postfix) with ESMTPS id 69C7E3858018 for ; Mon, 23 Aug 2021 19:52:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 69C7E3858018 Received: by mail-qv1-xf32.google.com with SMTP id ew6so2599228qvb.5 for ; Mon, 23 Aug 2021 12:52:06 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=DTIpRo64P5x8Tia2PWusZ9jsE2lCoEda9DprPb8DM+M=; b=hlVz4rBQKh6MRzBrVj467lvDlwntfh4UqjY+tEM7Jn8wp00fewVsrpksU1qxATaU+5 bcZF4VsuH7KW8qKId5vjEyt8zWtNmrCmIIel7S1yIE7i/LT2e/zwJBKW2gvICcCyx42A 6xypwlZmi5vGxzwiN6a103/A9ruUjtjyVMyanM2bAOxWz5yoviWcb06u5+w82eN3JYF6 aqyG8J8dr0dv8MjNNRI4Q+1qBRur8avlP/axyzXYhCDTtoEtG/kv4SfJckn9/Xnl9bCo JNRQSDd8ENNnY1e6OrfbAvJQgft3BdrzxrPmJEr5C8xfKAoSR0YI5OoHqD0pUXy3sx6w FRjg== X-Gm-Message-State: AOAM5316t425cm9SYKWVtEYObpQHgLngP2Mu+M/BCwRWL0WOPlqs00Nj FR+xIE6JHn3NVFhoKJixyZM/VhHNO6T25A== X-Google-Smtp-Source: ABdhPJyIbUTfMHTWVXv4oCoICQYz675XTIprsBGYmDGDcuD45CalnfyzHohBlIl+RCcE8/epoGStjw== X-Received: by 2002:a05:6214:29ee:: with SMTP id jv14mr34956664qvb.5.1629748325817; Mon, 23 Aug 2021 12:52:05 -0700 (PDT) Received: from birita.. ([2804:431:c7ca:cd83:c38b:b50d:5d9a:43d4]) by smtp.gmail.com with ESMTPSA id s10sm9210935qko.134.2021.08.23.12.52.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Aug 2021 12:52:05 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v2 07/19] support: Add support_wait_for_thread_exit Date: Mon, 23 Aug 2021 16:50:35 -0300 Message-Id: <20210823195047.543237-8-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210823195047.543237-1-adhemerval.zanella@linaro.org> References: <20210823195047.543237-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Cc: Florian Weimer Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" From: Florian Weimer Wait until all threads except the current thread has exited. --- support/Makefile | 3 +- support/support.h | 4 ++ support/support_wait_for_thread_exit.c | 72 ++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 support/support_wait_for_thread_exit.c diff --git a/support/Makefile b/support/Makefile index a462781718..ef2b1a980a 100644 --- a/support/Makefile +++ b/support/Makefile @@ -82,9 +82,10 @@ libsupport-routines = \ support_test_compare_blob \ support_test_compare_failure \ support_test_compare_string \ - support_write_file_string \ support_test_main \ support_test_verify_impl \ + support_wait_for_thread_exit \ + support_write_file_string \ temp_file \ timespec \ timespec-time64 \ diff --git a/support/support.h b/support/support.h index 834dba9097..a5978b939a 100644 --- a/support/support.h +++ b/support/support.h @@ -174,6 +174,10 @@ timer_t support_create_timer (uint64_t sec, long int nsec, bool repeat, /* Disable the timer TIMER. */ void support_delete_timer (timer_t timer); +/* Wait until all threads except the current thread have exited (as + far as the kernel is concerned). */ +void support_wait_for_thread_exit (void); + struct support_stack { void *stack; diff --git a/support/support_wait_for_thread_exit.c b/support/support_wait_for_thread_exit.c new file mode 100644 index 0000000000..658a813810 --- /dev/null +++ b/support/support_wait_for_thread_exit.c @@ -0,0 +1,72 @@ +/* Wait until all threads except the current thread has exited. + Copyright (C) 2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include +#include +#include + +void +support_wait_for_thread_exit (void) +{ +#ifdef __linux__ + DIR *proc_self_task = opendir ("/proc/self/task"); + TEST_VERIFY_EXIT (proc_self_task != NULL); + + while (true) + { + errno = 0; + struct dirent *e = readdir (proc_self_task); + if (e == NULL && errno != 0) + FAIL_EXIT1 ("readdir: %m"); + if (e == NULL) + { + /* Only the main thread remains. Testing may continue. */ + closedir (proc_self_task); + return; + } + + if (strcmp (e->d_name, ".") == 0 || strcmp (e->d_name, "..") == 0) + continue; + + int task_tid = atoi (e->d_name); + if (task_tid <= 0) + FAIL_EXIT1 ("Invalid /proc/self/task entry: %s", e->d_name); + + if (task_tid == gettid ()) + /* The current thread. Keep scanning for other + threads. */ + continue; + + /* task_tid does not refer to this thread here, i.e., there is + another running thread. */ + + /* Small timeout to give the thread a chance to exit. */ + usleep (50 * 1000); + + /* Start scanning the directory from the start. */ + rewinddir (proc_self_task); + } +#else + /* Use a large timeout because we cannot verify that the thread has + exited. */ + usleep (5 * 1000 * 1000); +#endif +}