From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id ACB2F3858D33 for ; Thu, 23 Feb 2023 01:09:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org ACB2F3858D33 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1677114540; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to; bh=NCm17Uq2NHeJeUNEwAGbPzp8iyoS7/b/13VK0Xz+FAY=; b=MRRvOnxMwqmyInb2h0SWhtrUoOSccFhhYawNZePack5wP+mgAn6TWucC6m9Ejn844DjUDr 4iCcwMrLNWK5f8SycogFxUrVXV+sWyjpJPaNmEn7d+c+VUovvBkkBfI2KnA4AuZSLC07dZ 9aYTc3M6/OGvB2xGz7Os0OAXOM+ToiA= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-49-jHdX93MGMOeqlXlBhoQa9g-1; Wed, 22 Feb 2023 20:08:58 -0500 X-MC-Unique: jHdX93MGMOeqlXlBhoQa9g-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 75A7685CBE2; Thu, 23 Feb 2023 01:08:58 +0000 (UTC) Received: from greed.delorie.com (unknown [10.22.8.35]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 5C15D2166B29; Thu, 23 Feb 2023 01:08:58 +0000 (UTC) Received: from greed.delorie.com.redhat.com (localhost [127.0.0.1]) by greed.delorie.com (8.15.2/8.15.2) with ESMTP id 31N18vpx965732; Wed, 22 Feb 2023 20:08:57 -0500 From: DJ Delorie To: Adhemerval Zanella Cc: libc-alpha@sourceware.org Subject: Re: [PATCH v2] i386: Use pthread_barrier for synchronization on tst-bz21269 In-Reply-To: <20230221192640.3650934-1-adhemerval.zanella@linaro.org> Date: Wed, 22 Feb 2023 20:08:57 -0500 Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,KAM_NUMSUBJECT,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Adhemerval Zanella writes: > - - C11 atomics instead of plain access. > + - Use pthread_barrier instead of atomic and futexes. Ok. > -#include > - > #include > -#include Ok. > +#include > + > +#define NITER 5 Ok. > -static int > -futex (int *uaddr, int futex_op, int val, void *timeout, int *uaddr2, > - int val3) > -{ > - return syscall (SYS_futex, uaddr, futex_op, val, timeout, uaddr2, val3); > -} > - Ok. > - TEST_VERIFY_EXIT (sigaction (sig, &sa, 0) == 0); > + xsigaction (sig, &sa, 0); Ok. > -/* Possible values of futex: > - 0: thread is idle. > - 1: thread armed. > - 2: thread should clear LDT entry 0. > - 3: thread should exit. */ > -static atomic_uint ftx; > +static pthread_barrier_t barrier; Ok. > - while (1) > + for (int i = 0; i < NITER; i++) Matches the loop in main, ok. > - futex ((int *) &ftx, FUTEX_WAIT, 1, NULL, NULL, 0); > - while (atomic_load (&ftx) != 2) > - { > - if (atomic_load (&ftx) >= 3) > - return NULL; > - } > + xpthread_barrier_wait (&barrier); First barrier, ok. Both threads start here > /* clear LDT entry 0. */ > const struct user_desc desc = { 0 }; > xmodify_ldt (1, &desc, sizeof (desc)); While we're doing this, main thread is also modifying its ldt? > - /* If ftx == 2, set it to zero, If ftx == 100, quit. */ > - if (atomic_fetch_add (&ftx, -2) != 2) > - return NULL; > + /* Wait for 'ss' set in main thread. */ > + xpthread_barrier_wait (&barrier); Second barrier. Ok. After this, the main thread sets SS... I think the barriers are still in the wrong locations. the original code did this (wrt main): * wait for thread to be idle * xmodify_ldt() * set thread to "armed" * set SS to something "funny" * tell thread to run (=2) - thread calls xmodify_ltd and goes idle (=0) * wait for thread idle Then, it sets SS *before* the thread clears the LDT. So I think the barriers need to be placed like this: -main- -thread- for (;;) { xmodify_ldt (set it) - set SS - -- barrier -- - xmodify_ldt (clear it) -- barrier -- fail - }