mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Joakim Sindholt <opensource@zhasha.com>
To: musl@lists.openwall.com
Subject: Re: [musl] Suggestion for thread safety
Date: Sat, 26 Feb 2022 12:38:55 +0100	[thread overview]
Message-ID: <20220226123855.392c22acb208a966210c7af2@zhasha.com> (raw)
In-Reply-To: <CAOZ3c1rkxt3SgTvpzb4g+HJi1+rwhhbnDP_wE8+aZyREA8dyZg@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 1122 bytes --]

On Sat, 26 Feb 2022 09:56:04 +0000, Lee Shallis <gb2985@gmail.com> wrote:
> On Wed, 23 Feb 2022 at 18:58, Markus Wichmann <nullplan@gmx.net> 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

[-- Attachment #2: buglock.c --]
[-- Type: text/plain, Size: 1270 bytes --]

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <stdint.h>
#include <errno.h>

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;
}

  reply	other threads:[~2022-02-26 11:39 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-21 11:36 Lee Shallis
2022-02-21 17:42 ` Markus Wichmann
2022-02-23  0:30   ` Lee Shallis
2022-02-23 18:57     ` Markus Wichmann
2022-02-23 20:06       ` Rich Felker
2022-02-26  9:56       ` Lee Shallis
2022-02-26 11:38         ` Joakim Sindholt [this message]
2022-02-27 23:32           ` Lee Shallis
2022-02-28  0:15             ` Rich Felker
2022-02-28  8:48             ` Joakim Sindholt
2022-02-28 14:43               ` Lee Shallis
2022-02-28 15:19                 ` Rich Felker
2022-02-28 15:50                 ` Joakim Sindholt
2022-02-28 16:07                   ` Lee Shallis
2022-03-02  1:44                     ` Lee Shallis
2022-02-23  1:19 ` Rich Felker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220226123855.392c22acb208a966210c7af2@zhasha.com \
    --to=opensource@zhasha.com \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).