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 2711 invoked from network); 26 Feb 2022 11:39:09 -0000 Received: from mother.openwall.net (195.42.179.200) by inbox.vuxu.org with ESMTPUTF8; 26 Feb 2022 11:39:09 -0000 Received: (qmail 23978 invoked by uid 550); 26 Feb 2022 11:39:07 -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 23945 invoked from network); 26 Feb 2022 11:39:07 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zhasha.com; s=wirbelwind; t=1645875535; bh=BV5wcpdfJVxk92AkgyiHt567gu6jw/aSKESb0DOtoww=; h=Date:From:To:Subject:In-Reply-To:References; b=TZPsyTyHdvwBYUzJwz5FvU2QeeAqvvuO6ICattUE8dCtBE598jppQB7Q/aacabAKx A0oQb/NVD33W05XPQJ3XtgqCWMw3YjyTQuC0+OqyRydIZHbTZpzSjECjwifii+rTTa flyvgyvWhL0g+kRFaAIAlGIXrA6zibzWdFHzHB4I= Date: Sat, 26 Feb 2022 12:38:55 +0100 From: Joakim Sindholt To: musl@lists.openwall.com Message-Id: <20220226123855.392c22acb208a966210c7af2@zhasha.com> In-Reply-To: References: <20220221174223.GA2079@voyager> <20220223185746.GB2079@voyager> 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=_Sat__26_Feb_2022_12_38_55_+0100_BfJMqo2k.wNQmd=4" Subject: Re: [musl] Suggestion for thread safety This is a multi-part message in MIME format. --Multipart=_Sat__26_Feb_2022_12_38_55_+0100_BfJMqo2k.wNQmd=4 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Sat, 26 Feb 2022 09:56:04 +0000, Lee Shallis wrote: > On Wed, 23 Feb 2022 at 18:58, Markus Wichmann wrote: > > > > On Wed, Feb 23, 2022 at 12:30:43AM +0000, Lee Shallis wrote: > > > think of the lock as the same as a mutex, just simpler, > > > > It isn't really simpler than a fast mutex, but a lot buggier. > > > There are no bugs, I made sure to test this after all, I deliberately > created a situation where any bugs would show them selves, the only > thing that was a potential problem I've now fixed (after doing some > research to find out if 0 is ever a valid thread id), the concept > remains the same, just with support for developer calling LockSiData > twice: Did I use it wrong then? zhasha@wirbelwind /home/zhasha ; gcc -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 = 2, expected 1 zhasha@wirbelwind /home/zhasha ; ./a.out var = 2, expected 1 var = 1, expected 0 zhasha@wirbelwind /home/zhasha ; ./a.out var = 1, expected 0 --Multipart=_Sat__26_Feb_2022_12_38_55_+0100_BfJMqo2k.wNQmd=4 Content-Type: text/plain; name="buglock.c" Content-Disposition: attachment; filename="buglock.c" Content-Transfer-Encoding: 7bit #include #include #include #include #include static void NoPause(void *ud) { (void)ud; } typedef int dint; typedef const char* LOCK; void (*pauseCB)(void *) = NoPause; static void LockSiData( LOCK **shared, LOCK *ptr ) { while ( *shared != ptr ) { if ( !(*shared) ) *shared = ptr; pauseCB( ptr ); } } static dint FreeSiData( LOCK **shared, LOCK *ud ) { if ( *shared == ud ) { *shared = NULL; return 0; } return EPERM; } static LOCK *l = 0; static volatile int var = 0; static void *thread(void *arg) { int i; while (1) { LockSiData(&l, arg); i = var++; if (i != 0) { printf("var = %d, expected 0\n", i); exit(1); } i = var--; if (i != 1) { printf("var = %d, expected 1\n", i); exit(1); } FreeSiData(&l, arg); } return 0; } int main(void) { pthread_t pt; uintptr_t i; for (i = 1; i <= 2; i++) { if ((errno = pthread_create(&pt, 0, thread, (void *)i)) != 0) { printf("pthread_create failed: %m\n"); return 1; } } pthread_exit(0); return 0; } --Multipart=_Sat__26_Feb_2022_12_38_55_+0100_BfJMqo2k.wNQmd=4--