From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pl1-x629.google.com (mail-pl1-x629.google.com [IPv6:2607:f8b0:4864:20::629]) by sourceware.org (Postfix) with ESMTPS id BF31C3857C5D for ; Thu, 19 Aug 2021 16:12:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org BF31C3857C5D Received: by mail-pl1-x629.google.com with SMTP id l11so4201543plk.6 for ; Thu, 19 Aug 2021 09:12:54 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=uVf8NqXvQ830xHwFOKCPkxoHiMHJ++QnjX7fEApKgvc=; b=iZhm8ykHD4mcGgjbiTCgjACPF5I02N2bTfiZRDZpIDIhOPBvIZwNCMRxgAz6SpwdnW t4z+6MA5znVAN1cc+NmvAjTmVZOwtBMFQwIn/jcs9T6m+irdjoGrSD7e0HeqTW9QGTNw LQJsDvsZ1+Z7LWH7E1P1a81lCFza+9bz6IqskHbubEavDhGPjxvdP0APgEKm9Lo/jTtV qtb59cMlWtILe1J18oIfUtCYPanrC7weAtKA+y7KycNb+Ippxop42Hm5emHwVVvhDGEN aCPe+LEr/J+A0pXDYK7+A7WC5Pe5x88lqwjLBcqSmTMuPbs1Z2boD2iO3Fvsu+gid0An kjNA== X-Gm-Message-State: AOAM533Jqi+s2uX/NghDgpqWCj2cNm/mATAQ+/X7/En2/tqe4ZBQxLBR myi0nU4yLBXk5qYaxkjHN5/hdEWhQzgTtg== X-Google-Smtp-Source: ABdhPJwSG5yvmdvM+4ZbekU5pFQvQ2jIIPZPaa8+nrWL7GwKqnH6J17xkr6ValsRReAPOVISeAwTHg== X-Received: by 2002:a17:903:4112:b029:12d:3ba8:7888 with SMTP id r18-20020a1709034112b029012d3ba87888mr12542007pld.52.1629389573778; Thu, 19 Aug 2021 09:12:53 -0700 (PDT) Received: from ?IPv6:2804:431:c7ca:cd83:aa1a:7bd:9935:9bba? ([2804:431:c7ca:cd83:aa1a:7bd:9935:9bba]) by smtp.gmail.com with ESMTPSA id n41sm3893364pfv.43.2021.08.19.09.12.52 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 19 Aug 2021 09:12:53 -0700 (PDT) Subject: Re: [PATCH 1/3] support: Add support_wait_for_thread_exit To: Florian Weimer , libc-alpha@sourceware.org References: <3c76c76c7f0a25bfb9581a43693ebab13b50043d.1629208174.git.fweimer@redhat.com> From: Adhemerval Zanella Message-ID: <5c0a15c2-8447-86fe-73f2-8f27147423cc@linaro.org> Date: Thu, 19 Aug 2021 13:12:51 -0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 MIME-Version: 1.0 In-Reply-To: <3c76c76c7f0a25bfb9581a43693ebab13b50043d.1629208174.git.fweimer@redhat.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-13.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, NICE_REPLY_A, 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-List-Received-Date: Thu, 19 Aug 2021 16:12:59 -0000 On 17/08/2021 10:50, Florian Weimer via Libc-alpha wrote: > --- > support/support.h | 4 ++ > support/support_wait_for_thread_exit.c | 67 ++++++++++++++++++++++++++ Missing the Makefile entry for support_wait_for_thread_exit > 2 files changed, 71 insertions(+) > create mode 100644 support/support_wait_for_thread_exit.c > > 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; Ok. > diff --git a/support/support_wait_for_thread_exit.c b/support/support_wait_for_thread_exit.c > new file mode 100644 > index 0000000000..808901f746 > --- /dev/null > +++ b/support/support_wait_for_thread_exit.c > @@ -0,0 +1,67 @@ > +/* 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) > +{ > + DIR *proc_self_task = opendir ("/proc/self/task"); > + if (proc_self_task == NULL) > + /* Use a large timeout because we cannot verify that the thread > + has exited. */ > + usleep (5 * 1000 * 1000); > + else > + { > + int count; > + > + wait_again: > + count = 0; > + rewinddir (proc_self_task); > + 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. */ > + TEST_VERIFY (count >= 1); > + closedir (proc_self_task); > + return; > + } > + > + if (strcmp (e->d_name, ".") == 0 || strcmp (e->d_name, "..") == 0) > + continue; > + > + ++count; > + if (count > 1) > + { > + /* Small timeout to give the thread a chance to exit. */ > + usleep (50 * 1000); > + goto wait_again; > + } > + } > + } > +} > I am not very found of interfaces that poll kernel resources using timeouts, but I don't think we have a better Linux interface to handle this.