From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.4 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,MAILING_LIST_MULTI,NICE_REPLY_A,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 30241 invoked from network); 28 Feb 2022 08:48:30 -0000 Received: from mother.openwall.net (195.42.179.200) by inbox.vuxu.org with ESMTPUTF8; 28 Feb 2022 08:48:30 -0000 Received: (qmail 29842 invoked by uid 550); 28 Feb 2022 08:48:26 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Reply-To: musl@lists.openwall.com Received: (qmail 29803 invoked from network); 28 Feb 2022 08:48:26 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zhasha.com; s=wirbelwind; t=1646038094; bh=d/ZpTyK3RYLVNdMlnsr8/2Ngg76G/t1qNbUKAlQ875Q=; h=Date:From:To:Subject:In-Reply-To:References; b=WT49yE95kA34MPj3KCGBe3Rnig6uB06DqBtJdJQsDVmSf3eUfZnSGZ/uKnXZ6BZz9 UhZ4ANyoqVjGK9EAYIou1JK58xb0DtWW9HI8er/rbQrPhYiwfDwQ6XsauUSm4ITBpj PuSxzMD5IYP5NzolnwPpwDXUuskj+IHihCet/154= Date: Mon, 28 Feb 2022 09:48:14 +0100 From: Joakim Sindholt To: musl@lists.openwall.com Message-Id: <20220228094814.be34ddcf7be25724e8f8c21b@zhasha.com> In-Reply-To: References: <20220221174223.GA2079@voyager> <20220223185746.GB2079@voyager> <20220226123855.392c22acb208a966210c7af2@zhasha.com> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.33; x86_64-redhat-linux-gnu) Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="Multipart=_Mon__28_Feb_2022_09_48_14_+0100_b9sNEiVfjC1uKp08" Subject: Re: [musl] Suggestion for thread safety This is a multi-part message in MIME format. --Multipart=_Mon__28_Feb_2022_09_48_14_+0100_b9sNEiVfjC1uKp08 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Sun, 27 Feb 2022 23:32:47 +0000, Lee Shallis wrote: > Yes, as I mentioned before, pauseCB is supposed to have it's pointer > be changed by the developer, in other words you forgot to plugin a > pthreads compatible call prior to your threads starting, considering > you made that mistake I suppose it is a good thing I since switched to > a non-redirectable pointer: How many times do you want to do this? zhasha@wirbelwind /home/zhasha ; gcc -D_GNU_SOURCE -lpthread buglock.c zhasha@wirbelwind /home/zhasha ; ./a.out var = 1, expected 0 zhasha@wirbelwind /home/zhasha ; ./a.out var = 1, expected 0 zhasha@wirbelwind /home/zhasha ; ./a.out var = 1, expected 0 zhasha@wirbelwind /home/zhasha ; ./a.out var = 1, expected 0 zhasha@wirbelwind /home/zhasha ; ./a.out var = 1, expected 0 var = 2, expected 1 zhasha@wirbelwind /home/zhasha ; ./a.out var = 1, expected 0 var = 2, expected 1 --Multipart=_Mon__28_Feb_2022_09_48_14_+0100_b9sNEiVfjC1uKp08 Content-Type: text/plain; name="buglock.c" Content-Disposition: attachment; filename="buglock.c" Content-Transfer-Encoding: 7bit #include #include #include #include #include #include typedef int dint; typedef pid_t WHICH; typedef struct _LOCK LOCK; struct _LOCK { uint num; WHICH tid; }; WHICH Which() { return gettid(); } void Pause() { pthread_yield(); } void LockSiData( LOCK *shared ) { WHICH tid = Which(); while ( shared->tid != tid ) { if ( !(shared->tid) ) shared->tid = tid; Pause(); } shared->num++; } dint FreeSiData( LOCK *shared ) { if ( shared->tid == Which() ) { shared->num--; if ( !(shared->num) ) shared->tid = (WHICH)0; return 0; } return EACCES; } static LOCK l; static volatile int var = 0; static void *thread(void *arg) { int i; while (1) { LockSiData(&l); i = var++; if (i != 0) { printf("var = %d, expected 0\n", i); exit(1); } clock_nanosleep(CLOCK_MONOTONIC, 0, &(struct timespec){.tv_sec = 0, .tv_nsec = 1}, 0); i = var--; if (i != 1) { printf("var = %d, expected 1\n", i); exit(1); } FreeSiData(&l); } return 0; } int main(void) { pthread_t pt; int i; for (i = 0; i < 2; i++) { if ((errno = pthread_create(&pt, 0, thread, 0)) != 0) { printf("pthread_create failed: %m\n"); return 1; } } pthread_exit(0); } --Multipart=_Mon__28_Feb_2022_09_48_14_+0100_b9sNEiVfjC1uKp08--